index.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import {Fragment, useCallback, useEffect, useMemo} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import DatePageFilter from 'sentry/components/datePageFilter';
  6. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  7. import SearchBar from 'sentry/components/events/searchBar';
  8. import IdBadge from 'sentry/components/idBadge';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  11. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  12. import {
  13. ProfilingBreadcrumbs,
  14. ProfilingBreadcrumbsProps,
  15. } from 'sentry/components/profiling/profilingBreadcrumbs';
  16. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  17. import SmartSearchBar, {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  18. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  19. import {t} from 'sentry/locale';
  20. import {space} from 'sentry/styles/space';
  21. import {PageFilters, Project} from 'sentry/types';
  22. import {defined, generateQueryWithTag} from 'sentry/utils';
  23. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  24. import EventView from 'sentry/utils/discover/eventView';
  25. import {formatTagKey, isAggregateField} from 'sentry/utils/discover/fields';
  26. import {useCurrentProjectFromRouteParam} from 'sentry/utils/profiling/hooks/useCurrentProjectFromRouteParam';
  27. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  28. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  29. import {decodeScalar} from 'sentry/utils/queryString';
  30. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  31. import useOrganization from 'sentry/utils/useOrganization';
  32. import withPageFilters from 'sentry/utils/withPageFilters';
  33. import Tags from 'sentry/views/discover/tags';
  34. import {DEFAULT_PROFILING_DATETIME_SELECTION} from 'sentry/views/profiling/utils';
  35. import {ProfileSummaryContent} from './content';
  36. interface ProfileSummaryPageProps {
  37. location: Location;
  38. params: {
  39. projectId?: Project['slug'];
  40. };
  41. selection: PageFilters;
  42. }
  43. function ProfileSummaryPage(props: ProfileSummaryPageProps) {
  44. const organization = useOrganization();
  45. const project = useCurrentProjectFromRouteParam();
  46. const profilingUsingTransactions = organization.features.includes(
  47. 'profiling-using-transactions'
  48. );
  49. useEffect(() => {
  50. trackAdvancedAnalyticsEvent('profiling_views.profile_summary', {
  51. organization,
  52. project_platform: project?.platform,
  53. project_id: project?.id,
  54. });
  55. // ignore currentProject so we don't block the analytics event
  56. // or fire more than once unnecessarily
  57. // eslint-disable-next-line react-hooks/exhaustive-deps
  58. }, [organization]);
  59. const transaction = decodeScalar(props.location.query.transaction);
  60. const rawQuery = useMemo(
  61. () => decodeScalar(props.location.query.query, ''),
  62. [props.location.query.query]
  63. );
  64. const query = useMemo(() => {
  65. const search = new MutableSearch(rawQuery);
  66. if (defined(transaction)) {
  67. search.setFilterValues('transaction', [transaction]);
  68. }
  69. // there are no aggregations happening on this page,
  70. // so remove any aggregate filters
  71. Object.keys(search.filters).forEach(field => {
  72. if (isAggregateField(field)) {
  73. search.removeFilter(field);
  74. }
  75. });
  76. return search.formatString();
  77. }, [rawQuery, transaction]);
  78. const profilesAggregateQuery = useProfileEvents<'count()'>({
  79. fields: ['count()'],
  80. sort: {key: 'count()', order: 'desc'},
  81. referrer: 'api.profiling.profile-summary-table', // TODO
  82. query,
  83. enabled: profilingUsingTransactions,
  84. });
  85. const profilesCount = useMemo(() => {
  86. if (profilesAggregateQuery.status !== 'success') {
  87. return null;
  88. }
  89. return (profilesAggregateQuery.data?.[0]?.data?.[0]?.['count()'] as number) || null;
  90. }, [profilesAggregateQuery]);
  91. const filtersQuery = useMemo(() => {
  92. // To avoid querying for the filters each time the query changes,
  93. // do not pass the user query to get the filters.
  94. const search = new MutableSearch('');
  95. if (defined(transaction)) {
  96. search.setFilterValues('transaction_name', [transaction]);
  97. }
  98. return search.formatString();
  99. }, [transaction]);
  100. const profileFilters = useProfileFilters({
  101. query: filtersQuery,
  102. selection: props.selection,
  103. disabled: profilingUsingTransactions,
  104. });
  105. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  106. (searchQuery: string) => {
  107. browserHistory.push({
  108. ...props.location,
  109. query: {
  110. ...props.location.query,
  111. query: searchQuery || undefined,
  112. cursor: undefined,
  113. },
  114. });
  115. },
  116. [props.location]
  117. );
  118. const breadcrumbTrails: ProfilingBreadcrumbsProps['trails'] = useMemo(() => {
  119. return [
  120. {
  121. type: 'landing',
  122. payload: {
  123. query: props.location.query,
  124. },
  125. },
  126. {
  127. type: 'profile summary',
  128. payload: {
  129. projectSlug: project?.slug ?? '',
  130. query: props.location.query,
  131. transaction: transaction ?? '',
  132. },
  133. },
  134. ];
  135. }, [props.location.query, project?.slug, transaction]);
  136. const eventView = useMemo(() => {
  137. const _eventView = EventView.fromNewQueryWithLocation(
  138. {
  139. id: undefined,
  140. version: 2,
  141. name: transaction || '',
  142. fields: [],
  143. query,
  144. projects: project ? [parseInt(project.id, 10)] : [],
  145. },
  146. props.location
  147. );
  148. _eventView.additionalConditions.setFilterValues('has', ['profile.id']);
  149. return _eventView;
  150. }, [props.location, project, query, transaction]);
  151. function generateTagUrl(key: string, value: string) {
  152. return {
  153. ...props.location,
  154. query: generateQueryWithTag(props.location.query, {key: formatTagKey(key), value}),
  155. };
  156. }
  157. return (
  158. <SentryDocumentTitle
  159. title={t('Profiling \u2014 Profile Summary')}
  160. orgSlug={organization.slug}
  161. >
  162. <PageFiltersContainer
  163. shouldForceProject={defined(project)}
  164. forceProject={project}
  165. specificProjectSlugs={defined(project) ? [project.slug] : []}
  166. defaultSelection={
  167. profilingUsingTransactions
  168. ? {datetime: DEFAULT_PROFILING_DATETIME_SELECTION}
  169. : undefined
  170. }
  171. >
  172. <Layout.Page>
  173. {project && transaction && (
  174. <Fragment>
  175. <Layout.Header>
  176. <Layout.HeaderContent>
  177. <ProfilingBreadcrumbs
  178. organization={organization}
  179. trails={breadcrumbTrails}
  180. />
  181. <Layout.Title>
  182. {project ? (
  183. <IdBadge
  184. project={project}
  185. avatarSize={28}
  186. hideName
  187. avatarProps={{hasTooltip: true, tooltip: project.slug}}
  188. />
  189. ) : null}
  190. {transaction}
  191. </Layout.Title>
  192. </Layout.HeaderContent>
  193. </Layout.Header>
  194. <Layout.Body>
  195. <Layout.Main fullWidth={!profilingUsingTransactions}>
  196. <ActionBar>
  197. <PageFilterBar condensed>
  198. <EnvironmentPageFilter />
  199. <DatePageFilter alignDropdown="left" />
  200. </PageFilterBar>
  201. {profilingUsingTransactions ? (
  202. <SearchBar
  203. searchSource="profile_summary"
  204. organization={organization}
  205. projectIds={eventView.project}
  206. query={rawQuery}
  207. onSearch={handleSearch}
  208. maxQueryLength={MAX_QUERY_LENGTH}
  209. />
  210. ) : (
  211. <SmartSearchBar
  212. organization={organization}
  213. hasRecentSearches
  214. searchSource="profile_summary"
  215. supportedTags={profileFilters}
  216. query={rawQuery}
  217. onSearch={handleSearch}
  218. maxQueryLength={MAX_QUERY_LENGTH}
  219. />
  220. )}
  221. </ActionBar>
  222. <ProfileSummaryContent
  223. location={props.location}
  224. project={project}
  225. selection={props.selection}
  226. transaction={transaction}
  227. query={query}
  228. />
  229. </Layout.Main>
  230. {profilingUsingTransactions && (
  231. <Layout.Side>
  232. <Tags
  233. generateUrl={generateTagUrl}
  234. totalValues={profilesCount}
  235. eventView={eventView}
  236. organization={organization}
  237. location={props.location}
  238. />
  239. </Layout.Side>
  240. )}
  241. </Layout.Body>
  242. </Fragment>
  243. )}
  244. </Layout.Page>
  245. </PageFiltersContainer>
  246. </SentryDocumentTitle>
  247. );
  248. }
  249. const ActionBar = styled('div')`
  250. display: grid;
  251. gap: ${space(2)};
  252. grid-template-columns: min-content auto;
  253. margin-bottom: ${space(2)};
  254. `;
  255. export default withPageFilters(ProfileSummaryPage);