@grafana/alerting
Version:
Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution
1 lines • 5.25 kB
Source Map (JSON)
{"version":3,"file":"useMatchPolicies.mjs","sources":["../../../../../src/grafana/notificationPolicies/hooks/useMatchPolicies.ts"],"sourcesContent":["import { useCallback } from 'react';\n\nimport { RoutingTree, notificationsAPI } from '../../api/notifications/v0alpha1/notifications.api.gen';\nimport { Label } from '../../matchers/types';\nimport { USER_DEFINED_TREE_NAME } from '../consts';\nimport { Route, RouteWithID } from '../types';\nimport { RouteMatchResult, 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\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() {\n const { data, ...rest } = notificationsAPI.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 * 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":["notificationsAPI"],"mappings":";;;;;;AAqCO,SAAS,6BAAA,GAAgC;AAC9C,EAAA,MAAM,EAAE,IAAA,EAAM,GAAG,MAAK,GAAIA,cAAA,CAAiB,UAAU,eAAA,CAAgB,QAAA;AAAA,IACnE,EAAC;AAAA,IACD;AAAA,MACE,cAAA,EAAgB,IAAA;AAAA,MAChB,kBAAA,EAAoB;AAAA;AACtB,GACF;AAEA,EAAA,MAAM,gBAAA,GAAmB,WAAA;AAAA,IACvB,CAAC,SAAA,KAAsB;AA/C3B,MAAA,IAAA,EAAA;AA+C8B,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,E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