addAnnotationModalCtrl.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import moment from 'moment';
  4. export class AddAnnotationModalCtrl {
  5. annotationTimeFormat = 'YYYY-MM-DD HH:mm:ss';
  6. annotationTimeFrom: any;
  7. annotationTimeTo: any = null;
  8. annotationTitle: string;
  9. annotationTextFrom: string;
  10. annotationTextTo: string;
  11. graphCtrl: any;
  12. /** @ngInject */
  13. constructor(private $scope) {
  14. this.graphCtrl = $scope.ctrl;
  15. $scope.ctrl = this;
  16. this.annotationTimeFrom = moment($scope.annotationTimeRange.from).format(this.annotationTimeFormat);
  17. if ($scope.annotationTimeRange.to) {
  18. this.annotationTimeTo = moment($scope.annotationTimeRange.to).format(this.annotationTimeFormat);
  19. }
  20. }
  21. addAnnotation() {
  22. let dashboardId = this.graphCtrl.dashboard.id;
  23. let panelId = this.graphCtrl.panel.id;
  24. let timeFrom = moment(this.annotationTimeFrom, this.annotationTimeFormat).valueOf();
  25. let annotationFrom = {
  26. dashboardId: dashboardId,
  27. panelId: panelId,
  28. time: timeFrom,
  29. title: this.annotationTitle,
  30. text: this.annotationTextFrom
  31. };
  32. let annotations = [annotationFrom];
  33. if (this.annotationTimeTo) {
  34. let timeTo = moment(this.annotationTimeTo, this.annotationTimeFormat).valueOf();
  35. let annotationTo = {
  36. dashboardId: dashboardId,
  37. panelId: panelId,
  38. time: timeTo,
  39. title: this.annotationTitle,
  40. text: this.annotationTextTo
  41. };
  42. annotations.push(annotationTo);
  43. }
  44. this.graphCtrl.pushAnnotations(annotations)
  45. .then(response => {
  46. console.log(response);
  47. this.close();
  48. })
  49. .catch(error => {
  50. console.log(error);
  51. this.close();
  52. });
  53. }
  54. close() {
  55. this.$scope.dismiss();
  56. }
  57. }
  58. angular
  59. .module('grafana.controllers')
  60. .controller('AddAnnotationModalCtrl', AddAnnotationModalCtrl);