datasource.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import _ from 'lodash';
  2. import TableModel from 'app/core/table_model';
  3. class TestDataDatasource {
  4. id: any;
  5. /** @ngInject */
  6. constructor(instanceSettings, private backendSrv, private $q) {
  7. this.id = instanceSettings.id;
  8. }
  9. query(options) {
  10. const queries = _.filter(options.targets, item => {
  11. return item.hide !== true;
  12. }).map(item => {
  13. return {
  14. refId: item.refId,
  15. scenarioId: item.scenarioId,
  16. intervalMs: options.intervalMs,
  17. maxDataPoints: options.maxDataPoints,
  18. stringInput: item.stringInput,
  19. points: item.points,
  20. alias: item.alias,
  21. datasourceId: this.id,
  22. };
  23. });
  24. if (queries.length === 0) {
  25. return this.$q.when({ data: [] });
  26. }
  27. return this.backendSrv
  28. .datasourceRequest({
  29. method: 'POST',
  30. url: '/api/tsdb/query',
  31. data: {
  32. from: options.range.from.valueOf().toString(),
  33. to: options.range.to.valueOf().toString(),
  34. queries: queries,
  35. },
  36. })
  37. .then(res => {
  38. const data = [];
  39. if (res.data.results) {
  40. _.forEach(res.data.results, queryRes => {
  41. if (queryRes.tables) {
  42. for (const table of queryRes.tables) {
  43. const model = new TableModel();
  44. model.rows = table.rows;
  45. model.columns = table.columns;
  46. data.push(model);
  47. }
  48. }
  49. for (const series of queryRes.series) {
  50. data.push({
  51. target: series.name,
  52. datapoints: series.points,
  53. });
  54. }
  55. });
  56. }
  57. console.log(res);
  58. return { data: data };
  59. });
  60. }
  61. annotationQuery(options) {
  62. let timeWalker = options.range.from.valueOf();
  63. const to = options.range.to.valueOf();
  64. const events = [];
  65. const eventCount = 10;
  66. const step = (to - timeWalker) / eventCount;
  67. for (let i = 0; i < eventCount; i++) {
  68. events.push({
  69. annotation: options.annotation,
  70. time: timeWalker,
  71. text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
  72. tags: ['text', 'server'],
  73. });
  74. timeWalker += step;
  75. }
  76. return this.$q.when(events);
  77. }
  78. }
  79. export { TestDataDatasource };