index.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 {location, organization, eventView, transactionName, setError} = props;
  60. const eventsDisplayFilterName = decodeEventsDisplayFilterFromLocation(location);
  61. const spanOperationBreakdownFilter = decodeFilterFromLocation(location);
  62. const webVital = getWebVital(location);
  63. const totalEventsView = eventView.clone();
  64. const percentilesView = getPercentilesEventView(eventView);
  65. totalEventsView.sorts = [];
  66. totalEventsView.fields = [{field: 'count()', width: -1}];
  67. const getFilteredEventView = (percentiles: PercentileValues) => {
  68. const filter = getEventsFilterOptions(spanOperationBreakdownFilter, percentiles)[
  69. eventsDisplayFilterName
  70. ];
  71. const filteredEventView = eventView?.clone();
  72. if (filteredEventView && filter?.query) {
  73. const query = new MutableSearch(filteredEventView.query);
  74. filter.query.forEach(item => query.setFilterValues(item[0], [item[1]]));
  75. filteredEventView.query = query.formatString();
  76. }
  77. return filteredEventView;
  78. };
  79. const onChangeSpanOperationBreakdownFilter = (
  80. newFilter: SpanOperationBreakdownFilter
  81. ) => {
  82. trackAnalyticsEvent({
  83. eventName: 'Performance Views: Transaction Events Ops Breakdown Filter Dropdown',
  84. eventKey: 'performance_views.transactionEvents.ops_filter_dropdown.selection',
  85. organization_id: parseInt(organization.id, 10),
  86. action: newFilter as string,
  87. });
  88. // Check to see if the current table sort matches the EventsDisplayFilter.
  89. // If it does, we can re-sort using the new SpanOperationBreakdownFilter
  90. const eventsFilterOptionSort = getEventsFilterOptions(spanOperationBreakdownFilter)[
  91. eventsDisplayFilterName
  92. ].sort;
  93. const currentSort = eventView?.sorts?.[0];
  94. let sortQuery = {};
  95. if (
  96. eventsFilterOptionSort?.kind === currentSort?.kind &&
  97. eventsFilterOptionSort?.field === currentSort?.field
  98. ) {
  99. sortQuery = filterEventsDisplayToLocationQuery(eventsDisplayFilterName, newFilter);
  100. }
  101. const nextQuery: Location['query'] = {
  102. ...removeHistogramQueryStrings(location, [ZOOM_START, ZOOM_END]),
  103. ...filterToLocationQuery(newFilter),
  104. ...sortQuery,
  105. };
  106. if (newFilter === SpanOperationBreakdownFilter.None) {
  107. delete nextQuery.breakdown;
  108. }
  109. browserHistory.push({
  110. pathname: location.pathname,
  111. query: nextQuery,
  112. });
  113. };
  114. const onChangeEventsDisplayFilter = (newFilterName: EventsDisplayFilterName) => {
  115. trackAnalyticsEvent({
  116. eventName: 'Performance Views: Transaction Events Display Filter Dropdown',
  117. eventKey: 'performance_views.transactionEvents.display_filter_dropdown.selection',
  118. organization_id: parseInt(organization.id, 10),
  119. action: newFilterName as string,
  120. });
  121. const nextQuery: Location['query'] = {
  122. ...removeHistogramQueryStrings(location, [ZOOM_START, ZOOM_END]),
  123. ...filterEventsDisplayToLocationQuery(newFilterName, spanOperationBreakdownFilter),
  124. };
  125. if (newFilterName === EventsDisplayFilterName.p100) {
  126. delete nextQuery.showTransaction;
  127. }
  128. browserHistory.push({
  129. pathname: location.pathname,
  130. query: nextQuery,
  131. });
  132. };
  133. return (
  134. <DiscoverQuery
  135. eventView={totalEventsView}
  136. orgSlug={organization.slug}
  137. location={location}
  138. setError={error => setError(error?.message)}
  139. referrer="api.performance.transaction-summary"
  140. cursor="0:0:0"
  141. useEvents
  142. >
  143. {({isLoading: isTotalEventsLoading, tableData: table}) => {
  144. const totalEventCount: string =
  145. table?.data[0]?.['count()']?.toLocaleString() || '';
  146. return (
  147. <DiscoverQuery
  148. eventView={percentilesView}
  149. orgSlug={organization.slug}
  150. location={location}
  151. referrer="api.performance.transaction-events"
  152. useEvents
  153. >
  154. {({isLoading, tableData}) => {
  155. if (isTotalEventsLoading || isLoading) {
  156. return (
  157. <Layout.Main fullWidth>
  158. <LoadingIndicator />
  159. </Layout.Main>
  160. );
  161. }
  162. const percentileData = tableData?.data?.[0];
  163. const percentiles = mapPercentileValues(percentileData);
  164. const filteredEventView = getFilteredEventView(percentiles);
  165. return (
  166. <EventsContent
  167. totalEventCount={totalEventCount}
  168. location={location}
  169. organization={organization}
  170. eventView={filteredEventView}
  171. transactionName={transactionName}
  172. spanOperationBreakdownFilter={spanOperationBreakdownFilter}
  173. onChangeSpanOperationBreakdownFilter={
  174. onChangeSpanOperationBreakdownFilter
  175. }
  176. eventsDisplayFilterName={eventsDisplayFilterName}
  177. onChangeEventsDisplayFilter={onChangeEventsDisplayFilter}
  178. percentileValues={percentiles}
  179. webVital={webVital}
  180. setError={setError}
  181. />
  182. );
  183. }}
  184. </DiscoverQuery>
  185. );
  186. }}
  187. </DiscoverQuery>
  188. );
  189. }
  190. function getDocumentTitle(transactionName: string): string {
  191. const hasTransactionName =
  192. typeof transactionName === 'string' && String(transactionName).trim().length > 0;
  193. if (hasTransactionName) {
  194. return [String(transactionName).trim(), t('Events')].join(' \u2014 ');
  195. }
  196. return [t('Summary'), t('Events')].join(' \u2014 ');
  197. }
  198. function getWebVital(location: Location): WebVital | undefined {
  199. const webVital = decodeScalar(location.query.webVital, '') as WebVital;
  200. if (Object.values(WebVital).includes(webVital)) {
  201. return webVital;
  202. }
  203. return undefined;
  204. }
  205. function generateEventView({
  206. location,
  207. organization,
  208. transactionName,
  209. }: {
  210. location: Location;
  211. organization: Organization;
  212. transactionName: string;
  213. }): EventView {
  214. const query = decodeScalar(location.query.query, '');
  215. const conditions = new MutableSearch(query);
  216. conditions.setFilterValues('event.type', ['transaction']);
  217. conditions.setFilterValues('transaction', [transactionName]);
  218. Object.keys(conditions.filters).forEach(field => {
  219. if (isAggregateField(field)) {
  220. conditions.removeFilter(field);
  221. }
  222. });
  223. // Default fields for relative span view
  224. const fields = [
  225. 'id',
  226. 'user.display',
  227. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  228. 'transaction.duration',
  229. 'trace',
  230. 'timestamp',
  231. ];
  232. if (organization.features.includes('session-replay-ui')) {
  233. fields.push('replayId');
  234. }
  235. const breakdown = decodeFilterFromLocation(location);
  236. if (breakdown !== SpanOperationBreakdownFilter.None) {
  237. fields.splice(2, 1, `spans.${breakdown}`);
  238. } else {
  239. fields.push(...SPAN_OP_BREAKDOWN_FIELDS);
  240. }
  241. const webVital = getWebVital(location);
  242. if (webVital) {
  243. fields.splice(3, 0, webVital);
  244. }
  245. return EventView.fromNewQueryWithLocation(
  246. {
  247. id: undefined,
  248. version: 2,
  249. name: transactionName,
  250. fields,
  251. query: conditions.formatString(),
  252. projects: [],
  253. orderby: decodeScalar(location.query.sort, '-timestamp'),
  254. },
  255. location
  256. );
  257. }
  258. export default withProjects(withOrganization(TransactionEvents));