index.tsx 27 KB

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