index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import {browserHistory} from 'react-router';
  2. import {Location} from 'history';
  3. import * as Layout from 'sentry/components/layouts/thirds';
  4. import LoadingIndicator from 'sentry/components/loadingIndicator';
  5. import {t} from 'sentry/locale';
  6. import {Organization, Project} from 'sentry/types';
  7. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  8. import DiscoverQuery from 'sentry/utils/discover/discoverQuery';
  9. import EventView from 'sentry/utils/discover/eventView';
  10. import {
  11. isAggregateField,
  12. SPAN_OP_BREAKDOWN_FIELDS,
  13. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  14. } from 'sentry/utils/discover/fields';
  15. import {WebVital} from 'sentry/utils/fields';
  16. import {removeHistogramQueryStrings} from 'sentry/utils/performance/histogram';
  17. import {decodeScalar} from 'sentry/utils/queryString';
  18. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  19. import withOrganization from 'sentry/utils/withOrganization';
  20. import withProjects from 'sentry/utils/withProjects';
  21. import {
  22. decodeFilterFromLocation,
  23. filterToLocationQuery,
  24. SpanOperationBreakdownFilter,
  25. } from '../filter';
  26. import PageLayout, {ChildProps} from '../pageLayout';
  27. import Tab from '../tabs';
  28. import {ZOOM_END, ZOOM_START} from '../transactionOverview/latencyChart/utils';
  29. import EventsContent from './content';
  30. import {
  31. decodeEventsDisplayFilterFromLocation,
  32. EventsDisplayFilterName,
  33. filterEventsDisplayToLocationQuery,
  34. getEventsFilterOptions,
  35. getPercentilesEventView,
  36. mapPercentileValues,
  37. } from './utils';
  38. type PercentileValues = Record<EventsDisplayFilterName, number>;
  39. type Props = {
  40. location: Location;
  41. organization: Organization;
  42. projects: Project[];
  43. };
  44. function TransactionEvents(props: Props) {
  45. const {location, organization, projects} = props;
  46. return (
  47. <PageLayout
  48. location={location}
  49. organization={organization}
  50. projects={projects}
  51. tab={Tab.Events}
  52. getDocumentTitle={getDocumentTitle}
  53. generateEventView={generateEventView}
  54. childComponent={EventsContentWrapper}
  55. />
  56. );
  57. }
  58. function EventsContentWrapper(props: ChildProps) {
  59. const {
  60. location,
  61. organization,
  62. eventView,
  63. transactionName,
  64. setError,
  65. projectId,
  66. projects,
  67. } = props;
  68. const eventsDisplayFilterName = decodeEventsDisplayFilterFromLocation(location);
  69. const spanOperationBreakdownFilter = decodeFilterFromLocation(location);
  70. const webVital = getWebVital(location);
  71. const totalEventsView = eventView.clone();
  72. const percentilesView = getPercentilesEventView(eventView);
  73. totalEventsView.sorts = [];
  74. totalEventsView.fields = [{field: 'count()', width: -1}];
  75. const getFilteredEventView = (percentiles: PercentileValues) => {
  76. const filter = getEventsFilterOptions(spanOperationBreakdownFilter, percentiles)[
  77. eventsDisplayFilterName
  78. ];
  79. const filteredEventView = eventView?.clone();
  80. if (filteredEventView && filter?.query) {
  81. const query = new MutableSearch(filteredEventView.query);
  82. filter.query.forEach(item => query.setFilterValues(item[0], [item[1]]));
  83. filteredEventView.query = query.formatString();
  84. }
  85. return filteredEventView;
  86. };
  87. const onChangeSpanOperationBreakdownFilter = (
  88. newFilter: SpanOperationBreakdownFilter
  89. ) => {
  90. trackAnalyticsEvent({
  91. eventName: 'Performance Views: Transaction Events Ops Breakdown Filter Dropdown',
  92. eventKey: 'performance_views.transactionEvents.ops_filter_dropdown.selection',
  93. organization_id: parseInt(organization.id, 10),
  94. action: newFilter as string,
  95. });
  96. // Check to see if the current table sort matches the EventsDisplayFilter.
  97. // If it does, we can re-sort using the new SpanOperationBreakdownFilter
  98. const eventsFilterOptionSort = getEventsFilterOptions(spanOperationBreakdownFilter)[
  99. eventsDisplayFilterName
  100. ].sort;
  101. const currentSort = eventView?.sorts?.[0];
  102. let sortQuery = {};
  103. if (
  104. eventsFilterOptionSort?.kind === currentSort?.kind &&
  105. eventsFilterOptionSort?.field === currentSort?.field
  106. ) {
  107. sortQuery = filterEventsDisplayToLocationQuery(eventsDisplayFilterName, newFilter);
  108. }
  109. const nextQuery: Location['query'] = {
  110. ...removeHistogramQueryStrings(location, [ZOOM_START, ZOOM_END]),
  111. ...filterToLocationQuery(newFilter),
  112. ...sortQuery,
  113. };
  114. if (newFilter === SpanOperationBreakdownFilter.None) {
  115. delete nextQuery.breakdown;
  116. }
  117. browserHistory.push({
  118. pathname: location.pathname,
  119. query: nextQuery,
  120. });
  121. };
  122. const onChangeEventsDisplayFilter = (newFilterName: EventsDisplayFilterName) => {
  123. trackAnalyticsEvent({
  124. eventName: 'Performance Views: Transaction Events Display Filter Dropdown',
  125. eventKey: 'performance_views.transactionEvents.display_filter_dropdown.selection',
  126. organization_id: parseInt(organization.id, 10),
  127. action: newFilterName as string,
  128. });
  129. const nextQuery: Location['query'] = {
  130. ...removeHistogramQueryStrings(location, [ZOOM_START, ZOOM_END]),
  131. ...filterEventsDisplayToLocationQuery(newFilterName, spanOperationBreakdownFilter),
  132. };
  133. if (newFilterName === EventsDisplayFilterName.p100) {
  134. delete nextQuery.showTransaction;
  135. }
  136. browserHistory.push({
  137. pathname: location.pathname,
  138. query: nextQuery,
  139. });
  140. };
  141. return (
  142. <DiscoverQuery
  143. eventView={totalEventsView}
  144. orgSlug={organization.slug}
  145. location={location}
  146. setError={error => setError(error?.message)}
  147. referrer="api.performance.transaction-summary"
  148. cursor="0:0:0"
  149. useEvents
  150. >
  151. {({isLoading: isTotalEventsLoading, tableData: table}) => {
  152. const totalEventCount: string =
  153. table?.data[0]?.['count()']?.toLocaleString() || '';
  154. return (
  155. <DiscoverQuery
  156. eventView={percentilesView}
  157. orgSlug={organization.slug}
  158. location={location}
  159. referrer="api.performance.transaction-events"
  160. useEvents
  161. >
  162. {({isLoading, tableData}) => {
  163. if (isTotalEventsLoading || isLoading) {
  164. return (
  165. <Layout.Main fullWidth>
  166. <LoadingIndicator />
  167. </Layout.Main>
  168. );
  169. }
  170. const percentileData = tableData?.data?.[0];
  171. const percentiles = mapPercentileValues(percentileData);
  172. const filteredEventView = getFilteredEventView(percentiles);
  173. return (
  174. <EventsContent
  175. totalEventCount={totalEventCount}
  176. location={location}
  177. organization={organization}
  178. eventView={filteredEventView}
  179. transactionName={transactionName}
  180. spanOperationBreakdownFilter={spanOperationBreakdownFilter}
  181. onChangeSpanOperationBreakdownFilter={
  182. onChangeSpanOperationBreakdownFilter
  183. }
  184. eventsDisplayFilterName={eventsDisplayFilterName}
  185. onChangeEventsDisplayFilter={onChangeEventsDisplayFilter}
  186. percentileValues={percentiles}
  187. projectId={projectId}
  188. projects={projects}
  189. webVital={webVital}
  190. setError={setError}
  191. />
  192. );
  193. }}
  194. </DiscoverQuery>
  195. );
  196. }}
  197. </DiscoverQuery>
  198. );
  199. }
  200. function getDocumentTitle(transactionName: string): string {
  201. const hasTransactionName =
  202. typeof transactionName === 'string' && String(transactionName).trim().length > 0;
  203. if (hasTransactionName) {
  204. return [String(transactionName).trim(), t('Events')].join(' \u2014 ');
  205. }
  206. return [t('Summary'), t('Events')].join(' \u2014 ');
  207. }
  208. function getWebVital(location: Location): WebVital | undefined {
  209. const webVital = decodeScalar(location.query.webVital, '') as WebVital;
  210. if (Object.values(WebVital).includes(webVital)) {
  211. return webVital;
  212. }
  213. return undefined;
  214. }
  215. function generateEventView({
  216. location,
  217. organization,
  218. transactionName,
  219. }: {
  220. location: Location;
  221. organization: Organization;
  222. transactionName: string;
  223. }): EventView {
  224. const query = decodeScalar(location.query.query, '');
  225. const conditions = new MutableSearch(query);
  226. conditions.setFilterValues('event.type', ['transaction']);
  227. conditions.setFilterValues('transaction', [transactionName]);
  228. Object.keys(conditions.filters).forEach(field => {
  229. if (isAggregateField(field)) {
  230. conditions.removeFilter(field);
  231. }
  232. });
  233. // Default fields for relative span view
  234. const fields = [
  235. 'id',
  236. 'user.display',
  237. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  238. 'transaction.duration',
  239. 'trace',
  240. 'timestamp',
  241. ];
  242. if (organization.features.includes('session-replay-ui')) {
  243. fields.push('replayId');
  244. }
  245. const breakdown = decodeFilterFromLocation(location);
  246. if (breakdown !== SpanOperationBreakdownFilter.None) {
  247. fields.splice(2, 1, `spans.${breakdown}`);
  248. } else {
  249. fields.push(...SPAN_OP_BREAKDOWN_FIELDS);
  250. }
  251. const webVital = getWebVital(location);
  252. if (webVital) {
  253. fields.splice(3, 0, webVital);
  254. }
  255. return EventView.fromNewQueryWithLocation(
  256. {
  257. id: undefined,
  258. version: 2,
  259. name: transactionName,
  260. fields,
  261. query: conditions.formatString(),
  262. projects: [],
  263. orderby: decodeScalar(location.query.sort, '-timestamp'),
  264. },
  265. location
  266. );
  267. }
  268. export default withProjects(withOrganization(TransactionEvents));