event_editor.ts 2.6 KB

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