mirror of
https://github.com/grafana/grafana.git
synced 2026-07-30 00:08:10 -05:00
Alerting: Fix data source copy when switching alert rule types (#73854)
This commit is contained in:
@@ -68,6 +68,7 @@ const dataSources = {
|
||||
{
|
||||
type: DataSourceType.Prometheus,
|
||||
name: 'cortex with ruler',
|
||||
isDefault: true,
|
||||
},
|
||||
{ alerting: true }
|
||||
),
|
||||
@@ -185,9 +186,6 @@ describe('RuleEditor cloud: checking editable data sources', () => {
|
||||
|
||||
await ui.inputs.name.find();
|
||||
|
||||
const removeExpressionsButtons = screen.getAllByLabelText('Remove expression');
|
||||
expect(removeExpressionsButtons).toHaveLength(2);
|
||||
|
||||
const switchToCloudButton = screen.getByText('Switch to data source-managed alert rule');
|
||||
expect(switchToCloudButton).toBeInTheDocument();
|
||||
|
||||
@@ -200,8 +198,8 @@ describe('RuleEditor cloud: checking editable data sources', () => {
|
||||
const dataSourceSelect = ui.inputs.dataSource.get();
|
||||
await userEvent.click(byRole('combobox').get(dataSourceSelect));
|
||||
|
||||
expect(await byText('loki with ruler').query()).toBeInTheDocument();
|
||||
expect(byText('cortex with ruler').query()).toBeInTheDocument();
|
||||
expect(byText('loki with ruler').query()).toBeInTheDocument();
|
||||
expect(byText('loki with local rule store').query()).not.toBeInTheDocument();
|
||||
expect(byText('prom without ruler api').query()).not.toBeInTheDocument();
|
||||
expect(byText('splunk').query()).not.toBeInTheDocument();
|
||||
|
||||
+10
-12
@@ -175,11 +175,6 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P
|
||||
(updatedQueries: AlertQuery[]) => {
|
||||
const query = updatedQueries[0];
|
||||
|
||||
const dataSourceSettings = getDataSourceSrv().getInstanceSettings(query.datasourceUid);
|
||||
if (!dataSourceSettings) {
|
||||
throw new Error('The Data source has not been defined.');
|
||||
}
|
||||
|
||||
if (!isPromOrLokiQuery(query.model)) {
|
||||
return;
|
||||
}
|
||||
@@ -187,13 +182,12 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P
|
||||
const expression = query.model.expr;
|
||||
|
||||
setValue('queries', updatedQueries, { shouldValidate: false });
|
||||
setValue('dataSourceName', dataSourceSettings.name);
|
||||
setValue('expression', expression);
|
||||
updateExpressionAndDatasource(updatedQueries);
|
||||
|
||||
dispatch(setRecordingRulesQueries({ recordingRuleQueries: updatedQueries, expression }));
|
||||
runQueriesPreview();
|
||||
},
|
||||
[runQueriesPreview, setValue]
|
||||
[runQueriesPreview, setValue, updateExpressionAndDatasource]
|
||||
);
|
||||
|
||||
const recordingRuleDefaultDatasource = rulesSourcesWithRuler[0];
|
||||
@@ -312,10 +306,14 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P
|
||||
const typeInForm = getValues('type');
|
||||
if (typeInForm === RuleFormType.cloudAlerting) {
|
||||
setValue('type', RuleFormType.grafana);
|
||||
setValue('dataSourceName', null); // set data source name back to "null"
|
||||
|
||||
prevExpressions.length > 0 && restoreExpressionsInQueries();
|
||||
prevCondition && setValue('condition', prevCondition);
|
||||
} else {
|
||||
setValue('type', RuleFormType.cloudAlerting);
|
||||
updateExpressionAndDatasource(queries);
|
||||
|
||||
const expressions = queries.filter((query) => query.datasourceUid === ExpressionDatasourceUID);
|
||||
setPrevExpressions(expressions);
|
||||
removeExpressionsInQueries();
|
||||
@@ -324,12 +322,12 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P
|
||||
}, [
|
||||
getValues,
|
||||
setValue,
|
||||
prevExpressions.length,
|
||||
restoreExpressionsInQueries,
|
||||
prevCondition,
|
||||
updateExpressionAndDatasource,
|
||||
queries,
|
||||
removeExpressionsInQueries,
|
||||
restoreExpressionsInQueries,
|
||||
setPrevExpressions,
|
||||
prevExpressions,
|
||||
prevCondition,
|
||||
condition,
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user