event_manager.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. import tinycolor from 'tinycolor2';
  4. import {MetricsPanelCtrl} from 'app/plugins/sdk';
  5. import {AnnotationEvent} from './event';
  6. import {OK_COLOR, ALERTING_COLOR, NO_DATA_COLOR, DEFAULT_ANNOTATION_COLOR, REGION_FILL_ALPHA} from 'app/core/utils/colors';
  7. export class EventManager {
  8. event: AnnotationEvent;
  9. editorOpen: boolean;
  10. constructor(private panelCtrl: MetricsPanelCtrl) {}
  11. editorClosed() {
  12. this.event = null;
  13. this.editorOpen = false;
  14. this.panelCtrl.render();
  15. }
  16. editorOpened() {
  17. this.editorOpen = true;
  18. }
  19. updateTime(range) {
  20. if (!this.event) {
  21. this.event = new AnnotationEvent();
  22. this.event.dashboardId = this.panelCtrl.dashboard.id;
  23. this.event.panelId = this.panelCtrl.panel.id;
  24. }
  25. // update time
  26. this.event.time = moment(range.from);
  27. this.event.isRegion = false;
  28. if (range.to) {
  29. this.event.timeEnd = moment(range.to);
  30. this.event.isRegion = true;
  31. }
  32. this.panelCtrl.render();
  33. }
  34. editEvent(event, elem?) {
  35. this.event = event;
  36. this.panelCtrl.render();
  37. }
  38. addFlotEvents(annotations, flotOptions) {
  39. if (!this.event && annotations.length === 0) {
  40. return;
  41. }
  42. var types = {
  43. $__alerting: {
  44. color: ALERTING_COLOR,
  45. position: 'BOTTOM',
  46. markerSize: 5,
  47. },
  48. $__ok: {
  49. color: OK_COLOR,
  50. position: 'BOTTOM',
  51. markerSize: 5,
  52. },
  53. $__no_data: {
  54. color: NO_DATA_COLOR,
  55. position: 'BOTTOM',
  56. markerSize: 5,
  57. },
  58. $__editing: {
  59. color: DEFAULT_ANNOTATION_COLOR,
  60. position: 'BOTTOM',
  61. markerSize: 5,
  62. },
  63. };
  64. if (this.event) {
  65. if (this.event.isRegion) {
  66. annotations = [
  67. {
  68. isRegion: true,
  69. min: this.event.time.valueOf(),
  70. timeEnd: this.event.timeEnd.valueOf(),
  71. text: this.event.text,
  72. eventType: '$__editing',
  73. editModel: this.event,
  74. },
  75. ];
  76. } else {
  77. annotations = [
  78. {
  79. min: this.event.time.valueOf(),
  80. text: this.event.text,
  81. editModel: this.event,
  82. eventType: '$__editing',
  83. },
  84. ];
  85. }
  86. } else {
  87. // annotations from query
  88. for (var i = 0; i < annotations.length; i++) {
  89. var item = annotations[i];
  90. // add properties used by jquery flot events
  91. item.min = item.time;
  92. item.max = item.time;
  93. item.eventType = item.source.name;
  94. if (item.newState) {
  95. item.eventType = '$__' + item.newState;
  96. continue;
  97. }
  98. if (!types[item.source.name]) {
  99. types[item.source.name] = {
  100. color: item.source.iconColor,
  101. position: 'BOTTOM',
  102. markerSize: 5,
  103. };
  104. }
  105. }
  106. }
  107. let regions = getRegions(annotations);
  108. addRegionMarking(regions, flotOptions);
  109. let eventSectionHeight = 20;
  110. let eventSectionMargin = 7;
  111. flotOptions.grid.eventSectionHeight = eventSectionMargin;
  112. flotOptions.xaxis.eventSectionHeight = eventSectionHeight;
  113. flotOptions.events = {
  114. levels: _.keys(types).length + 1,
  115. data: annotations,
  116. types: types,
  117. manager: this,
  118. };
  119. }
  120. }
  121. function getRegions(events) {
  122. return _.filter(events, 'isRegion');
  123. }
  124. function addRegionMarking(regions, flotOptions) {
  125. let markings = flotOptions.grid.markings;
  126. let defaultColor = DEFAULT_ANNOTATION_COLOR;
  127. let fillColor;
  128. _.each(regions, region => {
  129. if (region.source) {
  130. fillColor = region.source.iconColor || defaultColor;
  131. } else {
  132. fillColor = defaultColor;
  133. }
  134. fillColor = addAlphaToRGB(fillColor, REGION_FILL_ALPHA);
  135. markings.push({xaxis: {from: region.min, to: region.timeEnd}, color: fillColor});
  136. });
  137. }
  138. function addAlphaToRGB(colorString: string, alpha: number): string {
  139. let color = tinycolor(colorString);
  140. if (color.isValid()) {
  141. color.setAlpha(alpha);
  142. return color.toRgbString();
  143. } else {
  144. return colorString;
  145. }
  146. }