Alerting: Fix bug when saving a rule more than once (#96658)

* Update existing property once we save the new rule data and stay in the same page

* prettier

* use cache invalidation for updating the existing property

* fix cloud usecase
This commit is contained in:
Sonia Aguilar 2024-11-21 09:25:15 +01:00 committed by GitHub
parent e995d4f682
commit a15c612427
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 10 deletions

View File

@ -20,7 +20,7 @@ import { ExportFormats } from '../components/export/providers';
import { Folder } from '../types/rule-form'; import { Folder } from '../types/rule-form';
import { getDatasourceAPIUid, GRAFANA_RULES_SOURCE_NAME, isGrafanaRulesSource } from '../utils/datasource'; import { getDatasourceAPIUid, GRAFANA_RULES_SOURCE_NAME, isGrafanaRulesSource } from '../utils/datasource';
import { arrayKeyValuesToObject } from '../utils/labels'; import { arrayKeyValuesToObject } from '../utils/labels';
import { isCloudRuleIdentifier, isPrometheusRuleIdentifier } from '../utils/rules'; import { isCloudRuleIdentifier, isGrafanaRulerRule, isPrometheusRuleIdentifier } from '../utils/rules';
import { alertingApi, WithNotificationOptions } from './alertingApi'; import { alertingApi, WithNotificationOptions } from './alertingApi';
import { import {
@ -323,6 +323,9 @@ export const alertRuleApi = alertingApi.injectEndpoints({
type: 'RuleGroup', type: 'RuleGroup',
id: `${namespace}/${payload.name}`, id: `${namespace}/${payload.name}`,
}, },
...payload.rules
.filter((rule) => isGrafanaRulerRule(rule))
.map((rule) => ({ type: 'GrafanaRulerRule', id: rule.grafana_alert.uid }) as const),
], ],
}), }),

View File

@ -163,6 +163,7 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => {
? getRuleGroupLocationFromRuleWithLocation(existing) ? getRuleGroupLocationFromRuleWithLocation(existing)
: getRuleGroupLocationFromFormValues(values); : getRuleGroupLocationFromFormValues(values);
const targetRuleGroupIdentifier = getRuleGroupLocationFromFormValues(values);
// @TODO move this to a hook too to make sure the logic here is tested for regressions? // @TODO move this to a hook too to make sure the logic here is tested for regressions?
if (!existing) { if (!existing) {
// when creating a new rule, we save the manual routing setting , and editorSettings.simplifiedQueryEditor to the local storage // when creating a new rule, we save the manual routing setting , and editorSettings.simplifiedQueryEditor to the local storage
@ -171,7 +172,6 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => {
grafanaTypeRule && trackNewGrafanaAlertRuleFormSavedSuccess(); // new Grafana-managed rule grafanaTypeRule && trackNewGrafanaAlertRuleFormSavedSuccess(); // new Grafana-managed rule
} else { } else {
const ruleIdentifier = fromRulerRuleAndRuleGroupIdentifier(ruleGroupIdentifier, existing.rule); const ruleIdentifier = fromRulerRuleAndRuleGroupIdentifier(ruleGroupIdentifier, existing.rule);
const targetRuleGroupIdentifier = getRuleGroupLocationFromFormValues(values);
await updateRuleInRuleGroup.execute( await updateRuleInRuleGroup.execute(
ruleGroupIdentifier, ruleGroupIdentifier,
ruleIdentifier, ruleIdentifier,
@ -181,19 +181,21 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => {
); );
} }
const { dataSourceName, namespaceName, groupName } = ruleGroupIdentifier; const { dataSourceName, namespaceName, groupName } = targetRuleGroupIdentifier;
if (exitOnSave) { if (exitOnSave) {
const returnToUrl = returnTo || getReturnToUrl(ruleGroupIdentifier, ruleDefinition); const returnToUrl = returnTo || getReturnToUrl(targetRuleGroupIdentifier, ruleDefinition);
locationService.push(returnToUrl); locationService.push(returnToUrl);
return; return;
} } else {
// we stay in the same page
// Cloud Ruler rules identifier changes on update due to containing rule name and hash components // Cloud Ruler rules identifier changes on update due to containing rule name and hash components
// After successful update we need to update the URL to avoid displaying 404 errors // After successful update we need to update the URL to avoid displaying 404 errors
if (isCloudRulerRule(ruleDefinition)) { if (isCloudRulerRule(ruleDefinition)) {
const updatedRuleIdentifier = fromRulerRule(dataSourceName, namespaceName, groupName, ruleDefinition); const updatedRuleIdentifier = fromRulerRule(dataSourceName, namespaceName, groupName, ruleDefinition);
locationService.replace(`/alerting/${encodeURIComponent(stringifyIdentifier(updatedRuleIdentifier))}/edit`); locationService.replace(`/alerting/${encodeURIComponent(stringifyIdentifier(updatedRuleIdentifier))}/edit`);
}
} }
}; };