sidebar.tsx 8.8 KB

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