query_ctrl.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import _ from 'lodash';
  2. import { QueryCtrl } from 'app/plugins/sdk';
  3. import moment from 'moment';
  4. import { defaultQuery } from './StreamHandler';
  5. import { getBackendSrv } from 'app/core/services/backend_srv';
  6. export class TestDataQueryCtrl extends QueryCtrl {
  7. static templateUrl = 'partials/query.editor.html';
  8. scenarioList: any;
  9. scenario: any;
  10. newPointValue: number;
  11. newPointTime: any;
  12. selectedPoint: any;
  13. /** @ngInject */
  14. constructor($scope: any, $injector: any) {
  15. super($scope, $injector);
  16. this.target.scenarioId = this.target.scenarioId || 'random_walk';
  17. this.scenarioList = [];
  18. this.newPointTime = moment();
  19. this.selectedPoint = { text: 'Select point', value: null };
  20. }
  21. getPoints() {
  22. return _.map(this.target.points, (point, index) => {
  23. return {
  24. text: moment(point[1]).format('MMMM Do YYYY, H:mm:ss') + ' : ' + point[0],
  25. value: index,
  26. };
  27. });
  28. }
  29. pointSelected(option) {
  30. this.selectedPoint = option;
  31. }
  32. deletePoint() {
  33. this.target.points.splice(this.selectedPoint.value, 1);
  34. this.selectedPoint = { text: 'Select point', value: null };
  35. this.refresh();
  36. }
  37. addPoint() {
  38. this.target.points = this.target.points || [];
  39. this.target.points.push([this.newPointValue, this.newPointTime.valueOf()]);
  40. this.target.points = _.sortBy(this.target.points, p => p[1]);
  41. this.refresh();
  42. }
  43. $onInit() {
  44. return getBackendSrv()
  45. .get('/api/tsdb/testdata/scenarios')
  46. .then(res => {
  47. this.scenarioList = res;
  48. this.scenario = _.find(this.scenarioList, { id: this.target.scenarioId });
  49. });
  50. }
  51. scenarioChanged() {
  52. this.scenario = _.find(this.scenarioList, { id: this.target.scenarioId });
  53. this.target.stringInput = this.scenario.stringInput;
  54. if (this.target.scenarioId === 'manual_entry') {
  55. this.target.points = this.target.points || [];
  56. } else {
  57. delete this.target.points;
  58. }
  59. if (this.target.scenarioId === 'streaming_client') {
  60. this.target.stream = _.defaults(this.target.stream || {}, defaultQuery);
  61. } else {
  62. delete this.target.stream;
  63. }
  64. this.refresh();
  65. }
  66. streamChanged() {
  67. this.refresh();
  68. }
  69. }