UNPKG

@grafana/alerting

Version:

Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution

130 lines (125 loc) 4.4 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var lodash = require('lodash'); var utils = require('../matchers/utils.cjs'); "use strict"; const INHERITABLE_KEYS = ["receiver", "group_by", "group_wait", "group_interval", "repeat_interval"]; function findMatchingRoutes(route, labels, matchingJourney = []) { var _a; let childMatches = []; const matchResult = utils.matchLabels((_a = route.matchers) != null ? _a : [], labels); const currentMatchInfo = { route, matchDetails: matchResult.details, matched: matchResult.matches }; const currentMatchingJourney = [...matchingJourney, currentMatchInfo]; if (!matchResult.matches) { return []; } if (route.routes) { for (const child of route.routes) { const matchingChildren = findMatchingRoutes(child, labels, currentMatchingJourney); childMatches = childMatches.concat(matchingChildren); if (matchingChildren.length && !child.continue) { break; } } } if (childMatches.length === 0) { childMatches.push({ route, labels, matchingJourney: currentMatchingJourney }); } return childMatches; } function computeInheritedTree(parent) { var _a; return { ...parent, routes: (_a = parent.routes) == null ? void 0 : _a.map((child) => { const inheritedProperties = getInheritedProperties(parent, child); return computeInheritedTree({ ...child, ...inheritedProperties }); }) }; } function getInheritedProperties(parentRoute, childRoute, propertiesParentInherited) { const propsFromParent = lodash.pick(parentRoute, INHERITABLE_KEYS); const inheritableProperties = { ...propsFromParent, ...propertiesParentInherited }; const inherited = lodash.reduce( inheritableProperties, // @ts-ignore TSGO / TS7 see above comment about the reduce function signature. (inheritedProperties, parentValue, property) => { var _a; const parentHasValue = parentValue != null; const inheritableValues = [void 0, "", null]; const childIsInheriting = inheritableValues.some((value) => childRoute[property] === value); const inheritFromValue = childIsInheriting && parentHasValue; const inheritEmptyGroupByFromParent = property === "group_by" && parentHasValue && lodash.isArray(childRoute[property]) && ((_a = childRoute[property]) == null ? void 0 : _a.length) === 0; const inheritFromParent = inheritFromValue || inheritEmptyGroupByFromParent; if (inheritFromParent) { inheritedProperties[property] = parentValue; } return inheritedProperties; }, {} ); return inherited; } function addUniqueIdentifier(route) { var _a, _b; return { id: lodash.uniqueId("route-"), ...route, routes: (_b = (_a = route.routes) == null ? void 0 : _a.map(addUniqueIdentifier)) != null ? _b : [] }; } function matchInstancesToRoute(rootRoute, instances) { const matchedPolicies = /* @__PURE__ */ new Map(); const expandedTree = addUniqueIdentifier(computeInheritedTree(rootRoute)); const matchesArray = instances.flatMap((labels) => findMatchingRoutes(expandedTree, labels)); const groupedByRoute = lodash.groupBy(matchesArray, (match) => match.route.id); Object.entries(groupedByRoute).forEach(([_key, match]) => { matchedPolicies.set(match[0].route, match); }); return { expandedTree, matchedPolicies }; } function convertRoutingTreeToRoute(routingTree) { const convertRoutingTreeRoutes = (routes) => { return routes.map( (route) => ({ ...route, routes: route.routes ? convertRoutingTreeRoutes(route.routes) : [] }) ); }; const rootRoute = { ...routingTree.spec.defaults, continue: false, active_time_intervals: [], mute_time_intervals: [], matchers: [], // Root route has no matchers (catch-all) routes: convertRoutingTreeRoutes(routingTree.spec.routes) }; return rootRoute; } exports.INHERITABLE_KEYS = INHERITABLE_KEYS; exports.addUniqueIdentifier = addUniqueIdentifier; exports.computeInheritedTree = computeInheritedTree; exports.convertRoutingTreeToRoute = convertRoutingTreeToRoute; exports.findMatchingRoutes = findMatchingRoutes; exports.getInheritedProperties = getInheritedProperties; exports.matchInstancesToRoute = matchInstancesToRoute; //# sourceMappingURL=utils.cjs.map