datasource.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import _ from 'lodash';
  2. class TestDataDatasource {
  3. id: any;
  4. /** @ngInject */
  5. constructor(instanceSettings, private backendSrv, private $q) {
  6. this.id = instanceSettings.id;
  7. }
  8. query(options) {
  9. var queries = _.filter(options.targets, item => {
  10. return item.hide !== true;
  11. }).map(item => {
  12. return {
  13. refId: item.refId,
  14. scenarioId: item.scenarioId,
  15. intervalMs: options.intervalMs,
  16. maxDataPoints: options.maxDataPoints,
  17. stringInput: item.stringInput,
  18. points: item.points,
  19. alias: item.alias,
  20. datasourceId: this.id,
  21. };
  22. });
  23. if (queries.length === 0) {
  24. return this.$q.when({ data: [] });
  25. }
  26. return this.backendSrv
  27. .post('/api/tsdb/query', {
  28. from: options.range.from.valueOf().toString(),
  29. to: options.range.to.valueOf().toString(),
  30. queries: queries,
  31. })
  32. .then(res => {
  33. var data = [];
  34. if (res.results) {
  35. _.forEach(res.results, queryRes => {
  36. for (let series of queryRes.series) {
  37. data.push({
  38. target: series.name,
  39. datapoints: series.points,
  40. });
  41. }
  42. });
  43. }
  44. return { data: data };
  45. });
  46. }
  47. annotationQuery(options) {
  48. return this.backendSrv.get('/api/annotations', {
  49. from: options.range.from.valueOf(),
  50. to: options.range.to.valueOf(),
  51. limit: options.limit,
  52. type: options.type,
  53. });
  54. }
  55. }
  56. export { TestDataDatasource };