datasource.ts 3.5 KB

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