threshold_manager.ts 7.2 KB

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