datasource.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import _ from 'lodash';
  2. class GrafanaDatasource {
  3. /** @ngInject */
  4. constructor(private backendSrv, private $q, private templateSrv) {}
  5. query(options) {
  6. return this.backendSrv
  7. .get('/api/tsdb/testdata/random-walk', {
  8. from: options.range.from.valueOf(),
  9. to: options.range.to.valueOf(),
  10. intervalMs: options.intervalMs,
  11. maxDataPoints: options.maxDataPoints,
  12. })
  13. .then(res => {
  14. const data = [];
  15. if (res.results) {
  16. _.forEach(res.results, queryRes => {
  17. for (const series of queryRes.series) {
  18. data.push({
  19. target: series.name,
  20. datapoints: series.points,
  21. });
  22. }
  23. });
  24. }
  25. return { data: data };
  26. });
  27. }
  28. metricFindQuery(options) {
  29. return this.$q.when({ data: [] });
  30. }
  31. annotationQuery(options) {
  32. const params: any = {
  33. from: options.range.from.valueOf(),
  34. to: options.range.to.valueOf(),
  35. limit: options.annotation.limit,
  36. tags: options.annotation.tags,
  37. matchAny: options.annotation.matchAny,
  38. };
  39. if (options.annotation.type === 'dashboard') {
  40. // if no dashboard id yet return
  41. if (!options.dashboard.id) {
  42. return this.$q.when([]);
  43. }
  44. // filter by dashboard id
  45. params.dashboardId = options.dashboard.id;
  46. // remove tags filter if any
  47. delete params.tags;
  48. } else {
  49. // require at least one tag
  50. if (!_.isArray(options.annotation.tags) || options.annotation.tags.length === 0) {
  51. return this.$q.when([]);
  52. }
  53. const delimiter = '__delimiter__';
  54. const tags = [];
  55. for (const t of params.tags) {
  56. const renderedValues = this.templateSrv.replace(t, {}, value => {
  57. if (typeof value === 'string') {
  58. return value;
  59. }
  60. return value.join(delimiter);
  61. });
  62. for (const tt of renderedValues.split(delimiter)) {
  63. tags.push(tt);
  64. }
  65. }
  66. params.tags = tags;
  67. }
  68. return this.backendSrv.get('/api/annotations', params);
  69. }
  70. }
  71. export { GrafanaDatasource };