body.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import moment from 'moment';
  6. import type {Client} from 'sentry/api';
  7. import {Alert} from 'sentry/components/alert';
  8. import {getInterval} from 'sentry/components/charts/utils';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import type {ChangeData} from 'sentry/components/organizations/timeRangeSelector';
  11. import PageTimeRangeSelector from 'sentry/components/pageTimeRangeSelector';
  12. import Panel from 'sentry/components/panels/panel';
  13. import PanelBody from 'sentry/components/panels/panelBody';
  14. import Placeholder from 'sentry/components/placeholder';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {Organization, Project} from 'sentry/types';
  18. import {RuleActionsCategories} from 'sentry/types/alerts';
  19. import MetricHistory from 'sentry/views/alerts/rules/metric/details/metricHistory';
  20. import {Dataset, MetricRule, TimePeriod} from 'sentry/views/alerts/rules/metric/types';
  21. import {extractEventTypeFilterFromRule} from 'sentry/views/alerts/rules/metric/utils/getEventTypeFilter';
  22. import {isOnDemandMetricAlert} from 'sentry/views/alerts/rules/metric/utils/onDemandMetricAlert';
  23. import {getAlertRuleActionCategory} from 'sentry/views/alerts/rules/utils';
  24. import {AlertRuleStatus, Incident} from 'sentry/views/alerts/types';
  25. import {isCrashFreeAlert} from '../utils/isCrashFreeAlert';
  26. import {
  27. API_INTERVAL_POINTS_LIMIT,
  28. SELECTOR_RELATIVE_PERIODS,
  29. TIME_WINDOWS,
  30. TimePeriodType,
  31. } from './constants';
  32. import MetricChart from './metricChart';
  33. import RelatedIssues from './relatedIssues';
  34. import RelatedTransactions from './relatedTransactions';
  35. import {MetricDetailsSidebar} from './sidebar';
  36. interface MetricDetailsBodyProps extends RouteComponentProps<{}, {}> {
  37. api: Client;
  38. location: Location;
  39. organization: Organization;
  40. timePeriod: TimePeriodType;
  41. incidents?: Incident[];
  42. project?: Project;
  43. rule?: MetricRule;
  44. selectedIncident?: Incident | null;
  45. }
  46. export default function MetricDetailsBody({
  47. api,
  48. project,
  49. rule,
  50. incidents,
  51. organization,
  52. timePeriod,
  53. selectedIncident,
  54. location,
  55. router,
  56. }: MetricDetailsBodyProps) {
  57. function getPeriodInterval() {
  58. const startDate = moment.utc(timePeriod.start);
  59. const endDate = moment.utc(timePeriod.end);
  60. const timeWindow = rule?.timeWindow;
  61. const startEndDifferenceMs = endDate.diff(startDate);
  62. if (
  63. timeWindow &&
  64. (startEndDifferenceMs < API_INTERVAL_POINTS_LIMIT * timeWindow * 60 * 1000 ||
  65. // Special case 7 days * 1m interval over the api limit
  66. startEndDifferenceMs === TIME_WINDOWS[TimePeriod.SEVEN_DAYS])
  67. ) {
  68. return `${timeWindow}m`;
  69. }
  70. return getInterval({start: timePeriod.start, end: timePeriod.end}, 'high');
  71. }
  72. function getFilter() {
  73. const {dataset, query} = rule ?? {};
  74. if (!rule) {
  75. return null;
  76. }
  77. const eventType = isCrashFreeAlert(dataset)
  78. ? null
  79. : extractEventTypeFilterFromRule(rule);
  80. return [eventType, query].join(' ').split(' ');
  81. }
  82. const handleTimePeriodChange = (datetime: ChangeData) => {
  83. const {start, end, relative} = datetime;
  84. if (start && end) {
  85. return router.push({
  86. ...location,
  87. query: {
  88. start: moment(start).utc().format(),
  89. end: moment(end).utc().format(),
  90. },
  91. });
  92. }
  93. return router.push({
  94. ...location,
  95. query: {
  96. period: relative,
  97. },
  98. });
  99. };
  100. if (!rule || !project) {
  101. return (
  102. <Layout.Body>
  103. <Layout.Main>
  104. <Placeholder height="38px" />
  105. <ChartPanel>
  106. <PanelBody withPadding>
  107. <Placeholder height="200px" />
  108. </PanelBody>
  109. </ChartPanel>
  110. </Layout.Main>
  111. <Layout.Side>
  112. <Placeholder height="200px" />
  113. </Layout.Side>
  114. </Layout.Body>
  115. );
  116. }
  117. const {query, dataset} = rule;
  118. const queryWithTypeFilter = `${query} ${extractEventTypeFilterFromRule(rule)}`.trim();
  119. const relativeOptions = {
  120. ...SELECTOR_RELATIVE_PERIODS,
  121. ...(rule.timeWindow > 1 ? {[TimePeriod.FOURTEEN_DAYS]: t('Last 14 days')} : {}),
  122. };
  123. const isSnoozed = rule.snooze;
  124. const ruleActionCategory = getAlertRuleActionCategory(rule);
  125. return (
  126. <Fragment>
  127. {selectedIncident?.alertRule.status === AlertRuleStatus.SNAPSHOT && (
  128. <StyledLayoutBody>
  129. <StyledAlert type="warning" showIcon>
  130. {t('Alert Rule settings have been updated since this alert was triggered.')}
  131. </StyledAlert>
  132. </StyledLayoutBody>
  133. )}
  134. <Layout.Body>
  135. <Layout.Main>
  136. {isSnoozed && (
  137. <Alert showIcon>
  138. {ruleActionCategory === RuleActionsCategories.NO_DEFAULT
  139. ? tct(
  140. "[creator] muted this alert so these notifications won't be sent in the future.",
  141. {creator: rule.snoozeCreatedBy}
  142. )
  143. : tct(
  144. "[creator] muted this alert[forEveryone]so you won't get these notifications in the future.",
  145. {
  146. creator: rule.snoozeCreatedBy,
  147. forEveryone: rule.snoozeForEveryone ? ' for everyone ' : ' ',
  148. }
  149. )}
  150. </Alert>
  151. )}
  152. <StyledPageTimeRangeSelector
  153. organization={organization}
  154. relative={timePeriod.period ?? ''}
  155. start={(timePeriod.custom && timePeriod.start) || null}
  156. end={(timePeriod.custom && timePeriod.end) || null}
  157. utc={null}
  158. onUpdate={handleTimePeriodChange}
  159. relativeOptions={relativeOptions}
  160. showAbsolute={false}
  161. disallowArbitraryRelativeRanges
  162. />
  163. <MetricChart
  164. api={api}
  165. rule={rule}
  166. incidents={incidents}
  167. timePeriod={timePeriod}
  168. selectedIncident={selectedIncident}
  169. organization={organization}
  170. project={project}
  171. interval={getPeriodInterval()}
  172. query={isCrashFreeAlert(dataset) ? query : queryWithTypeFilter}
  173. filter={getFilter()}
  174. isOnDemandMetricAlert={isOnDemandMetricAlert(dataset, query)}
  175. />
  176. <DetailWrapper>
  177. <ActivityWrapper>
  178. <MetricHistory incidents={incidents} />
  179. {[Dataset.METRICS, Dataset.SESSIONS, Dataset.ERRORS].includes(dataset) && (
  180. <RelatedIssues
  181. organization={organization}
  182. rule={rule}
  183. projects={[project]}
  184. timePeriod={timePeriod}
  185. query={
  186. dataset === Dataset.ERRORS
  187. ? queryWithTypeFilter
  188. : isCrashFreeAlert(dataset)
  189. ? `${query} error.unhandled:true`
  190. : undefined
  191. }
  192. />
  193. )}
  194. {dataset === Dataset.TRANSACTIONS && (
  195. <RelatedTransactions
  196. organization={organization}
  197. location={location}
  198. rule={rule}
  199. projects={[project]}
  200. timePeriod={timePeriod}
  201. filter={extractEventTypeFilterFromRule(rule)}
  202. />
  203. )}
  204. </ActivityWrapper>
  205. </DetailWrapper>
  206. </Layout.Main>
  207. <Layout.Side>
  208. <MetricDetailsSidebar rule={rule} />
  209. </Layout.Side>
  210. </Layout.Body>
  211. </Fragment>
  212. );
  213. }
  214. const DetailWrapper = styled('div')`
  215. display: flex;
  216. flex: 1;
  217. @media (max-width: ${p => p.theme.breakpoints.small}) {
  218. flex-direction: column-reverse;
  219. }
  220. `;
  221. const StyledLayoutBody = styled(Layout.Body)`
  222. flex-grow: 0;
  223. padding-bottom: 0 !important;
  224. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  225. grid-template-columns: auto;
  226. }
  227. `;
  228. const StyledAlert = styled(Alert)`
  229. margin: 0;
  230. `;
  231. const ActivityWrapper = styled('div')`
  232. display: flex;
  233. flex: 1;
  234. flex-direction: column;
  235. width: 100%;
  236. `;
  237. const ChartPanel = styled(Panel)`
  238. margin-top: ${space(2)};
  239. `;
  240. const StyledPageTimeRangeSelector = styled(PageTimeRangeSelector)`
  241. margin-bottom: ${space(2)};
  242. `;