datasource.ts 3.8 KB

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