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));