diff --git a/public/app/plugins/panel/alertlist/UnifiedAlertList.tsx b/public/app/plugins/panel/alertlist/UnifiedAlertList.tsx index 5f8c608b484..a29404959f7 100644 --- a/public/app/plugins/panel/alertlist/UnifiedAlertList.tsx +++ b/public/app/plugins/panel/alertlist/UnifiedAlertList.tsx @@ -135,18 +135,53 @@ export function UnifiedAlertList(props: PanelProps) { [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) { } }; - //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);