diff --git a/public/app/features/alerting/unified/hooks/useCombinedRule.ts b/public/app/features/alerting/unified/hooks/useCombinedRule.ts index 7f0239142fc..a01cce491b8 100644 --- a/public/app/features/alerting/unified/hooks/useCombinedRule.ts +++ b/public/app/features/alerting/unified/hooks/useCombinedRule.ts @@ -1,6 +1,7 @@ import { useEffect, useMemo } from 'react'; import { useAsync } from 'react-use'; +import { isGrafanaRulesSource } from 'app/features/alerting/unified/utils/datasource'; import { CombinedRule, RuleIdentifier, RulesSource, RuleWithLocation } from 'app/types/unified-alerting'; import { RulerRuleGroupDTO } from 'app/types/unified-alerting-dto'; @@ -10,7 +11,8 @@ import { getDataSourceByName } from '../utils/datasource'; import * as ruleId from '../utils/rule-id'; import { isCloudRuleIdentifier, isGrafanaRuleIdentifier, isPrometheusRuleIdentifier } from '../utils/rules'; -import { attachRulerRulesToCombinedRules, combineRulesNamespaces } from './useCombinedRuleNamespaces'; +import { attachRulerRulesToCombinedRules, combineRulesNamespace } from './useCombinedRuleNamespaces'; +import { stringifyFolder, useFolder } from './useFolder'; export function useCloudCombinedRulesMatching( ruleName: string, @@ -116,6 +118,10 @@ export function useCombinedRule({ ruleIdentifier, limitAlerts }: Props): Request refetchOnMountOrArgChange: true, } ); + // in case of Grafana folder, we need to use the folder name instead of uid, as in promrules we don't use uid + const isGrafanaRule = isGrafanaRulesSource(ruleSourceName); + const folder = useFolder(isGrafanaRule ? ruleLocation?.namespace : undefined); + const namespaceName = isGrafanaRule && folder.folder ? stringifyFolder(folder.folder) : ruleLocation?.namespace; const [ fetchRulerRuleGroup, @@ -139,9 +145,9 @@ export function useCombinedRule({ ruleIdentifier, limitAlerts }: Props): Request return; } - const rulerConfig = rulerRuleGroup ? { [ruleLocation.namespace]: [rulerRuleGroup] } : {}; + const rulerConfig = rulerRuleGroup && namespaceName ? { [namespaceName]: [rulerRuleGroup] } : {}; - const combinedNamespaces = combineRulesNamespaces(ruleSource, promRuleNs, rulerConfig); + const combinedNamespaces = combineRulesNamespace(ruleSource, promRuleNs, rulerConfig); const combinedRules = combinedNamespaces.flatMap((ns) => ns.groups).flatMap((group) => group.rules); const matchingRule = combinedRules.find((rule) => @@ -149,7 +155,7 @@ export function useCombinedRule({ ruleIdentifier, limitAlerts }: Props): Request ); return matchingRule; - }, [ruleIdentifier, ruleSourceName, promRuleNs, rulerRuleGroup, ruleSource, ruleLocation]); + }, [ruleIdentifier, ruleSourceName, promRuleNs, rulerRuleGroup, ruleSource, ruleLocation, namespaceName]); return { loading: isLoadingDsFeatures || isLoadingPromRules || isLoadingRulerGroup, diff --git a/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.ts b/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.ts index a8d9ac4be77..c95a2b2ebe6 100644 --- a/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.ts +++ b/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.ts @@ -126,7 +126,7 @@ export function useCombinedRuleNamespaces( }, [promRulesResponses, rulerRulesResponses, rulesSources, grafanaPromRuleNamespaces]); } -export function combineRulesNamespaces( +export function combineRulesNamespace( rulesSource: RulesSource, promNamespaces: RuleNamespace[], rulerRules?: RulerRulesConfigDTO diff --git a/public/app/features/alerting/unified/hooks/useFolder.ts b/public/app/features/alerting/unified/hooks/useFolder.ts index c627453445b..e4fcd3882f8 100644 --- a/public/app/features/alerting/unified/hooks/useFolder.ts +++ b/public/app/features/alerting/unified/hooks/useFolder.ts @@ -32,3 +32,7 @@ export function useFolder(uid?: string): ReturnBag { loading: false, }; } + +export function stringifyFolder({ title, parents }: FolderDTO) { + return parents && parents?.length ? [...parents.map((p) => p.title), title].join('/') : title; +}