index.tsx 26 KB

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