From 6fffbe4e1c6d6b3852e12280469ef8c17b8a70eb Mon Sep 17 00:00:00 2001 From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com> Date: Thu, 14 Dec 2023 13:28:10 +0100 Subject: [PATCH] Alerting: Fix queries and expressions in rule view details. (#79497) * fix queries and expressions in rule-view details * Add test --- .../unified/GrafanaRuleQueryViewer.test.tsx | 80 +++++++++++++++++++ .../unified/GrafanaRuleQueryViewer.tsx | 8 +- 2 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 public/app/features/alerting/unified/GrafanaRuleQueryViewer.test.tsx diff --git a/public/app/features/alerting/unified/GrafanaRuleQueryViewer.test.tsx b/public/app/features/alerting/unified/GrafanaRuleQueryViewer.test.tsx new file mode 100644 index 000000000000..6539c35b6776 --- /dev/null +++ b/public/app/features/alerting/unified/GrafanaRuleQueryViewer.test.tsx @@ -0,0 +1,80 @@ +import { render } from '@testing-library/react'; +import { noop } from 'lodash'; +import React from 'react'; + +import { DataSourceRef } from '@grafana/schema'; +import { AlertQuery } from 'app/types/unified-alerting-dto'; + +import { GrafanaRuleQueryViewer } from './GrafanaRuleQueryViewer'; + +describe('GrafanaRuleQueryViewer', () => { + it('renders without crashing', () => { + const getDataSourceQuery = (refId: string) => { + const query: AlertQuery = { + refId: refId, + datasourceUid: 'abc123', + queryType: '', + relativeTimeRange: { + from: 600, + to: 0, + }, + model: { + refId: 'A', + }, + }; + return query; + }; + const queries = [ + getDataSourceQuery('A'), + getDataSourceQuery('B'), + getDataSourceQuery('C'), + getDataSourceQuery('D'), + getDataSourceQuery('E'), + ]; + + const getExpression = (refId: string, dsRef: DataSourceRef) => { + const expr = { + refId: refId, + datasourceUid: '__expr__', + queryType: '', + model: { + refId: refId, + type: 'classic_conditions', + datasource: dsRef, + conditions: [ + { + type: 'query', + evaluator: { + params: [3], + type: 'gt', + }, + operator: { + type: 'and', + }, + query: { + params: ['A'], + }, + reducer: { + params: [], + type: 'last', + }, + }, + ], + }, + }; + return expr; + }; + + const expressions = [ + getExpression('A', { type: '' }), + getExpression('B', { type: '' }), + getExpression('C', { type: '' }), + getExpression('D', { type: '' }), + ]; + const { getByTestId } = render( + + ); + expect(getByTestId('queries-container')).toHaveStyle('flex-wrap: wrap'); + expect(getByTestId('expressions-container')).toHaveStyle('flex-wrap: wrap'); + }); +}); diff --git a/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx b/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx index d6c17ded2490..995ce8d64881 100644 --- a/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx +++ b/public/app/features/alerting/unified/GrafanaRuleQueryViewer.tsx @@ -5,7 +5,7 @@ import React from 'react'; import { DataSourceInstanceSettings, GrafanaTheme2, PanelData, RelativeTimeRange } from '@grafana/data'; import { config } from '@grafana/runtime'; -import { Badge, useStyles2, Stack } from '@grafana/ui'; +import { Badge, Stack, useStyles2 } from '@grafana/ui'; import { mapRelativeTimeRangeToOption } from '@grafana/ui/src/components/DateTimePickers/RelativeTimeRangePicker/utils'; import { AlertQuery } from '../../../types/unified-alerting-dto'; @@ -14,8 +14,8 @@ import { downsamplingTypes, ExpressionQuery, ExpressionQueryType, - reducerModes, ReducerMode, + reducerModes, reducerTypes, thresholdFunctions, upsamplingTypes, @@ -51,7 +51,7 @@ export function GrafanaRuleQueryViewer({ return (
- + {dataQueries.map(({ model, relativeTimeRange, refId, datasourceUid }, index) => { const dataSource = dsByUid[datasourceUid]; @@ -73,7 +73,7 @@ export function GrafanaRuleQueryViewer({
- + {expressions.map(({ model, refId, datasourceUid }, index) => { const dataSource = dsByUid[datasourceUid];