mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
e995d4f682
commit
a15c612427
@ -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),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
@ -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,13 +181,14 @@ 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
|
||||||
@ -195,6 +196,7 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => {
|
|||||||
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`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteRule = async () => {
|
const deleteRule = async () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user