alertRuleStatus.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // Anomaly detection alerts have different labels
  63. const statusLabel = activeIncident ? t('Critical') : t('Resolved');
  64. if (activeIncident) {
  65. iconColor =
  66. trigger?.label === AlertRuleTriggerType.CRITICAL
  67. ? 'errorText'
  68. : trigger?.label === AlertRuleTriggerType.WARNING
  69. ? 'warningText'
  70. : 'successText';
  71. iconDirection = rule.thresholdType === AlertRuleThresholdType.ABOVE ? 'up' : 'down';
  72. } else {
  73. // Use the Resolved threshold type, which is opposite of Critical
  74. iconDirection = rule.thresholdType === AlertRuleThresholdType.ABOVE ? 'down' : 'up';
  75. thresholdTypeText =
  76. rule.thresholdType === AlertRuleThresholdType.ABOVE ? t('Below') : t('Above');
  77. }
  78. return (
  79. <FlexCenter>
  80. {rule.detectionType !== AlertRuleComparisonType.DYNAMIC && (
  81. <IconArrow color={iconColor} direction={iconDirection} />
  82. )}
  83. {rule.detectionType !== AlertRuleComparisonType.DYNAMIC ? (
  84. <TriggerText>
  85. {`${thresholdTypeText} ${
  86. rule.latestIncident || (!rule.latestIncident && !resolvedTrigger)
  87. ? trigger?.alertThreshold?.toLocaleString()
  88. : resolvedTrigger?.toLocaleString()
  89. }`}
  90. {getThresholdUnits(
  91. rule.aggregate,
  92. rule.comparisonDelta
  93. ? AlertRuleComparisonType.CHANGE
  94. : AlertRuleComparisonType.COUNT
  95. )}
  96. </TriggerText>
  97. ) : (
  98. <TriggerText>{statusLabel}</TriggerText>
  99. )}
  100. </FlexCenter>
  101. );
  102. }
  103. const IssueAlertStatusWrapper = styled('div')`
  104. display: flex;
  105. align-items: center;
  106. gap: ${space(1)};
  107. line-height: 2;
  108. `;
  109. const TriggerText = styled('div')`
  110. margin-left: ${space(1)};
  111. white-space: nowrap;
  112. font-variant-numeric: tabular-nums;
  113. `;
  114. // TODO: explore utilizing the FlexContainer from app/components/container/flex.tsx
  115. const FlexCenter = styled('div')`
  116. display: flex;
  117. align-items: center;
  118. `;