threshold_manager.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. var 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. var handleElem = $(evt.currentTarget).parents(".alert-handle-wrapper");
  29. var handleIndex = $(evt.currentTarget).data("handleIndex");
  30. var lastY = null;
  31. var posTop;
  32. var plot = this.plot;
  33. var panelCtrl = this.panelCtrl;
  34. var model = this.thresholds[handleIndex];
  35. function dragging(evt) {
  36. if (lastY === null) {
  37. lastY = evt.clientY;
  38. } else {
  39. var 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. var 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(function() {
  55. panelCtrl.render();
  56. panelCtrl.events.emit('threshold-changed', {threshold: model, handleIndex: handleIndex});
  57. });
  58. }
  59. lastY = null;
  60. posTop = handleElem.position().top;
  61. handleElem.on("mousemove", dragging);
  62. handleElem.on("mouseup", stopped);
  63. handleElem.on("mouseleave", stopped);
  64. }
  65. cleanUp() {
  66. this.placeholder.find(".alert-handle-wrapper").remove();
  67. this.needsCleanup = false;
  68. }
  69. renderHandle(handleIndex, defaultHandleTopPos) {
  70. var model = this.thresholds[handleIndex];
  71. var value = model.value;
  72. var valueStr = value;
  73. var handleTopPos = 0;
  74. // handle no value
  75. if (!_.isNumber(value)) {
  76. valueStr = '';
  77. handleTopPos = defaultHandleTopPos;
  78. } else {
  79. var valueCanvasPos = this.plot.p2c({x: 0, y: value});
  80. handleTopPos = Math.round(Math.min(Math.max(valueCanvasPos.top, 0), this.height) - 6);
  81. }
  82. var handleElem = $(this.getHandleHtml(handleIndex, model, valueStr));
  83. this.placeholder.append(handleElem);
  84. handleElem.toggleClass('alert-handle-wrapper--no-value', valueStr === '');
  85. handleElem.css({top: handleTopPos});
  86. }
  87. shouldDrawHandles() {
  88. return !this.hasSecondYAxis && this.panelCtrl.editingThresholds && this.panelCtrl.panel.thresholds.length > 0;
  89. }
  90. prepare(elem, data) {
  91. this.hasSecondYAxis = false;
  92. for (var i = 0; i < data.length; i++) {
  93. if (data[i].yaxis > 1) {
  94. this.hasSecondYAxis = true;
  95. break;
  96. }
  97. }
  98. if (this.shouldDrawHandles()) {
  99. var thresholdMargin = this.panelCtrl.panel.thresholds.length > 1 ? '220px' : '110px';
  100. elem.css('margin-right', thresholdMargin);
  101. } else if (this.needsCleanup) {
  102. elem.css('margin-right', '0');
  103. }
  104. }
  105. draw(plot) {
  106. this.thresholds = this.panelCtrl.panel.thresholds;
  107. this.plot = plot;
  108. this.placeholder = plot.getPlaceholder();
  109. if (this.needsCleanup) {
  110. this.cleanUp();
  111. }
  112. if (!this.shouldDrawHandles()) {
  113. return;
  114. }
  115. this.height = plot.height();
  116. if (this.thresholds.length > 0) {
  117. this.renderHandle(0, 10);
  118. }
  119. if (this.thresholds.length > 1) {
  120. this.renderHandle(1, this.height-30);
  121. }
  122. this.placeholder.off('mousedown', '.alert-handle');
  123. this.placeholder.on('mousedown', '.alert-handle', this.initDragging.bind(this));
  124. this.needsCleanup = true;
  125. }
  126. addFlotOptions(options, panel) {
  127. if (!panel.thresholds || panel.thresholds.length === 0) {
  128. return;
  129. }
  130. var gtLimit = Infinity;
  131. var ltLimit = -Infinity;
  132. var i, threshold, other;
  133. for (i = 0; i < panel.thresholds.length; i++) {
  134. threshold = panel.thresholds[i];
  135. if (!_.isNumber(threshold.value)) {
  136. continue;
  137. }
  138. var limit;
  139. switch (threshold.op) {
  140. case 'gt': {
  141. limit = gtLimit;
  142. // if next threshold is less then op and greater value, then use that as limit
  143. if (panel.thresholds.length > i+1) {
  144. other = panel.thresholds[i+1];
  145. if (other.value > threshold.value) {
  146. limit = other.value;
  147. ltLimit = limit;
  148. }
  149. }
  150. break;
  151. }
  152. case 'lt': {
  153. limit = ltLimit;
  154. // if next threshold is less then op and greater value, then use that as limit
  155. if (panel.thresholds.length > i+1) {
  156. other = panel.thresholds[i+1];
  157. if (other.value < threshold.value) {
  158. limit = other.value;
  159. gtLimit = limit;
  160. }
  161. }
  162. break;
  163. }
  164. }
  165. var fillColor, lineColor;
  166. switch (threshold.colorMode) {
  167. case 'critical': {
  168. fillColor = 'rgba(234, 112, 112, 0.12)';
  169. lineColor = 'rgba(237, 46, 24, 0.60)';
  170. break;
  171. }
  172. case 'warning': {
  173. fillColor = 'rgba(235, 138, 14, 0.12)';
  174. lineColor = 'rgba(247, 149, 32, 0.60)';
  175. break;
  176. }
  177. case 'ok': {
  178. fillColor = 'rgba(11, 237, 50, 0.090)';
  179. lineColor = 'rgba(6,163,69, 0.60)';
  180. break;
  181. }
  182. case 'custom': {
  183. fillColor = threshold.fillColor;
  184. lineColor = threshold.lineColor;
  185. break;
  186. }
  187. }
  188. // fill
  189. if (threshold.fill) {
  190. options.grid.markings.push({yaxis: {from: threshold.value, to: limit}, color: fillColor});
  191. }
  192. if (threshold.line) {
  193. options.grid.markings.push({yaxis: {from: threshold.value, to: threshold.value}, color: lineColor});
  194. }
  195. }
  196. }
  197. }