body.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. import * as React from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import moment from 'moment';
  6. import {Client} from 'app/api';
  7. import Alert from 'app/components/alert';
  8. import ActorAvatar from 'app/components/avatar/actorAvatar';
  9. import {SectionHeading} from 'app/components/charts/styles';
  10. import {getInterval} from 'app/components/charts/utils';
  11. import DropdownControl, {DropdownItem} from 'app/components/dropdownControl';
  12. import Duration from 'app/components/duration';
  13. import IdBadge from 'app/components/idBadge';
  14. import {KeyValueTable, KeyValueTableRow} from 'app/components/keyValueTable';
  15. import * as Layout from 'app/components/layouts/thirds';
  16. import {Panel, PanelBody} from 'app/components/panels';
  17. import Placeholder from 'app/components/placeholder';
  18. import {parseSearch} from 'app/components/searchSyntax/parser';
  19. import HighlightQuery from 'app/components/searchSyntax/renderer';
  20. import TimeSince from 'app/components/timeSince';
  21. import Tooltip from 'app/components/tooltip';
  22. import {IconCheckmark, IconFire, IconInfo, IconWarning} from 'app/icons';
  23. import {t, tct} from 'app/locale';
  24. import overflowEllipsis from 'app/styles/overflowEllipsis';
  25. import space from 'app/styles/space';
  26. import {Actor, DateString, Organization, Project} from 'app/types';
  27. import Projects from 'app/utils/projects';
  28. import {
  29. AlertRuleThresholdType,
  30. Dataset,
  31. IncidentRule,
  32. Trigger,
  33. } from 'app/views/alerts/incidentRules/types';
  34. import {extractEventTypeFilterFromRule} from 'app/views/alerts/incidentRules/utils/getEventTypeFilter';
  35. import Timeline from 'app/views/alerts/rules/details/timeline';
  36. import AlertBadge from '../../alertBadge';
  37. import {AlertRuleStatus, Incident, IncidentStatus} from '../../types';
  38. import {API_INTERVAL_POINTS_LIMIT, TIME_OPTIONS, TimePeriodType} from './constants';
  39. import MetricChart from './metricChart';
  40. import RelatedIssues from './relatedIssues';
  41. import RelatedTransactions from './relatedTransactions';
  42. type Props = {
  43. api: Client;
  44. rule?: IncidentRule;
  45. incidents?: Incident[];
  46. timePeriod: TimePeriodType;
  47. selectedIncident?: Incident | null;
  48. organization: Organization;
  49. location: Location;
  50. handleTimePeriodChange: (value: string) => void;
  51. handleZoom: (start: DateString, end: DateString) => void;
  52. } & RouteComponentProps<{orgId: string}, {}>;
  53. export default class DetailsBody extends React.Component<Props> {
  54. getMetricText(): React.ReactNode {
  55. const {rule} = this.props;
  56. if (!rule) {
  57. return '';
  58. }
  59. const {aggregate} = rule;
  60. return tct('[metric]', {
  61. metric: aggregate,
  62. });
  63. }
  64. getTimeWindow(): React.ReactNode {
  65. const {rule} = this.props;
  66. if (!rule) {
  67. return '';
  68. }
  69. const {timeWindow} = rule;
  70. return tct('[window]', {
  71. window: <Duration seconds={timeWindow * 60} />,
  72. });
  73. }
  74. getInterval() {
  75. const {
  76. timePeriod: {start, end},
  77. rule,
  78. } = this.props;
  79. const startDate = moment.utc(start);
  80. const endDate = moment.utc(end);
  81. const timeWindow = rule?.timeWindow;
  82. if (
  83. timeWindow &&
  84. endDate.diff(startDate) < API_INTERVAL_POINTS_LIMIT * timeWindow * 60 * 1000
  85. ) {
  86. return `${timeWindow}m`;
  87. }
  88. return getInterval({start, end}, true);
  89. }
  90. getFilter() {
  91. const {rule} = this.props;
  92. if (!rule) {
  93. return null;
  94. }
  95. const eventType = extractEventTypeFilterFromRule(rule);
  96. const parsedQuery = parseSearch([eventType, rule.query].join(' '));
  97. return (
  98. <Filters>{parsedQuery && <HighlightQuery parsedQuery={parsedQuery} />}</Filters>
  99. );
  100. }
  101. renderTrigger(trigger: Trigger): React.ReactNode {
  102. const {rule} = this.props;
  103. if (!rule) {
  104. return null;
  105. }
  106. const status =
  107. trigger.label === 'critical' ? (
  108. <StatusWrapper>
  109. <IconFire color="red300" size="sm" /> Critical
  110. </StatusWrapper>
  111. ) : trigger.label === 'warning' ? (
  112. <StatusWrapper>
  113. <IconWarning color="yellow300" size="sm" /> Warning
  114. </StatusWrapper>
  115. ) : (
  116. <StatusWrapper>
  117. <IconCheckmark color="green300" size="sm" isCircled /> Resolved
  118. </StatusWrapper>
  119. );
  120. const thresholdTypeText =
  121. rule.thresholdType === AlertRuleThresholdType.ABOVE ? t('above') : t('below');
  122. return (
  123. <TriggerCondition>
  124. {status}
  125. <TriggerText>{`${thresholdTypeText} ${trigger.alertThreshold}`}</TriggerText>
  126. </TriggerCondition>
  127. );
  128. }
  129. renderRuleDetails() {
  130. const {rule} = this.props;
  131. if (rule === undefined) {
  132. return <Placeholder height="200px" />;
  133. }
  134. const criticalTrigger = rule?.triggers.find(({label}) => label === 'critical');
  135. const warningTrigger = rule?.triggers.find(({label}) => label === 'warning');
  136. const ownerId = rule.owner?.split(':')[1];
  137. const teamActor = ownerId && {type: 'team' as Actor['type'], id: ownerId, name: ''};
  138. return (
  139. <React.Fragment>
  140. <SidebarGroup>
  141. <Heading>{t('Metric')}</Heading>
  142. <RuleText>{this.getMetricText()}</RuleText>
  143. </SidebarGroup>
  144. <SidebarGroup>
  145. <Heading>{t('Environment')}</Heading>
  146. <RuleText>{rule.environment ?? 'All'}</RuleText>
  147. </SidebarGroup>
  148. <SidebarGroup>
  149. <Heading>{t('Filters')}</Heading>
  150. {this.getFilter()}
  151. </SidebarGroup>
  152. <SidebarGroup>
  153. <Heading>{t('Conditions')}</Heading>
  154. {criticalTrigger && this.renderTrigger(criticalTrigger)}
  155. {warningTrigger && this.renderTrigger(warningTrigger)}
  156. </SidebarGroup>
  157. <SidebarGroup>
  158. <Heading>{t('Other Details')}</Heading>
  159. <KeyValueTable>
  160. <KeyValueTableRow
  161. keyName={t('Team')}
  162. value={
  163. teamActor ? <ActorAvatar actor={teamActor} size={24} /> : 'Unassigned'
  164. }
  165. />
  166. {rule.createdBy && (
  167. <KeyValueTableRow
  168. keyName={t('Created By')}
  169. value={<CreatedBy>{rule.createdBy.name ?? '-'}</CreatedBy>}
  170. />
  171. )}
  172. {rule.dateModified && (
  173. <KeyValueTableRow
  174. keyName={t('Last Modified')}
  175. value={<TimeSince date={rule.dateModified} suffix={t('ago')} />}
  176. />
  177. )}
  178. </KeyValueTable>
  179. </SidebarGroup>
  180. </React.Fragment>
  181. );
  182. }
  183. renderMetricStatus() {
  184. const {incidents} = this.props;
  185. // get current status
  186. const activeIncident = incidents?.find(({dateClosed}) => !dateClosed);
  187. const status = activeIncident ? activeIncident.status : IncidentStatus.CLOSED;
  188. const latestIncident = incidents?.length ? incidents[0] : null;
  189. // The date at which the alert was triggered or resolved
  190. const activityDate = activeIncident
  191. ? activeIncident.dateStarted
  192. : latestIncident
  193. ? latestIncident.dateClosed
  194. : null;
  195. return (
  196. <StatusContainer>
  197. <HeaderItem>
  198. <Heading noMargin>{t('Current Status')}</Heading>
  199. <Status>
  200. <AlertBadge status={status} hideText />
  201. {activeIncident ? t('Triggered') : t('Resolved')}
  202. {activityDate ? <TimeSince date={activityDate} /> : '-'}
  203. </Status>
  204. </HeaderItem>
  205. </StatusContainer>
  206. );
  207. }
  208. renderLoading() {
  209. return (
  210. <Layout.Body>
  211. <Layout.Main>
  212. <Placeholder height="38px" />
  213. <ChartPanel>
  214. <PanelBody withPadding>
  215. <Placeholder height="200px" />
  216. </PanelBody>
  217. </ChartPanel>
  218. </Layout.Main>
  219. <Layout.Side>
  220. <Placeholder height="200px" />
  221. </Layout.Side>
  222. </Layout.Body>
  223. );
  224. }
  225. render() {
  226. const {
  227. api,
  228. rule,
  229. incidents,
  230. location,
  231. organization,
  232. timePeriod,
  233. selectedIncident,
  234. handleZoom,
  235. params: {orgId},
  236. } = this.props;
  237. if (!rule) {
  238. return this.renderLoading();
  239. }
  240. const {query, projects: projectSlugs} = rule;
  241. const queryWithTypeFilter = `${query} ${extractEventTypeFilterFromRule(rule)}`.trim();
  242. return (
  243. <Projects orgId={orgId} slugs={projectSlugs}>
  244. {({initiallyLoaded, projects}) => {
  245. return initiallyLoaded ? (
  246. <React.Fragment>
  247. {selectedIncident &&
  248. selectedIncident.alertRule.status === AlertRuleStatus.SNAPSHOT && (
  249. <StyledLayoutBody>
  250. <StyledAlert type="warning" icon={<IconInfo size="md" />}>
  251. {t(
  252. 'Alert Rule settings have been updated since this alert was triggered.'
  253. )}
  254. </StyledAlert>
  255. </StyledLayoutBody>
  256. )}
  257. <StyledLayoutBodyWrapper>
  258. <Layout.Main>
  259. <HeaderContainer>
  260. <HeaderGrid>
  261. <HeaderItem>
  262. <Heading noMargin>{t('Display')}</Heading>
  263. <ChartControls>
  264. <DropdownControl label={timePeriod.display}>
  265. {TIME_OPTIONS.map(({label, value}) => (
  266. <DropdownItem
  267. key={value}
  268. eventKey={value}
  269. onSelect={this.props.handleTimePeriodChange}
  270. >
  271. {label}
  272. </DropdownItem>
  273. ))}
  274. </DropdownControl>
  275. </ChartControls>
  276. </HeaderItem>
  277. {projects && projects.length && (
  278. <HeaderItem>
  279. <Heading noMargin>{t('Project')}</Heading>
  280. <IdBadge avatarSize={16} project={projects[0]} />
  281. </HeaderItem>
  282. )}
  283. <HeaderItem>
  284. <Heading noMargin>
  285. {t('Time Interval')}
  286. <Tooltip
  287. title={t(
  288. 'The time window over which the metric is evaluated.'
  289. )}
  290. >
  291. <IconInfo size="xs" color="gray200" />
  292. </Tooltip>
  293. </Heading>
  294. <RuleText>{this.getTimeWindow()}</RuleText>
  295. </HeaderItem>
  296. </HeaderGrid>
  297. </HeaderContainer>
  298. <MetricChart
  299. api={api}
  300. rule={rule}
  301. incidents={incidents}
  302. timePeriod={timePeriod}
  303. selectedIncident={selectedIncident}
  304. organization={organization}
  305. projects={projects}
  306. interval={this.getInterval()}
  307. filter={this.getFilter()}
  308. query={queryWithTypeFilter}
  309. orgId={orgId}
  310. handleZoom={handleZoom}
  311. />
  312. <DetailWrapper>
  313. <ActivityWrapper>
  314. {rule?.dataset === Dataset.ERRORS && (
  315. <RelatedIssues
  316. organization={organization}
  317. rule={rule}
  318. projects={((projects as Project[]) || []).filter(project =>
  319. rule.projects.includes(project.slug)
  320. )}
  321. timePeriod={timePeriod}
  322. />
  323. )}
  324. {rule?.dataset === Dataset.TRANSACTIONS && (
  325. <RelatedTransactions
  326. organization={organization}
  327. location={location}
  328. rule={rule}
  329. projects={((projects as Project[]) || []).filter(project =>
  330. rule.projects.includes(project.slug)
  331. )}
  332. start={timePeriod.start}
  333. end={timePeriod.end}
  334. filter={extractEventTypeFilterFromRule(rule)}
  335. />
  336. )}
  337. </ActivityWrapper>
  338. </DetailWrapper>
  339. </Layout.Main>
  340. <Layout.Side>
  341. {this.renderMetricStatus()}
  342. <Timeline
  343. api={api}
  344. organization={organization}
  345. rule={rule}
  346. incidents={incidents}
  347. />
  348. {this.renderRuleDetails()}
  349. </Layout.Side>
  350. </StyledLayoutBodyWrapper>
  351. </React.Fragment>
  352. ) : (
  353. <Placeholder height="200px" />
  354. );
  355. }}
  356. </Projects>
  357. );
  358. }
  359. }
  360. const SidebarGroup = styled('div')`
  361. margin-bottom: ${space(3)};
  362. `;
  363. const DetailWrapper = styled('div')`
  364. display: flex;
  365. flex: 1;
  366. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  367. flex-direction: column-reverse;
  368. }
  369. `;
  370. const StatusWrapper = styled('div')`
  371. display: flex;
  372. align-items: center;
  373. svg {
  374. margin-right: ${space(0.5)};
  375. }
  376. `;
  377. const HeaderContainer = styled('div')`
  378. height: 60px;
  379. display: flex;
  380. flex-direction: row;
  381. align-content: flex-start;
  382. `;
  383. const HeaderGrid = styled('div')`
  384. display: grid;
  385. grid-template-columns: auto auto auto;
  386. align-items: stretch;
  387. grid-gap: 60px;
  388. `;
  389. const HeaderItem = styled('div')`
  390. flex: 1;
  391. display: flex;
  392. flex-direction: column;
  393. > *:nth-child(2) {
  394. flex: 1;
  395. display: flex;
  396. align-items: center;
  397. }
  398. `;
  399. const StyledLayoutBody = styled(Layout.Body)`
  400. flex-grow: 0;
  401. padding-bottom: 0 !important;
  402. @media (min-width: ${p => p.theme.breakpoints[1]}) {
  403. grid-template-columns: auto;
  404. }
  405. `;
  406. const StyledLayoutBodyWrapper = styled(Layout.Body)`
  407. margin-bottom: -${space(3)};
  408. `;
  409. const StyledAlert = styled(Alert)`
  410. margin: 0;
  411. `;
  412. const ActivityWrapper = styled('div')`
  413. display: flex;
  414. flex: 1;
  415. flex-direction: column;
  416. width: 100%;
  417. `;
  418. const Status = styled('div')`
  419. position: relative;
  420. display: grid;
  421. grid-template-columns: auto auto auto;
  422. grid-gap: ${space(0.5)};
  423. font-size: ${p => p.theme.fontSizeLarge};
  424. `;
  425. const StatusContainer = styled('div')`
  426. height: 60px;
  427. display: flex;
  428. margin-bottom: ${space(1.5)};
  429. `;
  430. const Heading = styled(SectionHeading)<{noMargin?: boolean}>`
  431. display: grid;
  432. grid-template-columns: auto auto;
  433. justify-content: flex-start;
  434. margin-top: ${p => (p.noMargin ? 0 : space(2))};
  435. margin-bottom: ${space(0.5)};
  436. line-height: 1;
  437. gap: ${space(1)};
  438. `;
  439. const ChartControls = styled('div')`
  440. display: flex;
  441. flex-direction: row;
  442. align-items: center;
  443. `;
  444. const ChartPanel = styled(Panel)`
  445. margin-top: ${space(2)};
  446. `;
  447. const RuleText = styled('div')`
  448. font-size: ${p => p.theme.fontSizeLarge};
  449. `;
  450. const Filters = styled('span')`
  451. overflow-wrap: break-word;
  452. word-break: break-word;
  453. white-space: pre-wrap;
  454. font-size: ${p => p.theme.fontSizeSmall};
  455. line-height: 25px;
  456. font-family: ${p => p.theme.text.familyMono};
  457. `;
  458. const TriggerCondition = styled('div')`
  459. display: flex;
  460. align-items: center;
  461. `;
  462. const TriggerText = styled('div')`
  463. margin-left: ${space(0.5)};
  464. white-space: nowrap;
  465. `;
  466. const CreatedBy = styled('div')`
  467. ${overflowEllipsis}
  468. `;