alertRuleStatus.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import styled from '@emotion/styled';
  2. import {IconArrow, IconMute, IconNot} from 'sentry/icons';
  3. import {t} from 'sentry/locale';
  4. import {space} from 'sentry/styles/space';
  5. import type {ColorOrAlias} from 'sentry/utils/theme';
  6. import {hasActiveIncident} from 'sentry/views/alerts/list/rules/utils';
  7. import {getThresholdUnits} from 'sentry/views/alerts/rules/metric/constants';
  8. import {
  9. AlertRuleComparisonType,
  10. AlertRuleThresholdType,
  11. AlertRuleTriggerType,
  12. } from 'sentry/views/alerts/rules/metric/types';
  13. import {type CombinedMetricIssueAlerts, IncidentStatus} from 'sentry/views/alerts/types';
  14. import {isIssueAlert} from 'sentry/views/alerts/utils';
  15. interface Props {
  16. rule: CombinedMetricIssueAlerts;
  17. }
  18. export default function AlertRuleStatus({rule}: Props) {
  19. const activeIncident = hasActiveIncident(rule);
  20. function renderSnoozeStatus(): React.ReactNode {
  21. return (
  22. <IssueAlertStatusWrapper>
  23. <IconMute size="sm" color="subText" />
  24. {t('Muted')}
  25. </IssueAlertStatusWrapper>
  26. );
  27. }
  28. if (isIssueAlert(rule)) {
  29. if (rule.status === 'disabled') {
  30. return (
  31. <IssueAlertStatusWrapper>
  32. <IconNot size="sm" color="subText" />
  33. {t('Disabled')}
  34. </IssueAlertStatusWrapper>
  35. );
  36. }
  37. if (rule.snooze) {
  38. return renderSnoozeStatus();
  39. }
  40. return null;
  41. }
  42. if (rule.snooze) {
  43. return renderSnoozeStatus();
  44. }
  45. const criticalTrigger = rule.triggers.find(
  46. ({label}) => label === AlertRuleTriggerType.CRITICAL
  47. );
  48. const warningTrigger = rule.triggers.find(
  49. ({label}) => label === AlertRuleTriggerType.WARNING
  50. );
  51. const resolvedTrigger = rule.resolveThreshold;
  52. const trigger =
  53. activeIncident && rule.latestIncident?.status === IncidentStatus.CRITICAL
  54. ? criticalTrigger
  55. : warningTrigger ?? criticalTrigger;
  56. let iconColor: ColorOrAlias = 'successText';
  57. let iconDirection: 'up' | 'down' | undefined;
  58. let thresholdTypeText =
  59. activeIncident && rule.thresholdType === AlertRuleThresholdType.ABOVE
  60. ? t('Above')
  61. : t('Below');
  62. if (activeIncident) {
  63. iconColor =
  64. trigger?.label === AlertRuleTriggerType.CRITICAL
  65. ? 'errorText'
  66. : trigger?.label === AlertRuleTriggerType.WARNING
  67. ? 'warningText'
  68. : 'successText';
  69. iconDirection = rule.thresholdType === AlertRuleThresholdType.ABOVE ? 'up' : 'down';
  70. } else {
  71. // Use the Resolved threshold type, which is opposite of Critical
  72. iconDirection = rule.thresholdType === AlertRuleThresholdType.ABOVE ? 'down' : 'up';
  73. thresholdTypeText =
  74. rule.thresholdType === AlertRuleThresholdType.ABOVE ? t('Below') : t('Above');
  75. }
  76. return (
  77. <FlexCenter>
  78. <IconArrow color={iconColor} direction={iconDirection} />
  79. <TriggerText>
  80. {`${thresholdTypeText} ${
  81. rule.latestIncident || (!rule.latestIncident && !resolvedTrigger)
  82. ? trigger?.alertThreshold?.toLocaleString()
  83. : resolvedTrigger?.toLocaleString()
  84. }`}
  85. {getThresholdUnits(
  86. rule.aggregate,
  87. rule.comparisonDelta
  88. ? AlertRuleComparisonType.CHANGE
  89. : AlertRuleComparisonType.COUNT
  90. )}
  91. </TriggerText>
  92. </FlexCenter>
  93. );
  94. }
  95. const IssueAlertStatusWrapper = styled('div')`
  96. display: flex;
  97. align-items: center;
  98. gap: ${space(1)};
  99. line-height: 2;
  100. `;
  101. const TriggerText = styled('div')`
  102. margin-left: ${space(1)};
  103. white-space: nowrap;
  104. font-variant-numeric: tabular-nums;
  105. `;
  106. // TODO: explore utilizing the FlexContainer from app/components/container/flex.tsx
  107. const FlexCenter = styled('div')`
  108. display: flex;
  109. align-items: center;
  110. `;