index.tsx 9.8 KB

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