ThresholdMapper.ts 2.2 KB

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