threshold_manager.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import 'vendor/flot/jquery.flot';
  2. import $ from 'jquery';
  3. import { isNumber } from 'lodash';
  4. import { PanelCtrl } from 'app/angular/panel/panel_ctrl';
  5. import { config } from 'app/core/config';
  6. import { CoreEvents } from 'app/types';
  7. export class ThresholdManager {
  8. plot: any;
  9. placeholder: any;
  10. height: any;
  11. thresholds: any;
  12. needsCleanup = false;
  13. hasSecondYAxis: any;
  14. constructor(private panelCtrl: PanelCtrl) {}
  15. getHandleHtml(handleIndex: any, model: { colorMode: string }, valueStr: any) {
  16. let stateClass = model.colorMode;
  17. if (model.colorMode === 'custom') {
  18. stateClass = 'critical';
  19. }
  20. return `
  21. <div class="alert-handle-wrapper alert-handle-wrapper--T${handleIndex}">
  22. <div class="alert-handle-line alert-handle-line--${stateClass}">
  23. </div>
  24. <div class="alert-handle" data-handle-index="${handleIndex}">
  25. <i class="icon-gf icon-gf-${stateClass} alert-state-${stateClass}"></i>
  26. <span class="alert-handle-value">${valueStr}<i class="alert-handle-grip"></i></span>
  27. </div>
  28. </div>`;
  29. }
  30. initDragging(evt: any) {
  31. const handleElem = $(evt.currentTarget).parents('.alert-handle-wrapper');
  32. const handleIndex = $(evt.currentTarget).data('handleIndex');
  33. let lastY: number | null = null;
  34. let posTop: number;
  35. const plot = this.plot;
  36. const panelCtrl = this.panelCtrl;
  37. const model = this.thresholds[handleIndex];
  38. function dragging(evt: any) {
  39. if (lastY === null) {
  40. lastY = evt.clientY;
  41. } else {
  42. const diff = evt.clientY - lastY;
  43. posTop = posTop + diff;
  44. lastY = evt.clientY;
  45. handleElem.css({ top: posTop + diff });
  46. }
  47. }
  48. function stopped() {
  49. // calculate graph level
  50. let graphValue = plot.c2p({ left: 0, top: posTop }).y;
  51. graphValue = parseInt(graphValue.toFixed(0), 10);
  52. model.value = graphValue;
  53. handleElem.off('mousemove', dragging);
  54. document.removeEventListener('mouseup', stopped);
  55. // trigger digest and render
  56. panelCtrl.$scope.$apply(() => {
  57. panelCtrl.render();
  58. panelCtrl.events.emit(CoreEvents.thresholdChanged, {
  59. threshold: model,
  60. handleIndex: handleIndex,
  61. });
  62. });
  63. }
  64. lastY = null;
  65. posTop = handleElem.position().top;
  66. handleElem.on('mousemove', dragging);
  67. document.addEventListener('mouseup', stopped);
  68. }
  69. cleanUp() {
  70. this.placeholder.find('.alert-handle-wrapper').remove();
  71. this.needsCleanup = false;
  72. }
  73. renderHandle(handleIndex: number, defaultHandleTopPos: number) {
  74. const model = this.thresholds[handleIndex];
  75. // alerting defines
  76. if (!model.visible && (this.panelCtrl as any).alert) {
  77. return;
  78. }
  79. const value = model.value;
  80. let valueStr = value;
  81. let handleTopPos = 0;
  82. // handle no value
  83. if (!isNumber(value)) {
  84. valueStr = '';
  85. handleTopPos = defaultHandleTopPos;
  86. } else {
  87. const valueCanvasPos = this.plot.p2c({ x: 0, y: value });
  88. handleTopPos = Math.round(Math.min(Math.max(valueCanvasPos.top, 0), this.height) - 6);
  89. }
  90. const handleElem = $(this.getHandleHtml(handleIndex, model, valueStr));
  91. this.placeholder.append(handleElem);
  92. handleElem.toggleClass('alert-handle-wrapper--no-value', valueStr === '');
  93. handleElem.css({ top: handleTopPos });
  94. }
  95. shouldDrawHandles() {
  96. // @ts-ignore
  97. return !this.hasSecondYAxis && this.panelCtrl.editingThresholds && this.panelCtrl.panel.thresholds.length > 0;
  98. }
  99. prepare(elem: JQuery, data: any[]) {
  100. this.hasSecondYAxis = false;
  101. for (let i = 0; i < data.length; i++) {
  102. if (data[i].yaxis > 1) {
  103. this.hasSecondYAxis = true;
  104. break;
  105. }
  106. }
  107. if (this.shouldDrawHandles()) {
  108. const thresholdMargin = this.panelCtrl.panel.thresholds.length > 1 ? '220px' : '110px';
  109. elem.css('margin-right', thresholdMargin);
  110. } else if (this.needsCleanup) {
  111. elem.css('margin-right', '0');
  112. }
  113. }
  114. draw(plot: any) {
  115. this.thresholds = this.panelCtrl.panel.thresholds;
  116. this.plot = plot;
  117. this.placeholder = plot.getPlaceholder();
  118. if (this.needsCleanup) {
  119. this.cleanUp();
  120. }
  121. if (!this.shouldDrawHandles()) {
  122. return;
  123. }
  124. this.height = plot.height();
  125. if (this.thresholds.length > 0) {
  126. this.renderHandle(0, 10);
  127. }
  128. if (this.thresholds.length > 1) {
  129. this.renderHandle(1, this.height - 30);
  130. }
  131. this.placeholder.off('mousedown', '.alert-handle');
  132. this.placeholder.on('mousedown', '.alert-handle', this.initDragging.bind(this));
  133. this.needsCleanup = true;
  134. }
  135. addFlotOptions(options: any, panel: any) {
  136. if (!panel.thresholds || panel.thresholds.length === 0) {
  137. return;
  138. }
  139. let gtLimit = Infinity;
  140. let ltLimit = -Infinity;
  141. let i, threshold, other;
  142. for (i = 0; i < panel.thresholds.length; i++) {
  143. threshold = panel.thresholds[i];
  144. if (!isNumber(threshold.value)) {
  145. continue;
  146. }
  147. let limit;
  148. switch (threshold.op) {
  149. case 'gt': {
  150. limit = gtLimit;
  151. // if next threshold is less then op and greater value, then use that as limit
  152. if (panel.thresholds.length > i + 1) {
  153. other = panel.thresholds[i + 1];
  154. if (other.value > threshold.value) {
  155. limit = other.value;
  156. ltLimit = limit;
  157. }
  158. }
  159. break;
  160. }
  161. case 'lt': {
  162. limit = ltLimit;
  163. // if next threshold is less then op and greater value, then use that as limit
  164. if (panel.thresholds.length > i + 1) {
  165. other = panel.thresholds[i + 1];
  166. if (other.value < threshold.value) {
  167. limit = other.value;
  168. gtLimit = limit;
  169. }
  170. }
  171. break;
  172. }
  173. }
  174. let fillColor, lineColor;
  175. switch (threshold.colorMode) {
  176. case 'critical': {
  177. fillColor = 'rgba(234, 112, 112, 0.12)';
  178. lineColor = 'rgba(237, 46, 24, 0.60)';
  179. break;
  180. }
  181. case 'warning': {
  182. fillColor = 'rgba(235, 138, 14, 0.12)';
  183. lineColor = 'rgba(247, 149, 32, 0.60)';
  184. break;
  185. }
  186. case 'ok': {
  187. fillColor = 'rgba(11, 237, 50, 0.090)';
  188. lineColor = 'rgba(6,163,69, 0.60)';
  189. break;
  190. }
  191. case 'custom': {
  192. fillColor = threshold.fillColor;
  193. lineColor = threshold.lineColor;
  194. break;
  195. }
  196. }
  197. // fill
  198. if (threshold.fill) {
  199. if (threshold.yaxis === 'right' && this.hasSecondYAxis) {
  200. options.grid.markings.push({
  201. y2axis: { from: threshold.value, to: limit },
  202. color: config.theme.visualization.getColorByName(fillColor),
  203. });
  204. } else {
  205. options.grid.markings.push({
  206. yaxis: { from: threshold.value, to: limit },
  207. color: config.theme.visualization.getColorByName(fillColor),
  208. });
  209. }
  210. }
  211. if (threshold.line) {
  212. if (threshold.yaxis === 'right' && this.hasSecondYAxis) {
  213. options.grid.markings.push({
  214. y2axis: { from: threshold.value, to: threshold.value },
  215. color: config.theme.visualization.getColorByName(lineColor),
  216. });
  217. } else {
  218. options.grid.markings.push({
  219. yaxis: { from: threshold.value, to: threshold.value },
  220. color: config.theme.visualization.getColorByName(lineColor),
  221. });
  222. }
  223. }
  224. }
  225. }
  226. }