mirror of
https://github.com/grafana/grafana.git
synced 2025-09-23 13:23:19 +08:00
make alert tab create alert button work with default datasource (#40334)
This commit is contained in:
@ -34,9 +34,16 @@ const dataSources = {
|
|||||||
prometheus: mockDataSource<PromOptions>({
|
prometheus: mockDataSource<PromOptions>({
|
||||||
name: 'Prometheus',
|
name: 'Prometheus',
|
||||||
type: DataSourceType.Prometheus,
|
type: DataSourceType.Prometheus,
|
||||||
|
isDefault: false,
|
||||||
|
}),
|
||||||
|
default: mockDataSource<PromOptions>({
|
||||||
|
name: 'Default',
|
||||||
|
type: DataSourceType.Prometheus,
|
||||||
|
isDefault: true,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
dataSources.prometheus.meta.alerting = true;
|
dataSources.prometheus.meta.alerting = true;
|
||||||
|
dataSources.default.meta.alerting = true;
|
||||||
|
|
||||||
const mocks = {
|
const mocks = {
|
||||||
getAllDataSources: typeAsJestMock(getAllDataSources),
|
getAllDataSources: typeAsJestMock(getAllDataSources),
|
||||||
@ -165,6 +172,10 @@ describe('PanelAlertTabContent', () => {
|
|||||||
dsService.datasources[dataSources.prometheus.name] = new PrometheusDatasource(
|
dsService.datasources[dataSources.prometheus.name] = new PrometheusDatasource(
|
||||||
dataSources.prometheus
|
dataSources.prometheus
|
||||||
) as DataSourceApi<any, any>;
|
) as DataSourceApi<any, any>;
|
||||||
|
dsService.datasources[dataSources.default.name] = new PrometheusDatasource(dataSources.default) as DataSourceApi<
|
||||||
|
any,
|
||||||
|
any
|
||||||
|
>;
|
||||||
setDataSourceSrv(dsService);
|
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 () => {
|
it('Will take into account datasource minInterval', async () => {
|
||||||
((getDatasourceSrv() as any) as MockDataSourceSrv).datasources[dataSources.prometheus.name].interval = '7m';
|
((getDatasourceSrv() as any) as MockDataSourceSrv).datasources[dataSources.prometheus.name].interval = '7m';
|
||||||
|
|
||||||
|
@ -229,8 +229,8 @@ const dataQueriesToGrafanaQueries = async (
|
|||||||
): Promise<AlertQuery[]> => {
|
): Promise<AlertQuery[]> => {
|
||||||
const result: AlertQuery[] = [];
|
const result: AlertQuery[] = [];
|
||||||
for (const target of queries) {
|
for (const target of queries) {
|
||||||
const dsName = target.datasource || datasourceName;
|
const datasource = await getDataSourceSrv().get(target.datasource || datasourceName);
|
||||||
const datasource = await getDataSourceSrv().get(dsName);
|
const dsName = datasource.name;
|
||||||
|
|
||||||
const range = rangeUtil.relativeToTimeRange(relativeTimeRange);
|
const range = rangeUtil.relativeToTimeRange(relativeTimeRange);
|
||||||
const { interval, intervalMs } = getIntervals(range, minInterval ?? datasource.interval, maxDataPoints);
|
const { interval, intervalMs } = getIntervals(range, minInterval ?? datasource.interval, maxDataPoints);
|
||||||
|
Reference in New Issue
Block a user