From 2ab832e0259ee5695a1139c0156473cc5042d628 Mon Sep 17 00:00:00 2001 From: Konrad Lalik Date: Mon, 29 Jan 2024 13:02:04 +0100 Subject: [PATCH] Alerting: Use only the alert condition query as the graph threshold (#81228) Use only the alert condition query as the graph threshold --- .../unified/GrafanaRuleQueryViewer.tsx | 2 +- .../components/rule-editor/QueryRows.tsx | 4 +-- .../components/rule-editor/util.test.ts | 28 ++++++++++--------- .../unified/components/rule-editor/util.ts | 10 ++++++- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx b/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx index 8ae943faecc..52ba9bbb731 100644 --- a/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx +++ b/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx @@ -46,7 +46,7 @@ export function GrafanaRuleQueryViewer({ const expressions = queries.filter((q) => isExpressionQuery(q.model)); const styles = useStyles2(getExpressionViewerStyles); - const thresholds = getThresholdsForQueries(queries); + const thresholds = getThresholdsForQueries(queries, condition); return ( diff --git a/public/app/features/alerting/unified/components/rule-editor/QueryRows.tsx b/public/app/features/alerting/unified/components/rule-editor/QueryRows.tsx index fc17b0b323e..55cb20a1dac 100644 --- a/public/app/features/alerting/unified/components/rule-editor/QueryRows.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/QueryRows.tsx @@ -144,8 +144,8 @@ export class QueryRows extends PureComponent { }; render() { - const { queries, expressions } = this.props; - const thresholdByRefId = getThresholdsForQueries([...queries, ...expressions]); + const { queries, expressions, condition } = this.props; + const thresholdByRefId = getThresholdsForQueries([...queries, ...expressions], condition); return ( 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 fda4601ead3..ec51a60f31e 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 @@ -245,12 +245,12 @@ describe('checkForPathSeparator', () => { describe('getThresholdsForQueries', () => { it('should work for threshold condition', () => { - const queries = createThresholdExample('gt'); - expect(getThresholdsForQueries(queries)).toMatchSnapshot(); + const [queries, condition] = createThresholdExample('gt'); + expect(getThresholdsForQueries(queries, condition)).toMatchSnapshot(); }); it('should work for classic_condition', () => { - const [dataQuery] = createThresholdExample('gt'); + const [[dataQuery]] = createThresholdExample('gt'); const classicCondition = { refId: 'B', @@ -282,7 +282,7 @@ describe('getThresholdsForQueries', () => { }, }; - const thresholdsClassic = getThresholdsForQueries([dataQuery, classicCondition]); + const thresholdsClassic = getThresholdsForQueries([dataQuery, classicCondition], classicCondition.refId); expect(thresholdsClassic).toMatchSnapshot(); }); @@ -331,30 +331,32 @@ describe('getThresholdsForQueries', () => { }; expect(() => { - const thresholds = getThresholdsForQueries([dataQuery, classicCondition]); + const thresholds = getThresholdsForQueries([dataQuery, classicCondition], classicCondition.refId); expect(thresholds).toStrictEqual({}); }).not.toThrowError(); }); it('should work for within_range', () => { - const queries = createThresholdExample('within_range'); - const thresholds = getThresholdsForQueries(queries); + const [queries, condition] = createThresholdExample('within_range'); + const thresholds = getThresholdsForQueries(queries, condition); expect(thresholds).toMatchSnapshot(); }); it('should work for lt and gt', () => { - expect(getThresholdsForQueries(createThresholdExample('gt'))).toMatchSnapshot(); - expect(getThresholdsForQueries(createThresholdExample('lt'))).toMatchSnapshot(); + const [gtQueries, qtCondition] = createThresholdExample('gt'); + const [ltQueries, ltCondition] = createThresholdExample('lt'); + expect(getThresholdsForQueries(gtQueries, qtCondition)).toMatchSnapshot(); + expect(getThresholdsForQueries(ltQueries, ltCondition)).toMatchSnapshot(); }); it('should work for outside_range', () => { - const queries = createThresholdExample('outside_range'); - const thresholds = getThresholdsForQueries(queries); + const [queries, condition] = createThresholdExample('outside_range'); + const thresholds = getThresholdsForQueries(queries, condition); expect(thresholds).toMatchSnapshot(); }); }); -function createThresholdExample(thresholdType: string): AlertQuery[] { +function createThresholdExample(thresholdType: string): [AlertQuery[], string] { const dataQuery: AlertQuery = { refId: 'A', datasourceUid: 'abc123', @@ -403,7 +405,7 @@ function createThresholdExample(thresholdType: string): AlertQuery[] { }, }; - return [dataQuery, reduceExpression, thresholdExpression]; + return [[dataQuery, reduceExpression, thresholdExpression], thresholdExpression.refId]; } describe('findRenamedReferences', () => { 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 3bc456259a6..118c0057d47 100644 --- a/public/app/features/alerting/unified/components/rule-editor/util.ts +++ b/public/app/features/alerting/unified/components/rule-editor/util.ts @@ -144,10 +144,14 @@ export type ThresholdDefinitions = Record; /** * This function will retrieve threshold definitions for the given array of data and expression queries. */ -export function getThresholdsForQueries(queries: AlertQuery[]) { +export function getThresholdsForQueries(queries: AlertQuery[], condition: string | null) { const thresholds: ThresholdDefinitions = {}; const SUPPORTED_EXPRESSION_TYPES = [ExpressionQueryType.threshold, ExpressionQueryType.classic]; + if (!condition) { + return thresholds; + } + for (const query of queries) { if (!isExpressionQuery(query.model)) { continue; @@ -162,6 +166,10 @@ export function getThresholdsForQueries(queries: AlertQuery[]) { continue; } + if (query.model.refId !== condition) { + continue; + } + // if any of the conditions are a "range" we switch to an "area" threshold view and ignore single threshold values // the time series panel does not support both. const hasRangeThreshold = query.model.conditions.some(isRangeCondition);