pageOverview.tsx 13 KB

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