datasource.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. if (queryRes.error) {
  36. throw {error: queryRes.error, message: queryRes.error};
  37. }
  38. for (let series of queryRes.series) {
  39. data.push({
  40. target: series.name,
  41. datapoints: series.points
  42. });
  43. }
  44. });
  45. }
  46. return {data: data};
  47. });
  48. }
  49. }