event_editor.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. import {coreModule} from 'app/core/core';
  4. import {MetricsPanelCtrl} from 'app/plugins/sdk';
  5. import {AnnotationEvent} from './event';
  6. export class EventEditorCtrl {
  7. panelCtrl: MetricsPanelCtrl;
  8. event: AnnotationEvent;
  9. timeRange: {from: number, to: number};
  10. form: any;
  11. close: any;
  12. timeFormated: string;
  13. /** @ngInject **/
  14. constructor(private annotationsSrv) {
  15. this.event.panelId = this.panelCtrl.panel.id;
  16. this.event.dashboardId = this.panelCtrl.dashboard.id;
  17. // Annotations query returns time as Unix timestamp in milliseconds
  18. this.event.time = tryEpochToMoment(this.event.time);
  19. if (this.event.isRegion) {
  20. this.event.timeEnd = tryEpochToMoment(this.event.timeEnd);
  21. }
  22. this.timeFormated = this.panelCtrl.dashboard.formatDate(this.event.time);
  23. }
  24. save() {
  25. if (!this.form.$valid) {
  26. return;
  27. }
  28. let saveModel = _.cloneDeep(this.event);
  29. saveModel.time = saveModel.time.valueOf();
  30. saveModel.timeEnd = 0;
  31. if (saveModel.isRegion) {
  32. saveModel.timeEnd = this.event.timeEnd.valueOf();
  33. if (saveModel.timeEnd < saveModel.time) {
  34. console.log('invalid time');
  35. return;
  36. }
  37. }
  38. if (saveModel.id) {
  39. this.annotationsSrv.updateAnnotationEvent(saveModel)
  40. .then(() => {
  41. this.panelCtrl.refresh();
  42. this.close();
  43. })
  44. .catch(() => {
  45. this.panelCtrl.refresh();
  46. this.close();
  47. });
  48. } else {
  49. this.annotationsSrv.saveAnnotationEvent(saveModel)
  50. .then(() => {
  51. this.panelCtrl.refresh();
  52. this.close();
  53. })
  54. .catch(() => {
  55. this.panelCtrl.refresh();
  56. this.close();
  57. });
  58. }
  59. }
  60. delete() {
  61. return this.annotationsSrv.deleteAnnotationEvent(this.event)
  62. .then(() => {
  63. this.panelCtrl.refresh();
  64. this.close();
  65. })
  66. .catch(() => {
  67. this.panelCtrl.refresh();
  68. this.close();
  69. });
  70. }
  71. }
  72. function tryEpochToMoment(timestamp) {
  73. if (timestamp && _.isNumber(timestamp)) {
  74. let epoch = Number(timestamp);
  75. return moment(epoch);
  76. } else {
  77. return timestamp;
  78. }
  79. }
  80. export function eventEditor() {
  81. return {
  82. restrict: 'E',
  83. controller: EventEditorCtrl,
  84. bindToController: true,
  85. controllerAs: 'ctrl',
  86. templateUrl: 'public/app/features/annotations/partials/event_editor.html',
  87. scope: {
  88. "panelCtrl": "=",
  89. "event": "=",
  90. "close": "&",
  91. }
  92. };
  93. }
  94. coreModule.directive('eventEditor', eventEditor);