threshold_manager.ts 6.9 KB

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