query_ctrl.ts 1.8 KB

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