datasource.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import _ from 'lodash';
  2. import {
  3. DataSourceApi,
  4. DataQueryRequest,
  5. DataSourceInstanceSettings,
  6. DataQueryResponse,
  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 { queryMetricTree } from './metricTree';
  13. import { Observable, from, merge } from 'rxjs';
  14. import { runStream } from './runStreams';
  15. import templateSrv from 'app/features/templating/template_srv';
  16. type TestData = TimeSeries | TableData;
  17. export class TestDataDataSource extends DataSourceApi<TestDataQuery> {
  18. constructor(instanceSettings: DataSourceInstanceSettings) {
  19. super(instanceSettings);
  20. }
  21. query(options: DataQueryRequest<TestDataQuery>): Observable<DataQueryResponse> {
  22. const queries: any[] = [];
  23. const streams: Array<Observable<DataQueryResponse>> = [];
  24. // Start streams and prepare queries
  25. for (const target of options.targets) {
  26. if (target.scenarioId === 'streaming_client') {
  27. streams.push(runStream(target, options));
  28. } else {
  29. queries.push({
  30. ...target,
  31. intervalMs: options.intervalMs,
  32. maxDataPoints: options.maxDataPoints,
  33. datasourceId: this.id,
  34. alias: templateSrv.replace(target.alias || ''),
  35. });
  36. }
  37. }
  38. if (queries.length) {
  39. const req: Promise<DataQueryResponse> = 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) => this.processQueryResult(queries, res));
  52. streams.push(from(req));
  53. }
  54. return merge(...streams);
  55. }
  56. processQueryResult(queries: any, res: any): DataQueryResponse {
  57. const data: TestData[] = [];
  58. for (const query of queries) {
  59. const results = res.data.results[query.refId];
  60. for (const t of results.tables || []) {
  61. const table = t as TableData;
  62. table.refId = query.refId;
  63. table.name = query.alias;
  64. data.push(table);
  65. }
  66. for (const series of results.series || []) {
  67. data.push({ target: series.name, datapoints: series.points, refId: query.refId, tags: series.tags });
  68. }
  69. }
  70. return { data };
  71. }
  72. annotationQuery(options: any) {
  73. let timeWalker = options.range.from.valueOf();
  74. const to = options.range.to.valueOf();
  75. const events = [];
  76. const eventCount = 10;
  77. const step = (to - timeWalker) / eventCount;
  78. for (let i = 0; i < eventCount; i++) {
  79. events.push({
  80. annotation: options.annotation,
  81. time: timeWalker,
  82. text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
  83. tags: ['text', 'server'],
  84. });
  85. timeWalker += step;
  86. }
  87. return Promise.resolve(events);
  88. }
  89. getQueryDisplayText(query: TestDataQuery) {
  90. if (query.alias) {
  91. return query.scenarioId + ' as ' + query.alias;
  92. }
  93. return query.scenarioId;
  94. }
  95. testDatasource() {
  96. return Promise.resolve({
  97. status: 'success',
  98. message: 'Data source is working',
  99. });
  100. }
  101. getScenarios(): Promise<Scenario[]> {
  102. return getBackendSrv().get('/api/tsdb/testdata/scenarios');
  103. }
  104. metricFindQuery(query: string) {
  105. return new Promise<MetricFindValue[]>((resolve, reject) => {
  106. setTimeout(() => {
  107. const children = queryMetricTree(templateSrv.replace(query));
  108. const items = children.map(item => ({ value: item.name, text: item.name }));
  109. resolve(items);
  110. }, 100);
  111. });
  112. }
  113. }