datasource.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. };
  22. });
  23. if (queries.length === 0) {
  24. return this.$q.when({data: []});
  25. }
  26. return this.backendSrv.post('/api/tsdb/query', {
  27. from: options.range.from.valueOf().toString(),
  28. to: options.range.to.valueOf().toString(),
  29. queries: queries,
  30. }).then(res => {
  31. console.log('mysql response', res);
  32. var data = [];
  33. if (res.results) {
  34. _.forEach(res.results, queryRes => {
  35. for (let series of queryRes.series) {
  36. data.push({
  37. target: series.name,
  38. datapoints: series.points
  39. });
  40. }
  41. });
  42. }
  43. return {data: data};
  44. });
  45. }
  46. }