sidebar.tsx 11 KB

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