-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AMBARI-26590: Models and Updater hooks for services #4117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shaur97
wants to merge
1
commit into
apache:frontend-refactor
Choose a base branch
from
shaur97:shaur97-upadaters-hooks-for-services
base: frontend-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+7,247
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
290 changes: 290 additions & 0 deletions
290
ambari-web/latest/src/hooks/useAmbariMetricsConfigUpdater.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,290 @@ | ||
| import { useContext, useEffect } from "react"; | ||
| import { cloneDeep, find, get, isEmpty, isEqual } from "lodash"; | ||
| import { ServiceApi } from "../api/ServiceApi.ts"; | ||
| import { cachedServiceApi } from "../api/CachedServiceApi.ts"; | ||
| import { updateServiceAlertsAndStateFromCentralizedApi } from "../Utils/centralizedServiceStateUtils.ts"; | ||
| import { ServiceComponentMetricsEnums } from "../enums/ServiceComponentMetricsEnums.ts"; | ||
| import { AppContext } from "../store/context.tsx"; | ||
| import { ServiceContext } from "../store/ServiceContext.tsx"; | ||
| import { | ||
| componentFinishStates, | ||
| maintenanceStates, | ||
| } from "../screens/Hosts/constants.ts"; | ||
| import { Categories } from "../enums/Categories.ts"; | ||
|
|
||
| export const useAmbariMetricsConfigUpdater = () => { | ||
| // @ts-ignore | ||
| const { services, clusterName, parsedSocketMessages } = useContext(AppContext); | ||
|
|
||
| // Early return if AMBARI_METRICS service is not installed | ||
| const isAmbariMetricsInstalled = services && Array.isArray(services) && | ||
| services.some((service: any) => service.ServiceInfo.service_name === "AMBARI_METRICS"); | ||
|
|
||
| if (!isAmbariMetricsInstalled) { | ||
| return; | ||
| } | ||
|
|
||
| const { polledHostComponentsData, masterSlaveClientsData, serviceStatesData } = | ||
| useContext(ServiceContext); | ||
| //@ts-ignore | ||
| const { allServiceModels, updateRegistry } = useContext(ServiceContext); | ||
| //const vdpStackVersion = get(cluster, "version", "").split("-")[1]; | ||
|
|
||
| const fetchAmbariMetricsMasterSlaveClientsData = async () => { | ||
| // 🚀 OPTIMIZATION: Try centralized cache first, fallback to masterSlaveClientsData | ||
|
|
||
| let ambariMetricsComponentsData = cachedServiceApi.getServiceComponentData("AMBARI_METRICS"); | ||
|
|
||
| if (!ambariMetricsComponentsData) { | ||
| ambariMetricsComponentsData = Object.values(masterSlaveClientsData).filter( | ||
| (item) => get(item, "ServiceComponentInfo.service_name") === "AMBARI_METRICS" | ||
| ); | ||
| } else { | ||
| } | ||
|
|
||
| return ambariMetricsComponentsData; | ||
| }; | ||
|
|
||
| const updateComponentObjectForSelectMaster = (componentData: any) => { | ||
| let masterComponent = find( | ||
| //@ts-ignore | ||
| polledHostComponentsData?.items, | ||
| (item) => | ||
| get(item, "ServiceComponentInfo.service_name") === "AMBARI_METRICS" && | ||
| get(item, "ServiceComponentInfo.component_name") === | ||
| componentData.componentName | ||
| ); | ||
|
|
||
| if (masterComponent) { | ||
| const hostComponents = get(masterComponent, "host_components"); | ||
| if (hostComponents && hostComponents.length > 0) { | ||
| hostComponents.forEach((hostComponent: any) => { | ||
| if ( | ||
| get(hostComponent, "HostRoles.component_name") === | ||
| componentData.componentName | ||
| ) { | ||
| componentData.hostComponents.forEach((host: any) => { | ||
| const polledHostComponentHostName = get( | ||
| hostComponent, | ||
| "HostRoles.host_name" | ||
| ); | ||
| const componentDataHostName = get(host, "HostRoles.host_name"); | ||
| if (componentDataHostName === polledHostComponentHostName) { | ||
| host.state = get(hostComponent, "HostRoles.state"); | ||
| host.passiveState = get( | ||
| hostComponent, | ||
| "HostRoles.maintenance_state" | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| return componentData; | ||
| }; | ||
|
|
||
| const findMasterSlaveComponents = async () => { | ||
| const items = await fetchAmbariMetricsMasterSlaveClientsData(); | ||
|
|
||
| if (!allServiceModels["ambari_metrics"]) { | ||
| return; | ||
| } | ||
|
|
||
| const currentConfig = cloneDeep(allServiceModels["ambari_metrics"]); | ||
| let masterComponents: any[] = []; | ||
| let slaveComponents: any[] = []; | ||
|
|
||
| items?.forEach((item: any) => { | ||
| let componentData = { | ||
| componentName: get(item, "ServiceComponentInfo.component_name"), | ||
| displayName: get(item, "ServiceComponentInfo.display_name"), | ||
| category: get(item, "ServiceComponentInfo.category"), | ||
| installedCount: get(item, "ServiceComponentInfo.installed_count"), | ||
| startedCount: get(item, "ServiceComponentInfo.started_count"), | ||
| totalCount: get(item, "ServiceComponentInfo.total_count"), | ||
| hostComponents: get(item, "host_components"), | ||
| }; | ||
|
|
||
| if (componentData.category === Categories.MASTER) { | ||
| if ( | ||
| componentData.componentName === "METRICS_COLLECTOR" || | ||
| componentData.componentName === "METRICS_GRAFANA" | ||
| ) { | ||
| const masterComponentDataWithState = | ||
| updateComponentObjectForSelectMaster(componentData); | ||
| masterComponents.push(masterComponentDataWithState); | ||
| } | ||
| } else if (componentData.category === Categories.SLAVE) { | ||
| slaveComponents.push(componentData); | ||
| } | ||
| }); | ||
|
|
||
| currentConfig[ | ||
| ServiceComponentMetricsEnums.AMBARI_METRICS.masterComponents | ||
| ] = masterComponents; | ||
| currentConfig[ServiceComponentMetricsEnums.AMBARI_METRICS.slaveComponents] = | ||
| slaveComponents; | ||
|
|
||
| if (!isEqual(allServiceModels["ambari_metrics"], currentConfig)) { | ||
| allServiceModels["ambari_metrics"].updateConfig(currentConfig); | ||
| updateRegistry(allServiceModels); | ||
| } | ||
| }; | ||
|
|
||
| //check for updating metrics monitor | ||
| //@ts-ignore | ||
| const updateAmbariMetricsHostComponentsData = async () => { | ||
| let updatedConfig = cloneDeep(allServiceModels["ambari_metrics"]); | ||
| const serviceName = "AMBARI_METRICS"; // Replace with the desired service name | ||
| const fields = `ServiceComponentInfo/service_name,ServiceComponentInfo/category,ServiceComponentInfo/installed_count,ServiceComponentInfo/started_count,ServiceComponentInfo/init_count,ServiceComponentInfo/install_failed_count,ServiceComponentInfo/unknown_count,ServiceComponentInfo/total_count,ServiceComponentInfo/display_name,host_components/HostRoles/host_name&minimal_response=true`; | ||
| await ServiceApi.getAllServiceComponentsListAndInitialMetrics( | ||
| clusterName, | ||
| `${fields}&ServiceComponentInfo/service_name=${serviceName}` | ||
| ); | ||
|
|
||
| const components = [{ name: "METRICS_MONITOR", metric: "metricsMonitors" }]; | ||
|
|
||
| components.forEach((component) => { | ||
| // const hostComponents = findHostComponentItems("RANGER", component.name, response); | ||
| const hostComponents = { ServiceComponentInfo: {} as any }; | ||
|
|
||
| if (!hostComponents || !hostComponents.ServiceComponentInfo) { | ||
| return; // Skip if hostComponents is undefined or doesn't have ServiceComponentInfo | ||
| } | ||
|
|
||
| const installedCount = | ||
| hostComponents.ServiceComponentInfo.installed_count; | ||
| const startedCount = hostComponents.ServiceComponentInfo.started_count; | ||
| const totalCount = hostComponents.ServiceComponentInfo.total_count; | ||
| updatedConfig[ | ||
| ServiceComponentMetricsEnums.AMBARI_METRICS[ | ||
| `${component.metric}Started` as keyof typeof ServiceComponentMetricsEnums.AMBARI_METRICS | ||
| ] as any | ||
| ] = startedCount; | ||
| updatedConfig[ | ||
| ServiceComponentMetricsEnums.AMBARI_METRICS[ | ||
| `${component.metric}Installed` as keyof typeof ServiceComponentMetricsEnums.AMBARI_METRICS | ||
| ] as any | ||
| ] = installedCount; | ||
| updatedConfig[ | ||
| ServiceComponentMetricsEnums.AMBARI_METRICS[ | ||
| `${component.metric}Total` as keyof typeof ServiceComponentMetricsEnums.AMBARI_METRICS | ||
| ] as any | ||
| ] = totalCount; | ||
| }); | ||
|
|
||
| if (!isEqual(allServiceModels["ambari_metrics"], updatedConfig)) { | ||
| allServiceModels["ambari_metrics"].updateConfig(updatedConfig); | ||
| updateRegistry(allServiceModels); | ||
| } | ||
| }; | ||
|
|
||
| const parseWebSocketMessages = async () => { | ||
| let latestHostOperationMessage = {} as any; | ||
| if (parsedSocketMessages.length > 0) { | ||
| latestHostOperationMessage = parsedSocketMessages.find( | ||
| (message) => message.destination === "/events/hostcomponents" | ||
| ); | ||
| } | ||
|
|
||
| if ( | ||
| latestHostOperationMessage && | ||
| latestHostOperationMessage.hostComponents | ||
| ) { | ||
| for (const hostComponent of latestHostOperationMessage.hostComponents) { | ||
| if (hostComponent.currentState in componentFinishStates) { | ||
| //await updateRangerHostComponentsData(); | ||
| await findMasterSlaveComponents(); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| const updateGrafanaComponent = async () => { | ||
| //@ts-ignore | ||
| if (!polledHostComponentsData?.items) return; | ||
|
|
||
| const currentConfig = cloneDeep(allServiceModels["ambari_metrics"]); | ||
|
|
||
| let grafana = find( | ||
| //@ts-ignore | ||
| polledHostComponentsData.items, | ||
| (item: any) => | ||
| get(item, "ServiceComponentInfo.service_name") === "AMBARI_METRICS" && | ||
| get(item, "ServiceComponentInfo.component_name") === "METRICS_GRAFANA" | ||
| ); | ||
| grafana.state = get(grafana, "HostRoles.state"); | ||
| currentConfig[ServiceComponentMetricsEnums.AMBARI_METRICS.grafana] = | ||
| grafana; | ||
|
|
||
| // Only update if we have changes | ||
| if ( | ||
| !isEqual(allServiceModels["ambari_metrics"], currentConfig) && | ||
| !isEmpty(currentConfig) | ||
| ) { | ||
| allServiceModels["ambari_metrics"].updateConfig(currentConfig); | ||
| updateRegistry(allServiceModels); | ||
| } | ||
| }; | ||
|
|
||
| const updateServiceMaintenanceState = (maintenanceState: string) => { | ||
| let configToBeUpdated = cloneDeep(allServiceModels["ambari_metrics"]); | ||
| configToBeUpdated.isInPassiveForService = | ||
| maintenanceState === maintenanceStates[0]; //signifies ON, assigns ON/PFF | ||
|
|
||
| if (!isEqual(allServiceModels["ambari_metrics"], configToBeUpdated)) { | ||
| allServiceModels["ambari_metrics"].updateConfig(configToBeUpdated); | ||
| updateRegistry(allServiceModels); | ||
| } | ||
| }; | ||
|
|
||
| const updateAlertsAndServiceStateData = async () => { | ||
| // Use centralized service state API instead of individual call | ||
| updateServiceAlertsAndStateFromCentralizedApi("AMBARI_METRICS", "ambari_metrics", allServiceModels, updateRegistry); | ||
| }; | ||
| const parseAlertsWebSocketMessages = async () => { | ||
| let latestHostOperationMessage = {} as any; | ||
| if (parsedSocketMessages.length > 0) { | ||
| latestHostOperationMessage = parsedSocketMessages.find( | ||
| (message) => message.service_name === "AMBARI_METRICS" | ||
| ); | ||
| } | ||
| if ( | ||
| latestHostOperationMessage && | ||
| (componentFinishStates.includes(latestHostOperationMessage.state) || | ||
| (latestHostOperationMessage.maintenance_state && maintenanceStates.includes(latestHostOperationMessage.maintenance_state))) | ||
| ) { | ||
| await updateServiceMaintenanceState( | ||
| latestHostOperationMessage.maintenance_state | ||
| ); | ||
| await updateAlertsAndServiceStateData(); | ||
| } | ||
| }; | ||
|
|
||
| //usePolling(pollServiceComponentInfoApi, 1000); | ||
|
|
||
| useEffect(() => { | ||
| //@ts-ignore | ||
| if (polledHostComponentsData?.items) { | ||
| findMasterSlaveComponents(); | ||
| updateGrafanaComponent(); | ||
| } | ||
| }, [polledHostComponentsData]); | ||
|
|
||
| useEffect(() => { | ||
| //pollServiceComponentInfoApi(); | ||
| findMasterSlaveComponents(); | ||
| updateAmbariMetricsHostComponentsData(); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| updateAlertsAndServiceStateData(); | ||
| }, [allServiceModels, serviceStatesData]); | ||
|
|
||
| useEffect(() => { | ||
| parseWebSocketMessages(); | ||
| parseAlertsWebSocketMessages(); | ||
| }, [parsedSocketMessages]); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shaur97 can you add license header in all new files?