index.tsx 26 KB

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