pageSamplePerformanceTable.tsx 9.8 KB

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