datasource.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. return { data: data };
  58. });
  59. }
  60. annotationQuery(options) {
  61. let timeWalker = options.range.from.valueOf();
  62. const to = options.range.to.valueOf();
  63. const events = [];
  64. const eventCount = 10;
  65. const step = (to - timeWalker) / eventCount;
  66. for (let i = 0; i < eventCount; i++) {
  67. events.push({
  68. annotation: options.annotation,
  69. time: timeWalker,
  70. text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
  71. tags: ['text', 'server'],
  72. });
  73. timeWalker += step;
  74. }
  75. return this.$q.when(events);
  76. }
  77. testDatasource() {
  78. return Promise.resolve({
  79. status: 'success',
  80. message: 'Data source is working',
  81. });
  82. }
  83. }
  84. export { TestDataDatasource };