pageOverview.tsx 13 KB

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