body.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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(): string[] | null {
  73. if (!rule) {
  74. return null;
  75. }
  76. const {dataset, query} = rule;
  77. if (isCrashFreeAlert(dataset)) {
  78. return query.trim().split(' ');
  79. }
  80. const eventType = extractEventTypeFilterFromRule(rule);
  81. return (query ? `(${eventType}) AND (${query.trim()})` : eventType).split(' ');
  82. }
  83. const handleTimePeriodChange = (datetime: ChangeData) => {
  84. const {start, end, relative} = datetime;
  85. if (start && end) {
  86. return router.push({
  87. ...location,
  88. query: {
  89. start: moment(start).utc().format(),
  90. end: moment(end).utc().format(),
  91. },
  92. });
  93. }
  94. return router.push({
  95. ...location,
  96. query: {
  97. period: relative,
  98. },
  99. });
  100. };
  101. if (!rule || !project) {
  102. return (
  103. <Layout.Body>
  104. <Layout.Main>
  105. <Placeholder height="38px" />
  106. <ChartPanel>
  107. <PanelBody withPadding>
  108. <Placeholder height="200px" />
  109. </PanelBody>
  110. </ChartPanel>
  111. </Layout.Main>
  112. <Layout.Side>
  113. <Placeholder height="200px" />
  114. </Layout.Side>
  115. </Layout.Body>
  116. );
  117. }
  118. const {dataset, aggregate, query} = rule;
  119. const eventType = extractEventTypeFilterFromRule(rule);
  120. const queryWithTypeFilter = (
  121. query ? `(${query}) AND (${eventType})` : eventType
  122. ).trim();
  123. const relativeOptions = {
  124. ...SELECTOR_RELATIVE_PERIODS,
  125. ...(rule.timeWindow > 1 ? {[TimePeriod.FOURTEEN_DAYS]: t('Last 14 days')} : {}),
  126. };
  127. const isSnoozed = rule.snooze;
  128. const ruleActionCategory = getAlertRuleActionCategory(rule);
  129. const isOnDemandAlert = isOnDemandMetricAlert(dataset, aggregate, query);
  130. return (
  131. <Fragment>
  132. {selectedIncident?.alertRule.status === AlertRuleStatus.SNAPSHOT && (
  133. <StyledLayoutBody>
  134. <StyledAlert type="warning" showIcon>
  135. {t('Alert Rule settings have been updated since this alert was triggered.')}
  136. </StyledAlert>
  137. </StyledLayoutBody>
  138. )}
  139. <Layout.Body>
  140. <Layout.Main>
  141. {isSnoozed && (
  142. <Alert showIcon>
  143. {ruleActionCategory === RuleActionsCategories.NO_DEFAULT
  144. ? tct(
  145. "[creator] muted this alert so these notifications won't be sent in the future.",
  146. {creator: rule.snoozeCreatedBy}
  147. )
  148. : tct(
  149. "[creator] muted this alert[forEveryone]so you won't get these notifications in the future.",
  150. {
  151. creator: rule.snoozeCreatedBy,
  152. forEveryone: rule.snoozeForEveryone ? ' for everyone ' : ' ',
  153. }
  154. )}
  155. </Alert>
  156. )}
  157. <StyledPageTimeRangeSelector
  158. organization={organization}
  159. relative={timePeriod.period ?? ''}
  160. start={(timePeriod.custom && timePeriod.start) || null}
  161. end={(timePeriod.custom && timePeriod.end) || null}
  162. utc={null}
  163. onUpdate={handleTimePeriodChange}
  164. relativeOptions={relativeOptions}
  165. showAbsolute={false}
  166. disallowArbitraryRelativeRanges
  167. />
  168. <MetricChart
  169. api={api}
  170. rule={rule}
  171. incidents={incidents}
  172. timePeriod={timePeriod}
  173. selectedIncident={selectedIncident}
  174. organization={organization}
  175. project={project}
  176. interval={getPeriodInterval()}
  177. query={isCrashFreeAlert(dataset) ? query : queryWithTypeFilter}
  178. filter={getFilter()}
  179. isOnDemandAlert={isOnDemandAlert}
  180. />
  181. <DetailWrapper>
  182. <ActivityWrapper>
  183. <MetricHistory incidents={incidents} />
  184. {[Dataset.METRICS, Dataset.SESSIONS, Dataset.ERRORS].includes(dataset) && (
  185. <RelatedIssues
  186. organization={organization}
  187. rule={rule}
  188. projects={[project]}
  189. timePeriod={timePeriod}
  190. query={
  191. dataset === Dataset.ERRORS
  192. ? // Not using (query) AND (event.type:x) because issues doesn't support it yet
  193. `${extractEventTypeFilterFromRule(rule)} ${query}`.trim()
  194. : isCrashFreeAlert(dataset)
  195. ? `${query} error.unhandled:true`.trim()
  196. : undefined
  197. }
  198. />
  199. )}
  200. {dataset === Dataset.TRANSACTIONS && (
  201. <RelatedTransactions
  202. organization={organization}
  203. location={location}
  204. rule={rule}
  205. projects={[project]}
  206. timePeriod={timePeriod}
  207. filter={extractEventTypeFilterFromRule(rule)}
  208. />
  209. )}
  210. </ActivityWrapper>
  211. </DetailWrapper>
  212. </Layout.Main>
  213. <Layout.Side>
  214. <MetricDetailsSidebar rule={rule} isOnDemandMetricAlert={isOnDemandAlert} />
  215. </Layout.Side>
  216. </Layout.Body>
  217. </Fragment>
  218. );
  219. }
  220. const DetailWrapper = styled('div')`
  221. display: flex;
  222. flex: 1;
  223. @media (max-width: ${p => p.theme.breakpoints.small}) {
  224. flex-direction: column-reverse;
  225. }
  226. `;
  227. const StyledLayoutBody = styled(Layout.Body)`
  228. flex-grow: 0;
  229. padding-bottom: 0 !important;
  230. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  231. grid-template-columns: auto;
  232. }
  233. `;
  234. const StyledAlert = styled(Alert)`
  235. margin: 0;
  236. `;
  237. const ActivityWrapper = styled('div')`
  238. display: flex;
  239. flex: 1;
  240. flex-direction: column;
  241. width: 100%;
  242. `;
  243. const ChartPanel = styled(Panel)`
  244. margin-top: ${space(2)};
  245. `;
  246. const StyledPageTimeRangeSelector = styled(PageTimeRangeSelector)`
  247. margin-bottom: ${space(2)};
  248. `;