query_ctrl.ts 2.8 KB

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