mirror of https://github.com/grafana/grafana
MixedDataSource: refactor, cleanup, and add tests (#18948)
* merge master * move mixed datasource * all responses in test * fix tests/mocks * remove ?pull/19115/head
parent
509b1a9d65
commit
62a808bf1f
@ -0,0 +1,35 @@ |
||||
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']); |
||||
}); |
||||
}); |
@ -0,0 +1,67 @@ |
||||
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({}); |
||||
} |
||||
} |
@ -1,51 +0,0 @@ |
||||
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 }; |
@ -1,2 +1,2 @@ |
||||
import { MixedDatasource } from './datasource'; |
||||
import { MixedDatasource } from './MixedDataSource'; |
||||
export { MixedDatasource, MixedDatasource as Datasource }; |
||||
|
Loading…
Reference in new issue