index.tsx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. import {useCallback, useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {Button, LinkButton} from 'sentry/components/button';
  5. import {CompactSelect} from 'sentry/components/compactSelect';
  6. import type {SelectOption} from 'sentry/components/compactSelect/types';
  7. import Count from 'sentry/components/count';
  8. import {DateTime} from 'sentry/components/dateTime';
  9. import ErrorBoundary from 'sentry/components/errorBoundary';
  10. import SearchBar from 'sentry/components/events/searchBar';
  11. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  12. import IdBadge from 'sentry/components/idBadge';
  13. import * as Layout from 'sentry/components/layouts/thirds';
  14. import Link from 'sentry/components/links/link';
  15. import LoadingIndicator from 'sentry/components/loadingIndicator';
  16. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  17. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  18. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  19. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  20. import PerformanceDuration from 'sentry/components/performanceDuration';
  21. import {AggregateFlamegraph} from 'sentry/components/profiling/flamegraph/aggregateFlamegraph';
  22. import {AggregateFlamegraphTreeTable} from 'sentry/components/profiling/flamegraph/aggregateFlamegraphTreeTable';
  23. import {FlamegraphSearch} from 'sentry/components/profiling/flamegraph/flamegraphToolbar/flamegraphSearch';
  24. import type {ProfilingBreadcrumbsProps} from 'sentry/components/profiling/profilingBreadcrumbs';
  25. import {ProfilingBreadcrumbs} from 'sentry/components/profiling/profilingBreadcrumbs';
  26. import {SegmentedControl} from 'sentry/components/segmentedControl';
  27. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  28. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  29. import SmartSearchBar from 'sentry/components/smartSearchBar';
  30. import {TabList, Tabs} from 'sentry/components/tabs';
  31. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  32. import {IconPanel} from 'sentry/icons';
  33. import {t} from 'sentry/locale';
  34. import {space} from 'sentry/styles/space';
  35. import type {PageFilters} from 'sentry/types/core';
  36. import type {Organization} from 'sentry/types/organization';
  37. import type {Project} from 'sentry/types/project';
  38. import type {DeepPartial} from 'sentry/types/utils';
  39. import {defined} from 'sentry/utils';
  40. import {browserHistory} from 'sentry/utils/browserHistory';
  41. import type EventView from 'sentry/utils/discover/eventView';
  42. import {isAggregateField} from 'sentry/utils/discover/fields';
  43. import type {CanvasScheduler} from 'sentry/utils/profiling/canvasScheduler';
  44. import {
  45. CanvasPoolManager,
  46. useCanvasScheduler,
  47. } from 'sentry/utils/profiling/canvasScheduler';
  48. import type {FlamegraphState} from 'sentry/utils/profiling/flamegraph/flamegraphStateProvider/flamegraphContext';
  49. import {FlamegraphStateProvider} from 'sentry/utils/profiling/flamegraph/flamegraphStateProvider/flamegraphContextProvider';
  50. import {FlamegraphThemeProvider} from 'sentry/utils/profiling/flamegraph/flamegraphThemeProvider';
  51. import type {Frame} from 'sentry/utils/profiling/frame';
  52. import {useAggregateFlamegraphQuery} from 'sentry/utils/profiling/hooks/useAggregateFlamegraphQuery';
  53. import {useCurrentProjectFromRouteParam} from 'sentry/utils/profiling/hooks/useCurrentProjectFromRouteParam';
  54. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  55. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  56. import {generateProfileFlamechartRoute} from 'sentry/utils/profiling/routes';
  57. import {decodeScalar} from 'sentry/utils/queryString';
  58. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  59. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  60. import {useLocation} from 'sentry/utils/useLocation';
  61. import useOrganization from 'sentry/utils/useOrganization';
  62. import usePageFilters from 'sentry/utils/usePageFilters';
  63. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  64. import {
  65. FlamegraphProvider,
  66. useFlamegraph,
  67. } from 'sentry/views/profiling/flamegraphProvider';
  68. import {ProfilesSummaryChart} from 'sentry/views/profiling/landing/profilesSummaryChart';
  69. import {ProfileGroupProvider} from 'sentry/views/profiling/profileGroupProvider';
  70. import type {ProfilingFieldType} from 'sentry/views/profiling/profileSummary/content';
  71. import {ProfilesTable} from 'sentry/views/profiling/profileSummary/profilesTable';
  72. import {DEFAULT_PROFILING_DATETIME_SELECTION} from 'sentry/views/profiling/utils';
  73. import {MostRegressedProfileFunctions} from './regressedProfileFunctions';
  74. import {SlowestProfileFunctions} from './slowestProfileFunctions';
  75. const noop = () => void 0;
  76. function decodeViewOrDefault(
  77. value: string | string[] | null | undefined,
  78. defaultValue: 'flamegraph' | 'profiles'
  79. ): 'flamegraph' | 'profiles' {
  80. if (!value || Array.isArray(value)) {
  81. return defaultValue;
  82. }
  83. if (value === 'flamegraph' || value === 'profiles') {
  84. return value;
  85. }
  86. return defaultValue;
  87. }
  88. const DEFAULT_FLAMEGRAPH_PREFERENCES: DeepPartial<FlamegraphState> = {
  89. preferences: {
  90. sorting: 'alphabetical' satisfies FlamegraphState['preferences']['sorting'],
  91. },
  92. };
  93. interface ProfileSummaryHeaderProps {
  94. location: Location;
  95. onViewChange: (newView: 'flamegraph' | 'profiles') => void;
  96. organization: Organization;
  97. project: Project | null;
  98. query: string;
  99. transaction: string;
  100. view: 'flamegraph' | 'profiles';
  101. }
  102. function ProfileSummaryHeader(props: ProfileSummaryHeaderProps) {
  103. const breadcrumbTrails: ProfilingBreadcrumbsProps['trails'] = useMemo(() => {
  104. return [
  105. {
  106. type: 'landing',
  107. payload: {
  108. query: props.location.query,
  109. },
  110. },
  111. {
  112. type: 'profile summary',
  113. payload: {
  114. projectSlug: props.project?.slug ?? '',
  115. query: props.location.query,
  116. transaction: props.transaction,
  117. },
  118. },
  119. ];
  120. }, [props.location.query, props.project?.slug, props.transaction]);
  121. const transactionSummaryTarget =
  122. props.project &&
  123. props.transaction &&
  124. transactionSummaryRouteWithQuery({
  125. orgSlug: props.organization.slug,
  126. transaction: props.transaction,
  127. projectID: props.project.id,
  128. query: {query: props.query},
  129. });
  130. return (
  131. <ProfilingHeader>
  132. <ProfilingHeaderContent>
  133. <ProfilingBreadcrumbs
  134. organization={props.organization}
  135. trails={breadcrumbTrails}
  136. />
  137. <Layout.Title>
  138. <ProfilingTitleContainer>
  139. {props.project ? (
  140. <IdBadge
  141. hideName
  142. project={props.project}
  143. avatarSize={22}
  144. avatarProps={{hasTooltip: true, tooltip: props.project.slug}}
  145. />
  146. ) : null}
  147. {props.transaction}
  148. </ProfilingTitleContainer>
  149. </Layout.Title>
  150. </ProfilingHeaderContent>
  151. {transactionSummaryTarget && (
  152. <StyledHeaderActions>
  153. <FeedbackWidgetButton />
  154. <LinkButton to={transactionSummaryTarget} size="sm">
  155. {t('View Transaction Summary')}
  156. </LinkButton>
  157. </StyledHeaderActions>
  158. )}
  159. <Tabs onChange={props.onViewChange} value={props.view}>
  160. <TabList hideBorder>
  161. <TabList.Item key="flamegraph">{t('Flamegraph')}</TabList.Item>
  162. <TabList.Item key="profiles">{t('Sampled Profiles')}</TabList.Item>
  163. </TabList>
  164. </Tabs>
  165. </ProfilingHeader>
  166. );
  167. }
  168. const ProfilingHeader = styled(Layout.Header)`
  169. padding: ${space(1)} ${space(2)} ${space(0)} ${space(2)} !important;
  170. `;
  171. const ProfilingHeaderContent = styled(Layout.HeaderContent)`
  172. margin-bottom: ${space(1)};
  173. h1 {
  174. line-height: normal;
  175. }
  176. `;
  177. const StyledHeaderActions = styled(Layout.HeaderActions)`
  178. display: flex;
  179. flex-direction: row;
  180. gap: ${space(1)};
  181. `;
  182. const ProfilingTitleContainer = styled('div')`
  183. display: flex;
  184. align-items: center;
  185. gap: ${space(1)};
  186. font-size: ${p => p.theme.fontSizeLarge};
  187. `;
  188. interface ProfileFiltersProps {
  189. location: Location;
  190. organization: Organization;
  191. projectIds: EventView['project'];
  192. query: string;
  193. selection: PageFilters;
  194. transaction: string | undefined;
  195. usingTransactions: boolean;
  196. }
  197. function ProfileFilters(props: ProfileFiltersProps) {
  198. const filtersQuery = useMemo(() => {
  199. // To avoid querying for the filters each time the query changes,
  200. // do not pass the user query to get the filters.
  201. const search = new MutableSearch('');
  202. if (defined(props.transaction)) {
  203. search.setFilterValues('transaction_name', [props.transaction]);
  204. }
  205. return search.formatString();
  206. }, [props.transaction]);
  207. const profileFilters = useProfileFilters({
  208. query: filtersQuery,
  209. selection: props.selection,
  210. disabled: props.usingTransactions,
  211. });
  212. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  213. (searchQuery: string) => {
  214. browserHistory.push({
  215. ...props.location,
  216. query: {
  217. ...props.location.query,
  218. query: searchQuery || undefined,
  219. cursor: undefined,
  220. },
  221. });
  222. },
  223. [props.location]
  224. );
  225. return (
  226. <ActionBar>
  227. <PageFilterBar condensed>
  228. <EnvironmentPageFilter />
  229. <DatePageFilter />
  230. </PageFilterBar>
  231. {props.usingTransactions ? (
  232. <SearchBar
  233. searchSource="profile_summary"
  234. organization={props.organization}
  235. projectIds={props.projectIds}
  236. query={props.query}
  237. onSearch={handleSearch}
  238. maxQueryLength={MAX_QUERY_LENGTH}
  239. />
  240. ) : (
  241. <SmartSearchBar
  242. organization={props.organization}
  243. hasRecentSearches
  244. projectIds={props.projectIds}
  245. searchSource="profile_summary"
  246. supportedTags={profileFilters}
  247. query={props.query}
  248. onSearch={handleSearch}
  249. maxQueryLength={MAX_QUERY_LENGTH}
  250. />
  251. )}
  252. </ActionBar>
  253. );
  254. }
  255. const ActionBar = styled('div')`
  256. display: grid;
  257. gap: ${space(1)};
  258. grid-template-columns: min-content auto;
  259. padding: ${space(1)} ${space(1)};
  260. background-color: ${p => p.theme.background};
  261. `;
  262. interface ProfileSummaryPageProps {
  263. location: Location;
  264. params: {
  265. projectId?: Project['slug'];
  266. };
  267. selection: PageFilters;
  268. view: 'flamegraph' | 'profile list';
  269. }
  270. function ProfileSummaryPage(props: ProfileSummaryPageProps) {
  271. const organization = useOrganization();
  272. const project = useCurrentProjectFromRouteParam();
  273. const {selection} = usePageFilters();
  274. const profilingUsingTransactions = organization.features.includes(
  275. 'profiling-using-transactions'
  276. );
  277. const transaction = decodeScalar(props.location.query.transaction);
  278. if (!transaction) {
  279. throw new TypeError(
  280. `Profile summary requires a transaction query params, got ${
  281. transaction?.toString() ?? transaction
  282. }`
  283. );
  284. }
  285. const rawQuery = decodeScalar(props.location?.query?.query, '');
  286. const projectIds: number[] = useMemo(() => {
  287. if (!defined(project)) {
  288. return [];
  289. }
  290. const projects = parseInt(project.id, 10);
  291. if (isNaN(projects)) {
  292. return [];
  293. }
  294. return [projects];
  295. }, [project]);
  296. const projectSlugs: string[] = useMemo(() => {
  297. return defined(project) ? [project.slug] : [];
  298. }, [project]);
  299. const query = useMemo(() => {
  300. const search = new MutableSearch(rawQuery);
  301. if (defined(transaction)) {
  302. search.setFilterValues('transaction', [transaction]);
  303. }
  304. // there are no aggregations happening on this page,
  305. // so remove any aggregate filters
  306. Object.keys(search.filters).forEach(field => {
  307. if (isAggregateField(field)) {
  308. search.removeFilter(field);
  309. }
  310. });
  311. return search.formatString();
  312. }, [rawQuery, transaction]);
  313. const {data, isLoading, isError} = useAggregateFlamegraphQuery({
  314. transaction,
  315. environments: selection.environments,
  316. projects: selection.projects,
  317. datetime: selection.datetime,
  318. });
  319. const [visualization, setVisualization] = useLocalStorageState<
  320. 'flamegraph' | 'call tree'
  321. >('flamegraph-visualization', 'flamegraph');
  322. const onVisualizationChange = useCallback(
  323. (value: 'flamegraph' | 'call tree') => {
  324. setVisualization(value);
  325. },
  326. [setVisualization]
  327. );
  328. const [hideRegressions, setHideRegressions] = useLocalStorageState<boolean>(
  329. 'flamegraph-hide-regressions',
  330. false
  331. );
  332. const [frameFilter, setFrameFilter] = useLocalStorageState<
  333. 'system' | 'application' | 'all'
  334. >('flamegraph-frame-filter', 'application');
  335. const onFrameFilterChange = useCallback(
  336. (value: 'system' | 'application' | 'all') => {
  337. setFrameFilter(value);
  338. },
  339. [setFrameFilter]
  340. );
  341. const flamegraphFrameFilter: ((frame: Frame) => boolean) | undefined = useMemo(() => {
  342. if (frameFilter === 'all') {
  343. return () => true;
  344. }
  345. if (frameFilter === 'application') {
  346. return frame => frame.is_application;
  347. }
  348. return frame => !frame.is_application;
  349. }, [frameFilter]);
  350. const canvasPoolManager = useMemo(() => new CanvasPoolManager(), []);
  351. const scheduler = useCanvasScheduler(canvasPoolManager);
  352. const location = useLocation();
  353. const [view, setView] = useState<'flamegraph' | 'profiles'>(
  354. decodeViewOrDefault(location.query.view, 'flamegraph')
  355. );
  356. useEffect(() => {
  357. const newView = decodeViewOrDefault(location.query.view, 'flamegraph');
  358. if (newView !== view) {
  359. setView(decodeViewOrDefault(location.query.view, 'flamegraph'));
  360. }
  361. }, [location.query.view, view]);
  362. const onSetView = useCallback(
  363. (newView: 'flamegraph' | 'profiles') => {
  364. setView(newView);
  365. browserHistory.push({
  366. ...location,
  367. query: {
  368. ...location.query,
  369. view: newView,
  370. },
  371. });
  372. },
  373. [location]
  374. );
  375. const onHideRegressionsClick = useCallback(() => {
  376. return setHideRegressions(!hideRegressions);
  377. }, [hideRegressions, setHideRegressions]);
  378. return (
  379. <SentryDocumentTitle
  380. title={t('Profiling \u2014 Profile Summary')}
  381. orgSlug={organization.slug}
  382. >
  383. <ProfileSummaryContainer>
  384. <PageFiltersContainer
  385. shouldForceProject={defined(project)}
  386. forceProject={project}
  387. specificProjectSlugs={projectSlugs}
  388. defaultSelection={
  389. profilingUsingTransactions
  390. ? {datetime: DEFAULT_PROFILING_DATETIME_SELECTION}
  391. : undefined
  392. }
  393. >
  394. <ProfileSummaryHeader
  395. view={view}
  396. onViewChange={onSetView}
  397. organization={organization}
  398. location={props.location}
  399. project={project}
  400. query={query}
  401. transaction={transaction}
  402. />
  403. <ProfileFilters
  404. projectIds={projectIds}
  405. organization={organization}
  406. location={props.location}
  407. query={rawQuery}
  408. selection={props.selection}
  409. transaction={transaction}
  410. usingTransactions={profilingUsingTransactions}
  411. />
  412. <ProfilesSummaryChart
  413. referrer="api.profiling.profile-summary-chart"
  414. query={query}
  415. hideCount
  416. />
  417. {view === 'profiles' ? (
  418. <ProfilesTable />
  419. ) : (
  420. <ProfileVisualizationContainer hideRegressions={hideRegressions}>
  421. <ProfileVisualization>
  422. <ProfileGroupProvider
  423. traceID=""
  424. type="flamegraph"
  425. input={data ?? null}
  426. frameFilter={flamegraphFrameFilter}
  427. >
  428. <FlamegraphStateProvider initialState={DEFAULT_FLAMEGRAPH_PREFERENCES}>
  429. <FlamegraphThemeProvider>
  430. <FlamegraphProvider>
  431. <AggregateFlamegraphContainer>
  432. <AggregateFlamegraphToolbar
  433. scheduler={scheduler}
  434. canvasPoolManager={canvasPoolManager}
  435. visualization={visualization}
  436. onVisualizationChange={onVisualizationChange}
  437. frameFilter={frameFilter}
  438. onFrameFilterChange={onFrameFilterChange}
  439. hideSystemFrames={false}
  440. setHideSystemFrames={noop}
  441. onHideRegressionsClick={onHideRegressionsClick}
  442. />
  443. {isLoading ? (
  444. <RequestStateMessageContainer>
  445. <LoadingIndicator />
  446. </RequestStateMessageContainer>
  447. ) : isError ? (
  448. <RequestStateMessageContainer>
  449. {t('There was an error loading the flamegraph.')}
  450. </RequestStateMessageContainer>
  451. ) : null}
  452. {visualization === 'flamegraph' ? (
  453. <AggregateFlamegraph
  454. canvasPoolManager={canvasPoolManager}
  455. scheduler={scheduler}
  456. />
  457. ) : (
  458. <AggregateFlamegraphTreeTable
  459. recursion={null}
  460. expanded={false}
  461. frameFilter={frameFilter}
  462. canvasPoolManager={canvasPoolManager}
  463. />
  464. )}
  465. </AggregateFlamegraphContainer>
  466. </FlamegraphProvider>
  467. </FlamegraphThemeProvider>
  468. </FlamegraphStateProvider>
  469. </ProfileGroupProvider>
  470. </ProfileVisualization>
  471. {hideRegressions ? null : (
  472. <ProfileDigestContainer>
  473. <ProfileDigestScrollContainer>
  474. <ProfileDigest onViewChange={onSetView} transaction={transaction} />
  475. <MostRegressedProfileFunctions transaction={transaction} />
  476. <SlowestProfileFunctions transaction={transaction} />
  477. </ProfileDigestScrollContainer>
  478. </ProfileDigestContainer>
  479. )}
  480. </ProfileVisualizationContainer>
  481. )}
  482. </PageFiltersContainer>
  483. </ProfileSummaryContainer>
  484. </SentryDocumentTitle>
  485. );
  486. }
  487. const RequestStateMessageContainer = styled('div')`
  488. position: absolute;
  489. left: 0;
  490. right: 0;
  491. top: 0;
  492. bottom: 0;
  493. display: flex;
  494. justify-content: center;
  495. align-items: center;
  496. color: ${p => p.theme.subText};
  497. `;
  498. const AggregateFlamegraphContainer = styled('div')`
  499. display: flex;
  500. flex-direction: column;
  501. flex: 1 1 100%;
  502. height: 100%;
  503. width: 100%;
  504. overflow: hidden;
  505. position: absolute;
  506. left: 0px;
  507. top: 0px;
  508. `;
  509. interface AggregateFlamegraphToolbarProps {
  510. canvasPoolManager: CanvasPoolManager;
  511. frameFilter: 'system' | 'application' | 'all';
  512. hideSystemFrames: boolean;
  513. onFrameFilterChange: (value: 'system' | 'application' | 'all') => void;
  514. onHideRegressionsClick: () => void;
  515. onVisualizationChange: (value: 'flamegraph' | 'call tree') => void;
  516. scheduler: CanvasScheduler;
  517. setHideSystemFrames: (value: boolean) => void;
  518. visualization: 'flamegraph' | 'call tree';
  519. }
  520. function AggregateFlamegraphToolbar(props: AggregateFlamegraphToolbarProps) {
  521. const flamegraph = useFlamegraph();
  522. const flamegraphs = useMemo(() => [flamegraph], [flamegraph]);
  523. const spans = useMemo(() => [], []);
  524. const frameSelectOptions: SelectOption<'system' | 'application' | 'all'>[] =
  525. useMemo(() => {
  526. return [
  527. {value: 'system', label: t('System Frames')},
  528. {value: 'application', label: t('Application Frames')},
  529. {value: 'all', label: t('All Frames')},
  530. ];
  531. }, []);
  532. const onResetZoom = useCallback(() => {
  533. props.scheduler.dispatch('reset zoom');
  534. }, [props.scheduler]);
  535. const onFrameFilterChange = useCallback(
  536. (value: {value: 'application' | 'system' | 'all'}) => {
  537. props.onFrameFilterChange(value.value);
  538. },
  539. [props]
  540. );
  541. return (
  542. <AggregateFlamegraphToolbarContainer>
  543. <ViewSelectContainer>
  544. <SegmentedControl
  545. aria-label={t('View')}
  546. size="xs"
  547. value={props.visualization}
  548. onChange={props.onVisualizationChange}
  549. >
  550. <SegmentedControl.Item key="flamegraph">
  551. {t('Flamegraph')}
  552. </SegmentedControl.Item>
  553. <SegmentedControl.Item key="call tree">{t('Call Tree')}</SegmentedControl.Item>
  554. </SegmentedControl>
  555. </ViewSelectContainer>
  556. <AggregateFlamegraphSearch
  557. spans={spans}
  558. canvasPoolManager={props.canvasPoolManager}
  559. flamegraphs={flamegraphs}
  560. />
  561. <Button size="xs" onClick={onResetZoom}>
  562. {t('Reset Zoom')}
  563. </Button>
  564. <CompactSelect
  565. onChange={onFrameFilterChange}
  566. value={props.frameFilter}
  567. size="xs"
  568. options={frameSelectOptions}
  569. />
  570. <Button
  571. size="xs"
  572. onClick={props.onHideRegressionsClick}
  573. title={t('Expand or collapse the view')}
  574. >
  575. <IconPanel size="xs" direction="right" />
  576. </Button>
  577. </AggregateFlamegraphToolbarContainer>
  578. );
  579. }
  580. const ViewSelectContainer = styled('div')`
  581. min-width: 160px;
  582. `;
  583. const AggregateFlamegraphToolbarContainer = styled('div')`
  584. display: flex;
  585. justify-content: space-between;
  586. gap: ${space(1)};
  587. padding: ${space(1)} ${space(0.5)};
  588. background-color: ${p => p.theme.background};
  589. /*
  590. force height to be the same as profile digest header,
  591. but subtract 1px for the border that doesnt exist on the header
  592. */
  593. height: 41px;
  594. `;
  595. const AggregateFlamegraphSearch = styled(FlamegraphSearch)`
  596. max-width: 300px;
  597. `;
  598. const ProfileVisualization = styled('div')`
  599. grid-area: visualization;
  600. position: relative;
  601. height: 100%;
  602. `;
  603. const ProfileDigestContainer = styled('div')`
  604. grid-area: digest;
  605. border-left: 1px solid ${p => p.theme.border};
  606. background-color: ${p => p.theme.background};
  607. display: flex;
  608. flex: 1 1 100%;
  609. flex-direction: column;
  610. position: relative;
  611. overflow: hidden;
  612. `;
  613. const ProfileDigestScrollContainer = styled('div')`
  614. position: absolute;
  615. left: 0;
  616. right: 0;
  617. top: 0;
  618. bottom: 0;
  619. display: flex;
  620. flex-direction: column;
  621. `;
  622. const ProfileVisualizationContainer = styled('div')<{hideRegressions}>`
  623. display: grid;
  624. /* false positive for grid layout */
  625. /* stylelint-disable */
  626. grid-template-areas: ${p =>
  627. p.hideRegressions ? "'visualization'" : "'visualization digest'"};
  628. grid-template-columns: ${p => (p.hideRegressions ? `100%` : `60% 40%`)};
  629. flex: 1 1 100%;
  630. `;
  631. const ProfileSummaryContainer = styled('div')`
  632. display: flex;
  633. flex-direction: column;
  634. flex: 1 1 100%;
  635. /*
  636. * The footer component is a sibling of this div.
  637. * Remove it so the flamegraph can take up the
  638. * entire screen.
  639. */
  640. ~ footer {
  641. display: none;
  642. }
  643. `;
  644. const PROFILE_DIGEST_FIELDS = [
  645. 'last_seen()',
  646. 'p75()',
  647. 'p95()',
  648. 'p99()',
  649. 'count()',
  650. ] satisfies ProfilingFieldType[];
  651. const percentiles = ['p75()', 'p95()', 'p99()'] as const;
  652. interface ProfileDigestProps {
  653. onViewChange: (newView: 'flamegraph' | 'profiles') => void;
  654. transaction: string;
  655. }
  656. function ProfileDigest(props: ProfileDigestProps) {
  657. const location = useLocation();
  658. const organization = useOrganization();
  659. const project = useCurrentProjectFromRouteParam();
  660. const query = useMemo(() => {
  661. const conditions = new MutableSearch('');
  662. conditions.setFilterValues('transaction', [props.transaction]);
  663. return conditions.formatString();
  664. }, [props.transaction]);
  665. const profilesCursor = useMemo(
  666. () => decodeScalar(location.query.cursor),
  667. [location.query.cursor]
  668. );
  669. const profiles = useProfileEvents<ProfilingFieldType>({
  670. cursor: profilesCursor,
  671. fields: PROFILE_DIGEST_FIELDS,
  672. query,
  673. sort: {key: 'last_seen()', order: 'desc'},
  674. referrer: 'api.profiling.profile-summary-table',
  675. });
  676. const data = profiles.data?.data?.[0];
  677. const latestProfile = useProfileEvents<ProfilingFieldType>({
  678. cursor: profilesCursor,
  679. fields: ['profile.id', 'timestamp'],
  680. query: '',
  681. sort: {key: 'timestamp', order: 'desc'},
  682. limit: 1,
  683. referrer: 'api.profiling.profile-summary-table',
  684. });
  685. const profile = latestProfile.data?.data?.[0];
  686. const flamegraphTarget =
  687. project && profile
  688. ? generateProfileFlamechartRoute({
  689. orgSlug: organization.slug,
  690. projectSlug: project.slug,
  691. profileId: profile?.['profile.id'] as string,
  692. })
  693. : undefined;
  694. return (
  695. <ProfileDigestHeader>
  696. <div>
  697. <ProfileDigestLabel>{t('Last Seen')}</ProfileDigestLabel>
  698. <div>
  699. {profiles.isLoading ? (
  700. ''
  701. ) : profiles.isError ? (
  702. ''
  703. ) : flamegraphTarget ? (
  704. <Link to={flamegraphTarget}>
  705. <DateTime date={new Date(data?.['last_seen()'] as string)} />
  706. </Link>
  707. ) : (
  708. <DateTime date={new Date(data?.['last_seen()'] as string)} />
  709. )}
  710. </div>
  711. </div>
  712. {percentiles.map(p => {
  713. return (
  714. <ProfileDigestColumn key={p}>
  715. <ProfileDigestLabel>{p}</ProfileDigestLabel>
  716. <div>
  717. {profiles.isLoading ? (
  718. ''
  719. ) : profiles.isError ? (
  720. ''
  721. ) : (
  722. <PerformanceDuration nanoseconds={data?.[p] as number} abbreviation />
  723. )}
  724. </div>
  725. </ProfileDigestColumn>
  726. );
  727. })}
  728. <ProfileDigestColumn>
  729. <ProfileDigestLabel>{t('profiles')}</ProfileDigestLabel>
  730. <div>
  731. {profiles.isLoading ? (
  732. ''
  733. ) : profiles.isError ? (
  734. ''
  735. ) : (
  736. <Link onClick={() => props.onViewChange('profiles')} to="">
  737. <Count value={data?.['count()'] as number} />
  738. </Link>
  739. )}
  740. </div>
  741. </ProfileDigestColumn>
  742. </ProfileDigestHeader>
  743. );
  744. }
  745. const ProfileDigestColumn = styled('div')`
  746. text-align: right;
  747. `;
  748. const ProfileDigestHeader = styled('div')`
  749. display: flex;
  750. justify-content: space-between;
  751. align-items: center;
  752. padding: 0 ${space(1)};
  753. border-bottom: 1px solid ${p => p.theme.border};
  754. /* force height to be same as toolbar */
  755. height: 42px;
  756. flex-shrink: 0;
  757. `;
  758. const ProfileDigestLabel = styled('span')`
  759. color: ${p => p.theme.textColor};
  760. font-size: ${p => p.theme.fontSizeSmall};
  761. font-weight: ${p => p.theme.fontWeightBold};
  762. text-transform: uppercase;
  763. `;
  764. export default function ProfileSummaryPageToggle(props: ProfileSummaryPageProps) {
  765. return (
  766. <ProfileSummaryContainer data-test-id="profile-summary-redesign">
  767. <ErrorBoundary>
  768. <ProfileSummaryPage {...props} />
  769. </ErrorBoundary>
  770. </ProfileSummaryContainer>
  771. );
  772. }