datasource.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import _ from 'lodash';
  2. import { DataSourceApi, DataQueryOptions, TableData, TimeSeries } from '@grafana/ui';
  3. import { TestDataQuery, Scenario } from './types';
  4. type TestData = TimeSeries | TableData;
  5. export interface TestDataRegistry {
  6. [key: string]: TestData[];
  7. }
  8. export class TestDataDatasource implements DataSourceApi<TestDataQuery> {
  9. id: number;
  10. /** @ngInject */
  11. constructor(instanceSettings, private backendSrv, private $q) {
  12. this.id = instanceSettings.id;
  13. }
  14. query(options: DataQueryOptions<TestDataQuery>) {
  15. const queries = _.filter(options.targets, item => {
  16. return item.hide !== true;
  17. }).map(item => {
  18. return {
  19. refId: item.refId,
  20. scenarioId: item.scenarioId,
  21. intervalMs: options.intervalMs,
  22. maxDataPoints: options.maxDataPoints,
  23. stringInput: item.stringInput,
  24. points: item.points,
  25. alias: item.alias,
  26. datasourceId: this.id,
  27. };
  28. });
  29. if (queries.length === 0) {
  30. return this.$q.when({ data: [] });
  31. }
  32. return this.backendSrv
  33. .datasourceRequest({
  34. method: 'POST',
  35. url: '/api/tsdb/query',
  36. data: {
  37. from: options.range.from.valueOf().toString(),
  38. to: options.range.to.valueOf().toString(),
  39. queries: queries,
  40. },
  41. })
  42. .then(res => {
  43. const data: TestData[] = [];
  44. // The results are not in the order we asked for them
  45. if (res.data.results) {
  46. const byRefID: TestDataRegistry = {};
  47. _.forEach(res.data.results, queryRes => {
  48. const refId = queryRes.refId || 'Result' + data.length + 1;
  49. const qdata: TestData[] = [];
  50. byRefID[refId] = qdata;
  51. if (queryRes.tables) {
  52. for (const table of queryRes.tables) {
  53. qdata.push(table as TableData);
  54. }
  55. }
  56. if (queryRes.series) {
  57. for (const series of queryRes.series) {
  58. qdata.push({
  59. target: series.name,
  60. datapoints: series.points,
  61. });
  62. }
  63. }
  64. });
  65. // Return them in the order they were asked for
  66. queries.forEach(q => {
  67. const found = byRefID[q.refId];
  68. if (found) {
  69. for (const d of found) {
  70. data.push(d);
  71. byRefID[q.refId] = null;
  72. }
  73. }
  74. });
  75. // In case there are items left over
  76. _.forEach(byRefID, v => {
  77. if (v) {
  78. for (const d of v) {
  79. data.push(d);
  80. }
  81. }
  82. });
  83. }
  84. return { data: data };
  85. });
  86. }
  87. annotationQuery(options) {
  88. let timeWalker = options.range.from.valueOf();
  89. const to = options.range.to.valueOf();
  90. const events = [];
  91. const eventCount = 10;
  92. const step = (to - timeWalker) / eventCount;
  93. for (let i = 0; i < eventCount; i++) {
  94. events.push({
  95. annotation: options.annotation,
  96. time: timeWalker,
  97. text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
  98. tags: ['text', 'server'],
  99. });
  100. timeWalker += step;
  101. }
  102. return this.$q.when(events);
  103. }
  104. testDatasource() {
  105. return Promise.resolve({
  106. status: 'success',
  107. message: 'Data source is working',
  108. });
  109. }
  110. getScenarios(): Promise<Scenario[]> {
  111. return this.backendSrv.get('/api/tsdb/testdata/scenarios');
  112. }
  113. }