event_manager.ts 4.2 KB

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