pageSamplePerformanceTable.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 {LinkButton} from 'sentry/components/button';
  6. import GridEditable, {
  7. COL_WIDTH_UNDEFINED,
  8. GridColumnHeader,
  9. GridColumnOrder,
  10. } from 'sentry/components/gridEditable';
  11. import {IconPlay} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {generateEventSlug} from 'sentry/utils/discover/urls';
  15. import {getDuration} from 'sentry/utils/formatters';
  16. import {getTransactionDetailsUrl} from 'sentry/utils/performance/urls';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import useProjects from 'sentry/utils/useProjects';
  20. import {useRoutes} from 'sentry/utils/useRoutes';
  21. import {PerformanceBadge} from 'sentry/views/performance/browser/webVitals/components/performanceBadge';
  22. import {
  23. PERFORMANCE_SCORE_MEDIANS,
  24. PERFORMANCE_SCORE_P90S,
  25. } from 'sentry/views/performance/browser/webVitals/utils/calculatePerformanceScore';
  26. import {TransactionSampleRow} from 'sentry/views/performance/browser/webVitals/utils/types';
  27. import {useTransactionSamplesWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/useTransactionSamplesWebVitalsQuery';
  28. import {generateReplayLink} from 'sentry/views/performance/transactionSummary/utils';
  29. type TransactionSampleRowWithScoreAndExtra = TransactionSampleRow & {
  30. score: number;
  31. view: any;
  32. };
  33. type Column = GridColumnHeader<keyof TransactionSampleRowWithScoreAndExtra>;
  34. const columnOrder: GridColumnOrder<keyof TransactionSampleRowWithScoreAndExtra>[] = [
  35. {key: 'user.display', width: COL_WIDTH_UNDEFINED, name: 'User'},
  36. {key: 'transaction.duration', width: COL_WIDTH_UNDEFINED, name: 'Duration'},
  37. {key: 'measurements.lcp', width: COL_WIDTH_UNDEFINED, name: 'LCP'},
  38. {key: 'measurements.fcp', width: COL_WIDTH_UNDEFINED, name: 'FCP'},
  39. {key: 'measurements.fid', width: COL_WIDTH_UNDEFINED, name: 'FID'},
  40. {key: 'measurements.cls', width: COL_WIDTH_UNDEFINED, name: 'CLS'},
  41. {key: 'measurements.ttfb', width: COL_WIDTH_UNDEFINED, name: 'TTFB'},
  42. {key: 'score', width: COL_WIDTH_UNDEFINED, name: 'Score'},
  43. {key: 'view', width: COL_WIDTH_UNDEFINED, name: 'View'},
  44. ];
  45. type Props = {
  46. transaction: string;
  47. };
  48. export function PageSamplePerformanceTable({transaction}: Props) {
  49. const location = useLocation();
  50. const {projects} = useProjects();
  51. const organization = useOrganization();
  52. const routes = useRoutes();
  53. const replayLinkGenerator = generateReplayLink(routes);
  54. const project = useMemo(
  55. () => projects.find(p => p.id === String(location.query.project)),
  56. [projects, location.query.project]
  57. );
  58. // Do 3 queries filtering on LCP to get a spread of good, meh, and poor events
  59. // We can't query by performance score yet, so we're using LCP as a best estimate
  60. const {data: goodData, isLoading: isGoodTransactionWebVitalsQueryLoading} =
  61. useTransactionSamplesWebVitalsQuery({
  62. limit: 3,
  63. transaction,
  64. query: `measurements.lcp:<${PERFORMANCE_SCORE_P90S.lcp}`,
  65. });
  66. const {data: mehData, isLoading: isMehTransactionWebVitalsQueryLoading} =
  67. useTransactionSamplesWebVitalsQuery({
  68. limit: 3,
  69. transaction,
  70. query: `measurements.lcp:<${PERFORMANCE_SCORE_MEDIANS.lcp} measurements.lcp:>=${PERFORMANCE_SCORE_P90S.lcp}`,
  71. });
  72. const {data: poorData, isLoading: isPoorTransactionWebVitalsQueryLoading} =
  73. useTransactionSamplesWebVitalsQuery({
  74. limit: 3,
  75. transaction,
  76. query: `measurements.lcp:>=${PERFORMANCE_SCORE_MEDIANS.lcp}`,
  77. });
  78. // In case we don't have enough data, get some transactions with no LCP data
  79. const {data: noLcpData, isLoading: isNoLcpTransactionWebVitalsQueryLoading} =
  80. useTransactionSamplesWebVitalsQuery({
  81. limit: 9,
  82. transaction,
  83. query: `!has:measurements.lcp`,
  84. });
  85. const data = [...goodData, ...mehData, ...poorData];
  86. // If we don't have enough data, fill in the rest with no LCP data
  87. if (data.length < 9) {
  88. data.push(...noLcpData.slice(0, 9 - data.length));
  89. }
  90. const isTransactionWebVitalsQueryLoading =
  91. isGoodTransactionWebVitalsQueryLoading ||
  92. isMehTransactionWebVitalsQueryLoading ||
  93. isPoorTransactionWebVitalsQueryLoading ||
  94. isNoLcpTransactionWebVitalsQueryLoading;
  95. const tableData: TransactionSampleRowWithScoreAndExtra[] = data
  96. .map(row => ({
  97. ...row,
  98. view: null,
  99. }))
  100. .sort((a, b) => a.score - b.score);
  101. const getFormattedDuration = (value: number) => {
  102. return getDuration(value, value < 1 ? 0 : 2, true);
  103. };
  104. function renderHeadCell(col: Column) {
  105. if (
  106. [
  107. 'measurements.fcp',
  108. 'measurements.lcp',
  109. 'measurements.ttfb',
  110. 'measurements.fid',
  111. 'measurements.cls',
  112. 'transaction.duration',
  113. ].includes(col.key)
  114. ) {
  115. return (
  116. <AlignRight>
  117. <span>{col.name}</span>
  118. </AlignRight>
  119. );
  120. }
  121. if (col.key === 'score') {
  122. return (
  123. <AlignCenter>
  124. <span>{col.name}</span>
  125. </AlignCenter>
  126. );
  127. }
  128. return <span>{col.name}</span>;
  129. }
  130. function renderBodyCell(col: Column, row: TransactionSampleRowWithScoreAndExtra) {
  131. const {key} = col;
  132. if (key === 'score') {
  133. return (
  134. <AlignCenter>
  135. <PerformanceBadge score={row.score} />
  136. </AlignCenter>
  137. );
  138. }
  139. if (key === 'transaction') {
  140. return (
  141. <NoOverflow>
  142. {project && (
  143. <StyledProjectAvatar
  144. project={project}
  145. direction="left"
  146. size={16}
  147. hasTooltip
  148. tooltip={project.slug}
  149. />
  150. )}
  151. <Link
  152. to={{...location, query: {...location.query, transaction: row.transaction}}}
  153. >
  154. {row.transaction}
  155. </Link>
  156. </NoOverflow>
  157. );
  158. }
  159. if (
  160. [
  161. 'measurements.fcp',
  162. 'measurements.lcp',
  163. 'measurements.ttfb',
  164. 'measurements.fid',
  165. 'transaction.duration',
  166. ].includes(key)
  167. ) {
  168. return (
  169. <AlignRight>
  170. {row[key] === null ? (
  171. <NoValue>{t('(no value)')}</NoValue>
  172. ) : (
  173. getFormattedDuration((row[key] as number) / 1000)
  174. )}
  175. </AlignRight>
  176. );
  177. }
  178. if (['measurements.cls', 'opportunity'].includes(key)) {
  179. return <AlignRight>{Math.round((row[key] as number) * 100) / 100}</AlignRight>;
  180. }
  181. if (key === 'view') {
  182. const eventSlug = generateEventSlug({...row, project: project?.slug});
  183. const eventTarget = getTransactionDetailsUrl(organization.slug, eventSlug);
  184. const replayTarget =
  185. row['transaction.duration'] !== null &&
  186. replayLinkGenerator(
  187. organization,
  188. {
  189. replayId: row.replayId,
  190. id: row.id,
  191. 'transaction.duration': row['transaction.duration'],
  192. timestamp: row.timestamp,
  193. },
  194. undefined
  195. );
  196. return (
  197. <NoOverflow>
  198. <Flex>
  199. <LinkButton to={eventTarget} size="xs">
  200. {t('Event')}
  201. </LinkButton>
  202. {row.replayId && replayTarget && (
  203. <LinkButton to={replayTarget} size="xs">
  204. <IconPlay size="xs" />
  205. </LinkButton>
  206. )}
  207. </Flex>
  208. </NoOverflow>
  209. );
  210. }
  211. return <NoOverflow>{row[key]}</NoOverflow>;
  212. }
  213. return (
  214. <span>
  215. <GridContainer>
  216. <GridEditable
  217. isLoading={isTransactionWebVitalsQueryLoading}
  218. columnOrder={columnOrder}
  219. columnSortBy={[]}
  220. data={tableData}
  221. grid={{
  222. renderHeadCell,
  223. renderBodyCell,
  224. }}
  225. location={location}
  226. />
  227. </GridContainer>
  228. </span>
  229. );
  230. }
  231. const NoOverflow = styled('span')`
  232. overflow: hidden;
  233. text-overflow: ellipsis;
  234. white-space: nowrap;
  235. `;
  236. const AlignRight = styled('span')<{color?: string}>`
  237. text-align: right;
  238. width: 100%;
  239. ${p => (p.color ? `color: ${p.color};` : '')}
  240. `;
  241. const AlignCenter = styled('span')`
  242. text-align: center;
  243. width: 100%;
  244. `;
  245. const StyledProjectAvatar = styled(ProjectAvatar)`
  246. top: ${space(0.25)};
  247. position: relative;
  248. padding-right: ${space(1)};
  249. `;
  250. const GridContainer = styled('div')`
  251. margin-bottom: ${space(1)};
  252. `;
  253. const Flex = styled('div')`
  254. display: flex;
  255. gap: ${space(1)};
  256. `;
  257. const NoValue = styled('span')`
  258. color: ${p => p.theme.gray300};
  259. `;