Alerting: Fix for data source filter on cloud rules. (#79327)
* fix data source filter not worling for cloud rules * Add test
This commit is contained in:
@@ -2,7 +2,9 @@ import { setDataSourceSrv } from '@grafana/runtime';
|
|||||||
|
|
||||||
import { PromAlertingRuleState } from '../../../../types/unified-alerting-dto';
|
import { PromAlertingRuleState } from '../../../../types/unified-alerting-dto';
|
||||||
import {
|
import {
|
||||||
|
getCloudRule,
|
||||||
mockAlertQuery,
|
mockAlertQuery,
|
||||||
|
mockCombinedCloudRuleNamespace,
|
||||||
mockCombinedRule,
|
mockCombinedRule,
|
||||||
mockCombinedRuleGroup,
|
mockCombinedRuleGroup,
|
||||||
mockCombinedRuleNamespace,
|
mockCombinedRuleNamespace,
|
||||||
@@ -154,16 +156,24 @@ describe('filterRules', function () {
|
|||||||
data: [mockAlertQuery({ datasourceUid: dataSources.loki.uid })],
|
data: [mockAlertQuery({ datasourceUid: dataSources.loki.uid })],
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
getCloudRule({ name: 'Cloud' }),
|
||||||
];
|
];
|
||||||
|
|
||||||
const ns = mockCombinedRuleNamespace({
|
const ns = mockCombinedRuleNamespace({
|
||||||
groups: [mockCombinedRuleGroup('Resources usage group', rules)],
|
groups: [mockCombinedRuleGroup('Resources usage group', rules)],
|
||||||
});
|
});
|
||||||
|
const cloudNs = mockCombinedCloudRuleNamespace(
|
||||||
|
{
|
||||||
|
groups: [mockCombinedRuleGroup('Resources usage group', rules)],
|
||||||
|
},
|
||||||
|
'Prometheus-ds'
|
||||||
|
);
|
||||||
|
|
||||||
const filtered = filterRules([ns], getFilter({ dataSourceNames: ['loki'] }));
|
const filtered = filterRules([ns, cloudNs], getFilter({ dataSourceNames: ['loki', 'Prometheus-ds'] }));
|
||||||
|
|
||||||
expect(filtered[0].groups[0].rules).toHaveLength(1);
|
expect(filtered[0].groups[0].rules).toHaveLength(2);
|
||||||
expect(filtered[0].groups[0].rules[0].name).toBe('Memory too low');
|
expect(filtered[0].groups[0].rules[0].name).toBe('Memory too low');
|
||||||
|
expect(filtered[0].groups[0].rules[1].name).toBe('Cloud');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to combine multiple predicates with AND', () => {
|
it('should be able to combine multiple predicates with AND', () => {
|
||||||
|
|||||||
@@ -204,9 +204,13 @@ const reduceGroups = (filterState: RulesFilter) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ('dataSourceNames' in matchesFilterFor) {
|
if ('dataSourceNames' in matchesFilterFor) {
|
||||||
const doesNotQueryDs = isGrafanaRulerRule(rule.rulerRule) && isQueryingDataSource(rule.rulerRule, filterState);
|
if (isGrafanaRulerRule(rule.rulerRule)) {
|
||||||
|
const doesNotQueryDs = isQueryingDataSource(rule.rulerRule, filterState);
|
||||||
|
|
||||||
if (doesNotQueryDs) {
|
if (doesNotQueryDs) {
|
||||||
|
matchesFilterFor.dataSourceNames = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
matchesFilterFor.dataSourceNames = true;
|
matchesFilterFor.dataSourceNames = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,16 +15,16 @@ import {
|
|||||||
ScopedVars,
|
ScopedVars,
|
||||||
TestDataSourceResponse,
|
TestDataSourceResponse,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
import { config, DataSourceSrv, GetDataSourceListFilters } from '@grafana/runtime';
|
import { DataSourceSrv, GetDataSourceListFilters, config } from '@grafana/runtime';
|
||||||
import { defaultDashboard } from '@grafana/schema';
|
import { defaultDashboard } from '@grafana/schema';
|
||||||
import { contextSrv } from 'app/core/services/context_srv';
|
import { contextSrv } from 'app/core/services/context_srv';
|
||||||
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
|
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
|
||||||
import {
|
import {
|
||||||
AlertmanagerAlert,
|
|
||||||
AlertManagerCortexConfig,
|
AlertManagerCortexConfig,
|
||||||
|
AlertState,
|
||||||
|
AlertmanagerAlert,
|
||||||
AlertmanagerGroup,
|
AlertmanagerGroup,
|
||||||
AlertmanagerStatus,
|
AlertmanagerStatus,
|
||||||
AlertState,
|
|
||||||
GrafanaManagedReceiverConfig,
|
GrafanaManagedReceiverConfig,
|
||||||
MatcherOperator,
|
MatcherOperator,
|
||||||
Silence,
|
Silence,
|
||||||
@@ -657,6 +657,17 @@ export function mockCombinedRuleNamespace(namespace: Partial<CombinedRuleNamespa
|
|||||||
...namespace,
|
...namespace,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
export function mockCombinedCloudRuleNamespace(
|
||||||
|
namespace: Partial<CombinedRuleNamespace>,
|
||||||
|
dataSourceName: string
|
||||||
|
): CombinedRuleNamespace {
|
||||||
|
return {
|
||||||
|
name: 'Grafana',
|
||||||
|
groups: [],
|
||||||
|
rulesSource: mockDataSource({ name: dataSourceName, uid: 'Prometheus-1' }),
|
||||||
|
...namespace,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function getGrafanaRule(override?: Partial<CombinedRule>, rulerOverride?: Partial<GrafanaRuleDefinition>) {
|
export function getGrafanaRule(override?: Partial<CombinedRule>, rulerOverride?: Partial<GrafanaRuleDefinition>) {
|
||||||
return mockCombinedRule({
|
return mockCombinedRule({
|
||||||
|
|||||||
Reference in New Issue
Block a user