datasource.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import _ from 'lodash';
  2. import TableModel from 'app/core/table_model';
  3. class TestDataDatasource {
  4. id: any;
  5. /** @ngInject */
  6. constructor(instanceSettings, private backendSrv, private $q) {
  7. this.id = instanceSettings.id;
  8. }
  9. query(options) {
  10. const queries = _.filter(options.targets, item => {
  11. return item.hide !== true;
  12. }).map(item => {
  13. return {
  14. refId: item.refId,
  15. scenarioId: item.scenarioId,
  16. intervalMs: options.intervalMs,
  17. maxDataPoints: options.maxDataPoints,
  18. stringInput: item.stringInput,
  19. points: item.points,
  20. alias: item.alias,
  21. datasourceId: this.id,
  22. };
  23. });
  24. if (queries.length === 0) {
  25. return this.$q.when({ data: [] });
  26. }
  27. return this.backendSrv
  28. .datasourceRequest({
  29. method: 'POST',
  30. url: '/api/tsdb/query',
  31. data: {
  32. from: options.range.from.valueOf().toString(),
  33. to: options.range.to.valueOf().toString(),
  34. queries: queries,
  35. },
  36. })
  37. .then(res => {
  38. const data = [];
  39. if (res.data.results) {
  40. _.forEach(res.data.results, queryRes => {
  41. if (queryRes.tables) {
  42. for (const table of queryRes.tables) {
  43. const model = new TableModel();
  44. model.rows = table.rows;
  45. model.columns = table.columns;
  46. data.push(model);
  47. }
  48. }
  49. for (const series of queryRes.series) {
  50. data.push({
  51. target: series.name,
  52. datapoints: series.points,
  53. });
  54. }
  55. });
  56. }
  57. return { data: data };
  58. });
  59. }
  60. annotationQuery(options) {
  61. let timeWalker = options.range.from.valueOf();
  62. const to = options.range.to.valueOf();
  63. const events = [];
  64. const eventCount = 10;
  65. const step = (to - timeWalker) / eventCount;
  66. for (let i = 0; i < eventCount; i++) {
  67. events.push({
  68. annotation: options.annotation,
  69. time: timeWalker,
  70. text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
  71. tags: ['text', 'server'],
  72. });
  73. timeWalker += step;
  74. }
  75. return this.$q.when(events);
  76. }
  77. }
  78. export { TestDataDatasource };