datasource.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. export class MysqlDatasource {
  4. id: any;
  5. name: any;
  6. /** @ngInject */
  7. constructor(instanceSettings, private backendSrv, private $q) {
  8. this.name = instanceSettings.name;
  9. this.id = instanceSettings.id;
  10. }
  11. query(options) {
  12. var queries = _.filter(options.targets, item => {
  13. return item.hide !== true;
  14. }).map(item => {
  15. return {
  16. refId: item.refId,
  17. intervalMs: options.intervalMs,
  18. maxDataPoints: options.maxDataPoints,
  19. datasourceId: this.id,
  20. rawSql: item.rawSql,
  21. format: item.format,
  22. };
  23. });
  24. if (queries.length === 0) {
  25. return this.$q.when({data: []});
  26. }
  27. return this.backendSrv.datasourceRequest({
  28. url: '/api/tsdb/query',
  29. method: 'POST',
  30. data: {
  31. from: options.range.from.valueOf().toString(),
  32. to: options.range.to.valueOf().toString(),
  33. queries: queries,
  34. }
  35. }).then(this.processQueryResult.bind(this));
  36. }
  37. processQueryResult(res) {
  38. var data = [];
  39. if (!res.data.results) {
  40. return {data: data};
  41. }
  42. for (let key in res.data.results) {
  43. let queryRes = res.data.results[key];
  44. if (queryRes.series) {
  45. for (let series of queryRes.series) {
  46. data.push({
  47. target: series.name,
  48. datapoints: series.points,
  49. refId: queryRes.refId,
  50. meta: queryRes.meta,
  51. });
  52. }
  53. }
  54. if (queryRes.tables) {
  55. for (let table of queryRes.tables) {
  56. table.type = 'table';
  57. table.refId = queryRes.refId;
  58. table.meta = queryRes.meta;
  59. data.push(table);
  60. }
  61. }
  62. }
  63. return {data: data};
  64. }
  65. }