pageOverview.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import {useMemo, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import omit from 'lodash/omit';
  5. import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
  6. import Breadcrumbs from 'sentry/components/breadcrumbs';
  7. import {LinkButton} from 'sentry/components/button';
  8. import {AggregateSpans} from 'sentry/components/events/interfaces/spans/aggregateSpans';
  9. import FeedbackWidget from 'sentry/components/feedback/widget/feedbackWidget';
  10. import {COL_WIDTH_UNDEFINED, GridColumnOrder} from 'sentry/components/gridEditable';
  11. import * as Layout from 'sentry/components/layouts/thirds';
  12. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  13. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  14. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  15. import {TabList, Tabs} from 'sentry/components/tabs';
  16. import {IconChevron} from 'sentry/icons';
  17. import {t} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import {defined} from 'sentry/utils';
  20. import {decodeScalar} from 'sentry/utils/queryString';
  21. import {useLocation} from 'sentry/utils/useLocation';
  22. import useOrganization from 'sentry/utils/useOrganization';
  23. import useProjects from 'sentry/utils/useProjects';
  24. import useRouter from 'sentry/utils/useRouter';
  25. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  26. import {PageOverviewSidebar} from 'sentry/views/performance/browser/webVitals/components/pageOverviewSidebar';
  27. import {PerformanceScoreBreakdownChart} from 'sentry/views/performance/browser/webVitals/components/performanceScoreBreakdownChart';
  28. import WebVitalMeters from 'sentry/views/performance/browser/webVitals/components/webVitalMeters';
  29. import {PageOverviewWebVitalsDetailPanel} from 'sentry/views/performance/browser/webVitals/pageOverviewWebVitalsDetailPanel';
  30. import {
  31. PageSamplePerformanceTable,
  32. TransactionSampleRowWithScoreAndExtra,
  33. } from 'sentry/views/performance/browser/webVitals/pageSamplePerformanceTable';
  34. import {calculatePerformanceScore} from 'sentry/views/performance/browser/webVitals/utils/calculatePerformanceScore';
  35. import {WebVitals} from 'sentry/views/performance/browser/webVitals/utils/types';
  36. import {useProjectWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/useProjectWebVitalsQuery';
  37. import {ModulePageProviders} from 'sentry/views/performance/database/modulePageProviders';
  38. import {transactionSummaryRouteWithQuery} from '../../transactionSummary/utils';
  39. export enum LandingDisplayField {
  40. OVERVIEW = 'overview',
  41. SPANS = 'spans',
  42. }
  43. const LANDING_DISPLAYS = [
  44. {
  45. label: t('Overview'),
  46. field: LandingDisplayField.OVERVIEW,
  47. },
  48. {
  49. label: t('Aggregate Spans'),
  50. field: LandingDisplayField.SPANS,
  51. },
  52. ];
  53. const SAMPLES_COLUMN_ORDER: GridColumnOrder<
  54. keyof TransactionSampleRowWithScoreAndExtra
  55. >[] = [
  56. {key: 'id', width: COL_WIDTH_UNDEFINED, name: 'Event ID'},
  57. {key: 'user.display', width: COL_WIDTH_UNDEFINED, name: 'User'},
  58. {key: 'measurements.lcp', width: COL_WIDTH_UNDEFINED, name: 'LCP'},
  59. {key: 'measurements.fcp', width: COL_WIDTH_UNDEFINED, name: 'FCP'},
  60. {key: 'measurements.fid', width: COL_WIDTH_UNDEFINED, name: 'FID'},
  61. {key: 'measurements.cls', width: COL_WIDTH_UNDEFINED, name: 'CLS'},
  62. {key: 'measurements.ttfb', width: COL_WIDTH_UNDEFINED, name: 'TTFB'},
  63. {key: 'profile.id', width: COL_WIDTH_UNDEFINED, name: 'Profile'},
  64. {key: 'replayId', width: COL_WIDTH_UNDEFINED, name: 'Replay'},
  65. {key: 'score', width: COL_WIDTH_UNDEFINED, name: 'Score'},
  66. ];
  67. function getCurrentTabSelection(selectedTab) {
  68. const tab = decodeScalar(selectedTab);
  69. if (tab && Object.values(LandingDisplayField).includes(tab as LandingDisplayField)) {
  70. return tab as LandingDisplayField;
  71. }
  72. return LandingDisplayField.OVERVIEW;
  73. }
  74. export default function PageOverview() {
  75. const organization = useOrganization();
  76. const location = useLocation();
  77. const {projects} = useProjects();
  78. const router = useRouter();
  79. const transaction = location.query.transaction
  80. ? Array.isArray(location.query.transaction)
  81. ? location.query.transaction[0]
  82. : location.query.transaction
  83. : undefined;
  84. const project = useMemo(
  85. () => projects.find(p => p.id === String(location.query.project)),
  86. [projects, location.query.project]
  87. );
  88. const tab = getCurrentTabSelection(location.query.tab);
  89. // TODO: When visiting page overview from a specific webvital detail panel in the landing page,
  90. // we should automatically default this webvital state to the respective webvital so the detail
  91. // panel in this page opens automatically.
  92. const [state, setState] = useState<{webVital: WebVitals | null}>({
  93. webVital: (location.query.webVital as WebVitals) ?? null,
  94. });
  95. const query = decodeScalar(location.query.query);
  96. const {data: pageData, isLoading} = useProjectWebVitalsQuery({transaction});
  97. if (transaction === undefined) {
  98. // redirect user to webvitals landing page
  99. window.location.href = normalizeUrl(
  100. `/organizations/${organization.slug}/performance/browser/pageloads/`
  101. );
  102. return null;
  103. }
  104. const transactionSummaryTarget =
  105. project &&
  106. !Array.isArray(location.query.project) && // Only render button to transaction summary when one project is selected.
  107. transaction &&
  108. transactionSummaryRouteWithQuery({
  109. orgSlug: organization.slug,
  110. transaction,
  111. query: {...location.query},
  112. projectID: project.id,
  113. });
  114. const projectScore = isLoading
  115. ? undefined
  116. : calculatePerformanceScore({
  117. lcp: pageData?.data[0]['p75(measurements.lcp)'] as number,
  118. fcp: pageData?.data[0]['p75(measurements.fcp)'] as number,
  119. cls: pageData?.data[0]['p75(measurements.cls)'] as number,
  120. ttfb: pageData?.data[0]['p75(measurements.ttfb)'] as number,
  121. fid: pageData?.data[0]['p75(measurements.fid)'] as number,
  122. });
  123. return (
  124. <ModulePageProviders title={[t('Performance'), t('Web Vitals')].join(' — ')}>
  125. <Tabs
  126. value={tab}
  127. onChange={value => {
  128. browserHistory.push({
  129. ...location,
  130. query: {
  131. ...location.query,
  132. tab: value,
  133. },
  134. });
  135. }}
  136. >
  137. <Layout.Header>
  138. <Layout.HeaderContent>
  139. <Breadcrumbs
  140. crumbs={[
  141. {
  142. label: 'Performance',
  143. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  144. preservePageFilters: true,
  145. },
  146. {
  147. label: 'Web Vitals',
  148. to: normalizeUrl(
  149. `/organizations/${organization.slug}/performance/browser/pageloads/`
  150. ),
  151. preservePageFilters: true,
  152. },
  153. ...(transaction ? [{label: 'Page Overview'}] : []),
  154. ]}
  155. />
  156. <Layout.Title>
  157. {transaction && project && <ProjectAvatar project={project} size={24} />}
  158. {transaction ?? t('Page Loads')}
  159. </Layout.Title>
  160. </Layout.HeaderContent>
  161. <Layout.HeaderActions>
  162. {transactionSummaryTarget && (
  163. <LinkButton to={transactionSummaryTarget} size="sm">
  164. {t('View Transaction Summary')}
  165. </LinkButton>
  166. )}
  167. </Layout.HeaderActions>
  168. <TabList hideBorder>
  169. {LANDING_DISPLAYS.map(({label, field}) => (
  170. <TabList.Item key={field}>{label}</TabList.Item>
  171. ))}
  172. </TabList>
  173. </Layout.Header>
  174. {tab === LandingDisplayField.SPANS ? (
  175. <Layout.Body>
  176. <Layout.Main fullWidth>
  177. {defined(transaction) && <AggregateSpans transaction={transaction} />}
  178. </Layout.Main>
  179. </Layout.Body>
  180. ) : (
  181. <Layout.Body>
  182. <FeedbackWidget />
  183. <Layout.Main>
  184. <TopMenuContainer>
  185. {transaction && (
  186. <ViewAllPagesButton
  187. to={{
  188. ...location,
  189. pathname: '/performance/browser/pageloads/',
  190. query: {...location.query, transaction: undefined},
  191. }}
  192. >
  193. <IconChevron direction="left" /> {t('View All Pages')}
  194. </ViewAllPagesButton>
  195. )}
  196. <PageFilterBar condensed>
  197. <ProjectPageFilter />
  198. <DatePageFilter />
  199. </PageFilterBar>
  200. </TopMenuContainer>
  201. <Flex>
  202. <PerformanceScoreBreakdownChart transaction={transaction} />
  203. </Flex>
  204. <WebVitalMetersContainer>
  205. <WebVitalMeters
  206. projectData={pageData}
  207. projectScore={projectScore}
  208. onClick={webVital => {
  209. router.replace({
  210. pathname: location.pathname,
  211. query: {...location.query, webVital},
  212. });
  213. setState({...state, webVital});
  214. }}
  215. transaction={transaction}
  216. />
  217. </WebVitalMetersContainer>
  218. <PageSamplePerformanceTableContainer>
  219. <PageSamplePerformanceTable
  220. transaction={transaction}
  221. columnOrder={SAMPLES_COLUMN_ORDER}
  222. limit={15}
  223. search={query}
  224. />
  225. </PageSamplePerformanceTableContainer>
  226. </Layout.Main>
  227. <Layout.Side>
  228. <PageOverviewSidebar
  229. projectScore={projectScore}
  230. transaction={transaction}
  231. />
  232. </Layout.Side>
  233. </Layout.Body>
  234. )}
  235. <PageOverviewWebVitalsDetailPanel
  236. webVital={state.webVital}
  237. onClose={() => {
  238. router.replace({
  239. pathname: router.location.pathname,
  240. query: omit(router.location.query, 'webVital'),
  241. });
  242. setState({...state, webVital: null});
  243. }}
  244. />
  245. </Tabs>
  246. </ModulePageProviders>
  247. );
  248. }
  249. const ViewAllPagesButton = styled(LinkButton)`
  250. margin-right: ${space(1)};
  251. `;
  252. const TopMenuContainer = styled('div')`
  253. margin-bottom: ${space(1)};
  254. display: flex;
  255. `;
  256. const Flex = styled('div')`
  257. display: flex;
  258. flex-direction: row;
  259. justify-content: space-between;
  260. width: 100%;
  261. gap: ${space(1)};
  262. `;
  263. const PageSamplePerformanceTableContainer = styled('div')`
  264. margin-top: ${space(1)};
  265. `;
  266. const WebVitalMetersContainer = styled('div')`
  267. margin: ${space(2)} 0 ${space(1)} 0;
  268. `;