fix issues with expression values in alert rule form page from dashboard

This commit is contained in:
laurenashleigh
2026-01-12 11:41:28 +00:00
parent 6582761689
commit 1e6eb20b5c
4 changed files with 28 additions and 50 deletions
@@ -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),
});
}}
>
@@ -251,10 +251,12 @@ export function formValuesFromPrefill(rule: Partial<RuleFormValues>): RuleFormVa
parsedRule = alertingAlertRuleFormSchema.parse(rule);
}
return revealHiddenQueries({
...getDefaultFormValues(rule.type),
...parsedRule,
});
return setQueryEditorSettings(
revealHiddenQueries({
...getDefaultFormValues(rule.type),
...parsedRule,
})
);
}
export function formValuesFromExistingRule(rule: RuleWithLocation<RulerRuleDTO>) {
@@ -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,
@@ -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<P
return undefined;
}
const lastQuery = grafanaQueries.at(-1);
if (!lastQuery) {
return undefined;
}
if (!grafanaQueries.find((query) => 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<P
folder,
queries: grafanaQueries,
name: vizPanel.state.title,
condition: grafanaQueries[grafanaQueries.length - 1].refId,
// Condition left empty - expressions will be created in the alert rule form under advanced options
condition: '',
annotations: [
{
key: Annotation.dashboardUID,