index.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 {t} from 'sentry/locale';
  6. import {Organization, PageFilters, Project} from 'sentry/types';
  7. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  8. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  9. import EventView from 'sentry/utils/discover/eventView';
  10. import {Column, isAggregateField, QueryFieldValue} from 'sentry/utils/discover/fields';
  11. import {WebVital} from 'sentry/utils/fields';
  12. import {MEPSettingProvider} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  13. import {removeHistogramQueryStrings} from 'sentry/utils/performance/histogram';
  14. import {decodeScalar} from 'sentry/utils/queryString';
  15. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  16. import useApi from 'sentry/utils/useApi';
  17. import withOrganization from 'sentry/utils/withOrganization';
  18. import withPageFilters from 'sentry/utils/withPageFilters';
  19. import withProjects from 'sentry/utils/withProjects';
  20. import {addRoutePerformanceContext} from '../../utils';
  21. import {
  22. decodeFilterFromLocation,
  23. filterToLocationQuery,
  24. SpanOperationBreakdownFilter,
  25. } from '../filter';
  26. import PageLayout, {ChildProps} from '../pageLayout';
  27. import Tab from '../tabs';
  28. import {
  29. PERCENTILE as VITAL_PERCENTILE,
  30. VITAL_GROUPS,
  31. } from '../transactionVitals/constants';
  32. import {ZOOM_END, ZOOM_START} from './latencyChart/utils';
  33. import SummaryContent from './content';
  34. // Used to cast the totals request to numbers
  35. // as React.ReactText
  36. type TotalValues = Record<string, number>;
  37. type Props = {
  38. location: Location;
  39. organization: Organization;
  40. projects: Project[];
  41. selection: PageFilters;
  42. };
  43. function TransactionOverview(props: Props) {
  44. const api = useApi();
  45. const {location, selection, organization, projects} = props;
  46. useEffect(() => {
  47. loadOrganizationTags(api, organization.slug, selection);
  48. addRoutePerformanceContext(selection);
  49. trackAdvancedAnalyticsEvent('performance_views.transaction_summary.view', {
  50. organization,
  51. });
  52. }, [selection, organization, api]);
  53. return (
  54. <MEPSettingProvider>
  55. <PageLayout
  56. location={location}
  57. organization={organization}
  58. projects={projects}
  59. tab={Tab.TransactionSummary}
  60. getDocumentTitle={getDocumentTitle}
  61. generateEventView={generateEventView}
  62. childComponent={OverviewContentWrapper}
  63. />
  64. </MEPSettingProvider>
  65. );
  66. }
  67. function OverviewContentWrapper(props: ChildProps) {
  68. const {
  69. location,
  70. organization,
  71. eventView,
  72. projectId,
  73. transactionName,
  74. transactionThreshold,
  75. transactionThresholdMetric,
  76. } = props;
  77. const useEvents = organization.features.includes(
  78. 'performance-frontend-use-events-endpoint'
  79. );
  80. const queryData = useDiscoverQuery({
  81. eventView: getTotalsEventView(organization, eventView),
  82. orgSlug: organization.slug,
  83. location,
  84. transactionThreshold,
  85. transactionThresholdMetric,
  86. referrer: 'api.performance.transaction-summary',
  87. useEvents,
  88. });
  89. const {data: tableData, isLoading, error} = queryData;
  90. const spanOperationBreakdownFilter = decodeFilterFromLocation(location);
  91. const onChangeFilter = (newFilter: SpanOperationBreakdownFilter) => {
  92. trackAdvancedAnalyticsEvent('performance_views.filter_dropdown.selection', {
  93. organization,
  94. action: newFilter as string,
  95. });
  96. const nextQuery: Location['query'] = {
  97. ...removeHistogramQueryStrings(location, [ZOOM_START, ZOOM_END]),
  98. ...filterToLocationQuery(newFilter),
  99. };
  100. if (newFilter === SpanOperationBreakdownFilter.None) {
  101. delete nextQuery.breakdown;
  102. }
  103. browserHistory.push({
  104. pathname: location.pathname,
  105. query: nextQuery,
  106. });
  107. };
  108. const totals: TotalValues | null =
  109. (tableData?.data?.[0] as {[k: string]: number}) ?? null;
  110. return (
  111. <SummaryContent
  112. location={location}
  113. organization={organization}
  114. eventView={eventView}
  115. projectId={projectId}
  116. transactionName={transactionName}
  117. isLoading={isLoading}
  118. error={error}
  119. totalValues={totals}
  120. onChangeFilter={onChangeFilter}
  121. spanOperationBreakdownFilter={spanOperationBreakdownFilter}
  122. />
  123. );
  124. }
  125. function getDocumentTitle(transactionName: string): string {
  126. const hasTransactionName =
  127. typeof transactionName === 'string' && String(transactionName).trim().length > 0;
  128. if (hasTransactionName) {
  129. return [String(transactionName).trim(), t('Performance')].join(' - ');
  130. }
  131. return [t('Summary'), t('Performance')].join(' - ');
  132. }
  133. function generateEventView({
  134. location,
  135. organization,
  136. transactionName,
  137. }: {
  138. location: Location;
  139. organization: Organization;
  140. transactionName: string;
  141. }): EventView {
  142. // Use the user supplied query but overwrite any transaction or event type
  143. // conditions they applied.
  144. const query = decodeScalar(location.query.query, '');
  145. const conditions = new MutableSearch(query);
  146. conditions.setFilterValues('event.type', ['transaction']);
  147. conditions.setFilterValues('transaction', [transactionName]);
  148. Object.keys(conditions.filters).forEach(field => {
  149. if (isAggregateField(field)) {
  150. conditions.removeFilter(field);
  151. }
  152. });
  153. const fields = ['id', 'user.display', 'transaction.duration', 'trace', 'timestamp'];
  154. if (organization.features.includes('session-replay-ui')) {
  155. fields.push('replayId');
  156. }
  157. return EventView.fromNewQueryWithLocation(
  158. {
  159. id: undefined,
  160. version: 2,
  161. name: transactionName,
  162. fields,
  163. query: conditions.formatString(),
  164. projects: [],
  165. },
  166. location
  167. );
  168. }
  169. function getTotalsEventView(
  170. _organization: Organization,
  171. eventView: EventView
  172. ): EventView {
  173. const vitals = VITAL_GROUPS.map(({vitals: vs}) => vs).reduce((keys: WebVital[], vs) => {
  174. vs.forEach(vital => keys.push(vital));
  175. return keys;
  176. }, []);
  177. const totalsColumns: QueryFieldValue[] = [
  178. {
  179. kind: 'function',
  180. function: ['p95', '', undefined, undefined],
  181. },
  182. {
  183. kind: 'function',
  184. function: ['count', '', undefined, undefined],
  185. },
  186. {
  187. kind: 'function',
  188. function: ['count_unique', 'user', undefined, undefined],
  189. },
  190. {
  191. kind: 'function',
  192. function: ['failure_rate', '', undefined, undefined],
  193. },
  194. {
  195. kind: 'function',
  196. function: ['tpm', '', undefined, undefined],
  197. },
  198. {
  199. kind: 'function',
  200. function: ['count_miserable', 'user', undefined, undefined],
  201. },
  202. {
  203. kind: 'function',
  204. function: ['user_misery', '', undefined, undefined],
  205. },
  206. {
  207. kind: 'function',
  208. function: ['apdex', '', undefined, undefined],
  209. },
  210. ];
  211. return eventView.withColumns([
  212. ...totalsColumns,
  213. ...vitals.map(
  214. vital =>
  215. ({
  216. kind: 'function',
  217. function: ['percentile', vital, VITAL_PERCENTILE.toString(), undefined],
  218. } as Column)
  219. ),
  220. ]);
  221. }
  222. export default withPageFilters(withProjects(withOrganization(TransactionOverview)));