From 738e023d13ff8339c15785d8804d4a241fb27821 Mon Sep 17 00:00:00 2001 From: Gilles De Mey Date: Thu, 10 Nov 2022 16:15:38 +0100 Subject: [PATCH] Alerting: Fix threshold expression rewire (#58334) --- .../components/rule-editor/util.test.ts | 30 +++++++++++++++++++ .../unified/components/rule-editor/util.ts | 3 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/public/app/features/alerting/unified/components/rule-editor/util.test.ts b/public/app/features/alerting/unified/components/rule-editor/util.test.ts index 2dec00d720a..42bb8d8fdb6 100644 --- a/public/app/features/alerting/unified/components/rule-editor/util.test.ts +++ b/public/app/features/alerting/unified/components/rule-editor/util.test.ts @@ -94,6 +94,28 @@ describe('rule-editor', () => { queryType: '', }; + const thresholdExpression = { + refId: 'C', + datasourceUid: '-100', + model: { + refId: 'C', + type: 'threshold', + expression: 'B', + datasource: { + type: '__expr__', + uid: '-100', + }, + conditions: [ + { + evaluator: { + params: [0, 'gt'], + }, + }, + ], + }, + queryType: '', + }; + describe('rewires query names', () => { it('should rewire classic expressions', () => { const queries: AlertQuery[] = [dataSource, classicCondition]; @@ -133,6 +155,14 @@ describe('rule-editor', () => { expect(queryModel.expression).toBe('C'); }); + it('should rewire threshold expressions', () => { + const queries: AlertQuery[] = [dataSource, reduceExpression, thresholdExpression]; + const rewiredQueries = queriesWithUpdatedReferences(queries, 'B', 'REDUCER'); + + const queryModel = rewiredQueries[2].model as ExpressionQuery; + expect(queryModel.expression).toBe('REDUCER'); + }); + it('should rewire multiple expressions', () => { const queries: AlertQuery[] = [dataSource, mathExpression, resampleExpression]; const rewiredQueries = queriesWithUpdatedReferences(queries, 'A', 'C'); diff --git a/public/app/features/alerting/unified/components/rule-editor/util.ts b/public/app/features/alerting/unified/components/rule-editor/util.ts index dd5ee24759f..5d833ab9de8 100644 --- a/public/app/features/alerting/unified/components/rule-editor/util.ts +++ b/public/app/features/alerting/unified/components/rule-editor/util.ts @@ -23,6 +23,7 @@ export function queriesWithUpdatedReferences( const isReduceExpression = query.model.type === 'reduce'; const isResampleExpression = query.model.type === 'resample'; const isClassicExpression = query.model.type === 'classic_conditions'; + const isThresholdExpression = query.model.type === 'threshold'; if (isMathExpression) { return { @@ -34,7 +35,7 @@ export function queriesWithUpdatedReferences( }; } - if (isResampleExpression || isReduceExpression) { + if (isResampleExpression || isReduceExpression || isThresholdExpression) { const isReferencing = query.model.expression === previousRefId; return {