datasource.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 (let i = 0; i < queries.length; i++) {
  47. const query = queries[i];
  48. const results = res.data.results[query.refId];
  49. if (results) {
  50. if (results.tables) {
  51. for (const table of results.tables) {
  52. data.push(table as TableData);
  53. }
  54. }
  55. if (results.series) {
  56. for (const series of results.series) {
  57. data.push({
  58. target: series.name,
  59. datapoints: series.points,
  60. });
  61. }
  62. }
  63. } else {
  64. console.warn('No Results for:', query);
  65. }
  66. }
  67. return { data: data };
  68. });
  69. }
  70. annotationQuery(options) {
  71. let timeWalker = options.range.from.valueOf();
  72. const to = options.range.to.valueOf();
  73. const events = [];
  74. const eventCount = 10;
  75. const step = (to - timeWalker) / eventCount;
  76. for (let i = 0; i < eventCount; i++) {
  77. events.push({
  78. annotation: options.annotation,
  79. time: timeWalker,
  80. text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
  81. tags: ['text', 'server'],
  82. });
  83. timeWalker += step;
  84. }
  85. return this.$q.when(events);
  86. }
  87. testDatasource() {
  88. return Promise.resolve({
  89. status: 'success',
  90. message: 'Data source is working',
  91. });
  92. }
  93. getScenarios(): Promise<Scenario[]> {
  94. return this.backendSrv.get('/api/tsdb/testdata/scenarios');
  95. }
  96. }