datasource.ts 2.9 KB

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