datasource.ts 915 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. class MixedDatasource {
  4. /** @ngInject */
  5. constructor(private $q, private datasourceSrv) {}
  6. query(options) {
  7. const sets = _.groupBy(options.targets, 'datasource');
  8. const promises = _.map(sets, targets => {
  9. const dsName = targets[0].datasource;
  10. if (dsName === '-- Mixed --') {
  11. return this.$q([]);
  12. }
  13. const filtered = _.filter(targets, t => {
  14. return !t.hide;
  15. });
  16. if (filtered.length === 0) {
  17. return { data: [] };
  18. }
  19. return this.datasourceSrv.get(dsName).then(ds => {
  20. const opt = angular.copy(options);
  21. opt.targets = filtered;
  22. return ds.query(opt);
  23. });
  24. });
  25. return this.$q.all(promises).then(results => {
  26. return { data: _.flatten(_.map(results, 'data')) };
  27. });
  28. }
  29. }
  30. export { MixedDatasource, MixedDatasource as Datasource };