sidebar.tsx 9.0 KB

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