threshold_mapper.ts 1.8 KB

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