pageOverview.tsx 11 KB

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