threshold_manager.ts 6.5 KB

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