query_ctrl.ts 2.5 KB

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