threshold_manager.ts 7.1 KB

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