addAnnotationModalCtrl.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.graphCtrl.inAddAnnotationMode = false;
  56. this.$scope.dismiss();
  57. }
  58. }
  59. angular
  60. .module('grafana.controllers')
  61. .controller('AddAnnotationModalCtrl', AddAnnotationModalCtrl);