pageOverview.tsx 12 KB

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