event_manager.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { each, filter, keys } from 'lodash';
  2. import tinycolor from 'tinycolor2';
  3. import { AnnotationEvent } from '@grafana/data';
  4. import {
  5. ALERTING_COLOR,
  6. DEFAULT_ANNOTATION_COLOR,
  7. NO_DATA_COLOR,
  8. OK_COLOR,
  9. PENDING_COLOR,
  10. REGION_FILL_ALPHA,
  11. } from '@grafana/ui';
  12. import { MetricsPanelCtrl } from 'app/angular/panel/metrics_panel_ctrl';
  13. export class EventManager {
  14. event: AnnotationEvent | null = null;
  15. editorOpen = false;
  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: { from: any; to: any }) {
  26. if (!this.event) {
  27. this.event = {};
  28. this.event.dashboardId = this.panelCtrl.dashboard.id;
  29. this.event.panelId = this.panelCtrl.panel.id;
  30. }
  31. // update time
  32. this.event.time = range.from;
  33. this.event.isRegion = false;
  34. if (range.to) {
  35. this.event.timeEnd = range.to;
  36. this.event.isRegion = true;
  37. }
  38. this.panelCtrl.render();
  39. }
  40. editEvent(event: AnnotationEvent, elem?: any) {
  41. this.event = event;
  42. this.panelCtrl.render();
  43. }
  44. addFlotEvents(annotations: any, flotOptions: any) {
  45. if (!this.event && annotations.length === 0) {
  46. return;
  47. }
  48. const types: any = {
  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. $__pending: {
  65. color: PENDING_COLOR,
  66. position: 'BOTTOM',
  67. markerSize: 5,
  68. },
  69. $__editing: {
  70. color: DEFAULT_ANNOTATION_COLOR,
  71. position: 'BOTTOM',
  72. markerSize: 5,
  73. },
  74. };
  75. if (this.event) {
  76. if (this.event.isRegion) {
  77. annotations = [
  78. {
  79. isRegion: true,
  80. min: this.event.time,
  81. timeEnd: this.event.timeEnd,
  82. text: this.event.text,
  83. eventType: '$__editing',
  84. editModel: this.event,
  85. },
  86. ];
  87. } else {
  88. annotations = [
  89. {
  90. min: this.event.time,
  91. text: this.event.text,
  92. editModel: this.event,
  93. eventType: '$__editing',
  94. },
  95. ];
  96. }
  97. } else {
  98. // annotations from query
  99. for (let i = 0; i < annotations.length; i++) {
  100. const item = annotations[i];
  101. // add properties used by jquery flot events
  102. item.min = item.time;
  103. item.max = item.time;
  104. item.eventType = item.type;
  105. if (item.newState) {
  106. item.eventType = '$__' + item.newState;
  107. continue;
  108. }
  109. if (!types[item.type]) {
  110. types[item.type] = {
  111. color: item.color,
  112. position: 'BOTTOM',
  113. markerSize: 5,
  114. };
  115. }
  116. }
  117. }
  118. const regions = getRegions(annotations);
  119. addRegionMarking(regions, flotOptions);
  120. const eventSectionHeight = 20;
  121. const eventSectionMargin = 7;
  122. flotOptions.grid.eventSectionHeight = eventSectionMargin;
  123. flotOptions.xaxis.eventSectionHeight = eventSectionHeight;
  124. flotOptions.events = {
  125. levels: keys(types).length + 1,
  126. data: annotations,
  127. types: types,
  128. manager: this,
  129. };
  130. }
  131. }
  132. function getRegions(events: AnnotationEvent[]) {
  133. return filter(events, 'isRegion');
  134. }
  135. function addRegionMarking(regions: any[], flotOptions: { grid: { markings: any } }) {
  136. const markings = flotOptions.grid.markings;
  137. const defaultColor = DEFAULT_ANNOTATION_COLOR;
  138. let fillColor;
  139. each(regions, (region) => {
  140. if (region.source) {
  141. fillColor = region.color || defaultColor;
  142. } else {
  143. fillColor = defaultColor;
  144. }
  145. fillColor = addAlphaToRGB(fillColor, REGION_FILL_ALPHA);
  146. markings.push({
  147. xaxis: { from: region.min, to: region.timeEnd },
  148. color: fillColor,
  149. });
  150. });
  151. }
  152. function addAlphaToRGB(colorString: string, alpha: number): string {
  153. const color = tinycolor(colorString);
  154. if (color.isValid()) {
  155. color.setAlpha(alpha);
  156. return color.toRgbString();
  157. } else {
  158. return colorString;
  159. }
  160. }