@grafana/alerting
Version:
Grafana Alerting Library – Build vertical integrations on top of the industry-leading alerting solution
120 lines (117 loc) • 4.11 kB
JavaScript
import { pick, reduce, isArray, uniqueId, groupBy } from 'lodash';
import { matchLabels } from '../matchers/utils.mjs';
;
const INHERITABLE_KEYS = ["receiver", "group_by", "group_wait", "group_interval", "repeat_interval"];
function findMatchingRoutes(route, labels, matchingJourney = []) {
var _a;
let childMatches = [];
const matchResult = 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 = pick(parentRoute, INHERITABLE_KEYS);
const inheritableProperties = {
...propsFromParent,
...propertiesParentInherited
};
const inherited = 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 && 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: 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 = 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;
}
export { INHERITABLE_KEYS, addUniqueIdentifier, computeInheritedTree, convertRoutingTreeToRoute, findMatchingRoutes, getInheritedProperties, matchInstancesToRoute };
//# sourceMappingURL=utils.mjs.map