index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. transactionName,
  136. }: {
  137. location: Location;
  138. transactionName: string;
  139. }): EventView {
  140. // Use the user supplied query but overwrite any transaction or event type
  141. // conditions they applied.
  142. const query = decodeScalar(location.query.query, '');
  143. const conditions = new MutableSearch(query);
  144. conditions.setFilterValues('event.type', ['transaction']);
  145. conditions.setFilterValues('transaction', [transactionName]);
  146. Object.keys(conditions.filters).forEach(field => {
  147. if (isAggregateField(field)) {
  148. conditions.removeFilter(field);
  149. }
  150. });
  151. const fields = ['id', 'user.display', 'transaction.duration', 'trace', 'timestamp'];
  152. return EventView.fromNewQueryWithLocation(
  153. {
  154. id: undefined,
  155. version: 2,
  156. name: transactionName,
  157. fields,
  158. query: conditions.formatString(),
  159. projects: [],
  160. },
  161. location
  162. );
  163. }
  164. function getTotalsEventView(
  165. _organization: Organization,
  166. eventView: EventView
  167. ): EventView {
  168. const vitals = VITAL_GROUPS.map(({vitals: vs}) => vs).reduce((keys: WebVital[], vs) => {
  169. vs.forEach(vital => keys.push(vital));
  170. return keys;
  171. }, []);
  172. const totalsColumns: QueryFieldValue[] = [
  173. {
  174. kind: 'function',
  175. function: ['p95', '', undefined, undefined],
  176. },
  177. {
  178. kind: 'function',
  179. function: ['count', '', undefined, undefined],
  180. },
  181. {
  182. kind: 'function',
  183. function: ['count_unique', 'user', undefined, undefined],
  184. },
  185. {
  186. kind: 'function',
  187. function: ['failure_rate', '', undefined, undefined],
  188. },
  189. {
  190. kind: 'function',
  191. function: ['tpm', '', undefined, undefined],
  192. },
  193. {
  194. kind: 'function',
  195. function: ['count_miserable', 'user', undefined, undefined],
  196. },
  197. {
  198. kind: 'function',
  199. function: ['user_misery', '', undefined, undefined],
  200. },
  201. {
  202. kind: 'function',
  203. function: ['apdex', '', undefined, undefined],
  204. },
  205. ];
  206. return eventView.withColumns([
  207. ...totalsColumns,
  208. ...vitals.map(
  209. vital =>
  210. ({
  211. kind: 'function',
  212. function: ['percentile', vital, VITAL_PERCENTILE.toString(), undefined],
  213. } as Column)
  214. ),
  215. ]);
  216. }
  217. export default withPageFilters(withProjects(withOrganization(TransactionOverview)));