From ea4bfc23ca05a466711d5a0a57c8597a2054783f Mon Sep 17 00:00:00 2001 From: Gilles De Mey Date: Tue, 16 Jan 2024 15:30:25 +0100 Subject: [PATCH] Alerting: Improve notification policies view performance (#80209) --- .../alerting/unified/NotificationPolicies.tsx | 6 ++++++ .../unified/utils/notification-policies.test.ts | 13 +++++++++++++ .../alerting/unified/utils/notification-policies.ts | 5 ++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/public/app/features/alerting/unified/NotificationPolicies.tsx b/public/app/features/alerting/unified/NotificationPolicies.tsx index be19ca4ed14..2d29c11cdb3 100644 --- a/public/app/features/alerting/unified/NotificationPolicies.tsx +++ b/public/app/features/alerting/unified/NotificationPolicies.tsx @@ -276,6 +276,12 @@ type RouteFilters = { export const findRoutesMatchingFilters = (rootRoute: RouteWithID, filters: RouteFilters): RouteWithID[] => { const { contactPointFilter, labelMatchersFilter = [] } = filters; + const hasFilter = contactPointFilter || labelMatchersFilter.length > 0; + + // if filters are empty we short-circuit this function + if (!hasFilter) { + return []; + } let matchedRoutes: RouteWithID[][] = []; diff --git a/public/app/features/alerting/unified/utils/notification-policies.test.ts b/public/app/features/alerting/unified/utils/notification-policies.test.ts index e5eb78e6706..8391e07528c 100644 --- a/public/app/features/alerting/unified/utils/notification-policies.test.ts +++ b/public/app/features/alerting/unified/utils/notification-policies.test.ts @@ -1,6 +1,7 @@ import { MatcherOperator, Route, RouteWithID } from 'app/plugins/datasource/alertmanager/types'; import { + InhertitableProperties, computeInheritedTree, findMatchingRoutes, getInheritedProperties, @@ -203,6 +204,18 @@ describe('getInheritedProperties()', () => { const childInherited = getInheritedProperties(parent, child); expect(childInherited).toHaveProperty('group_by', ['label']); }); + + // This scenario is technically impossible unless we have a bug in our code. + // A route cannot both specify a receiver and inherit it from its parent at the same time. + it('should inherit from parent instead of grandparent', () => { + const parent: Route = { receiver: 'parent' }; + const parentInherited: InhertitableProperties = { receiver: 'grandparent', group_by: ['foo'] }; + const child: Route = {}; + + const childInherited = getInheritedProperties(parent, child, parentInherited); + expect(childInherited).toHaveProperty('receiver', 'parent'); + expect(childInherited.group_by).toEqual(['foo']); + }); }); describe('regular "undefined" values', () => { diff --git a/public/app/features/alerting/unified/utils/notification-policies.ts b/public/app/features/alerting/unified/utils/notification-policies.ts index 504f162f3c1..06076324bcb 100644 --- a/public/app/features/alerting/unified/utils/notification-policies.ts +++ b/public/app/features/alerting/unified/utils/notification-policies.ts @@ -160,9 +160,8 @@ function getInheritedProperties( childRoute: Route, propertiesParentInherited?: Partial ) { - const fullParentProperties = merge({}, parentRoute, propertiesParentInherited); - - const inheritableProperties: InhertitableProperties = pick(fullParentProperties, INHERITABLE_KEYS); + const propsFromParent: InhertitableProperties = pick(parentRoute, INHERITABLE_KEYS); + const inheritableProperties: InhertitableProperties = merge({}, propertiesParentInherited, propsFromParent); // TODO how to solve this TypeScript mystery? const inherited = reduce(