Alerting: Use only the alert condition query as the graph threshold (#81228)

Use only the alert condition query as the graph threshold
This commit is contained in:
Konrad Lalik 2024-01-29 13:02:04 +01:00 committed by GitHub
parent 1d5edb2a18
commit 2ab832e025
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 17 deletions

View File

@ -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 (
<Stack gap={2} direction="column">

View File

@ -144,8 +144,8 @@ export class QueryRows extends PureComponent<Props> {
};
render() {
const { queries, expressions } = this.props;
const thresholdByRefId = getThresholdsForQueries([...queries, ...expressions]);
const { queries, expressions, condition } = this.props;
const thresholdByRefId = getThresholdsForQueries([...queries, ...expressions], condition);
return (
<DragDropContext onDragEnd={this.onDragEnd}>

View File

@ -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', () => {

View File

@ -144,10 +144,14 @@ export type ThresholdDefinitions = Record<string, ThresholdDefinition>;
/**
* 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);