ThresholdMapper.ts 1.8 KB

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