diff --git a/public/app/features/alerting/unified/components/AlertRuleDrawerForm.tsx b/public/app/features/alerting/unified/components/AlertRuleDrawerForm.tsx index ec6ad0af0d7..6832c8a4aa5 100644 --- a/public/app/features/alerting/unified/components/AlertRuleDrawerForm.tsx +++ b/public/app/features/alerting/unified/components/AlertRuleDrawerForm.tsx @@ -137,10 +137,10 @@ export function AlertRuleDrawerForm({ variant="secondary" type="button" onClick={() => { - const values = methods.getValues(); + const currentValues = methods.getValues(); onContinueInAlerting({ - ...values, - contactPoints: normalizeContactPoints(values.contactPoints), + ...currentValues, + contactPoints: normalizeContactPoints(currentValues.contactPoints), }); }} > diff --git a/public/app/features/alerting/unified/rule-editor/formDefaults.ts b/public/app/features/alerting/unified/rule-editor/formDefaults.ts index 064fd6c93e3..1912bd2a64b 100644 --- a/public/app/features/alerting/unified/rule-editor/formDefaults.ts +++ b/public/app/features/alerting/unified/rule-editor/formDefaults.ts @@ -251,10 +251,12 @@ export function formValuesFromPrefill(rule: Partial): RuleFormVa parsedRule = alertingAlertRuleFormSchema.parse(rule); } - return revealHiddenQueries({ - ...getDefaultFormValues(rule.type), - ...parsedRule, - }); + return setQueryEditorSettings( + revealHiddenQueries({ + ...getDefaultFormValues(rule.type), + ...parsedRule, + }) + ); } export function formValuesFromExistingRule(rule: RuleWithLocation) { diff --git a/public/app/features/alerting/unified/rule-editor/formProcessing.ts b/public/app/features/alerting/unified/rule-editor/formProcessing.ts index e986604422d..3bd0ae101c8 100644 --- a/public/app/features/alerting/unified/rule-editor/formProcessing.ts +++ b/public/app/features/alerting/unified/rule-editor/formProcessing.ts @@ -30,6 +30,21 @@ export function setQueryEditorSettings(values: RuleFormValues): RuleFormValues { // expression queries only const expressionQueries = values.queries.filter((query) => isExpressionQueryInAlert(query)); + // If we have data queries but no expressions (e.g., coming from dashboard panel), + // default to simplified mode so the form can create appropriate expressions + const hasDataQueries = dataQueries.length > 0; + const hasExpressions = expressionQueries.length > 0; + + if (hasDataQueries && !hasExpressions) { + return { + ...values, + editorSettings: { + simplifiedQueryEditor: true, + simplifiedNotificationEditor: true, + }, + }; + } + const queryParamsAreTransformable = areQueriesTransformableToSimpleCondition(dataQueries, expressionQueries); return { ...values, diff --git a/public/app/features/alerting/unified/utils/rule-form.ts b/public/app/features/alerting/unified/utils/rule-form.ts index 0e83a276851..882670b845b 100644 --- a/public/app/features/alerting/unified/utils/rule-form.ts +++ b/public/app/features/alerting/unified/utils/rule-form.ts @@ -7,7 +7,6 @@ import { ScopedVars, TimeRange, getDefaultRelativeTimeRange, - getNextRefId, rangeUtil, } from '@grafana/data'; import { PromQuery } from '@grafana/prometheus'; @@ -783,26 +782,6 @@ export const panelToRuleFormValues = async ( return undefined; } - const lastQuery = queries.at(-1); - if (!lastQuery) { - return undefined; - } - - if (!queries.find((query) => query.datasourceUid === ExpressionDatasourceUID)) { - const reduceExpression = getDefaultReduceExpression({ - inputRefId: lastQuery.refId, - reduceRefId: getNextRefId(queries), - }); - queries.push(reduceExpression); - - const thresholdExpression = getDefaultThresholdExpression({ - inputRefId: reduceExpression.refId, - thresholdRefId: getNextRefId(queries), - }); - - queries.push(thresholdExpression); - } - const { folderTitle, folderUid } = dashboard.meta; const folder = folderUid && folderTitle @@ -818,7 +797,8 @@ export const panelToRuleFormValues = async ( folder, queries, name: panel.title, - condition: queries[queries.length - 1].refId, + // Condition left empty - expressions will be created in the alert rule form under advanced options + condition: '', annotations: [ { key: Annotation.dashboardUID, @@ -870,26 +850,6 @@ export const scenesPanelToRuleFormValues = async (vizPanel: VizPanel): Promise

query.datasourceUid === ExpressionDatasourceUID)) { - const reduceExpression = getDefaultReduceExpression({ - inputRefId: lastQuery.refId, - reduceRefId: getNextRefId(grafanaQueries), - }); - grafanaQueries.push(reduceExpression); - - const thresholdExpression = getDefaultThresholdExpression({ - inputRefId: reduceExpression.refId, - thresholdRefId: getNextRefId(grafanaQueries), - }); - - grafanaQueries.push(thresholdExpression); - } - const { folderTitle, folderUid } = dashboard.state.meta; const folder = @@ -906,7 +866,8 @@ export const scenesPanelToRuleFormValues = async (vizPanel: VizPanel): Promise