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 {OnDemandWarningIcon} from 'sentry/components/alerts/onDemandMetricAlert';
  6. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  7. import {SectionHeading} from 'sentry/components/charts/styles';
  8. import DateTime from 'sentry/components/dateTime';
  9. import Duration from 'sentry/components/duration';
  10. import {KeyValueTable, KeyValueTableRow} from 'sentry/components/keyValueTable';
  11. import TimeSince from 'sentry/components/timeSince';
  12. import {IconDiamond} from 'sentry/icons';
  13. import {t, tct} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {Actor} from 'sentry/types';
  16. import getDynamicText from 'sentry/utils/getDynamicText';
  17. import {getSearchFilters, isOnDemandSearchKey} from 'sentry/utils/onDemandMetrics/index';
  18. import {COMPARISON_DELTA_OPTIONS} from 'sentry/views/alerts/rules/metric/constants';
  19. import {
  20. Action,
  21. AlertRuleThresholdType,
  22. AlertRuleTriggerType,
  23. MetricRule,
  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. return (
  132. <Fragment>
  133. <StatusContainer>
  134. <HeaderItem>
  135. <Heading noMargin>{t('Alert Status')}</Heading>
  136. <Status>
  137. <AlertBadge status={status} withText />
  138. </Status>
  139. </HeaderItem>
  140. <HeaderItem>
  141. <Heading noMargin>{t('Last Triggered')}</Heading>
  142. <Status>
  143. {activityDate ? <TimeSince date={activityDate} /> : t('No alerts triggered')}
  144. </Status>
  145. </HeaderItem>
  146. </StatusContainer>
  147. <SidebarGroup>
  148. {typeof criticalTrigger?.alertThreshold === 'number' && (
  149. <TriggerDescription
  150. rule={rule}
  151. label={criticalTrigger.label}
  152. threshold={criticalTrigger.alertThreshold}
  153. actions={criticalTrigger.actions}
  154. />
  155. )}
  156. {typeof warningTrigger?.alertThreshold === 'number' && (
  157. <TriggerDescription
  158. rule={rule}
  159. label={warningTrigger.label}
  160. actions={warningTrigger.actions}
  161. threshold={warningTrigger.alertThreshold}
  162. />
  163. )}
  164. {typeof rule.resolveThreshold === 'number' && (
  165. <TriggerDescription
  166. rule={rule}
  167. label={AlertRuleTriggerType.RESOLVE}
  168. threshold={rule.resolveThreshold}
  169. actions={[]}
  170. />
  171. )}
  172. </SidebarGroup>
  173. {showOnDemandMetricAlertUI && (
  174. <SidebarGroup>
  175. <Heading>{t('Filters Used')}</Heading>
  176. <KeyValueTable>
  177. {getSearchFilters(rule.query).map(({key, operator, value}) => (
  178. <FilterKeyValueTableRow
  179. key={key}
  180. keyName={key}
  181. operator={operator}
  182. value={value}
  183. />
  184. ))}
  185. </KeyValueTable>
  186. </SidebarGroup>
  187. )}
  188. <SidebarGroup>
  189. <Heading>{t('Alert Rule Details')}</Heading>
  190. <KeyValueTable>
  191. <KeyValueTableRow
  192. keyName={t('Environment')}
  193. value={<OverflowTableValue>{rule.environment ?? '-'}</OverflowTableValue>}
  194. />
  195. <KeyValueTableRow
  196. keyName={t('Date created')}
  197. value={
  198. <DateTime
  199. date={getDynamicText({
  200. value: rule.dateCreated,
  201. fixed: new Date('2021-04-20'),
  202. })}
  203. format="ll"
  204. />
  205. }
  206. />
  207. {rule.createdBy && (
  208. <KeyValueTableRow
  209. keyName={t('Created By')}
  210. value={
  211. <OverflowTableValue>{rule.createdBy.name ?? '-'}</OverflowTableValue>
  212. }
  213. />
  214. )}
  215. {rule.dateModified && (
  216. <KeyValueTableRow
  217. keyName={t('Last Modified')}
  218. value={<TimeSince date={rule.dateModified} suffix={t('ago')} />}
  219. />
  220. )}
  221. <KeyValueTableRow
  222. keyName={t('Team')}
  223. value={
  224. teamActor ? <ActorAvatar actor={teamActor} size={24} /> : t('Unassigned')
  225. }
  226. />
  227. </KeyValueTable>
  228. </SidebarGroup>
  229. </Fragment>
  230. );
  231. }
  232. function FilterKeyValueTableRow({
  233. keyName,
  234. operator,
  235. value,
  236. }: {
  237. keyName: string;
  238. operator: string;
  239. value: string;
  240. }) {
  241. return (
  242. <KeyValueTableRow
  243. keyName={
  244. <KeyWrapper>
  245. {isOnDemandSearchKey(keyName) && (
  246. <span>
  247. <OnDemandWarningIcon
  248. msg={t(
  249. 'We don’t routinely collect metrics from this property. As such, historical data may be limited.'
  250. )}
  251. />
  252. </span>
  253. )}
  254. {keyName}
  255. </KeyWrapper>
  256. }
  257. value={
  258. <OverflowTableValue>
  259. {operator} {value}
  260. </OverflowTableValue>
  261. }
  262. />
  263. );
  264. }
  265. const KeyWrapper = styled('div')`
  266. display: flex;
  267. gap: ${space(0.75)};
  268. > span {
  269. margin-top: ${space(0.25)};
  270. height: ${space(2)};
  271. }
  272. `;
  273. const SidebarGroup = styled('div')`
  274. margin-bottom: ${space(3)};
  275. `;
  276. const HeaderItem = styled('div')`
  277. flex: 1;
  278. display: flex;
  279. flex-direction: column;
  280. > *:nth-child(2) {
  281. flex: 1;
  282. display: flex;
  283. align-items: center;
  284. }
  285. `;
  286. const Status = styled('div')`
  287. position: relative;
  288. display: grid;
  289. grid-template-columns: auto auto auto;
  290. gap: ${space(0.5)};
  291. font-size: ${p => p.theme.fontSizeLarge};
  292. `;
  293. const StatusContainer = styled('div')`
  294. height: 60px;
  295. display: flex;
  296. margin-bottom: ${space(1)};
  297. `;
  298. const Heading = styled(SectionHeading)<{noMargin?: boolean}>`
  299. margin-top: ${p => (p.noMargin ? 0 : space(2))};
  300. margin-bottom: ${p => (p.noMargin ? 0 : space(1))};
  301. `;
  302. const OverflowTableValue = styled('div')`
  303. ${p => p.theme.overflowEllipsis}
  304. `;
  305. const TriggerContainer = styled('div')`
  306. display: grid;
  307. grid-template-rows: auto auto auto;
  308. gap: ${space(1)};
  309. margin-top: ${space(4)};
  310. `;
  311. const TriggerTitle = styled('div')`
  312. display: grid;
  313. grid-template-columns: 20px 1fr;
  314. align-items: center;
  315. `;
  316. const TriggerTitleText = styled('h4')`
  317. color: ${p => p.theme.subText};
  318. font-size: ${p => p.theme.fontSizeMedium};
  319. margin: 0;
  320. line-height: 24px;
  321. min-width: 40px;
  322. `;
  323. const TriggerStep = styled('div')`
  324. display: grid;
  325. grid-template-columns: 40px 1fr;
  326. align-items: stretch;
  327. `;
  328. const TriggerActions = styled('div')`
  329. display: grid;
  330. grid-template-columns: repeat(1fr);
  331. gap: ${space(0.25)};
  332. align-items: center;
  333. `;
  334. const TriggerText = styled('span')`
  335. display: block;
  336. background-color: ${p => p.theme.surface200};
  337. padding: ${space(0.25)} ${space(0.75)};
  338. border-radius: ${p => p.theme.borderRadius};
  339. color: ${p => p.theme.textColor};
  340. font-size: ${p => p.theme.fontSizeSmall};
  341. width: 100%;
  342. font-weight: 400;
  343. `;