@grafana/alerting
Version:
Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution
1 lines • 7.25 kB
Source Map (JSON)
{"version":3,"file":"useMatchPolicies.cjs","sources":["../../../../../src/grafana/notificationPolicies/hooks/useMatchPolicies.ts"],"sourcesContent":["import { useCallback, useMemo } from 'react';\n\nimport {\n type RoutingTree,\n generatedAPI as notificationsAPIv0alpha1,\n} from '@grafana/api-clients/rtkq/notifications.alerting/v0alpha1';\n\nimport { type Label } from '../../matchers/types';\nimport { USER_DEFINED_TREE_NAME } from '../consts';\nimport { type Route, type RouteWithID } from '../types';\nimport { type RouteMatchResult, type TreeMatch, convertRoutingTreeToRoute, matchInstancesToRoute } from '../utils';\n\nexport type RouteMatch = {\n route: Route;\n routeTree: {\n // Add some metadata about the tree that is useful for displaying diagnostics\n metadata: Pick<RoutingTree['metadata'], 'name'>;\n // We'll include the entire expanded policy tree for diagnostics\n expandedSpec: RouteWithID;\n };\n matchDetails: RouteMatchResult<RouteWithID>;\n};\n\nexport type InstanceMatchResult = {\n // The labels we used to match to our policies\n labels: Label[];\n // The routes that matched the labels where the key is a route and the value is an array of instances that match that route\n matchedRoutes: RouteMatch[];\n};\n\ninterface UseMatchInstancesToRouteTreesReturnType\n extends ReturnType<typeof notificationsAPIv0alpha1.endpoints.listRoutingTree.useQuery> {\n matchInstancesToRouteTrees: (instances: Label[][]) => InstanceMatchResult[];\n}\n\n/**\n * React hook that finds notification policy routes in all routing trees that match the provided set of alert instances.\n *\n * This hook queries the routing tree API and processes each tree to:\n * 1. Convert RoutingTree structures to Route structures\n * 2. Compute the inherited properties for each node in the tree\n * 3. Find routes within each tree that match the given set of labels\n *\n * @returns An object containing a `matchInstancesToRoutingTrees` function that takes alert instances\n * and returns an array of InstanceMatchResult objects, each containing the matched routes and matching details\n */\nexport function useMatchInstancesToRouteTrees(): UseMatchInstancesToRouteTreesReturnType {\n const { data, ...rest } = notificationsAPIv0alpha1.endpoints.listRoutingTree.useQuery(\n {},\n {\n refetchOnFocus: true,\n refetchOnReconnect: true,\n }\n );\n\n const memoizedFunction = useCallback(\n (instances: Label[][]) => matchInstancesToRouteTrees(data?.items ?? [], instances),\n [data?.items]\n );\n\n return {\n matchInstancesToRouteTrees: memoizedFunction,\n ...rest,\n };\n}\n\n/**\n * React hook that finds notification policy routes in a single routing tree that match the provided set of alert instances.\n *\n * Unlike `useMatchInstancesToRouteTrees` which matches against all trees,\n * this hook matches against a specific tree (e.g. one selected via the RoutingTreeSelector).\n *\n * @param routingTree - The routing tree to match against, or null if none selected\n * @param instances - Array of label sets to match against the tree\n *\n * @example\n * ```tsx\n * const [selectedTree, setSelectedTree] = useState<RoutingTree | null>(null);\n * const labels: Label[] = [['severity', 'critical'], ['team', 'platform']];\n *\n * const result = useMatchInstancesToSpecificRouteTree(selectedTree, [labels]);\n * // result.matchedPolicies → Map<Route, MatchResult[]>\n * // result.expandedTree → full tree with inherited properties\n * ```\n */\nexport function useMatchInstancesToSpecificRouteTree(\n routingTree: RoutingTree | null,\n instances: Label[][]\n): TreeMatch | null {\n return useMemo(() => {\n if (!routingTree) {\n return null;\n }\n\n const rootRoute = convertRoutingTreeToRoute(routingTree);\n return matchInstancesToRoute(rootRoute, instances);\n }, [routingTree, instances]);\n}\n\n/**\n * This function will match a set of labels to multiple routing trees. Assumes a list of routing trees has already been fetched.\n *\n * Use \"useMatchInstancesToRouteTrees\" if you want the hook to automatically fetch the latest definition of routing trees.\n */\nexport function matchInstancesToRouteTrees(trees: RoutingTree[], instances: Label[][]): InstanceMatchResult[] {\n // Process each tree and get matches for all instances\n const treeMatches = trees.map<TreeMatch>((tree) => {\n const rootRoute = convertRoutingTreeToRoute(tree);\n return matchInstancesToRoute(rootRoute, instances);\n });\n\n // Group results by instance\n return instances.map<InstanceMatchResult>((labels) => {\n // Collect matches for this specific instance from all trees\n const allMatchedRoutes = treeMatches.flatMap(({ expandedTree, matchedPolicies }, index) => {\n const tree = trees[index];\n\n return Array.from(matchedPolicies.entries()).flatMap(([route, results]) =>\n results\n .filter((matchDetails) => matchDetails.labels === labels)\n .map((matchDetails) => ({\n route,\n routeTree: {\n metadata: { name: tree.metadata.name ?? USER_DEFINED_TREE_NAME },\n expandedSpec: expandedTree,\n },\n matchDetails,\n }))\n );\n });\n\n return {\n labels,\n matchedRoutes: allMatchedRoutes,\n };\n });\n}\n"],"names":["notificationsAPIv0alpha1","useCallback","useMemo","convertRoutingTreeToRoute","matchInstancesToRoute","USER_DEFINED_TREE_NAME"],"mappings":";;;;;;;;;;AA8CO,SAAS,6BAAA,GAAyE;AACvF,EAAA,MAAM,EAAE,IAAA,EAAM,GAAG,MAAK,GAAIA,qBAAA,CAAyB,UAAU,eAAA,CAAgB,QAAA;AAAA,IAC3E,EAAC;AAAA,IACD;AAAA,MACE,cAAA,EAAgB,IAAA;AAAA,MAChB,kBAAA,EAAoB;AAAA;AACtB,GACF;AAEA,EAAA,MAAM,gBAAA,GAAmBC,iBAAA;AAAA,IACvB,CAAC,SAAA,KAAsB;AAxD3B,MAAA,IAAA,EAAA;AAwD8B,MAAA,OAAA,0BAAA,CAAA,CAA2B,EAAA,GAAA,IAAA,IAAA,IAAA,GAAA,KAAA,CAAA,GAAA,IAAA,CAAM,KAAA,KAAN,IAAA,GAAA,EAAA,GAAe,IAAI,SAAS,CAAA;AAAA,IAAA,CAAA;AAAA,IACjF,CAAC,6BAAM,KAAK;AAAA,GACd;AAEA,EAAA,OAAO;AAAA,IACL,0BAAA,EAA4B,gBAAA;AAAA,IAC5B,GAAG;AAAA,GACL;AACF;AAqBO,SAAS,oCAAA,CACd,aACA,SAAA,EACkB;AAClB,EAAA,OAAOC,cAAQ,MAAM;AACnB,IAAA,IAAI,CAAC,WAAA,EAAa;AAChB,MAAA,OAAO,IAAA;AAAA,IACT;AAEA,IAAA,MAAM,SAAA,GAAYC,gCAA0B,WAAW,CAAA;AACvD,IAAA,OAAOC,2BAAA,CAAsB,WAAW,SAAS,CAAA;AAAA,EACnD,CAAA,EAAG,CAAC,WAAA,EA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