index.tsx 25 KB

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