index.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import {useEffect} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import type {Location} from 'history';
  4. import {loadOrganizationTags} from 'sentry/actionCreators/tags';
  5. import LoadingContainer from 'sentry/components/loading/loadingContainer';
  6. import {t} from 'sentry/locale';
  7. import type {Organization, PageFilters, Project} from 'sentry/types';
  8. import {trackAnalytics} from 'sentry/utils/analytics';
  9. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  10. import EventView from 'sentry/utils/discover/eventView';
  11. import type {Column, QueryFieldValue} from 'sentry/utils/discover/fields';
  12. import {isAggregateField} from 'sentry/utils/discover/fields';
  13. import type {WebVital} from 'sentry/utils/fields';
  14. import {useMetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  15. import {
  16. getIsMetricsDataFromResults,
  17. useMEPDataContext,
  18. } from 'sentry/utils/performance/contexts/metricsEnhancedPerformanceDataContext';
  19. import {
  20. MEPSettingProvider,
  21. useMEPSettingContext,
  22. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  23. import {removeHistogramQueryStrings} from 'sentry/utils/performance/histogram';
  24. import {decodeScalar} from 'sentry/utils/queryString';
  25. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  26. import useApi from 'sentry/utils/useApi';
  27. import withOrganization from 'sentry/utils/withOrganization';
  28. import withPageFilters from 'sentry/utils/withPageFilters';
  29. import withProjects from 'sentry/utils/withProjects';
  30. import {
  31. getTransactionMEPParamsIfApplicable,
  32. getUnfilteredTotalsEventView,
  33. } from 'sentry/views/performance/transactionSummary/transactionOverview/utils';
  34. import {addRoutePerformanceContext} from '../../utils';
  35. import {
  36. decodeFilterFromLocation,
  37. filterToLocationQuery,
  38. SpanOperationBreakdownFilter,
  39. } from '../filter';
  40. import type {ChildProps} from '../pageLayout';
  41. import PageLayout from '../pageLayout';
  42. import Tab from '../tabs';
  43. import {
  44. PERCENTILE as VITAL_PERCENTILE,
  45. VITAL_GROUPS,
  46. } from '../transactionVitals/constants';
  47. import {ZOOM_END, ZOOM_START} from './latencyChart/utils';
  48. import SummaryContent from './content';
  49. // Used to cast the totals request to numbers
  50. // as React.ReactText
  51. type TotalValues = Record<string, number>;
  52. type Props = {
  53. location: Location;
  54. organization: Organization;
  55. projects: Project[];
  56. selection: PageFilters;
  57. };
  58. function TransactionOverview(props: Props) {
  59. const api = useApi();
  60. const {location, selection, organization, projects} = props;
  61. useEffect(() => {
  62. loadOrganizationTags(api, organization.slug, selection);
  63. addRoutePerformanceContext(selection);
  64. trackAnalytics('performance_views.transaction_summary.view', {
  65. organization,
  66. });
  67. }, [selection, organization, api]);
  68. return (
  69. <MEPSettingProvider>
  70. <PageLayout
  71. location={location}
  72. organization={organization}
  73. projects={projects}
  74. tab={Tab.TRANSACTION_SUMMARY}
  75. getDocumentTitle={getDocumentTitle}
  76. generateEventView={generateEventView}
  77. childComponent={CardinalityLoadingWrapper}
  78. />
  79. </MEPSettingProvider>
  80. );
  81. }
  82. function CardinalityLoadingWrapper(props: ChildProps) {
  83. const mepCardinalityContext = useMetricsCardinalityContext();
  84. if (mepCardinalityContext.isLoading) {
  85. return <LoadingContainer isLoading />;
  86. }
  87. return <OverviewContentWrapper {...props} />;
  88. }
  89. function OverviewContentWrapper(props: ChildProps) {
  90. const {
  91. location,
  92. organization,
  93. eventView,
  94. projectId,
  95. transactionName,
  96. transactionThreshold,
  97. transactionThresholdMetric,
  98. } = props;
  99. const mepContext = useMEPDataContext();
  100. const mepSetting = useMEPSettingContext();
  101. const mepCardinalityContext = useMetricsCardinalityContext();
  102. const queryExtras = getTransactionMEPParamsIfApplicable(
  103. mepSetting,
  104. mepCardinalityContext,
  105. organization
  106. );
  107. const queryData = useDiscoverQuery({
  108. eventView: getTotalsEventView(organization, eventView),
  109. orgSlug: organization.slug,
  110. location,
  111. transactionThreshold,
  112. transactionThresholdMetric,
  113. referrer: 'api.performance.transaction-summary',
  114. queryExtras,
  115. options: {
  116. refetchOnWindowFocus: false,
  117. },
  118. });
  119. // Count has to be total indexed events count because it's only used
  120. // in indexed events contexts
  121. const totalCountQueryData = useDiscoverQuery({
  122. eventView: getTotalCountEventView(organization, eventView),
  123. orgSlug: organization.slug,
  124. location,
  125. transactionThreshold,
  126. transactionThresholdMetric,
  127. referrer: 'api.performance.transaction-summary',
  128. });
  129. // Unfiltered count has to be total indexed events count because it's only used
  130. // in indexed events contexts
  131. const additionalQueryData = useDiscoverQuery({
  132. eventView: getUnfilteredTotalsEventView(eventView, location, ['count']),
  133. orgSlug: organization.slug,
  134. location,
  135. transactionThreshold,
  136. transactionThresholdMetric,
  137. referrer: 'api.performance.transaction-summary',
  138. });
  139. useEffect(() => {
  140. const isMetricsData = getIsMetricsDataFromResults(queryData.data);
  141. mepContext.setIsMetricsData(isMetricsData);
  142. }, [mepContext, queryData.data]);
  143. const {data: tableData, isLoading, error} = queryData;
  144. const {
  145. data: unfilteredTableData,
  146. isLoading: isAdditionalQueryLoading,
  147. error: additionalQueryError,
  148. } = additionalQueryData;
  149. const {
  150. data: totalCountTableData,
  151. isLoading: isTotalCountQueryLoading,
  152. error: totalCountQueryError,
  153. } = totalCountQueryData;
  154. const spanOperationBreakdownFilter = decodeFilterFromLocation(location);
  155. const onChangeFilter = (newFilter: SpanOperationBreakdownFilter) => {
  156. trackAnalytics('performance_views.filter_dropdown.selection', {
  157. organization,
  158. action: newFilter as string,
  159. });
  160. const nextQuery: Location['query'] = {
  161. ...removeHistogramQueryStrings(location, [ZOOM_START, ZOOM_END]),
  162. ...filterToLocationQuery(newFilter),
  163. };
  164. if (newFilter === SpanOperationBreakdownFilter.NONE) {
  165. delete nextQuery.breakdown;
  166. }
  167. browserHistory.push({
  168. pathname: location.pathname,
  169. query: nextQuery,
  170. });
  171. };
  172. let totals: TotalValues | null =
  173. (tableData?.data?.[0] as {
  174. [k: string]: number;
  175. }) ?? null;
  176. const totalCountData: TotalValues | null =
  177. (totalCountTableData?.data?.[0] as {[k: string]: number}) ?? null;
  178. // Count is always a count of indexed events,
  179. // while other fields could be either metrics or index based
  180. totals = {...totals, ...totalCountData};
  181. const unfilteredTotals: TotalValues | null =
  182. (unfilteredTableData?.data?.[0] as {[k: string]: number}) ?? null;
  183. return (
  184. <SummaryContent
  185. location={location}
  186. organization={organization}
  187. eventView={eventView}
  188. projectId={projectId}
  189. transactionName={transactionName}
  190. isLoading={isLoading || isAdditionalQueryLoading || isTotalCountQueryLoading}
  191. error={error || additionalQueryError || totalCountQueryError}
  192. totalValues={totals}
  193. onChangeFilter={onChangeFilter}
  194. spanOperationBreakdownFilter={spanOperationBreakdownFilter}
  195. unfilteredTotalValues={unfilteredTotals}
  196. />
  197. );
  198. }
  199. function getDocumentTitle(transactionName: string): string {
  200. const hasTransactionName =
  201. typeof transactionName === 'string' && String(transactionName).trim().length > 0;
  202. if (hasTransactionName) {
  203. return [String(transactionName).trim(), t('Performance')].join(' - ');
  204. }
  205. return [t('Summary'), t('Performance')].join(' - ');
  206. }
  207. function generateEventView({
  208. location,
  209. transactionName,
  210. }: {
  211. location: Location;
  212. organization: Organization;
  213. transactionName: string;
  214. }): EventView {
  215. // Use the user supplied query but overwrite any transaction or event type
  216. // conditions they applied.
  217. const query = decodeScalar(location.query.query, '');
  218. const conditions = new MutableSearch(query);
  219. conditions.setFilterValues('event.type', ['transaction']);
  220. conditions.setFilterValues('transaction', [transactionName]);
  221. Object.keys(conditions.filters).forEach(field => {
  222. if (isAggregateField(field)) {
  223. conditions.removeFilter(field);
  224. }
  225. });
  226. const fields = ['id', 'user.display', 'transaction.duration', 'trace', 'timestamp'];
  227. return EventView.fromNewQueryWithLocation(
  228. {
  229. id: undefined,
  230. version: 2,
  231. name: transactionName,
  232. fields,
  233. query: conditions.formatString(),
  234. projects: [],
  235. },
  236. location
  237. );
  238. }
  239. function getTotalCountEventView(
  240. _organization: Organization,
  241. eventView: EventView
  242. ): EventView {
  243. const totalCountField: QueryFieldValue = {
  244. kind: 'function',
  245. function: ['count', '', undefined, undefined],
  246. };
  247. return eventView.withColumns([totalCountField]);
  248. }
  249. function getTotalsEventView(
  250. _organization: Organization,
  251. eventView: EventView
  252. ): EventView {
  253. const vitals = VITAL_GROUPS.map(({vitals: vs}) => vs).reduce((keys: WebVital[], vs) => {
  254. vs.forEach(vital => keys.push(vital));
  255. return keys;
  256. }, []);
  257. const totalsColumns: QueryFieldValue[] = [
  258. {
  259. kind: 'function',
  260. function: ['p95', '', undefined, undefined],
  261. },
  262. {
  263. kind: 'function',
  264. function: ['count_unique', 'user', undefined, undefined],
  265. },
  266. {
  267. kind: 'function',
  268. function: ['failure_rate', '', undefined, undefined],
  269. },
  270. {
  271. kind: 'function',
  272. function: ['tpm', '', undefined, undefined],
  273. },
  274. {
  275. kind: 'function',
  276. function: ['count_miserable', 'user', undefined, undefined],
  277. },
  278. {
  279. kind: 'function',
  280. function: ['user_misery', '', undefined, undefined],
  281. },
  282. {
  283. kind: 'function',
  284. function: ['apdex', '', undefined, undefined],
  285. },
  286. {
  287. kind: 'function',
  288. function: ['sum', 'transaction.duration', undefined, undefined],
  289. },
  290. ];
  291. return eventView.withColumns([
  292. ...totalsColumns,
  293. ...vitals.map(
  294. vital =>
  295. ({
  296. kind: 'function',
  297. function: ['percentile', vital, VITAL_PERCENTILE.toString(), undefined],
  298. }) as Column
  299. ),
  300. ]);
  301. }
  302. export default withPageFilters(withProjects(withOrganization(TransactionOverview)));