event_manager.ts 4.1 KB

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