index.tsx 9.3 KB

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