sidebar.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {OnDemandWarningIcon} from 'sentry/components/alerts/onDemandMetricAlert';
  4. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  5. import AlertBadge from 'sentry/components/badge/alertBadge';
  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 {ActivationConditionType, MonitorType} from 'sentry/types/alerts';
  15. import type {Actor} from 'sentry/types/core';
  16. import getDynamicText from 'sentry/utils/getDynamicText';
  17. import {getSearchFilters, isOnDemandSearchKey} from 'sentry/utils/onDemandMetrics/index';
  18. import {capitalize} from 'sentry/utils/string/capitalize';
  19. import {COMPARISON_DELTA_OPTIONS} from 'sentry/views/alerts/rules/metric/constants';
  20. import type {Action, MetricRule} from 'sentry/views/alerts/rules/metric/types';
  21. import {
  22. AlertRuleComparisonType,
  23. AlertRuleThresholdType,
  24. AlertRuleTriggerType,
  25. } from 'sentry/views/alerts/rules/metric/types';
  26. import {IncidentStatus} from 'sentry/views/alerts/types';
  27. import {AlertWizardAlertNames} from 'sentry/views/alerts/wizard/options';
  28. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  29. interface MetricDetailsSidebarProps {
  30. rule: MetricRule;
  31. showOnDemandMetricAlertUI: boolean;
  32. }
  33. function TriggerDescription({
  34. rule,
  35. actions,
  36. label,
  37. threshold,
  38. }: {
  39. actions: Action[];
  40. label: string;
  41. rule: MetricRule;
  42. threshold: number;
  43. }) {
  44. const status =
  45. label === AlertRuleTriggerType.CRITICAL
  46. ? t('Critical')
  47. : label === AlertRuleTriggerType.WARNING
  48. ? t('Warning')
  49. : t('Resolved');
  50. const statusIconColor =
  51. label === AlertRuleTriggerType.CRITICAL
  52. ? 'errorText'
  53. : label === AlertRuleTriggerType.WARNING
  54. ? 'warningText'
  55. : 'successText';
  56. const defaultAction = t('Change alert status to %s', status);
  57. const aboveThreshold =
  58. label === AlertRuleTriggerType.RESOLVE
  59. ? rule.thresholdType === AlertRuleThresholdType.BELOW
  60. : rule.thresholdType === AlertRuleThresholdType.ABOVE;
  61. const thresholdTypeText = aboveThreshold
  62. ? rule.comparisonDelta
  63. ? t('higher')
  64. : t('above')
  65. : rule.comparisonDelta
  66. ? t('lower')
  67. : t('below');
  68. const timeWindow = <Duration seconds={rule.timeWindow * 60} />;
  69. const metricName = capitalize(
  70. AlertWizardAlertNames[getAlertTypeFromAggregateDataset(rule)]
  71. );
  72. const thresholdText = rule.comparisonDelta
  73. ? tct(
  74. '[metric] is [threshold]% [comparisonType] in [timeWindow] compared to the [comparisonDelta]',
  75. {
  76. metric: metricName,
  77. threshold,
  78. comparisonType: thresholdTypeText,
  79. timeWindow,
  80. comparisonDelta: (
  81. COMPARISON_DELTA_OPTIONS.find(({value}) => value === rule.comparisonDelta) ??
  82. COMPARISON_DELTA_OPTIONS[0]
  83. ).label,
  84. }
  85. )
  86. : rule.detectionType === AlertRuleComparisonType.DYNAMIC
  87. ? 'Dynamic threshold is reached'
  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. showOnDemandMetricAlertUI,
  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. let conditionType;
  135. const activationCondition =
  136. rule.monitorType === MonitorType.ACTIVATED &&
  137. typeof rule.activationCondition !== 'undefined' &&
  138. rule.activationCondition;
  139. switch (activationCondition) {
  140. case ActivationConditionType.DEPLOY_CREATION:
  141. conditionType = t('New Deploy');
  142. break;
  143. case ActivationConditionType.RELEASE_CREATION:
  144. conditionType = t('New Release');
  145. break;
  146. default:
  147. break;
  148. }
  149. return (
  150. <Fragment>
  151. <StatusContainer>
  152. <HeaderItem>
  153. <Heading noMargin>{t('Alert Status')}</Heading>
  154. <Status>
  155. <AlertBadge status={status} withText />
  156. </Status>
  157. </HeaderItem>
  158. <HeaderItem>
  159. <Heading noMargin>{t('Last Triggered')}</Heading>
  160. <Status>
  161. {activityDate ? <TimeSince date={activityDate} /> : t('No alerts triggered')}
  162. </Status>
  163. </HeaderItem>
  164. </StatusContainer>
  165. <SidebarGroup>
  166. {typeof criticalTrigger?.alertThreshold === 'number' && (
  167. <TriggerDescription
  168. rule={rule}
  169. label={criticalTrigger.label}
  170. threshold={criticalTrigger.alertThreshold}
  171. actions={criticalTrigger.actions}
  172. />
  173. )}
  174. {typeof warningTrigger?.alertThreshold === 'number' && (
  175. <TriggerDescription
  176. rule={rule}
  177. label={warningTrigger.label}
  178. actions={warningTrigger.actions}
  179. threshold={warningTrigger.alertThreshold}
  180. />
  181. )}
  182. {typeof rule.resolveThreshold === 'number' && (
  183. <TriggerDescription
  184. rule={rule}
  185. label={AlertRuleTriggerType.RESOLVE}
  186. threshold={rule.resolveThreshold}
  187. actions={[]}
  188. />
  189. )}
  190. </SidebarGroup>
  191. {showOnDemandMetricAlertUI && (
  192. <SidebarGroup>
  193. <Heading>{t('Filters Used')}</Heading>
  194. <KeyValueTable>
  195. {getSearchFilters(rule.query).map(({key, operator, value}) => (
  196. <FilterKeyValueTableRow
  197. key={key}
  198. keyName={key}
  199. operator={operator}
  200. value={value}
  201. />
  202. ))}
  203. </KeyValueTable>
  204. </SidebarGroup>
  205. )}
  206. <SidebarGroup>
  207. <Heading>{t('Alert Rule Details')}</Heading>
  208. <KeyValueTable>
  209. <KeyValueTableRow
  210. keyName={t('Environment')}
  211. value={<OverflowTableValue>{rule.environment ?? '-'}</OverflowTableValue>}
  212. />
  213. {rule.monitorType === MonitorType.ACTIVATED &&
  214. rule.activationCondition !== undefined && (
  215. <KeyValueTableRow
  216. keyName={t('Activated by')}
  217. value={<OverflowTableValue>{conditionType}</OverflowTableValue>}
  218. />
  219. )}
  220. <KeyValueTableRow
  221. keyName={t('Date created')}
  222. value={
  223. <DateTime
  224. date={getDynamicText({
  225. value: rule.dateCreated,
  226. fixed: new Date('2021-04-20'),
  227. })}
  228. format="ll"
  229. />
  230. }
  231. />
  232. {rule.createdBy && (
  233. <KeyValueTableRow
  234. keyName={t('Created by')}
  235. value={
  236. <OverflowTableValue>{rule.createdBy.name ?? '-'}</OverflowTableValue>
  237. }
  238. />
  239. )}
  240. {rule.dateModified && (
  241. <KeyValueTableRow
  242. keyName={t('Last modified')}
  243. value={<TimeSince date={rule.dateModified} suffix={t('ago')} />}
  244. />
  245. )}
  246. <KeyValueTableRow
  247. keyName={t('Team')}
  248. value={
  249. teamActor ? <ActorAvatar actor={teamActor} size={24} /> : t('Unassigned')
  250. }
  251. />
  252. {rule.detectionType === AlertRuleComparisonType.DYNAMIC && (
  253. <KeyValueTableRow
  254. keyName={t('Sensitivity')}
  255. value={
  256. rule.sensitivity
  257. ? rule.sensitivity.charAt(0).toUpperCase() + rule.sensitivity.slice(1)
  258. : ''
  259. } // NOTE: if the rule is dynamic, then there must be a sensitivity
  260. />
  261. )}
  262. {rule.detectionType === AlertRuleComparisonType.DYNAMIC && (
  263. <KeyValueTableRow
  264. keyName={t('Direction')}
  265. value={
  266. <OverflowTableValue>
  267. {rule.thresholdType === AlertRuleThresholdType.ABOVE
  268. ? 'Above threshold'
  269. : rule.thresholdType === AlertRuleThresholdType.ABOVE_AND_BELOW
  270. ? 'Above and below threshold'
  271. : 'Below threshold'}
  272. </OverflowTableValue>
  273. }
  274. />
  275. )}
  276. </KeyValueTable>
  277. </SidebarGroup>
  278. </Fragment>
  279. );
  280. }
  281. function FilterKeyValueTableRow({
  282. keyName,
  283. operator,
  284. value,
  285. }: {
  286. keyName: string;
  287. operator: string;
  288. value: string;
  289. }) {
  290. return (
  291. <KeyValueTableRow
  292. keyName={
  293. <KeyWrapper>
  294. {isOnDemandSearchKey(keyName) && (
  295. <span>
  296. <OnDemandWarningIcon
  297. msg={t(
  298. 'We don’t routinely collect metrics from this property. As such, historical data may be limited.'
  299. )}
  300. />
  301. </span>
  302. )}
  303. {keyName}
  304. </KeyWrapper>
  305. }
  306. value={
  307. <OverflowTableValue>
  308. {operator} {value}
  309. </OverflowTableValue>
  310. }
  311. />
  312. );
  313. }
  314. const KeyWrapper = styled('div')`
  315. display: flex;
  316. gap: ${space(0.75)};
  317. > span {
  318. margin-top: ${space(0.25)};
  319. height: ${space(2)};
  320. }
  321. `;
  322. const SidebarGroup = styled('div')`
  323. margin-bottom: ${space(3)};
  324. `;
  325. const HeaderItem = styled('div')`
  326. flex: 1;
  327. display: flex;
  328. flex-direction: column;
  329. > *:nth-child(2) {
  330. flex: 1;
  331. display: flex;
  332. align-items: center;
  333. }
  334. `;
  335. const Status = styled('div')`
  336. position: relative;
  337. display: grid;
  338. grid-template-columns: auto auto auto;
  339. gap: ${space(0.5)};
  340. font-size: ${p => p.theme.fontSizeLarge};
  341. `;
  342. const StatusContainer = styled('div')`
  343. height: 60px;
  344. display: flex;
  345. margin-bottom: ${space(1)};
  346. `;
  347. const Heading = styled(SectionHeading)<{noMargin?: boolean}>`
  348. margin-top: ${p => (p.noMargin ? 0 : space(2))};
  349. margin-bottom: ${p => (p.noMargin ? 0 : space(1))};
  350. `;
  351. const OverflowTableValue = styled('div')`
  352. ${p => p.theme.overflowEllipsis}
  353. `;
  354. const TriggerContainer = styled('div')`
  355. display: grid;
  356. grid-template-rows: auto auto auto;
  357. gap: ${space(1)};
  358. margin-top: ${space(4)};
  359. `;
  360. const TriggerTitle = styled('div')`
  361. display: grid;
  362. grid-template-columns: 20px 1fr;
  363. align-items: center;
  364. `;
  365. const TriggerTitleText = styled('h4')`
  366. color: ${p => p.theme.subText};
  367. font-size: ${p => p.theme.fontSizeMedium};
  368. margin: 0;
  369. line-height: 24px;
  370. min-width: 40px;
  371. `;
  372. const TriggerStep = styled('div')`
  373. display: grid;
  374. grid-template-columns: 40px 1fr;
  375. align-items: stretch;
  376. `;
  377. const TriggerActions = styled('div')`
  378. display: grid;
  379. grid-template-columns: repeat(1fr);
  380. gap: ${space(0.25)};
  381. align-items: center;
  382. `;
  383. const TriggerText = styled('span')`
  384. display: block;
  385. background-color: ${p => p.theme.surface200};
  386. padding: ${space(0.25)} ${space(0.75)};
  387. border-radius: ${p => p.theme.borderRadius};
  388. color: ${p => p.theme.textColor};
  389. font-size: ${p => p.theme.fontSizeSmall};
  390. width: 100%;
  391. font-weight: ${p => p.theme.fontWeightNormal};
  392. `;