index.tsx 27 KB

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