mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import _ from 'lodash';
|
|
|
|
import { DataSourceApi, DataQuery, DataQueryOptions } from '@grafana/ui';
|
|
import DatasourceSrv from 'app/features/plugins/datasource_srv';
|
|
|
|
class MixedDatasource implements DataSourceApi<DataQuery> {
|
|
/** @ngInject */
|
|
constructor(private datasourceSrv: DatasourceSrv) {}
|
|
|
|
query(options: DataQueryOptions<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([]);
|
|
}
|
|
|
|
const filtered = _.filter(targets, (t: DataQuery) => {
|
|
return !t.hide;
|
|
});
|
|
|
|
if (filtered.length === 0) {
|
|
return { data: [] };
|
|
}
|
|
|
|
return this.datasourceSrv.get(dsName).then(ds => {
|
|
const opt = _.cloneDeep(options);
|
|
opt.targets = filtered;
|
|
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 };
|