threshold_manager.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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", {
  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. var model = this.thresholds[handleIndex];
  74. var value = model.value;
  75. var valueStr = value;
  76. var handleTopPos = 0;
  77. // handle no value
  78. if (!_.isNumber(value)) {
  79. valueStr = "";
  80. handleTopPos = defaultHandleTopPos;
  81. } else {
  82. var valueCanvasPos = this.plot.p2c({ x: 0, y: value });
  83. handleTopPos = Math.round(
  84. Math.min(Math.max(valueCanvasPos.top, 0), this.height) - 6
  85. );
  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 (
  94. !this.hasSecondYAxis &&
  95. this.panelCtrl.editingThresholds &&
  96. this.panelCtrl.panel.thresholds.length > 0
  97. );
  98. }
  99. prepare(elem, data) {
  100. this.hasSecondYAxis = false;
  101. for (var 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. var thresholdMargin =
  109. this.panelCtrl.panel.thresholds.length > 1 ? "220px" : "110px";
  110. elem.css("margin-right", thresholdMargin);
  111. } else if (this.needsCleanup) {
  112. elem.css("margin-right", "0");
  113. }
  114. }
  115. draw(plot) {
  116. this.thresholds = this.panelCtrl.panel.thresholds;
  117. this.plot = plot;
  118. this.placeholder = plot.getPlaceholder();
  119. if (this.needsCleanup) {
  120. this.cleanUp();
  121. }
  122. if (!this.shouldDrawHandles()) {
  123. return;
  124. }
  125. this.height = plot.height();
  126. if (this.thresholds.length > 0) {
  127. this.renderHandle(0, 10);
  128. }
  129. if (this.thresholds.length > 1) {
  130. this.renderHandle(1, this.height - 30);
  131. }
  132. this.placeholder.off("mousedown", ".alert-handle");
  133. this.placeholder.on(
  134. "mousedown",
  135. ".alert-handle",
  136. this.initDragging.bind(this)
  137. );
  138. this.needsCleanup = true;
  139. }
  140. addFlotOptions(options, panel) {
  141. if (!panel.thresholds || panel.thresholds.length === 0) {
  142. return;
  143. }
  144. var gtLimit = Infinity;
  145. var ltLimit = -Infinity;
  146. var i, threshold, other;
  147. for (i = 0; i < panel.thresholds.length; i++) {
  148. threshold = panel.thresholds[i];
  149. if (!_.isNumber(threshold.value)) {
  150. continue;
  151. }
  152. var limit;
  153. switch (threshold.op) {
  154. case "gt": {
  155. limit = gtLimit;
  156. // if next threshold is less then op and greater value, then use that as limit
  157. if (panel.thresholds.length > i + 1) {
  158. other = panel.thresholds[i + 1];
  159. if (other.value > threshold.value) {
  160. limit = other.value;
  161. ltLimit = limit;
  162. }
  163. }
  164. break;
  165. }
  166. case "lt": {
  167. limit = ltLimit;
  168. // if next threshold is less then op and greater value, then use that as limit
  169. if (panel.thresholds.length > i + 1) {
  170. other = panel.thresholds[i + 1];
  171. if (other.value < threshold.value) {
  172. limit = other.value;
  173. gtLimit = limit;
  174. }
  175. }
  176. break;
  177. }
  178. }
  179. var fillColor, lineColor;
  180. switch (threshold.colorMode) {
  181. case "critical": {
  182. fillColor = "rgba(234, 112, 112, 0.12)";
  183. lineColor = "rgba(237, 46, 24, 0.60)";
  184. break;
  185. }
  186. case "warning": {
  187. fillColor = "rgba(235, 138, 14, 0.12)";
  188. lineColor = "rgba(247, 149, 32, 0.60)";
  189. break;
  190. }
  191. case "ok": {
  192. fillColor = "rgba(11, 237, 50, 0.090)";
  193. lineColor = "rgba(6,163,69, 0.60)";
  194. break;
  195. }
  196. case "custom": {
  197. fillColor = threshold.fillColor;
  198. lineColor = threshold.lineColor;
  199. break;
  200. }
  201. }
  202. // fill
  203. if (threshold.fill) {
  204. options.grid.markings.push({
  205. yaxis: { from: threshold.value, to: limit },
  206. color: fillColor
  207. });
  208. }
  209. if (threshold.line) {
  210. options.grid.markings.push({
  211. yaxis: { from: threshold.value, to: threshold.value },
  212. color: lineColor
  213. });
  214. }
  215. }
  216. }
  217. }