From ae7e8ff7757c95ea1065f429faa010608683e9b5 Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Fri, 4 Aug 2023 16:59:40 +0200 Subject: [PATCH] [v10.1.x] Alerting: Fix cloud rules editing (#72928) Co-authored-by: Konrad Lalik Fix cloud rules editing (#72927) --- .../alerting/unified/CloneRuleEditor.test.tsx | 3 +++ .../alert-groups/MatcherFilter.test.tsx | 6 +++--- .../rule-editor/CloudRulesSourcePicker.tsx | 12 ++++++++++-- .../CloudDataSourceSelector.tsx | 6 ++++-- .../QueryAndExpressionsStep.tsx | 2 +- .../features/alerting/unified/utils/rule-form.ts | 16 ++++++++++++++++ 6 files changed, 37 insertions(+), 8 deletions(-) diff --git a/public/app/features/alerting/unified/CloneRuleEditor.test.tsx b/public/app/features/alerting/unified/CloneRuleEditor.test.tsx index a40da75e65b..c3c0cd1a7a7 100644 --- a/public/app/features/alerting/unified/CloneRuleEditor.test.tsx +++ b/public/app/features/alerting/unified/CloneRuleEditor.test.tsx @@ -32,6 +32,7 @@ import { import { mockAlertmanagerConfigResponse } from './mocks/alertmanagerApi'; import { mockSearchApiResponse } from './mocks/grafanaApi'; import { mockRulerRulesApiResponse, mockRulerRulesGroupApiResponse } from './mocks/rulerApi'; +import { AlertingQueryRunner } from './state/AlertingQueryRunner'; import { RuleFormValues } from './types/rule-form'; import { Annotation } from './utils/constants'; import { GRAFANA_RULES_SOURCE_NAME } from './utils/datasource'; @@ -51,6 +52,8 @@ jest.mock('./components/rule-editor/notificaton-preview/NotificationPreview', () NotificationPreview: () =>
, })); +jest.spyOn(AlertingQueryRunner.prototype, 'run').mockImplementation(() => Promise.resolve()); + const server = setupServer(); beforeAll(() => { diff --git a/public/app/features/alerting/unified/components/alert-groups/MatcherFilter.test.tsx b/public/app/features/alerting/unified/components/alert-groups/MatcherFilter.test.tsx index b5c47e3a8ac..9043b8f8a7c 100644 --- a/public/app/features/alerting/unified/components/alert-groups/MatcherFilter.test.tsx +++ b/public/app/features/alerting/unified/components/alert-groups/MatcherFilter.test.tsx @@ -3,13 +3,13 @@ import userEvent from '@testing-library/user-event'; import lodash from 'lodash'; // eslint-disable-line lodash/import-scope import React from 'react'; -import { logInfo } from '@grafana/runtime'; +import * as runtime from '@grafana/runtime'; import { LogMessages } from '../../Analytics'; import { MatcherFilter } from './MatcherFilter'; -jest.mock('@grafana/runtime'); +const logInfoSpy = jest.spyOn(runtime, 'logInfo'); describe('Analytics', () => { beforeEach(() => { @@ -25,7 +25,7 @@ describe('Analytics', () => { const searchInput = screen.getByTestId('search-query-input'); await userEvent.type(searchInput, 'job='); - expect(logInfo).toHaveBeenCalledWith(LogMessages.filterByLabel); + expect(logInfoSpy).toHaveBeenCalledWith(LogMessages.filterByLabel); }); it('should call onChange handler', async () => { diff --git a/public/app/features/alerting/unified/components/rule-editor/CloudRulesSourcePicker.tsx b/public/app/features/alerting/unified/components/rule-editor/CloudRulesSourcePicker.tsx index cd2c21eb57b..cf90e30003e 100644 --- a/public/app/features/alerting/unified/components/rule-editor/CloudRulesSourcePicker.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/CloudRulesSourcePicker.tsx @@ -9,13 +9,14 @@ import { useRulesSourcesWithRuler } from '../../hooks/useRuleSourcesWithRuler'; import { fetchAllPromBuildInfoAction } from '../../state/actions'; interface Props { + disabled?: boolean; onChange: (ds: DataSourceInstanceSettings) => void; value: string | null; onBlur?: () => void; name?: string; } -export function CloudRulesSourcePicker({ value, ...props }: Props): JSX.Element { +export function CloudRulesSourcePicker({ value, disabled, ...props }: Props): JSX.Element { const rulesSourcesWithRuler = useRulesSourcesWithRuler(); const { loading = true } = useAsync(() => dispatch(fetchAllPromBuildInfoAction()), [dispatch]); @@ -28,6 +29,13 @@ export function CloudRulesSourcePicker({ value, ...props }: Props): JSX.Element ); return ( - + ); } diff --git a/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/CloudDataSourceSelector.tsx b/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/CloudDataSourceSelector.tsx index c8140a5eec1..510b50c4a3d 100644 --- a/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/CloudDataSourceSelector.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/CloudDataSourceSelector.tsx @@ -9,9 +9,10 @@ import { RuleFormType, RuleFormValues } from '../../../types/rule-form'; import { CloudRulesSourcePicker } from '../CloudRulesSourcePicker'; export interface CloudDataSourceSelectorProps { + disabled?: boolean; onChangeCloudDatasource: (datasourceUid: string) => void; } -export const CloudDataSourceSelector = ({ onChangeCloudDatasource }: CloudDataSourceSelectorProps) => { +export const CloudDataSourceSelector = ({ disabled, onChangeCloudDatasource }: CloudDataSourceSelectorProps) => { const { control, formState: { errors }, @@ -28,7 +29,7 @@ export const CloudDataSourceSelector = ({ onChangeCloudDatasource }: CloudDataSo {(ruleFormType === RuleFormType.cloudAlerting || ruleFormType === RuleFormType.cloudRecording) && ( ( { // reset location if switching data sources, as different rules source will have different groups and namespaces setValue('location', undefined); diff --git a/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/QueryAndExpressionsStep.tsx b/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/QueryAndExpressionsStep.tsx index 294438609da..4defe01b82d 100644 --- a/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/QueryAndExpressionsStep.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/QueryAndExpressionsStep.tsx @@ -340,7 +340,7 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P > {/* This is the cloud data source selector */} {(type === RuleFormType.cloudRecording || type === RuleFormType.cloudAlerting) && ( - + )} {/* This is the PromQL Editor for recording rules */} diff --git a/public/app/features/alerting/unified/utils/rule-form.ts b/public/app/features/alerting/unified/utils/rule-form.ts index 63eb52d6a68..f1301654ac8 100644 --- a/public/app/features/alerting/unified/utils/rule-form.ts +++ b/public/app/features/alerting/unified/utils/rule-form.ts @@ -174,11 +174,27 @@ export function rulerRuleToFormValues(ruleWithLocation: RuleWithLocation): RuleF } } else { if (isAlertingRulerRule(rule)) { + const datasourceUid = getDataSourceSrv().getInstanceSettings(ruleSourceName)?.uid ?? ''; + + const defaultQuery = { + refId: 'A', + datasourceUid, + queryType: '', + relativeTimeRange: getDefaultRelativeTimeRange(), + expr: rule.expr, + model: { + refId: 'A', + hide: false, + expr: rule.expr, + }, + }; + const alertingRuleValues = alertingRulerRuleToRuleForm(rule); return { ...defaultFormValues, ...alertingRuleValues, + queries: [defaultQuery], annotations: normalizeDefaultAnnotations(listifyLabelsOrAnnotations(rule.annotations, false)), type: RuleFormType.cloudAlerting, dataSourceName: ruleSourceName,