index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import {useCallback, useMemo} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import {LinkButton} from 'sentry/components/button';
  6. import DatePageFilter from 'sentry/components/datePageFilter';
  7. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  8. import ErrorBoundary from 'sentry/components/errorBoundary';
  9. import SearchBar from 'sentry/components/events/searchBar';
  10. import IdBadge from 'sentry/components/idBadge';
  11. import * as Layout from 'sentry/components/layouts/thirds';
  12. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  13. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  14. import {AggregateFlamegraph} from 'sentry/components/profiling/flamegraph/aggregateFlamegraph';
  15. import {
  16. ProfilingBreadcrumbs,
  17. ProfilingBreadcrumbsProps,
  18. } from 'sentry/components/profiling/profilingBreadcrumbs';
  19. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  20. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  21. import SmartSearchBar from 'sentry/components/smartSearchBar';
  22. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  23. import {t} from 'sentry/locale';
  24. import {space} from 'sentry/styles/space';
  25. import type {Organization, PageFilters, Project} from 'sentry/types';
  26. import {defined} from 'sentry/utils';
  27. import EventView from 'sentry/utils/discover/eventView';
  28. import {isAggregateField} from 'sentry/utils/discover/fields';
  29. import {FlamegraphStateProvider} from 'sentry/utils/profiling/flamegraph/flamegraphStateProvider/flamegraphContextProvider';
  30. import {FlamegraphThemeProvider} from 'sentry/utils/profiling/flamegraph/flamegraphThemeProvider';
  31. import {useAggregateFlamegraphQuery} from 'sentry/utils/profiling/hooks/useAggregateFlamegraphQuery';
  32. import {useCurrentProjectFromRouteParam} from 'sentry/utils/profiling/hooks/useCurrentProjectFromRouteParam';
  33. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  34. import {decodeScalar} from 'sentry/utils/queryString';
  35. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  36. import useOrganization from 'sentry/utils/useOrganization';
  37. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  38. import {ProfilesSummaryChart} from 'sentry/views/profiling/landing/profilesSummaryChart';
  39. import {ProfileGroupProvider} from 'sentry/views/profiling/profileGroupProvider';
  40. import {LegacySummaryPage} from 'sentry/views/profiling/profileSummary/legacySummaryPage';
  41. import {DEFAULT_PROFILING_DATETIME_SELECTION} from 'sentry/views/profiling/utils';
  42. import {MostRegressedProfileFunctions} from './regressedProfileFunctions';
  43. import {SlowestProfileFunctions} from './slowestProfileFunctions';
  44. interface ProfileSummaryHeaderProps {
  45. location: Location;
  46. organization: Organization;
  47. project: Project | null;
  48. query: string;
  49. transaction: string;
  50. }
  51. function ProfileSummaryHeader(props: ProfileSummaryHeaderProps) {
  52. const breadcrumbTrails: ProfilingBreadcrumbsProps['trails'] = useMemo(() => {
  53. return [
  54. {
  55. type: 'landing',
  56. payload: {
  57. query: props.location.query,
  58. },
  59. },
  60. {
  61. type: 'profile summary',
  62. payload: {
  63. projectSlug: props.project?.slug ?? '',
  64. query: props.location.query,
  65. transaction: props.transaction,
  66. },
  67. },
  68. ];
  69. }, [props.location.query, props.project?.slug, props.transaction]);
  70. const transactionSummaryTarget =
  71. props.project &&
  72. props.transaction &&
  73. transactionSummaryRouteWithQuery({
  74. orgSlug: props.organization.slug,
  75. transaction: props.transaction,
  76. projectID: props.project.id,
  77. query: {query: props.query},
  78. });
  79. return (
  80. <Layout.Header>
  81. <Layout.HeaderContent>
  82. <ProfilingBreadcrumbs
  83. organization={props.organization}
  84. trails={breadcrumbTrails}
  85. />
  86. <Layout.Title>
  87. {props.project ? (
  88. <IdBadge
  89. hideName
  90. project={props.project}
  91. avatarSize={28}
  92. avatarProps={{hasTooltip: true, tooltip: props.project.slug}}
  93. />
  94. ) : null}
  95. {props.transaction}
  96. </Layout.Title>
  97. </Layout.HeaderContent>
  98. {transactionSummaryTarget && (
  99. <Layout.HeaderActions>
  100. <LinkButton to={transactionSummaryTarget} size="sm">
  101. {t('View Transaction Summary')}
  102. </LinkButton>
  103. </Layout.HeaderActions>
  104. )}
  105. </Layout.Header>
  106. );
  107. }
  108. interface ProfileFiltersProps {
  109. location: Location;
  110. organization: Organization;
  111. projectIds: EventView['project'];
  112. query: string;
  113. selection: PageFilters;
  114. transaction: string | undefined;
  115. usingTransactions: boolean;
  116. }
  117. function ProfileFilters(props: ProfileFiltersProps) {
  118. const filtersQuery = useMemo(() => {
  119. // To avoid querying for the filters each time the query changes,
  120. // do not pass the user query to get the filters.
  121. const search = new MutableSearch('');
  122. if (defined(props.transaction)) {
  123. search.setFilterValues('transaction_name', [props.transaction]);
  124. }
  125. return search.formatString();
  126. }, [props.transaction]);
  127. const profileFilters = useProfileFilters({
  128. query: filtersQuery,
  129. selection: props.selection,
  130. disabled: props.usingTransactions,
  131. });
  132. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  133. (searchQuery: string) => {
  134. browserHistory.push({
  135. ...props.location,
  136. query: {
  137. ...props.location.query,
  138. query: searchQuery || undefined,
  139. cursor: undefined,
  140. },
  141. });
  142. },
  143. [props.location]
  144. );
  145. return (
  146. <ActionBar>
  147. <PageFilterBar condensed>
  148. <EnvironmentPageFilter />
  149. <DatePageFilter alignDropdown="left" />
  150. </PageFilterBar>
  151. {props.usingTransactions ? (
  152. <SearchBar
  153. searchSource="profile_summary"
  154. organization={props.organization}
  155. projectIds={props.projectIds}
  156. query={props.query}
  157. onSearch={handleSearch}
  158. maxQueryLength={MAX_QUERY_LENGTH}
  159. />
  160. ) : (
  161. <SmartSearchBar
  162. organization={props.organization}
  163. hasRecentSearches
  164. searchSource="profile_summary"
  165. supportedTags={profileFilters}
  166. query={props.query}
  167. onSearch={handleSearch}
  168. maxQueryLength={MAX_QUERY_LENGTH}
  169. />
  170. )}
  171. </ActionBar>
  172. );
  173. }
  174. const ActionBar = styled('div')`
  175. display: grid;
  176. gap: ${space(2)};
  177. grid-template-columns: min-content auto;
  178. margin-bottom: ${space(2)};
  179. `;
  180. interface ProfileSummaryPageProps {
  181. location: Location;
  182. params: {
  183. projectId?: Project['slug'];
  184. };
  185. selection: PageFilters;
  186. }
  187. function ProfileSummaryPage(props: ProfileSummaryPageProps) {
  188. const organization = useOrganization();
  189. const project = useCurrentProjectFromRouteParam();
  190. const profilingUsingTransactions = organization.features.includes(
  191. 'profiling-using-transactions'
  192. );
  193. const transaction = decodeScalar(props.location.query.transaction);
  194. if (!transaction) {
  195. throw new TypeError(
  196. `Profile summary requires a transaction query params, got ${
  197. transaction?.toString() ?? transaction
  198. }`
  199. );
  200. }
  201. const rawQuery = decodeScalar(props.location?.query?.query, '');
  202. const projectIds: number[] = useMemo(() => {
  203. if (!defined(project)) {
  204. return [];
  205. }
  206. const projects = parseInt(project.id, 10);
  207. if (isNaN(projects)) {
  208. return [];
  209. }
  210. return [projects];
  211. }, [project]);
  212. const projectSlugs: string[] = useMemo(() => {
  213. return defined(project) ? [project.slug] : [];
  214. }, [project]);
  215. const query = useMemo(() => {
  216. const search = new MutableSearch(rawQuery);
  217. if (defined(transaction)) {
  218. search.setFilterValues('transaction', [transaction]);
  219. }
  220. // there are no aggregations happening on this page,
  221. // so remove any aggregate filters
  222. Object.keys(search.filters).forEach(field => {
  223. if (isAggregateField(field)) {
  224. search.removeFilter(field);
  225. }
  226. });
  227. return search.formatString();
  228. }, [rawQuery, transaction]);
  229. const {data} = useAggregateFlamegraphQuery({transaction});
  230. return (
  231. <SentryDocumentTitle
  232. title={t('Profiling \u2014 Profile Summary')}
  233. orgSlug={organization.slug}
  234. >
  235. <ProfileSummaryContainer>
  236. <PageFiltersContainer
  237. shouldForceProject={defined(project)}
  238. forceProject={project}
  239. specificProjectSlugs={projectSlugs}
  240. defaultSelection={
  241. profilingUsingTransactions
  242. ? {datetime: DEFAULT_PROFILING_DATETIME_SELECTION}
  243. : undefined
  244. }
  245. >
  246. <ProfileSummaryHeader
  247. organization={organization}
  248. location={props.location}
  249. project={project}
  250. query={query}
  251. transaction={transaction}
  252. />
  253. <ProfileFilters
  254. projectIds={projectIds}
  255. organization={organization}
  256. location={props.location}
  257. query={rawQuery}
  258. selection={props.selection}
  259. transaction={transaction}
  260. usingTransactions={profilingUsingTransactions}
  261. />
  262. <ProfilesSummaryChart
  263. referrer="api.profiling.profile-summary-chart"
  264. query={query}
  265. hideCount
  266. />
  267. <ProfileVisualizationContainer>
  268. <ProfileVisualization>
  269. <ProfileGroupProvider
  270. type="flamegraph"
  271. input={data ?? null}
  272. traceID=""
  273. frameFilter={undefined}
  274. >
  275. <FlamegraphStateProvider
  276. initialState={{
  277. preferences: {
  278. sorting: 'alphabetical',
  279. },
  280. }}
  281. >
  282. <FlamegraphThemeProvider>
  283. <AggregateFlamegraph
  284. hideToolbar
  285. hideSystemFrames={false}
  286. setHideSystemFrames={() => void 0}
  287. />
  288. </FlamegraphThemeProvider>
  289. </FlamegraphStateProvider>
  290. </ProfileGroupProvider>
  291. </ProfileVisualization>
  292. <ProfileDigest>
  293. <MostRegressedProfileFunctions transaction={transaction} />
  294. <SlowestProfileFunctions transaction={transaction} />
  295. </ProfileDigest>
  296. </ProfileVisualizationContainer>
  297. </PageFiltersContainer>
  298. </ProfileSummaryContainer>
  299. </SentryDocumentTitle>
  300. );
  301. }
  302. const ProfileVisualization = styled('div')`
  303. grid-area: visualization;
  304. `;
  305. const ProfileDigest = styled('div')`
  306. grid-area: digest;
  307. `;
  308. const ProfileVisualizationContainer = styled('div')`
  309. display: grid;
  310. grid-template-areas: 'visualization digest';
  311. flex: 1 1 100%;
  312. `;
  313. const ProfileSummaryContainer = styled('div')`
  314. display: flex;
  315. flex-direction: column;
  316. flex: 1 1 100%;
  317. /*
  318. * The footer component is a sibling of this div.
  319. * Remove it so the flamegraph can take up the
  320. * entire screen.
  321. */
  322. ~ footer {
  323. display: none;
  324. }
  325. `;
  326. export default function ProfileSummaryPageToggle(props: ProfileSummaryPageProps) {
  327. const organization = useOrganization();
  328. if (organization.features.includes('profiling-summary-redesign')) {
  329. return (
  330. <ProfileSummaryContainer data-test-id="profile-summary-redesign">
  331. <ErrorBoundary>
  332. <ProfileSummaryPage {...props} />
  333. </ErrorBoundary>
  334. </ProfileSummaryContainer>
  335. );
  336. }
  337. return (
  338. <div data-test-id="profile-summary-legacy">
  339. <LegacySummaryPage {...props} />
  340. </div>
  341. );
  342. }