sidebar.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 {
  17. getSearchFilters,
  18. isOnDemandSearchKey,
  19. OnDemandWarningIcon,
  20. } from 'sentry/utils/onDemandMetrics/index';
  21. import {COMPARISON_DELTA_OPTIONS} from 'sentry/views/alerts/rules/metric/constants';
  22. import {
  23. Action,
  24. AlertRuleThresholdType,
  25. AlertRuleTriggerType,
  26. MetricRule,
  27. } from 'sentry/views/alerts/rules/metric/types';
  28. import {IncidentStatus} from 'sentry/views/alerts/types';
  29. import {AlertWizardAlertNames} from 'sentry/views/alerts/wizard/options';
  30. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  31. interface MetricDetailsSidebarProps {
  32. isOnDemandMetricAlert: boolean;
  33. rule: MetricRule;
  34. }
  35. function TriggerDescription({
  36. rule,
  37. actions,
  38. label,
  39. threshold,
  40. }: {
  41. actions: Action[];
  42. label: string;
  43. rule: MetricRule;
  44. threshold: number;
  45. }) {
  46. const status =
  47. label === AlertRuleTriggerType.CRITICAL
  48. ? t('Critical')
  49. : label === AlertRuleTriggerType.WARNING
  50. ? t('Warning')
  51. : t('Resolved');
  52. const statusIconColor =
  53. label === AlertRuleTriggerType.CRITICAL
  54. ? 'errorText'
  55. : label === AlertRuleTriggerType.WARNING
  56. ? 'warningText'
  57. : 'successText';
  58. const defaultAction = t('Change alert status to %s', status);
  59. const aboveThreshold =
  60. label === AlertRuleTriggerType.RESOLVE
  61. ? rule.thresholdType === AlertRuleThresholdType.BELOW
  62. : rule.thresholdType === AlertRuleThresholdType.ABOVE;
  63. const thresholdTypeText = aboveThreshold
  64. ? rule.comparisonDelta
  65. ? t('higher')
  66. : t('above')
  67. : rule.comparisonDelta
  68. ? t('lower')
  69. : t('below');
  70. const timeWindow = <Duration seconds={rule.timeWindow * 60} />;
  71. const metricName = capitalize(
  72. AlertWizardAlertNames[getAlertTypeFromAggregateDataset(rule)]
  73. );
  74. const thresholdText = rule.comparisonDelta
  75. ? tct(
  76. '[metric] is [threshold]% [comparisonType] in [timeWindow] compared to the [comparisonDelta]',
  77. {
  78. metric: metricName,
  79. threshold,
  80. comparisonType: thresholdTypeText,
  81. timeWindow,
  82. comparisonDelta: (
  83. COMPARISON_DELTA_OPTIONS.find(({value}) => value === rule.comparisonDelta) ??
  84. COMPARISON_DELTA_OPTIONS[0]
  85. ).label,
  86. }
  87. )
  88. : tct('[metric] is [condition] in [timeWindow]', {
  89. metric: metricName,
  90. condition: `${thresholdTypeText} ${threshold}`,
  91. timeWindow,
  92. });
  93. return (
  94. <TriggerContainer>
  95. <TriggerTitle>
  96. <IconDiamond color={statusIconColor} size="xs" />
  97. <TriggerTitleText>{t('%s Conditions', status)}</TriggerTitleText>
  98. </TriggerTitle>
  99. <TriggerStep>
  100. <TriggerTitleText>{t('When')}</TriggerTitleText>
  101. <TriggerActions>
  102. <TriggerText>{thresholdText}</TriggerText>
  103. </TriggerActions>
  104. </TriggerStep>
  105. <TriggerStep>
  106. <TriggerTitleText>{t('Then')}</TriggerTitleText>
  107. <TriggerActions>
  108. {actions.map(action => (
  109. <TriggerText key={action.id}>{action.desc}</TriggerText>
  110. ))}
  111. <TriggerText>{defaultAction}</TriggerText>
  112. </TriggerActions>
  113. </TriggerStep>
  114. </TriggerContainer>
  115. );
  116. }
  117. export function MetricDetailsSidebar({
  118. rule,
  119. isOnDemandMetricAlert,
  120. }: MetricDetailsSidebarProps) {
  121. // get current status
  122. const latestIncident = rule.latestIncident;
  123. const status = latestIncident ? latestIncident.status : IncidentStatus.CLOSED;
  124. // The date at which the alert was triggered or resolved
  125. const activityDate = latestIncident?.dateClosed ?? latestIncident?.dateStarted ?? null;
  126. const criticalTrigger = rule.triggers.find(
  127. ({label}) => label === AlertRuleTriggerType.CRITICAL
  128. );
  129. const warningTrigger = rule.triggers.find(
  130. ({label}) => label === AlertRuleTriggerType.WARNING
  131. );
  132. const ownerId = rule.owner?.split(':')[1];
  133. const teamActor = ownerId && {type: 'team' as Actor['type'], id: ownerId, name: ''};
  134. return (
  135. <Fragment>
  136. <StatusContainer>
  137. <HeaderItem>
  138. <Heading noMargin>{t('Alert Status')}</Heading>
  139. <Status>
  140. <AlertBadge status={status} withText />
  141. </Status>
  142. </HeaderItem>
  143. <HeaderItem>
  144. <Heading noMargin>{t('Last Triggered')}</Heading>
  145. <Status>
  146. {activityDate ? <TimeSince date={activityDate} /> : t('No alerts triggered')}
  147. </Status>
  148. </HeaderItem>
  149. </StatusContainer>
  150. <SidebarGroup>
  151. {typeof criticalTrigger?.alertThreshold === 'number' && (
  152. <TriggerDescription
  153. rule={rule}
  154. label={criticalTrigger.label}
  155. threshold={criticalTrigger.alertThreshold}
  156. actions={criticalTrigger.actions}
  157. />
  158. )}
  159. {typeof warningTrigger?.alertThreshold === 'number' && (
  160. <TriggerDescription
  161. rule={rule}
  162. label={warningTrigger.label}
  163. actions={warningTrigger.actions}
  164. threshold={warningTrigger.alertThreshold}
  165. />
  166. )}
  167. {typeof rule.resolveThreshold === 'number' && (
  168. <TriggerDescription
  169. rule={rule}
  170. label={AlertRuleTriggerType.RESOLVE}
  171. threshold={rule.resolveThreshold}
  172. actions={[]}
  173. />
  174. )}
  175. </SidebarGroup>
  176. {isOnDemandMetricAlert && (
  177. <SidebarGroup>
  178. <Heading>{t('Filters Used')}</Heading>
  179. <KeyValueTable>
  180. {getSearchFilters(rule.query).map(({key, operator, value}) => (
  181. <FilterKeyValueTableRow
  182. key={key}
  183. keyName={key}
  184. operator={operator}
  185. value={value}
  186. />
  187. ))}
  188. </KeyValueTable>
  189. </SidebarGroup>
  190. )}
  191. <SidebarGroup>
  192. <Heading>{t('Alert Rule Details')}</Heading>
  193. <KeyValueTable>
  194. <KeyValueTableRow
  195. keyName={t('Environment')}
  196. value={<OverflowTableValue>{rule.environment ?? '-'}</OverflowTableValue>}
  197. />
  198. <KeyValueTableRow
  199. keyName={t('Date created')}
  200. value={
  201. <DateTime
  202. date={getDynamicText({
  203. value: rule.dateCreated,
  204. fixed: new Date('2021-04-20'),
  205. })}
  206. format="ll"
  207. />
  208. }
  209. />
  210. {rule.createdBy && (
  211. <KeyValueTableRow
  212. keyName={t('Created By')}
  213. value={
  214. <OverflowTableValue>{rule.createdBy.name ?? '-'}</OverflowTableValue>
  215. }
  216. />
  217. )}
  218. {rule.dateModified && (
  219. <KeyValueTableRow
  220. keyName={t('Last Modified')}
  221. value={<TimeSince date={rule.dateModified} suffix={t('ago')} />}
  222. />
  223. )}
  224. <KeyValueTableRow
  225. keyName={t('Team')}
  226. value={
  227. teamActor ? <ActorAvatar actor={teamActor} size={24} /> : t('Unassigned')
  228. }
  229. />
  230. </KeyValueTable>
  231. </SidebarGroup>
  232. </Fragment>
  233. );
  234. }
  235. function FilterKeyValueTableRow({
  236. keyName,
  237. operator,
  238. value,
  239. }: {
  240. keyName: string;
  241. operator: string;
  242. value: string;
  243. }) {
  244. return (
  245. <KeyValueTableRow
  246. keyName={
  247. <KeyWrapper>
  248. {isOnDemandSearchKey(keyName) && (
  249. <OnDemandWarningIcon
  250. msg={t(
  251. 'We don’t routinely collect metrics from this property. As such, historical data may be limited.'
  252. )}
  253. />
  254. )}
  255. {keyName}
  256. </KeyWrapper>
  257. }
  258. value={
  259. <OverflowTableValue>
  260. {operator} {value}
  261. </OverflowTableValue>
  262. }
  263. />
  264. );
  265. }
  266. const KeyWrapper = styled('div')`
  267. display: flex;
  268. gap: ${space(0.75)};
  269. > span {
  270. margin-top: ${space(0.25)};
  271. height: ${space(2)};
  272. }
  273. `;
  274. const SidebarGroup = styled('div')`
  275. margin-bottom: ${space(3)};
  276. `;
  277. const HeaderItem = styled('div')`
  278. flex: 1;
  279. display: flex;
  280. flex-direction: column;
  281. > *:nth-child(2) {
  282. flex: 1;
  283. display: flex;
  284. align-items: center;
  285. }
  286. `;
  287. const Status = styled('div')`
  288. position: relative;
  289. display: grid;
  290. grid-template-columns: auto auto auto;
  291. gap: ${space(0.5)};
  292. font-size: ${p => p.theme.fontSizeLarge};
  293. `;
  294. const StatusContainer = styled('div')`
  295. height: 60px;
  296. display: flex;
  297. margin-bottom: ${space(1)};
  298. `;
  299. const Heading = styled(SectionHeading)<{noMargin?: boolean}>`
  300. margin-top: ${p => (p.noMargin ? 0 : space(2))};
  301. margin-bottom: ${p => (p.noMargin ? 0 : space(1))};
  302. `;
  303. const OverflowTableValue = styled('div')`
  304. ${p => p.theme.overflowEllipsis}
  305. `;
  306. const TriggerContainer = styled('div')`
  307. display: grid;
  308. grid-template-rows: auto auto auto;
  309. gap: ${space(1)};
  310. margin-top: ${space(4)};
  311. `;
  312. const TriggerTitle = styled('div')`
  313. display: grid;
  314. grid-template-columns: 20px 1fr;
  315. align-items: center;
  316. `;
  317. const TriggerTitleText = styled('h4')`
  318. color: ${p => p.theme.subText};
  319. font-size: ${p => p.theme.fontSizeMedium};
  320. margin: 0;
  321. line-height: 24px;
  322. min-width: 40px;
  323. `;
  324. const TriggerStep = styled('div')`
  325. display: grid;
  326. grid-template-columns: 40px 1fr;
  327. align-items: stretch;
  328. `;
  329. const TriggerActions = styled('div')`
  330. display: grid;
  331. grid-template-columns: repeat(1fr);
  332. gap: ${space(0.25)};
  333. align-items: center;
  334. `;
  335. const TriggerText = styled('span')`
  336. display: block;
  337. background-color: ${p => p.theme.surface200};
  338. padding: ${space(0.25)} ${space(0.75)};
  339. border-radius: ${p => p.theme.borderRadius};
  340. color: ${p => p.theme.textColor};
  341. font-size: ${p => p.theme.fontSizeSmall};
  342. width: 100%;
  343. font-weight: 400;
  344. `;