event_manager.ts 4.5 KB

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