index.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 {
  13. MEPSettingProvider,
  14. useMEPSettingContext,
  15. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  16. import {removeHistogramQueryStrings} from 'sentry/utils/performance/histogram';
  17. import {decodeScalar} from 'sentry/utils/queryString';
  18. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  19. import useApi from 'sentry/utils/useApi';
  20. import withOrganization from 'sentry/utils/withOrganization';
  21. import withPageFilters from 'sentry/utils/withPageFilters';
  22. import withProjects from 'sentry/utils/withProjects';
  23. import {
  24. getTransactionMEPParamsIfApplicable,
  25. getUnfilteredTotalsEventView,
  26. } from 'sentry/views/performance/transactionSummary/transactionOverview/utils';
  27. import {addRoutePerformanceContext} from '../../utils';
  28. import {
  29. decodeFilterFromLocation,
  30. filterToLocationQuery,
  31. SpanOperationBreakdownFilter,
  32. } from '../filter';
  33. import PageLayout, {ChildProps} from '../pageLayout';
  34. import Tab from '../pageLayout/tabs';
  35. import {
  36. PERCENTILE as VITAL_PERCENTILE,
  37. VITAL_GROUPS,
  38. } from '../transactionVitals/constants';
  39. import {ZOOM_END, ZOOM_START} from './latencyChart/utils';
  40. import SummaryContent from './content';
  41. // Used to cast the totals request to numbers
  42. // as React.ReactText
  43. type TotalValues = Record<string, number>;
  44. type Props = {
  45. location: Location;
  46. organization: Organization;
  47. projects: Project[];
  48. selection: PageFilters;
  49. };
  50. function TransactionOverview(props: Props) {
  51. const api = useApi();
  52. const {location, selection, organization, projects} = props;
  53. useEffect(() => {
  54. loadOrganizationTags(api, organization.slug, selection);
  55. addRoutePerformanceContext(selection);
  56. trackAdvancedAnalyticsEvent('performance_views.transaction_summary.view', {
  57. organization,
  58. });
  59. }, [selection, organization, api]);
  60. return (
  61. <MEPSettingProvider>
  62. <PageLayout
  63. location={location}
  64. organization={organization}
  65. projects={projects}
  66. tab={Tab.TransactionSummary}
  67. getDocumentTitle={getDocumentTitle}
  68. generateEventView={generateEventView}
  69. childComponent={OverviewContentWrapper}
  70. />
  71. </MEPSettingProvider>
  72. );
  73. }
  74. function OverviewContentWrapper(props: ChildProps) {
  75. const {
  76. location,
  77. organization,
  78. eventView,
  79. projectId,
  80. transactionName,
  81. transactionThreshold,
  82. transactionThresholdMetric,
  83. } = props;
  84. const mepSetting = useMEPSettingContext();
  85. const queryExtras = getTransactionMEPParamsIfApplicable(
  86. mepSetting,
  87. organization,
  88. location
  89. );
  90. const queryData = useDiscoverQuery({
  91. eventView: getTotalsEventView(organization, eventView),
  92. orgSlug: organization.slug,
  93. location,
  94. transactionThreshold,
  95. transactionThresholdMetric,
  96. referrer: 'api.performance.transaction-summary',
  97. queryExtras,
  98. });
  99. // Count has to be total indexed events count because it's only used
  100. // in indexed events contexts
  101. const totalCountQueryData = useDiscoverQuery({
  102. eventView: getTotalCountEventView(organization, eventView),
  103. orgSlug: organization.slug,
  104. location,
  105. transactionThreshold,
  106. transactionThresholdMetric,
  107. referrer: 'api.performance.transaction-summary',
  108. });
  109. const unfilteredQueryExtras = getTransactionMEPParamsIfApplicable(
  110. mepSetting,
  111. organization,
  112. location
  113. );
  114. const additionalQueryData = useDiscoverQuery({
  115. eventView: getUnfilteredTotalsEventView(eventView, location, ['tpm']),
  116. orgSlug: organization.slug,
  117. location,
  118. transactionThreshold,
  119. transactionThresholdMetric,
  120. referrer: 'api.performance.transaction-summary',
  121. queryExtras: unfilteredQueryExtras,
  122. });
  123. const {data: tableData, isLoading, error} = queryData;
  124. const {
  125. data: unfilteredTableData,
  126. isLoading: isAdditionalQueryLoading,
  127. error: additionalQueryError,
  128. } = additionalQueryData;
  129. const {
  130. data: totalCountTableData,
  131. isLoading: isTotalCountQueryLoading,
  132. error: totalCountQueryError,
  133. } = totalCountQueryData;
  134. const spanOperationBreakdownFilter = decodeFilterFromLocation(location);
  135. const onChangeFilter = (newFilter: SpanOperationBreakdownFilter) => {
  136. trackAdvancedAnalyticsEvent('performance_views.filter_dropdown.selection', {
  137. organization,
  138. action: newFilter as string,
  139. });
  140. const nextQuery: Location['query'] = {
  141. ...removeHistogramQueryStrings(location, [ZOOM_START, ZOOM_END]),
  142. ...filterToLocationQuery(newFilter),
  143. };
  144. if (newFilter === SpanOperationBreakdownFilter.None) {
  145. delete nextQuery.breakdown;
  146. }
  147. browserHistory.push({
  148. pathname: location.pathname,
  149. query: nextQuery,
  150. });
  151. };
  152. let totals: TotalValues | null =
  153. (tableData?.data?.[0] as {
  154. [k: string]: number;
  155. }) ?? null;
  156. const totalCountData: TotalValues | null =
  157. (totalCountTableData?.data?.[0] as {[k: string]: number}) ?? null;
  158. // Count is always a count of indexed events,
  159. // while other fields could be either metrics or index based
  160. totals = {...totals, ...totalCountData};
  161. const unfilteredTotals: TotalValues | null =
  162. (unfilteredTableData?.data?.[0] as {[k: string]: number}) ?? null;
  163. return (
  164. <SummaryContent
  165. location={location}
  166. organization={organization}
  167. eventView={eventView}
  168. projectId={projectId}
  169. transactionName={transactionName}
  170. isLoading={isLoading || isAdditionalQueryLoading || isTotalCountQueryLoading}
  171. error={error || additionalQueryError || totalCountQueryError}
  172. totalValues={totals}
  173. onChangeFilter={onChangeFilter}
  174. spanOperationBreakdownFilter={spanOperationBreakdownFilter}
  175. unfilteredTotalValues={unfilteredTotals}
  176. />
  177. );
  178. }
  179. function getDocumentTitle(transactionName: string): string {
  180. const hasTransactionName =
  181. typeof transactionName === 'string' && String(transactionName).trim().length > 0;
  182. if (hasTransactionName) {
  183. return [String(transactionName).trim(), t('Performance')].join(' - ');
  184. }
  185. return [t('Summary'), t('Performance')].join(' - ');
  186. }
  187. function generateEventView({
  188. location,
  189. transactionName,
  190. }: {
  191. location: Location;
  192. organization: Organization;
  193. transactionName: string;
  194. }): EventView {
  195. // Use the user supplied query but overwrite any transaction or event type
  196. // conditions they applied.
  197. const query = decodeScalar(location.query.query, '');
  198. const conditions = new MutableSearch(query);
  199. conditions.setFilterValues('event.type', ['transaction']);
  200. conditions.setFilterValues('transaction', [transactionName]);
  201. Object.keys(conditions.filters).forEach(field => {
  202. if (isAggregateField(field)) {
  203. conditions.removeFilter(field);
  204. }
  205. });
  206. const fields = ['id', 'user.display', 'transaction.duration', 'trace', 'timestamp'];
  207. return EventView.fromNewQueryWithLocation(
  208. {
  209. id: undefined,
  210. version: 2,
  211. name: transactionName,
  212. fields,
  213. query: conditions.formatString(),
  214. projects: [],
  215. },
  216. location
  217. );
  218. }
  219. function getTotalCountEventView(
  220. _organization: Organization,
  221. eventView: EventView
  222. ): EventView {
  223. const totalCountField: QueryFieldValue = {
  224. kind: 'function',
  225. function: ['count', '', undefined, undefined],
  226. };
  227. return eventView.withColumns([totalCountField]);
  228. }
  229. function getTotalsEventView(
  230. _organization: Organization,
  231. eventView: EventView
  232. ): EventView {
  233. const vitals = VITAL_GROUPS.map(({vitals: vs}) => vs).reduce((keys: WebVital[], vs) => {
  234. vs.forEach(vital => keys.push(vital));
  235. return keys;
  236. }, []);
  237. const totalsColumns: QueryFieldValue[] = [
  238. {
  239. kind: 'function',
  240. function: ['p95', '', undefined, undefined],
  241. },
  242. {
  243. kind: 'function',
  244. function: ['count_unique', 'user', undefined, undefined],
  245. },
  246. {
  247. kind: 'function',
  248. function: ['failure_rate', '', undefined, undefined],
  249. },
  250. {
  251. kind: 'function',
  252. function: ['tpm', '', undefined, undefined],
  253. },
  254. {
  255. kind: 'function',
  256. function: ['count_miserable', 'user', undefined, undefined],
  257. },
  258. {
  259. kind: 'function',
  260. function: ['user_misery', '', undefined, undefined],
  261. },
  262. {
  263. kind: 'function',
  264. function: ['apdex', '', undefined, undefined],
  265. },
  266. {
  267. kind: 'function',
  268. function: ['sum', 'transaction.duration', undefined, undefined],
  269. },
  270. ];
  271. return eventView.withColumns([
  272. ...totalsColumns,
  273. ...vitals.map(
  274. vital =>
  275. ({
  276. kind: 'function',
  277. function: ['percentile', vital, VITAL_PERCENTILE.toString(), undefined],
  278. } as Column)
  279. ),
  280. ]);
  281. }
  282. export default withPageFilters(withProjects(withOrganization(TransactionOverview)));