query_ctrl.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import _ from 'lodash';
  2. import { QueryCtrl } from 'app/plugins/sdk';
  3. import { defaultQuery } from './runStreams';
  4. import { getBackendSrv } from 'app/core/services/backend_srv';
  5. import { dateTime } from '@grafana/data';
  6. export const defaultPulse: any = {
  7. timeStep: 60,
  8. onCount: 3,
  9. onValue: 2,
  10. offCount: 3,
  11. offValue: 1,
  12. };
  13. export const defaultCSVWave: any = {
  14. timeStep: 60,
  15. valuesCSV: '0,0,2,2,1,1',
  16. };
  17. const showLabelsFor = ['random_walk', 'predictable_pulse', 'predictable_csv_wave'];
  18. export class TestDataQueryCtrl extends QueryCtrl {
  19. static templateUrl = 'partials/query.editor.html';
  20. scenarioList: any;
  21. scenario: any;
  22. newPointValue: number;
  23. newPointTime: any;
  24. selectedPoint: any;
  25. showLabels = false;
  26. /** @ngInject */
  27. constructor($scope: any, $injector: any) {
  28. super($scope, $injector);
  29. this.target.scenarioId = this.target.scenarioId || 'random_walk';
  30. this.scenarioList = [];
  31. this.newPointTime = dateTime();
  32. this.selectedPoint = { text: 'Select point', value: null };
  33. this.showLabels = showLabelsFor.includes(this.target.scenarioId);
  34. }
  35. getPoints() {
  36. return _.map(this.target.points, (point, index) => {
  37. return {
  38. text: dateTime(point[1]).format('MMMM Do YYYY, H:mm:ss') + ' : ' + point[0],
  39. value: index,
  40. };
  41. });
  42. }
  43. pointSelected(option: any) {
  44. this.selectedPoint = option;
  45. }
  46. deletePoint() {
  47. this.target.points.splice(this.selectedPoint.value, 1);
  48. this.selectedPoint = { text: 'Select point', value: null };
  49. this.refresh();
  50. }
  51. addPoint() {
  52. this.target.points = this.target.points || [];
  53. this.target.points.push([this.newPointValue, this.newPointTime.valueOf()]);
  54. this.target.points = _.sortBy(this.target.points, p => p[1]);
  55. this.refresh();
  56. }
  57. $onInit() {
  58. return getBackendSrv()
  59. .get('/api/tsdb/testdata/scenarios')
  60. .then((res: any) => {
  61. this.scenarioList = res;
  62. this.scenario = _.find(this.scenarioList, { id: this.target.scenarioId });
  63. });
  64. }
  65. scenarioChanged() {
  66. this.scenario = _.find(this.scenarioList, { id: this.target.scenarioId });
  67. this.target.stringInput = this.scenario.stringInput;
  68. this.showLabels = showLabelsFor.includes(this.target.scenarioId);
  69. if (this.target.scenarioId === 'manual_entry') {
  70. this.target.points = this.target.points || [];
  71. } else {
  72. delete this.target.points;
  73. }
  74. if (this.target.scenarioId === 'streaming_client') {
  75. this.target.stream = _.defaults(this.target.stream || {}, defaultQuery);
  76. } else {
  77. delete this.target.stream;
  78. }
  79. if (this.target.scenarioId === 'predictable_pulse') {
  80. this.target.pulseWave = _.defaults(this.target.pulseWave || {}, defaultPulse);
  81. } else {
  82. delete this.target.pulseWave;
  83. }
  84. if (this.target.scenarioId === 'predictable_csv_wave') {
  85. this.target.csvWave = _.defaults(this.target.csvWave || {}, defaultCSVWave);
  86. } else {
  87. delete this.target.csvWave;
  88. }
  89. this.refresh();
  90. }
  91. streamChanged() {
  92. this.refresh();
  93. }
  94. }