From 2991d4c2136f1a2c65eeb02a72958dfe33f4863a Mon Sep 17 00:00:00 2001 From: Virginia Cepeda Date: Thu, 13 Apr 2023 06:58:59 -0300 Subject: [PATCH] Alerting: Fix creating a recording rule when having multiple datasources (#66415) Fix creating recording rules when having multiple datasources --- .../components/rule-editor/RecordingRuleEditor.tsx | 8 +++++--- .../query-and-alert-condition/AlertType.tsx | 2 ++ .../QueryAndExpressionsStep.tsx | 13 ++++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/public/app/features/alerting/unified/components/rule-editor/RecordingRuleEditor.tsx b/public/app/features/alerting/unified/components/rule-editor/RecordingRuleEditor.tsx index 7a1d741d5ab..cba32d4f669 100644 --- a/public/app/features/alerting/unified/components/rule-editor/RecordingRuleEditor.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/RecordingRuleEditor.tsx @@ -57,17 +57,19 @@ export const RecordingRuleEditor: FC = ({ const handleChangedQuery = (changedQuery: DataQuery) => { const query = queries[0]; + const dataSourceId = getDataSourceSrv().getInstanceSettings(dataSourceName)?.uid; - if (!isPromOrLokiQuery(query.model)) { + if (!isPromOrLokiQuery(changedQuery) || !dataSourceId) { return; } - const expr = query.model.expr; + const expr = changedQuery.expr; const merged = { ...query, refId: changedQuery.refId, - queryType: query.model.queryType ?? '', + queryType: changedQuery.queryType ?? '', + datasourceUid: dataSourceId, expr, model: { refId: changedQuery.refId, diff --git a/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/AlertType.tsx b/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/AlertType.tsx index 73448a0ad42..49a75f973fe 100644 --- a/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/AlertType.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/AlertType.tsx @@ -67,6 +67,8 @@ export const AlertType = ({ editingExistingRule }: Props) => { onChange={(ds: DataSourceInstanceSettings) => { // reset location if switching data sources, as different rules source will have different groups and namespaces setValue('location', undefined); + // reset expression as they don't need to persist after changing datasources + setValue('expression', ''); onChange(ds?.name ?? null); }} /> 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 15d8facc044..68bb7fd6e87 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 @@ -198,19 +198,26 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P useEffect(() => { setPanelData({}); if (type === RuleFormType.cloudRecording) { + const expr = getValues('expression'); + const datasourceUid = + (editingExistingRule && getDataSourceSrv().getInstanceSettings(dataSourceName)?.uid) || + recordingRuleDefaultDatasource.uid; + const defaultQuery = { refId: 'A', - datasourceUid: recordingRuleDefaultDatasource.uid, + datasourceUid, queryType: '', relativeTimeRange: getDefaultRelativeTimeRange(), + expr, model: { refId: 'A', hide: false, + expr, }, }; - dispatch(setRecordingRulesQueries({ recordingRuleQueries: [defaultQuery], expression: getValues('expression') })); + dispatch(setRecordingRulesQueries({ recordingRuleQueries: [defaultQuery], expression: expr })); } - }, [type, recordingRuleDefaultDatasource, editingExistingRule, getValues]); + }, [type, recordingRuleDefaultDatasource, editingExistingRule, getValues, dataSourceName]); const onDuplicateQuery = useCallback((query: AlertQuery) => { dispatch(duplicateQuery(query));