datasource.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import _ from 'lodash';
  2. import { DataSourceApi, DataQueryRequest, 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: DataQueryRequest<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. // This sets up a cancel token
  42. requestId: options.requestId,
  43. })
  44. .then((res: any) => {
  45. const data: TestData[] = [];
  46. // Returns data in the order it was asked for.
  47. // if the response has data with different refId, it is ignored
  48. for (const query of queries) {
  49. const results = res.data.results[query.refId];
  50. if (!results) {
  51. console.warn('No Results for:', query);
  52. continue;
  53. }
  54. for (const t of results.tables || []) {
  55. const table = t as TableData;
  56. table.refId = query.refId;
  57. data.push(table);
  58. }
  59. for (const series of results.series || []) {
  60. data.push({ target: series.name, datapoints: series.points, refId: query.refId });
  61. }
  62. }
  63. return { data: data };
  64. });
  65. }
  66. annotationQuery(options) {
  67. let timeWalker = options.range.from.valueOf();
  68. const to = options.range.to.valueOf();
  69. const events = [];
  70. const eventCount = 10;
  71. const step = (to - timeWalker) / eventCount;
  72. for (let i = 0; i < eventCount; i++) {
  73. events.push({
  74. annotation: options.annotation,
  75. time: timeWalker,
  76. text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
  77. tags: ['text', 'server'],
  78. });
  79. timeWalker += step;
  80. }
  81. return this.$q.when(events);
  82. }
  83. getQueryDisplayText(query: TestDataQuery) {
  84. if (query.alias) {
  85. return query.scenarioId + ' as ' + query.alias;
  86. }
  87. return query.scenarioId;
  88. }
  89. testDatasource() {
  90. return Promise.resolve({
  91. status: 'success',
  92. message: 'Data source is working',
  93. });
  94. }
  95. getScenarios(): Promise<Scenario[]> {
  96. return this.backendSrv.get('/api/tsdb/testdata/scenarios');
  97. }
  98. }