getChangeStatus.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import type {Trigger} from 'sentry/views/alerts/rules/metric/types';
  2. import {
  3. AlertRuleThresholdType,
  4. AlertRuleTriggerType,
  5. } from 'sentry/views/alerts/rules/metric/types';
  6. export const getChangeStatus = (
  7. value: number,
  8. thresholdType: AlertRuleThresholdType,
  9. triggers: Trigger[]
  10. ): string => {
  11. const criticalTrigger = triggers?.find(
  12. trig => trig.label === AlertRuleTriggerType.CRITICAL
  13. );
  14. const warningTrigger = triggers?.find(
  15. trig => trig.label === AlertRuleTriggerType.WARNING
  16. );
  17. const criticalTriggerAlertThreshold =
  18. typeof criticalTrigger?.alertThreshold === 'number'
  19. ? criticalTrigger.alertThreshold
  20. : undefined;
  21. const warningTriggerAlertThreshold =
  22. typeof warningTrigger?.alertThreshold === 'number'
  23. ? warningTrigger.alertThreshold
  24. : undefined;
  25. // Need to catch the critical threshold cases before warning threshold cases
  26. if (
  27. thresholdType === AlertRuleThresholdType.ABOVE &&
  28. criticalTriggerAlertThreshold &&
  29. value >= criticalTriggerAlertThreshold
  30. ) {
  31. return AlertRuleTriggerType.CRITICAL;
  32. }
  33. if (
  34. thresholdType === AlertRuleThresholdType.ABOVE &&
  35. warningTriggerAlertThreshold &&
  36. value >= warningTriggerAlertThreshold
  37. ) {
  38. return AlertRuleTriggerType.WARNING;
  39. }
  40. // When threshold is below(lower than in comparison alerts) the % diff value is negative
  41. // It crosses the threshold if its abs value is greater than threshold
  42. // -80% change crosses below 60% threshold -1 * (-80) > 60
  43. if (
  44. thresholdType === AlertRuleThresholdType.BELOW &&
  45. criticalTriggerAlertThreshold &&
  46. -1 * value >= criticalTriggerAlertThreshold
  47. ) {
  48. return AlertRuleTriggerType.CRITICAL;
  49. }
  50. if (
  51. thresholdType === AlertRuleThresholdType.BELOW &&
  52. warningTriggerAlertThreshold &&
  53. -1 * value >= warningTriggerAlertThreshold
  54. ) {
  55. return AlertRuleTriggerType.WARNING;
  56. }
  57. return '';
  58. };