pageSamplePerformanceTable.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. import {useMemo} from 'react';
  2. import {Link} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
  5. import {Button, LinkButton} from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import SearchBar from 'sentry/components/events/searchBar';
  8. import GridEditable, {
  9. COL_WIDTH_UNDEFINED,
  10. GridColumnHeader,
  11. GridColumnOrder,
  12. } from 'sentry/components/gridEditable';
  13. import SortLink from 'sentry/components/gridEditable/sortLink';
  14. import ExternalLink from 'sentry/components/links/externalLink';
  15. import Pagination from 'sentry/components/pagination';
  16. import {Tooltip} from 'sentry/components/tooltip';
  17. import {IconChevron, IconPlay, IconProfiling} from 'sentry/icons';
  18. import {t} from 'sentry/locale';
  19. import {space} from 'sentry/styles/space';
  20. import {defined} from 'sentry/utils';
  21. import {Sort} from 'sentry/utils/discover/fields';
  22. import {generateEventSlug} from 'sentry/utils/discover/urls';
  23. import {getShortEventId} from 'sentry/utils/events';
  24. import {getDuration} from 'sentry/utils/formatters';
  25. import {getTransactionDetailsUrl} from 'sentry/utils/performance/urls';
  26. import {generateProfileFlamechartRoute} from 'sentry/utils/profiling/routes';
  27. import {decodeScalar} from 'sentry/utils/queryString';
  28. import {useLocation} from 'sentry/utils/useLocation';
  29. import useOrganization from 'sentry/utils/useOrganization';
  30. import useProjects from 'sentry/utils/useProjects';
  31. import useRouter from 'sentry/utils/useRouter';
  32. import {useRoutes} from 'sentry/utils/useRoutes';
  33. import {PerformanceBadge} from 'sentry/views/performance/browser/webVitals/components/performanceBadge';
  34. import {useTransactionSamplesWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/useTransactionSamplesWebVitalsQuery';
  35. import {
  36. DEFAULT_INDEXED_SORT,
  37. SORTABLE_INDEXED_FIELDS,
  38. SORTABLE_INDEXED_SCORE_FIELDS,
  39. TransactionSampleRowWithScore,
  40. } from 'sentry/views/performance/browser/webVitals/utils/types';
  41. import {useStoredScoresSetting} from 'sentry/views/performance/browser/webVitals/utils/useStoredScoresSetting';
  42. import {useWebVitalsSort} from 'sentry/views/performance/browser/webVitals/utils/useWebVitalsSort';
  43. import {generateReplayLink} from 'sentry/views/performance/transactionSummary/utils';
  44. type Column = GridColumnHeader<keyof TransactionSampleRowWithScore>;
  45. export const COLUMN_ORDER: GridColumnOrder<keyof TransactionSampleRowWithScore>[] = [
  46. {key: 'user.display', width: COL_WIDTH_UNDEFINED, name: 'User'},
  47. {key: 'transaction.duration', width: COL_WIDTH_UNDEFINED, name: 'Duration'},
  48. {key: 'measurements.lcp', width: COL_WIDTH_UNDEFINED, name: 'LCP'},
  49. {key: 'measurements.fcp', width: COL_WIDTH_UNDEFINED, name: 'FCP'},
  50. {key: 'measurements.fid', width: COL_WIDTH_UNDEFINED, name: 'FID'},
  51. {key: 'measurements.cls', width: COL_WIDTH_UNDEFINED, name: 'CLS'},
  52. {key: 'measurements.ttfb', width: COL_WIDTH_UNDEFINED, name: 'TTFB'},
  53. {key: 'totalScore', width: COL_WIDTH_UNDEFINED, name: 'Score'},
  54. ];
  55. type Props = {
  56. transaction: string;
  57. columnOrder?: GridColumnOrder<keyof TransactionSampleRowWithScore>[];
  58. limit?: number;
  59. search?: string;
  60. };
  61. export function PageSamplePerformanceTable({
  62. transaction,
  63. columnOrder,
  64. search,
  65. limit = 9,
  66. }: Props) {
  67. const location = useLocation();
  68. const {projects} = useProjects();
  69. const organization = useOrganization();
  70. const routes = useRoutes();
  71. const router = useRouter();
  72. const shouldUseStoredScores = useStoredScoresSetting();
  73. const sortableFields = shouldUseStoredScores
  74. ? SORTABLE_INDEXED_FIELDS
  75. : SORTABLE_INDEXED_FIELDS.filter(
  76. field => !SORTABLE_INDEXED_SCORE_FIELDS.includes(field)
  77. );
  78. const sort = useWebVitalsSort({
  79. defaultSort: DEFAULT_INDEXED_SORT,
  80. sortableFields: sortableFields as unknown as string[],
  81. });
  82. const replayLinkGenerator = generateReplayLink(routes);
  83. const project = useMemo(
  84. () => projects.find(p => p.id === String(location.query.project)),
  85. [projects, location.query.project]
  86. );
  87. const query = decodeScalar(location.query.query);
  88. // Do 3 queries filtering on LCP to get a spread of good, meh, and poor events
  89. // We can't query by performance score yet, so we're using LCP as a best estimate
  90. const {
  91. data: tableData,
  92. isLoading,
  93. pageLinks,
  94. } = useTransactionSamplesWebVitalsQuery({
  95. limit,
  96. transaction,
  97. query: search,
  98. withProfiles: true,
  99. });
  100. const getFormattedDuration = (value: number) => {
  101. return getDuration(value, value < 1 ? 0 : 2, true);
  102. };
  103. function renderHeadCell(col: Column) {
  104. function generateSortLink() {
  105. const key = col.key === 'totalScore' ? 'measurements.score.total' : col.key;
  106. let newSortDirection: Sort['kind'] = 'desc';
  107. if (sort?.field === key) {
  108. if (sort.kind === 'desc') {
  109. newSortDirection = 'asc';
  110. }
  111. }
  112. const newSort = `${newSortDirection === 'desc' ? '-' : ''}${key}`;
  113. return {
  114. ...location,
  115. query: {...location.query, sort: newSort},
  116. };
  117. }
  118. const canSort = (sortableFields as ReadonlyArray<string>).includes(col.key);
  119. if (
  120. [
  121. 'measurements.fcp',
  122. 'measurements.lcp',
  123. 'measurements.ttfb',
  124. 'measurements.fid',
  125. 'measurements.cls',
  126. 'transaction.duration',
  127. ].includes(col.key)
  128. ) {
  129. if (canSort) {
  130. return (
  131. <SortLink
  132. align="right"
  133. title={col.name}
  134. direction={sort?.field === col.key ? sort.kind : undefined}
  135. canSort={canSort}
  136. generateSortLink={generateSortLink}
  137. />
  138. );
  139. }
  140. return (
  141. <AlignRight>
  142. <span>{col.name}</span>
  143. </AlignRight>
  144. );
  145. }
  146. if (col.key === 'totalScore') {
  147. return (
  148. <SortLink
  149. title={
  150. <AlignCenter>
  151. <StyledTooltip
  152. isHoverable
  153. title={
  154. <span>
  155. {t('The overall performance rating of this page.')}
  156. <br />
  157. <ExternalLink href="https://docs.sentry.io/product/performance/web-vitals/#performance-score">
  158. {t('How is this calculated?')}
  159. </ExternalLink>
  160. </span>
  161. }
  162. >
  163. <TooltipHeader>{t('Perf Score')}</TooltipHeader>
  164. </StyledTooltip>
  165. </AlignCenter>
  166. }
  167. direction={sort?.field === col.key ? sort.kind : undefined}
  168. canSort={canSort}
  169. generateSortLink={generateSortLink}
  170. align={undefined}
  171. />
  172. );
  173. }
  174. if (col.key === 'replayId' || col.key === 'profile.id') {
  175. return (
  176. <AlignCenter>
  177. <span>{col.name}</span>
  178. </AlignCenter>
  179. );
  180. }
  181. return <span>{col.name}</span>;
  182. }
  183. function renderBodyCell(col: Column, row: TransactionSampleRowWithScore) {
  184. const {key} = col;
  185. if (key === 'totalScore') {
  186. return (
  187. <AlignCenter>
  188. <PerformanceBadge score={row.totalScore} />
  189. </AlignCenter>
  190. );
  191. }
  192. if (key === 'transaction') {
  193. return (
  194. <NoOverflow>
  195. {project && (
  196. <StyledProjectAvatar
  197. project={project}
  198. direction="left"
  199. size={16}
  200. hasTooltip
  201. tooltip={project.slug}
  202. />
  203. )}
  204. <Link
  205. to={{...location, query: {...location.query, transaction: row.transaction}}}
  206. >
  207. {row.transaction}
  208. </Link>
  209. </NoOverflow>
  210. );
  211. }
  212. if (
  213. [
  214. 'measurements.fcp',
  215. 'measurements.lcp',
  216. 'measurements.ttfb',
  217. 'measurements.fid',
  218. 'transaction.duration',
  219. ].includes(key)
  220. ) {
  221. return (
  222. <AlignRight>
  223. {row[key] === undefined ? (
  224. <NoValue>{' \u2014 '}</NoValue>
  225. ) : (
  226. getFormattedDuration((row[key] as number) / 1000)
  227. )}
  228. </AlignRight>
  229. );
  230. }
  231. if (['measurements.cls', 'opportunity'].includes(key)) {
  232. return (
  233. <AlignRight>
  234. {row[key] === undefined ? (
  235. <NoValue>{' \u2014 '}</NoValue>
  236. ) : (
  237. Math.round((row[key] as number) * 100) / 100
  238. )}
  239. </AlignRight>
  240. );
  241. }
  242. if (key === 'profile.id') {
  243. const profileTarget =
  244. defined(row.projectSlug) && defined(row['profile.id'])
  245. ? generateProfileFlamechartRoute({
  246. orgSlug: organization.slug,
  247. projectSlug: row.projectSlug,
  248. profileId: String(row['profile.id']),
  249. })
  250. : null;
  251. return (
  252. <NoOverflow>
  253. <AlignCenter>
  254. {profileTarget && (
  255. <Tooltip title={t('View Profile')}>
  256. <LinkButton to={profileTarget} size="xs">
  257. <IconProfiling size="xs" />
  258. </LinkButton>
  259. </Tooltip>
  260. )}
  261. </AlignCenter>
  262. </NoOverflow>
  263. );
  264. }
  265. if (key === 'replayId') {
  266. const replayTarget =
  267. row['transaction.duration'] !== undefined &&
  268. replayLinkGenerator(
  269. organization,
  270. {
  271. replayId: row.replayId,
  272. id: row.id,
  273. 'transaction.duration': row['transaction.duration'],
  274. timestamp: row.timestamp,
  275. },
  276. undefined
  277. );
  278. return (
  279. <NoOverflow>
  280. <AlignCenter>
  281. {replayTarget && Object.keys(replayTarget).length > 0 && (
  282. <Tooltip title={t('View Replay')}>
  283. <LinkButton to={replayTarget} size="xs">
  284. <IconPlay size="xs" />
  285. </LinkButton>
  286. </Tooltip>
  287. )}
  288. </AlignCenter>
  289. </NoOverflow>
  290. );
  291. }
  292. if (key === 'id') {
  293. const eventSlug = generateEventSlug({...row, project: row.projectSlug});
  294. const eventTarget = getTransactionDetailsUrl(organization.slug, eventSlug);
  295. return (
  296. <NoOverflow>
  297. <Tooltip title={t('View Transaction')}>
  298. <Link to={eventTarget}>{getShortEventId(row.id)}</Link>
  299. </Tooltip>
  300. </NoOverflow>
  301. );
  302. }
  303. return <NoOverflow>{row[key]}</NoOverflow>;
  304. }
  305. return (
  306. <span>
  307. <SearchBarContainer>
  308. <StyledSearchBar
  309. query={query}
  310. organization={organization}
  311. onSearch={queryString =>
  312. router.replace({
  313. ...location,
  314. query: {...location.query, query: queryString},
  315. })
  316. }
  317. />
  318. <StyledPagination pageLinks={pageLinks} disabled={isLoading} size="md" />
  319. {/* The Pagination component disappears if pageLinks is not defined,
  320. which happens any time the table data is loading. So we render a
  321. disabled button bar if pageLinks is not defined to minimize ui shifting */}
  322. {!pageLinks && (
  323. <Wrapper>
  324. <ButtonBar merged>
  325. <Button
  326. icon={<IconChevron direction="left" />}
  327. disabled
  328. aria-label={t('Previous')}
  329. />
  330. <Button
  331. icon={<IconChevron direction="right" />}
  332. disabled
  333. aria-label={t('Next')}
  334. />
  335. </ButtonBar>
  336. </Wrapper>
  337. )}
  338. </SearchBarContainer>
  339. <GridContainer>
  340. <GridEditable
  341. isLoading={isLoading}
  342. columnOrder={columnOrder ?? COLUMN_ORDER}
  343. columnSortBy={[]}
  344. data={tableData}
  345. grid={{
  346. renderHeadCell,
  347. renderBodyCell,
  348. }}
  349. location={location}
  350. minimumColWidth={70}
  351. />
  352. </GridContainer>
  353. </span>
  354. );
  355. }
  356. const NoOverflow = styled('span')`
  357. overflow: hidden;
  358. text-overflow: ellipsis;
  359. white-space: nowrap;
  360. `;
  361. const AlignRight = styled('span')<{color?: string}>`
  362. text-align: right;
  363. width: 100%;
  364. ${p => (p.color ? `color: ${p.color};` : '')}
  365. `;
  366. const AlignCenter = styled('div')`
  367. display: block;
  368. margin: auto;
  369. text-align: center;
  370. width: 100%;
  371. `;
  372. const StyledProjectAvatar = styled(ProjectAvatar)`
  373. top: ${space(0.25)};
  374. position: relative;
  375. padding-right: ${space(1)};
  376. `;
  377. // Not pretty but we need to override gridEditable styles since the original
  378. // styles have too much padding for small spaces
  379. const GridContainer = styled('div')`
  380. margin-bottom: ${space(1)};
  381. th {
  382. padding: 0 ${space(1)};
  383. }
  384. th:first-child {
  385. padding-left: ${space(2)};
  386. }
  387. th:last-child {
  388. padding-right: ${space(2)};
  389. }
  390. td {
  391. padding: ${space(1)};
  392. }
  393. td:first-child {
  394. padding-right: ${space(1)};
  395. padding-left: ${space(2)};
  396. }
  397. `;
  398. const NoValue = styled('span')`
  399. color: ${p => p.theme.gray300};
  400. `;
  401. const SearchBarContainer = styled('div')`
  402. display: flex;
  403. margin-top: ${space(2)};
  404. margin-bottom: ${space(1)};
  405. gap: ${space(1)};
  406. `;
  407. const StyledSearchBar = styled(SearchBar)`
  408. flex-grow: 1;
  409. `;
  410. const StyledPagination = styled(Pagination)`
  411. margin: 0;
  412. `;
  413. const Wrapper = styled('div')`
  414. display: flex;
  415. align-items: center;
  416. justify-content: flex-end;
  417. margin: 0;
  418. `;
  419. const TooltipHeader = styled('span')`
  420. ${p => p.theme.tooltipUnderline()};
  421. `;
  422. const StyledTooltip = styled(Tooltip)`
  423. top: 1px;
  424. position: relative;
  425. `;