Alerting: Fix RefIds not being shown when creating or editing Grafana-managed recording rule (#106840)

* fix refids when creating grafana-managed recording rule

* fix refids when editing grafana-managed recording rule

* review comments
This commit is contained in:
Sonia Aguilar
2025-06-18 06:48:13 +00:00
committed by GitHub
parent 78bec77ca1
commit 9905d3447c
2 changed files with 19 additions and 10 deletions
@@ -34,9 +34,12 @@ const KEEP_FIRING_FOR_DEFAULT = '0s';
export const DEFAULT_GROUP_EVALUATION_INTERVAL = formatPrometheusDuration(
clamp(GROUP_EVALUATION_MIN_INTERVAL_MS, GROUP_EVALUATION_INTERVAL_LOWER_BOUND, GROUP_EVALUATION_INTERVAL_UPPER_BOUND)
);
export const getDefaultFormValues = (): RuleFormValues => {
export const getDefaultFormValues = (ruleType?: RuleFormType): RuleFormValues => {
const { canCreateGrafanaRules, canCreateCloudRules } = getRulesAccess();
const type = (() => {
if (ruleType === RuleFormType.grafanaRecording) {
return RuleFormType.grafanaRecording;
}
if (canCreateGrafanaRules) {
return RuleFormType.grafana;
}
@@ -70,7 +73,7 @@ export const getDefaultFormValues = (): RuleFormValues => {
overrideGrouping: false,
overrideTimings: false,
muteTimeIntervals: [],
editorSettings: getDefaultEditorSettings(),
editorSettings: getDefaultEditorSettings(ruleType),
targetDatasourceUid: config.unifiedAlerting?.defaultRecordingRulesTargetDatasourceUID,
// cortex / loki
@@ -88,7 +91,11 @@ export const getDefautManualRouting = () => {
return manualRouting !== 'false';
};
function getDefaultEditorSettings() {
function getDefaultEditorSettings(ruleType?: RuleFormType) {
if (ruleType === RuleFormType.grafanaRecording) {
return undefined;
}
const editorSettingsEnabled = config.featureToggles.alertingQueryAndExpressionsStepMode ?? false;
if (!editorSettingsEnabled) {
return undefined;
@@ -109,7 +116,7 @@ export function formValuesFromQueryParams(ruleDefinition: string, type: RuleForm
ruleFromQueryParams = JSON.parse(ruleDefinition);
} catch (err) {
return {
...getDefaultFormValues(),
...getDefaultFormValues(type),
queries: getDefaultQueries(),
};
}
@@ -117,7 +124,7 @@ export function formValuesFromQueryParams(ruleDefinition: string, type: RuleForm
return setQueryEditorSettings(
setInstantOrRange(
revealHiddenQueries({
...getDefaultFormValues(),
...getDefaultFormValues(type),
...ruleFromQueryParams,
annotations: normalizeDefaultAnnotations(ruleFromQueryParams.annotations ?? []),
queries: ruleFromQueryParams.queries ?? getDefaultQueries(),
@@ -130,7 +137,7 @@ export function formValuesFromQueryParams(ruleDefinition: string, type: RuleForm
export function formValuesFromPrefill(rule: Partial<RuleFormValues>): RuleFormValues {
return revealHiddenQueries({
...getDefaultFormValues(),
...getDefaultFormValues(rule.type),
...rule,
});
}
@@ -141,7 +148,7 @@ export function formValuesFromExistingRule(rule: RuleWithLocation<RulerRuleDTO>)
export function defaultFormValuesForRuleType(ruleType: RuleFormType): RuleFormValues {
return {
...getDefaultFormValues(),
...getDefaultFormValues(ruleType),
condition: 'C',
queries: getDefaultQueries(isGrafanaRecordingRuleByType(ruleType)),
type: ruleType,
@@ -277,11 +277,12 @@ function getEditorSettingsFromDTO(ga: GrafanaRuleDefinition) {
export function rulerRuleToFormValues(ruleWithLocation: RuleWithLocation): RuleFormValues {
const { ruleSourceName, namespace, group, rule } = ruleWithLocation;
const isGrafanaRecordingRule = rulerRuleType.grafana.recordingRule(rule);
const defaultFormValues = getDefaultFormValues();
const defaultFormValues = getDefaultFormValues(isGrafanaRecordingRule ? RuleFormType.grafanaRecording : undefined);
if (isGrafanaRulesSource(ruleSourceName)) {
// GRAFANA-MANAGED RULES
if (rulerRuleType.grafana.recordingRule(rule)) {
if (isGrafanaRecordingRule) {
// grafana recording rule
const ga = rule.grafana_alert;
return {
@@ -382,7 +383,8 @@ export function rulerRuleToFormValues(ruleWithLocation: RuleWithLocation): RuleF
}
export function grafanaRuleDtoToFormValues(rule: RulerGrafanaRuleDTO, namespace: string): RuleFormValues {
const defaultFormValues = getDefaultFormValues();
const isGrafanaRecordingRule = rulerRuleType.grafana.recordingRule(rule);
const defaultFormValues = getDefaultFormValues(isGrafanaRecordingRule ? RuleFormType.grafanaRecording : undefined);
const ga = rule.grafana_alert;
const duration = rule.for;