From 5fe20a365d5cbe8549b70a52cffff411b10e25bc Mon Sep 17 00:00:00 2001 From: "grafana-delivery-bot[bot]" <132647405+grafana-delivery-bot[bot]@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:50:06 +0100 Subject: [PATCH] [v10.3.x] Alerting: Fix preview getting the correct queries from the form (#81481) Alerting: Fix preview getting the correct queries from the form (#80458) * fix preview getting the correct queries from the form * Remove setting queries in onChangeQueries handler as it only contains data queries and not expressions * Keep setValue('queries') but also adding expressions (cherry picked from commit 51f5e1af39c7adb20adf32656e4381f4e4541e2b) Co-authored-by: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com> --- .../query-and-alert-condition/QueryAndExpressionsStep.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 9360b934ad7..0aff0232535 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 @@ -186,8 +186,9 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P // Invocation cycle => onChange -> dispatch(setDataQueries) -> onRunQueries -> setDataQueries Reducer // As a workaround we update form values as soon as possible to avoid stale state // This way we can access up to date queries in runQueriesPreview without waiting for re-render - setValue('queries', updatedQueries, { shouldValidate: false }); - + const previousQueries = getValues('queries'); + const expressionQueries = previousQueries.filter((query) => isExpressionQuery(query.model)); + setValue('queries', [...updatedQueries, ...expressionQueries], { shouldValidate: false }); updateExpressionAndDatasource(updatedQueries); dispatch(setDataQueries(updatedQueries)); @@ -199,7 +200,7 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P dispatch(rewireExpressions({ oldRefId, newRefId })); } }, - [queries, setValue, updateExpressionAndDatasource] + [queries, updateExpressionAndDatasource, getValues, setValue] ); const onChangeRecordingRulesQueries = useCallback(