datasource.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import _ from 'lodash';
  2. import {
  3. DataSourceApi,
  4. DataQueryRequest,
  5. TableData,
  6. TimeSeries,
  7. DataSourceInstanceSettings,
  8. DataStreamObserver,
  9. } from '@grafana/ui';
  10. import { TestDataQuery, Scenario } from './types';
  11. import { getBackendSrv } from 'app/core/services/backend_srv';
  12. import { StreamHandler } from './StreamHandler';
  13. type TestData = TimeSeries | TableData;
  14. export interface TestDataRegistry {
  15. [key: string]: TestData[];
  16. }
  17. export class TestDataDatasource extends DataSourceApi<TestDataQuery> {
  18. streams = new StreamHandler();
  19. /** @ngInject */
  20. constructor(instanceSettings: DataSourceInstanceSettings) {
  21. super(instanceSettings);
  22. }
  23. query(options: DataQueryRequest<TestDataQuery>, observer: DataStreamObserver) {
  24. const queries = options.targets.map(item => {
  25. return {
  26. refId: item.refId,
  27. scenarioId: item.scenarioId,
  28. intervalMs: options.intervalMs,
  29. maxDataPoints: options.maxDataPoints,
  30. stringInput: item.stringInput,
  31. points: item.points,
  32. alias: item.alias,
  33. datasourceId: this.id,
  34. };
  35. });
  36. if (queries.length === 0) {
  37. return Promise.resolve({ data: [] });
  38. }
  39. // Currently we do not support mixed with client only streaming
  40. const resp = this.streams.process(options, observer);
  41. if (resp) {
  42. return Promise.resolve(resp);
  43. }
  44. return getBackendSrv()
  45. .datasourceRequest({
  46. method: 'POST',
  47. url: '/api/tsdb/query',
  48. data: {
  49. from: options.range.from.valueOf().toString(),
  50. to: options.range.to.valueOf().toString(),
  51. queries: queries,
  52. },
  53. // This sets up a cancel token
  54. requestId: options.requestId,
  55. })
  56. .then((res: any) => {
  57. const data: TestData[] = [];
  58. // Returns data in the order it was asked for.
  59. // if the response has data with different refId, it is ignored
  60. for (const query of queries) {
  61. const results = res.data.results[query.refId];
  62. if (!results) {
  63. console.warn('No Results for:', query);
  64. continue;
  65. }
  66. for (const t of results.tables || []) {
  67. const table = t as TableData;
  68. table.refId = query.refId;
  69. table.name = query.alias;
  70. data.push(table);
  71. }
  72. for (const series of results.series || []) {
  73. data.push({ target: series.name, datapoints: series.points, refId: query.refId });
  74. }
  75. }
  76. return { data: data };
  77. });
  78. }
  79. annotationQuery(options: any) {
  80. let timeWalker = options.range.from.valueOf();
  81. const to = options.range.to.valueOf();
  82. const events = [];
  83. const eventCount = 10;
  84. const step = (to - timeWalker) / eventCount;
  85. for (let i = 0; i < eventCount; i++) {
  86. events.push({
  87. annotation: options.annotation,
  88. time: timeWalker,
  89. text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
  90. tags: ['text', 'server'],
  91. });
  92. timeWalker += step;
  93. }
  94. return Promise.resolve(events);
  95. }
  96. getQueryDisplayText(query: TestDataQuery) {
  97. if (query.alias) {
  98. return query.scenarioId + ' as ' + query.alias;
  99. }
  100. return query.scenarioId;
  101. }
  102. testDatasource() {
  103. return Promise.resolve({
  104. status: 'success',
  105. message: 'Data source is working',
  106. });
  107. }
  108. getScenarios(): Promise<Scenario[]> {
  109. return getBackendSrv().get('/api/tsdb/testdata/scenarios');
  110. }
  111. }