index.tsx 9.3 KB

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