sidebar.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import {Fragment, PureComponent, ReactNode} from 'react';
  2. import styled from '@emotion/styled';
  3. import AlertBadge from 'sentry/components/alertBadge';
  4. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  5. import {SectionHeading} from 'sentry/components/charts/styles';
  6. import DateTime from 'sentry/components/dateTime';
  7. import Duration from 'sentry/components/duration';
  8. import {KeyValueTable, KeyValueTableRow} from 'sentry/components/keyValueTable';
  9. import TimeSince from 'sentry/components/timeSince';
  10. import {IconDiamond} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {Actor} from 'sentry/types';
  14. import getDynamicText from 'sentry/utils/getDynamicText';
  15. import {COMPARISON_DELTA_OPTIONS} from 'sentry/views/alerts/rules/metric/constants';
  16. import {
  17. Action,
  18. AlertRuleThresholdType,
  19. AlertRuleTriggerType,
  20. MetricRule,
  21. } from 'sentry/views/alerts/rules/metric/types';
  22. import {AlertWizardAlertNames} from 'sentry/views/alerts/wizard/options';
  23. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  24. import {IncidentStatus} from '../../../types';
  25. interface Props {
  26. rule: MetricRule;
  27. }
  28. export default class Sidebar extends PureComponent<Props> {
  29. getTimeWindow(): ReactNode {
  30. const {rule} = this.props;
  31. if (!rule) {
  32. return '';
  33. }
  34. const {timeWindow} = rule;
  35. return <Duration seconds={timeWindow * 60} />;
  36. }
  37. renderTrigger(label: string, threshold: number, actions: Action[]): ReactNode {
  38. const {rule} = this.props;
  39. const status =
  40. label === AlertRuleTriggerType.CRITICAL
  41. ? t('Critical')
  42. : label === AlertRuleTriggerType.WARNING
  43. ? t('Warning')
  44. : t('Resolved');
  45. const statusIconColor =
  46. label === AlertRuleTriggerType.CRITICAL
  47. ? 'errorText'
  48. : label === AlertRuleTriggerType.WARNING
  49. ? 'warningText'
  50. : 'successText';
  51. const defaultAction = t('Change alert status to %s', status);
  52. const aboveThreshold =
  53. label === AlertRuleTriggerType.RESOLVE
  54. ? rule.thresholdType === AlertRuleThresholdType.BELOW
  55. : rule.thresholdType === AlertRuleThresholdType.ABOVE;
  56. const thresholdTypeText = aboveThreshold
  57. ? rule.comparisonDelta
  58. ? t('higher')
  59. : t('above')
  60. : rule.comparisonDelta
  61. ? t('lower')
  62. : t('below');
  63. const thresholdText = rule.comparisonDelta
  64. ? tct(
  65. '[metric] is [threshold]% [comparisonType] in [timeWindow] compared to [comparisonDelta]',
  66. {
  67. metric: AlertWizardAlertNames[getAlertTypeFromAggregateDataset(rule)],
  68. threshold,
  69. comparisonType: thresholdTypeText,
  70. timeWindow: this.getTimeWindow(),
  71. comparisonDelta: (
  72. COMPARISON_DELTA_OPTIONS.find(
  73. ({value}) => value === rule.comparisonDelta
  74. ) ?? COMPARISON_DELTA_OPTIONS[0]
  75. ).label,
  76. }
  77. )
  78. : tct('[metric] is [condition] in [timeWindow]', {
  79. metric: AlertWizardAlertNames[getAlertTypeFromAggregateDataset(rule)],
  80. condition: `${thresholdTypeText} ${threshold}`,
  81. timeWindow: this.getTimeWindow(),
  82. });
  83. return (
  84. <TriggerContainer>
  85. <TriggerTitle>
  86. <IconDiamond color={statusIconColor} size="xs" />
  87. <TriggerTitleText>{t('%s Conditions', status)}</TriggerTitleText>
  88. </TriggerTitle>
  89. <TriggerStep>
  90. <TriggerTitleText>When</TriggerTitleText>
  91. <TriggerActions>
  92. <TriggerText>{thresholdText}</TriggerText>
  93. </TriggerActions>
  94. </TriggerStep>
  95. <TriggerStep>
  96. <TriggerTitleText>Then</TriggerTitleText>
  97. <TriggerActions>
  98. {actions.map(action => (
  99. <TriggerText key={action.id}>{action.desc}</TriggerText>
  100. ))}
  101. <TriggerText>{defaultAction}</TriggerText>
  102. </TriggerActions>
  103. </TriggerStep>
  104. </TriggerContainer>
  105. );
  106. }
  107. render() {
  108. const {rule} = this.props;
  109. // get current status
  110. const latestIncident = rule.latestIncident;
  111. const status = latestIncident ? latestIncident.status : IncidentStatus.CLOSED;
  112. // The date at which the alert was triggered or resolved
  113. const activityDate =
  114. latestIncident?.dateClosed ?? latestIncident?.dateStarted ?? null;
  115. const criticalTrigger = rule?.triggers.find(
  116. ({label}) => label === AlertRuleTriggerType.CRITICAL
  117. );
  118. const warningTrigger = rule?.triggers.find(
  119. ({label}) => label === AlertRuleTriggerType.WARNING
  120. );
  121. const ownerId = rule.owner?.split(':')[1];
  122. const teamActor = ownerId && {type: 'team' as Actor['type'], id: ownerId, name: ''};
  123. return (
  124. <Fragment>
  125. <StatusContainer>
  126. <HeaderItem>
  127. <Heading noMargin>{t('Alert Status')}</Heading>
  128. <Status>
  129. <AlertBadge status={status} withText />
  130. </Status>
  131. </HeaderItem>
  132. <HeaderItem>
  133. <Heading noMargin>{t('Last Triggered')}</Heading>
  134. <Status>
  135. {activityDate ? (
  136. <TimeSince date={activityDate} />
  137. ) : (
  138. t('No alerts triggered')
  139. )}
  140. </Status>
  141. </HeaderItem>
  142. </StatusContainer>
  143. <SidebarGroup>
  144. {typeof criticalTrigger?.alertThreshold === 'number' &&
  145. this.renderTrigger(
  146. criticalTrigger.label,
  147. criticalTrigger.alertThreshold,
  148. criticalTrigger.actions
  149. )}
  150. {typeof warningTrigger?.alertThreshold === 'number' &&
  151. this.renderTrigger(
  152. warningTrigger.label,
  153. warningTrigger.alertThreshold,
  154. warningTrigger.actions
  155. )}
  156. {typeof rule.resolveThreshold === 'number' &&
  157. this.renderTrigger(AlertRuleTriggerType.RESOLVE, rule.resolveThreshold, [])}
  158. </SidebarGroup>
  159. <SidebarGroup>
  160. <Heading>{t('Alert Rule Details')}</Heading>
  161. <KeyValueTable>
  162. <KeyValueTableRow
  163. keyName={t('Environment')}
  164. value={<OverflowTableValue>{rule.environment ?? '-'}</OverflowTableValue>}
  165. />
  166. <KeyValueTableRow
  167. keyName={t('Date created')}
  168. value={
  169. <DateTime
  170. date={getDynamicText({
  171. value: rule.dateCreated,
  172. fixed: new Date('2021-04-20'),
  173. })}
  174. format="ll"
  175. />
  176. }
  177. />
  178. {rule.createdBy && (
  179. <KeyValueTableRow
  180. keyName={t('Created By')}
  181. value={
  182. <OverflowTableValue>{rule.createdBy.name ?? '-'}</OverflowTableValue>
  183. }
  184. />
  185. )}
  186. {rule.dateModified && (
  187. <KeyValueTableRow
  188. keyName={t('Last Modified')}
  189. value={<TimeSince date={rule.dateModified} suffix={t('ago')} />}
  190. />
  191. )}
  192. <KeyValueTableRow
  193. keyName={t('Team')}
  194. value={
  195. teamActor ? <ActorAvatar actor={teamActor} size={24} /> : t('Unassigned')
  196. }
  197. />
  198. </KeyValueTable>
  199. </SidebarGroup>
  200. </Fragment>
  201. );
  202. }
  203. }
  204. const SidebarGroup = styled('div')`
  205. margin-bottom: ${space(3)};
  206. `;
  207. const HeaderItem = styled('div')`
  208. flex: 1;
  209. display: flex;
  210. flex-direction: column;
  211. > *:nth-child(2) {
  212. flex: 1;
  213. display: flex;
  214. align-items: center;
  215. }
  216. `;
  217. const Status = styled('div')`
  218. position: relative;
  219. display: grid;
  220. grid-template-columns: auto auto auto;
  221. gap: ${space(0.5)};
  222. font-size: ${p => p.theme.fontSizeLarge};
  223. `;
  224. const StatusContainer = styled('div')`
  225. height: 60px;
  226. display: flex;
  227. margin-bottom: ${space(1)};
  228. `;
  229. const Heading = styled(SectionHeading)<{noMargin?: boolean}>`
  230. margin-top: ${p => (p.noMargin ? 0 : space(2))};
  231. margin-bottom: ${p => (p.noMargin ? 0 : space(1))};
  232. `;
  233. const OverflowTableValue = styled('div')`
  234. ${p => p.theme.overflowEllipsis}
  235. `;
  236. const TriggerContainer = styled('div')`
  237. display: grid;
  238. grid-template-rows: auto auto auto;
  239. gap: ${space(1)};
  240. margin-top: ${space(4)};
  241. `;
  242. const TriggerTitle = styled('div')`
  243. display: grid;
  244. grid-template-columns: 20px 1fr;
  245. align-items: center;
  246. `;
  247. const TriggerTitleText = styled('h4')`
  248. color: ${p => p.theme.subText};
  249. font-size: ${p => p.theme.fontSizeMedium};
  250. margin: 0;
  251. line-height: 24px;
  252. min-width: 40px;
  253. `;
  254. const TriggerStep = styled('div')`
  255. display: grid;
  256. grid-template-columns: 40px 1fr;
  257. align-items: stretch;
  258. `;
  259. const TriggerActions = styled('div')`
  260. display: grid;
  261. grid-template-columns: repeat(1fr);
  262. gap: ${space(0.25)};
  263. align-items: center;
  264. `;
  265. const TriggerText = styled('span')`
  266. display: block;
  267. background-color: ${p => p.theme.surface200};
  268. padding: ${space(0.25)} ${space(0.75)};
  269. border-radius: ${p => p.theme.borderRadius};
  270. color: ${p => p.theme.textColor};
  271. font-size: ${p => p.theme.fontSizeSmall};
  272. width: 100%;
  273. font-weight: 400;
  274. `;