sidebar.tsx 10 KB

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