pageSamplePerformanceTable.tsx 10 KB

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