threshold_manager.ts 6.3 KB

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