From 39077769a8c1a2565f53023880905636cbdb6ecd Mon Sep 17 00:00:00 2001 From: Gilles De Mey Date: Fri, 25 Aug 2023 20:24:50 +0200 Subject: [PATCH] Alerting: Fix data source copy when switching alert rule types (#73854) --- .../RuleEditorCloudOnlyAllowed.test.tsx | 6 ++--- .../QueryAndExpressionsStep.tsx | 22 +++++++++---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/public/app/features/alerting/unified/RuleEditorCloudOnlyAllowed.test.tsx b/public/app/features/alerting/unified/RuleEditorCloudOnlyAllowed.test.tsx index b1a2a6ea4fc..dc52fb645ae 100644 --- a/public/app/features/alerting/unified/RuleEditorCloudOnlyAllowed.test.tsx +++ b/public/app/features/alerting/unified/RuleEditorCloudOnlyAllowed.test.tsx @@ -68,6 +68,7 @@ const dataSources = { { type: DataSourceType.Prometheus, name: 'cortex with ruler', + isDefault: true, }, { alerting: true } ), @@ -185,9 +186,6 @@ describe('RuleEditor cloud: checking editable data sources', () => { await ui.inputs.name.find(); - const removeExpressionsButtons = screen.getAllByLabelText('Remove expression'); - expect(removeExpressionsButtons).toHaveLength(2); - const switchToCloudButton = screen.getByText('Switch to data source-managed alert rule'); expect(switchToCloudButton).toBeInTheDocument(); @@ -200,8 +198,8 @@ describe('RuleEditor cloud: checking editable data sources', () => { const dataSourceSelect = ui.inputs.dataSource.get(); await userEvent.click(byRole('combobox').get(dataSourceSelect)); - expect(await byText('loki with ruler').query()).toBeInTheDocument(); expect(byText('cortex with ruler').query()).toBeInTheDocument(); + expect(byText('loki with ruler').query()).toBeInTheDocument(); expect(byText('loki with local rule store').query()).not.toBeInTheDocument(); expect(byText('prom without ruler api').query()).not.toBeInTheDocument(); expect(byText('splunk').query()).not.toBeInTheDocument(); 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 4defe01b82d..cae1906c3be 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 @@ -175,11 +175,6 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P (updatedQueries: AlertQuery[]) => { const query = updatedQueries[0]; - const dataSourceSettings = getDataSourceSrv().getInstanceSettings(query.datasourceUid); - if (!dataSourceSettings) { - throw new Error('The Data source has not been defined.'); - } - if (!isPromOrLokiQuery(query.model)) { return; } @@ -187,13 +182,12 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P const expression = query.model.expr; setValue('queries', updatedQueries, { shouldValidate: false }); - setValue('dataSourceName', dataSourceSettings.name); - setValue('expression', expression); + updateExpressionAndDatasource(updatedQueries); dispatch(setRecordingRulesQueries({ recordingRuleQueries: updatedQueries, expression })); runQueriesPreview(); }, - [runQueriesPreview, setValue] + [runQueriesPreview, setValue, updateExpressionAndDatasource] ); const recordingRuleDefaultDatasource = rulesSourcesWithRuler[0]; @@ -312,10 +306,14 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P const typeInForm = getValues('type'); if (typeInForm === RuleFormType.cloudAlerting) { setValue('type', RuleFormType.grafana); + setValue('dataSourceName', null); // set data source name back to "null" + prevExpressions.length > 0 && restoreExpressionsInQueries(); prevCondition && setValue('condition', prevCondition); } else { setValue('type', RuleFormType.cloudAlerting); + updateExpressionAndDatasource(queries); + const expressions = queries.filter((query) => query.datasourceUid === ExpressionDatasourceUID); setPrevExpressions(expressions); removeExpressionsInQueries(); @@ -324,12 +322,12 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P }, [ getValues, setValue, + prevExpressions.length, + restoreExpressionsInQueries, + prevCondition, + updateExpressionAndDatasource, queries, removeExpressionsInQueries, - restoreExpressionsInQueries, - setPrevExpressions, - prevExpressions, - prevCondition, condition, ]);