index.tsx 9.9 KB

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