index.tsx 9.2 KB

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