make alert tab create alert button work with default datasource (#40334)

This commit is contained in:
Domas 2021-10-14 09:40:43 +03:00 committed by GitHub
parent 6e395c8275
commit c43b82d44b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -34,9 +34,16 @@ const dataSources = {
prometheus: mockDataSource<PromOptions>({
name: 'Prometheus',
type: DataSourceType.Prometheus,
isDefault: false,
}),
default: mockDataSource<PromOptions>({
name: 'Default',
type: DataSourceType.Prometheus,
isDefault: true,
}),
};
dataSources.prometheus.meta.alerting = true;
dataSources.default.meta.alerting = true;
const mocks = {
getAllDataSources: typeAsJestMock(getAllDataSources),
@ -165,6 +172,10 @@ describe('PanelAlertTabContent', () => {
dsService.datasources[dataSources.prometheus.name] = new PrometheusDatasource(
dataSources.prometheus
) as DataSourceApi<any, any>;
dsService.datasources[dataSources.default.name] = new PrometheusDatasource(dataSources.default) as DataSourceApi<
any,
any
>;
setDataSourceSrv(dsService);
});
@ -189,6 +200,28 @@ describe('PanelAlertTabContent', () => {
});
});
it('Will work with default datasource', async () => {
await renderAlertTabContent(dashboard, ({
...panel,
datasource: undefined,
maxDataPoints: 100,
interval: '10s',
} as any) as PanelModel);
const button = await ui.createButton.find();
const href = button.href;
const match = href.match(/alerting\/new\?defaults=(.*)&returnTo=/);
expect(match).toHaveLength(2);
const defaults = JSON.parse(decodeURIComponent(match![1]));
expect(defaults.queries[0].model).toEqual({
expr: 'sum(some_metric [5m])) by (app)',
refId: 'A',
datasource: 'Default',
interval: '',
intervalMs: 300000,
maxDataPoints: 100,
});
});
it('Will take into account datasource minInterval', async () => {
((getDatasourceSrv() as any) as MockDataSourceSrv).datasources[dataSources.prometheus.name].interval = '7m';

View File

@ -229,8 +229,8 @@ const dataQueriesToGrafanaQueries = async (
): Promise<AlertQuery[]> => {
const result: AlertQuery[] = [];
for (const target of queries) {
const dsName = target.datasource || datasourceName;
const datasource = await getDataSourceSrv().get(dsName);
const datasource = await getDataSourceSrv().get(target.datasource || datasourceName);
const dsName = datasource.name;
const range = rangeUtil.relativeToTimeRange(relativeTimeRange);
const { interval, intervalMs } = getIntervals(range, minInterval ?? datasource.interval, maxDataPoints);