Commit 62a808bf by Ryan McKinley Committed by Torkel Ödegaard

MixedDataSource: refactor, cleanup, and add tests (#18948)

* merge master

* move mixed datasource

* all responses in test

* fix tests/mocks

* remove ?
parent 509b1a9d
import { DatasourceSrvMock, MockDataSourceApi } from 'test/mocks/datasource_srv';
import { getDataSourceSrv } from '@grafana/runtime';
import { getQueryOptions } from 'test/helpers/getQueryOptions';
import { DataSourceInstanceSettings } from '@grafana/ui';
import { MixedDatasource } from './module';
const defaultDS = new MockDataSourceApi('DefaultDS', { data: ['DDD'] });
const datasourceSrv = new DatasourceSrvMock(defaultDS, {
'-- Mixed --': new MixedDatasource({ name: 'mixed', id: 5 } as DataSourceInstanceSettings),
A: new MockDataSourceApi('DSA', { data: ['AAAA'] }),
B: new MockDataSourceApi('DSB', { data: ['BBBB'] }),
C: new MockDataSourceApi('DSC', { data: ['CCCC'] }),
});
jest.mock('@grafana/runtime', () => ({
getDataSourceSrv: () => {
return datasourceSrv;
},
}));
describe('MixedDatasource', () => {
const requestMixed = getQueryOptions({
targets: [
{ refId: 'QA', datasource: 'A' }, // 1
{ refId: 'QB', datasource: 'B' }, // 2
{ refId: 'QC', datasource: 'C' }, // 3
],
});
it('direct query should return results', async () => {
const ds = await getDataSourceSrv().get('-- Mixed --');
const res = await ds.query(requestMixed);
expect(res.data).toEqual(['AAAA', 'BBBB', 'CCCC']);
});
});
import cloneDeep from 'lodash/cloneDeep';
import groupBy from 'lodash/groupBy';
import map from 'lodash/map';
import flatten from 'lodash/flatten';
import filter from 'lodash/filter';
import {
DataSourceApi,
DataQuery,
DataQueryRequest,
DataQueryResponse,
DataStreamObserver,
DataSourceInstanceSettings,
} from '@grafana/ui';
import { getDataSourceSrv } from '@grafana/runtime';
export const MIXED_DATASOURCE_NAME = '-- Mixed --';
export class MixedDatasource extends DataSourceApi<DataQuery> {
constructor(instanceSettings: DataSourceInstanceSettings) {
super(instanceSettings);
}
async query(request: DataQueryRequest<DataQuery>, observer: DataStreamObserver): Promise<DataQueryResponse> {
// Remove any invalid queries
const queries = request.targets.filter(t => {
return t.datasource !== MIXED_DATASOURCE_NAME;
});
if (!queries.length) {
return Promise.resolve({ data: [] }); // nothing
}
const sets = groupBy(queries, 'datasource');
const promises = map(sets, (targets: DataQuery[]) => {
const dsName = targets[0].datasource;
return getDataSourceSrv()
.get(dsName)
.then((ds: DataSourceApi) => {
const opt = cloneDeep(request);
// Remove any unused hidden queries
if (!ds.meta.hiddenQueries) {
targets = filter(targets, (t: DataQuery) => {
return !t.hide;
});
if (targets.length === 0) {
return { data: [] };
}
}
opt.targets = targets;
return ds.query(opt);
});
});
return Promise.all(promises).then(results => {
return { data: flatten(map(results, 'data')) };
});
}
testDatasource() {
return Promise.resolve({});
}
}
import _ from 'lodash';
import { DataSourceApi, DataQuery, DataQueryRequest, DataSourceInstanceSettings } from '@grafana/ui';
import DatasourceSrv from 'app/features/plugins/datasource_srv';
class MixedDatasource extends DataSourceApi<DataQuery> {
/** @ngInject */
constructor(instanceSettings: DataSourceInstanceSettings, private datasourceSrv: DatasourceSrv) {
super(instanceSettings);
}
query(options: DataQueryRequest<DataQuery>) {
const sets = _.groupBy(options.targets, 'datasource');
const promises: any = _.map(sets, (targets: DataQuery[]) => {
const dsName = targets[0].datasource;
if (dsName === '-- Mixed --') {
return Promise.resolve([]);
}
if (targets.length === 0) {
return { data: [] };
}
return this.datasourceSrv.get(dsName).then(ds => {
// Remove any unused hidden queries
if (!ds.meta.hiddenQueries) {
targets = _.filter(targets, (t: DataQuery) => {
return !t.hide;
});
if (targets.length === 0) {
return { data: [] };
}
}
const opt = _.cloneDeep(options);
opt.targets = targets;
return ds.query(opt);
});
});
return Promise.all(promises).then(results => {
return { data: _.flatten(_.map(results, 'data')) };
});
}
testDatasource() {
return Promise.resolve({});
}
}
export { MixedDatasource, MixedDatasource as Datasource };
import { MixedDatasource } from './datasource';
import { MixedDatasource } from './MixedDataSource';
export { MixedDatasource, MixedDatasource as Datasource };
import { DataSourceApi, DataQueryRequest, DataQueryResponse, DataSourceInstanceSettings } from '@grafana/ui';
import {
DataSourceApi,
DataQueryRequest,
DataQueryResponse,
DataSourceInstanceSettings,
DataSourcePluginMeta,
} from '@grafana/ui';
export class DatasourceSrvMock {
constructor(private defaultDS: DataSourceApi, private datasources: { [name: string]: DataSourceApi }) {
......@@ -26,6 +32,8 @@ export class MockDataSourceApi extends DataSourceApi {
if (result) {
this.result = result;
}
this.meta = {} as DataSourcePluginMeta;
}
query(request: DataQueryRequest): Promise<DataQueryResponse> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment