index.tsx 25 KB

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