threshold_mapper.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. export class ThresholdMapper {
  2. static alertToGraphThresholds(panel) {
  3. if (panel.type !== 'graph') {
  4. return false;
  5. }
  6. for (var i = 0; i < panel.alert.conditions.length; i++) {
  7. let condition = panel.alert.conditions[i];
  8. if (condition.type !== 'query') {
  9. continue;
  10. }
  11. var evaluator = condition.evaluator;
  12. var thresholds = (panel.thresholds = []);
  13. switch (evaluator.type) {
  14. case 'gt': {
  15. let value = evaluator.params[0];
  16. thresholds.push({ value: value, op: 'gt' });
  17. break;
  18. }
  19. case 'lt': {
  20. let value = evaluator.params[0];
  21. thresholds.push({ value: value, op: 'lt' });
  22. break;
  23. }
  24. case 'outside_range': {
  25. let value1 = evaluator.params[0];
  26. let value2 = evaluator.params[1];
  27. if (value1 > value2) {
  28. thresholds.push({ value: value1, op: 'gt' });
  29. thresholds.push({ value: value2, op: 'lt' });
  30. } else {
  31. thresholds.push({ value: value1, op: 'lt' });
  32. thresholds.push({ value: value2, op: 'gt' });
  33. }
  34. break;
  35. }
  36. case 'within_range': {
  37. let value1 = evaluator.params[0];
  38. let value2 = evaluator.params[1];
  39. if (value1 > value2) {
  40. thresholds.push({ value: value1, op: 'lt' });
  41. thresholds.push({ value: value2, op: 'gt' });
  42. } else {
  43. thresholds.push({ value: value1, op: 'gt' });
  44. thresholds.push({ value: value2, op: 'lt' });
  45. }
  46. break;
  47. }
  48. }
  49. break;
  50. }
  51. for (var t of panel.thresholds) {
  52. t.fill = true;
  53. t.line = true;
  54. t.colorMode = 'critical';
  55. }
  56. var updated = true;
  57. return updated;
  58. }
  59. }