2017-12-20 12:33:33 +01:00
|
|
|
import _ from 'lodash';
|
2016-01-09 13:21:16 +01:00
|
|
|
|
2019-04-12 10:13:36 -07:00
|
|
|
import { DataSourceApi, DataQuery, DataQueryOptions } from '@grafana/ui';
|
|
|
|
|
import DatasourceSrv from 'app/features/plugins/datasource_srv';
|
|
|
|
|
|
|
|
|
|
class MixedDatasource implements DataSourceApi<DataQuery> {
|
2016-01-21 13:14:25 +01:00
|
|
|
/** @ngInject */
|
2019-04-12 10:13:36 -07:00
|
|
|
constructor(private datasourceSrv: DatasourceSrv) {}
|
2016-01-09 13:21:16 +01:00
|
|
|
|
2019-04-12 10:13:36 -07:00
|
|
|
query(options: DataQueryOptions<DataQuery>) {
|
2018-08-29 14:26:50 +02:00
|
|
|
const sets = _.groupBy(options.targets, 'datasource');
|
2019-04-15 12:11:52 +02:00
|
|
|
const promises: any = _.map(sets, (targets: DataQuery[]) => {
|
2018-08-29 14:26:50 +02:00
|
|
|
const dsName = targets[0].datasource;
|
2017-12-20 12:33:33 +01:00
|
|
|
if (dsName === '-- Mixed --') {
|
2019-04-12 10:13:36 -07:00
|
|
|
return Promise.resolve([]);
|
2016-01-09 13:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-12 10:13:36 -07:00
|
|
|
const filtered = _.filter(targets, (t: DataQuery) => {
|
2019-04-08 11:39:54 +02:00
|
|
|
return !t.hide;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (filtered.length === 0) {
|
|
|
|
|
return { data: [] };
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 14:27:03 +02:00
|
|
|
return this.datasourceSrv.get(dsName).then(ds => {
|
2019-04-12 10:13:36 -07:00
|
|
|
const opt = _.cloneDeep(options);
|
2019-04-08 11:39:54 +02:00
|
|
|
opt.targets = filtered;
|
2016-01-09 13:21:16 +01:00
|
|
|
return ds.query(opt);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-12 10:13:36 -07:00
|
|
|
return Promise.all(promises).then(results => {
|
2017-12-20 12:33:33 +01:00
|
|
|
return { data: _.flatten(_.map(results, 'data')) };
|
2016-01-09 13:21:16 +01:00
|
|
|
});
|
|
|
|
|
}
|
2019-04-12 10:13:36 -07:00
|
|
|
|
|
|
|
|
testDatasource() {
|
|
|
|
|
return Promise.resolve({});
|
|
|
|
|
}
|
2016-01-09 13:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
export { MixedDatasource, MixedDatasource as Datasource };
|