Alerting: Fix refetching grafana rules on alert list panel (#72242)

This commit is contained in:
Konrad Lalik 2023-07-26 10:37:22 +02:00 committed by GitHub
parent da31b8083a
commit e094adb5a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -135,18 +135,53 @@ export function UnifiedAlertList(props: PanelProps<UnifiedAlertListOptions>) {
[parsedOptions.alertInstanceLabelFilter]
);
// If the datasource is not defined we should NOT skip the query
// Undefined dataSourceName means that there is no datasource filter applied and we should fetch all the rules
const shouldFetchGrafanaRules = !dataSourceName || dataSourceName === GRAFANA_RULES_SOURCE_NAME;
//For grafana managed rules, get the result using RTK Query to avoid the need of using the redux store
//See https://github.com/grafana/grafana/pull/70482
const {
currentData: grafanaPromRules = [],
isLoading: grafanaRulesLoading,
refetch: refetchGrafanaPromRules,
} = usePrometheusRulesByNamespaceQuery(
{
limitAlerts: limitInstances ? INSTANCES_DISPLAY_LIMIT : undefined,
matcher: matcherList,
state: stateList,
},
{ skip: !shouldFetchGrafanaRules }
);
useEffect(() => {
//we need promRules and rulerRules for getting the uid when creating the alert link in panel in case of being a rulerRule.
if (!promRulesRequests.loading) {
fetchPromAndRuler({ dispatch, limitInstances, matcherList, dataSourceName, stateList });
}
const sub = dashboard?.events.subscribe(TimeRangeUpdatedEvent, () =>
fetchPromAndRuler({ dispatch, limitInstances, matcherList, dataSourceName, stateList })
);
const sub = dashboard?.events.subscribe(TimeRangeUpdatedEvent, () => {
if (shouldFetchGrafanaRules) {
refetchGrafanaPromRules();
}
if (!dataSourceName || dataSourceName !== GRAFANA_RULES_SOURCE_NAME) {
fetchPromAndRuler({ dispatch, limitInstances, matcherList, dataSourceName, stateList });
}
});
return () => {
sub?.unsubscribe();
};
}, [dispatch, dashboard, matcherList, stateList, limitInstances, dataSourceName, promRulesRequests.loading]);
}, [
dispatch,
dashboard,
matcherList,
stateList,
limitInstances,
dataSourceName,
refetchGrafanaPromRules,
shouldFetchGrafanaRules,
promRulesRequests.loading,
]);
const handleInstancesLimit = (limit: boolean) => {
if (limit) {
@ -158,18 +193,7 @@ export function UnifiedAlertList(props: PanelProps<UnifiedAlertListOptions>) {
}
};
//For grafana managed rules, get the result using RTK Query to avoid the need of using the redux store
//See https://github.com/grafana/grafana/pull/70482
const { currentData: promRules = [], isLoading: grafanaRulesLoading } = usePrometheusRulesByNamespaceQuery(
{
limitAlerts: limitInstances ? INSTANCES_DISPLAY_LIMIT : undefined,
matcher: matcherList,
state: stateList,
},
{ skip: dataSourceName !== GRAFANA_RULES_SOURCE_NAME }
);
const combinedRules = useCombinedRuleNamespaces(undefined, promRules);
const combinedRules = useCombinedRuleNamespaces(undefined, grafanaPromRules);
const someRulerRulesDispatched = isAsyncRequestMapSlicePartiallyDispatched(rulerRulesRequests);
const haveResults = isAsyncRequestMapSlicePartiallyFulfilled(promRulesRequests);