pagePerformanceTable.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import {useMemo, useState} from 'react';
  2. import {Link} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
  5. import GridEditable, {
  6. COL_WIDTH_UNDEFINED,
  7. GridColumnHeader,
  8. GridColumnOrder,
  9. } from 'sentry/components/gridEditable';
  10. import SortLink from 'sentry/components/gridEditable/sortLink';
  11. import SearchBar from 'sentry/components/searchBar';
  12. import {Tooltip} from 'sentry/components/tooltip';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {Sort} from 'sentry/utils/discover/fields';
  16. import {formatAbbreviatedNumber, getDuration} from 'sentry/utils/formatters';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import useProjects from 'sentry/utils/useProjects';
  20. import {PerformanceBadge} from 'sentry/views/performance/browser/webVitals/components/performanceBadge';
  21. import {calculateOpportunity} from 'sentry/views/performance/browser/webVitals/utils/calculateOpportunity';
  22. import {calculatePerformanceScore} from 'sentry/views/performance/browser/webVitals/utils/calculatePerformanceScore';
  23. import {
  24. Row,
  25. SORTABLE_FIELDS,
  26. } from 'sentry/views/performance/browser/webVitals/utils/types';
  27. import {useProjectWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/useProjectWebVitalsQuery';
  28. import {useTransactionWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/useTransactionWebVitalsQuery';
  29. import {useWebVitalsSort} from 'sentry/views/performance/browser/webVitals/utils/useWebVitalsSort';
  30. type RowWithScoreAndOpportunity = Row & {opportunity: number; score: number};
  31. type Column = GridColumnHeader<keyof RowWithScoreAndOpportunity>;
  32. const columnOrder: GridColumnOrder<keyof RowWithScoreAndOpportunity>[] = [
  33. {key: 'transaction', width: COL_WIDTH_UNDEFINED, name: 'Pages'},
  34. {key: 'count()', width: COL_WIDTH_UNDEFINED, name: 'Pageloads'},
  35. {key: 'p75(measurements.lcp)', width: COL_WIDTH_UNDEFINED, name: 'LCP'},
  36. {key: 'p75(measurements.fcp)', width: COL_WIDTH_UNDEFINED, name: 'FCP'},
  37. {key: 'p75(measurements.fid)', width: COL_WIDTH_UNDEFINED, name: 'FID'},
  38. {key: 'p75(measurements.cls)', width: COL_WIDTH_UNDEFINED, name: 'CLS'},
  39. {key: 'p75(measurements.ttfb)', width: COL_WIDTH_UNDEFINED, name: 'TTFB'},
  40. {key: 'score', width: COL_WIDTH_UNDEFINED, name: 'Score'},
  41. {key: 'opportunity', width: COL_WIDTH_UNDEFINED, name: 'Opportunity'},
  42. ];
  43. export function PagePerformanceTable() {
  44. const organization = useOrganization();
  45. const location = useLocation();
  46. const {projects} = useProjects();
  47. const [search, setSearch] = useState<string | undefined>(undefined);
  48. const project = useMemo(
  49. () => projects.find(p => p.id === String(location.query.project)),
  50. [projects, location.query.project]
  51. );
  52. const sort = useWebVitalsSort();
  53. const {data: projectData, isLoading: isProjectWebVitalsQueryLoading} =
  54. useProjectWebVitalsQuery({transaction: search});
  55. const projectScore = calculatePerformanceScore({
  56. lcp: projectData?.data[0]['p75(measurements.lcp)'] as number,
  57. fcp: projectData?.data[0]['p75(measurements.fcp)'] as number,
  58. cls: projectData?.data[0]['p75(measurements.cls)'] as number,
  59. ttfb: projectData?.data[0]['p75(measurements.ttfb)'] as number,
  60. fid: projectData?.data[0]['p75(measurements.fid)'] as number,
  61. });
  62. const {data, isLoading: isTransactionWebVitalsQueryLoading} =
  63. useTransactionWebVitalsQuery({limit: 10, transaction: search});
  64. const count = projectData?.data[0]['count()'] as number;
  65. const tableData: RowWithScoreAndOpportunity[] = data.map(row => ({
  66. ...row,
  67. opportunity: calculateOpportunity(
  68. projectScore.totalScore,
  69. count,
  70. row.score,
  71. row['count()']
  72. ),
  73. }));
  74. const getFormattedDuration = (value: number) => {
  75. return getDuration(value, value < 1 ? 0 : 2, true);
  76. };
  77. function renderHeadCell(col: Column) {
  78. function generateSortLink() {
  79. let newSortDirection: Sort['kind'] = 'desc';
  80. if (sort?.field === col.key) {
  81. if (sort.kind === 'desc') {
  82. newSortDirection = 'asc';
  83. }
  84. }
  85. const newSort = `${newSortDirection === 'desc' ? '-' : ''}${col.key}`;
  86. return {
  87. ...location,
  88. query: {...location.query, sort: newSort},
  89. };
  90. }
  91. const canSort = (SORTABLE_FIELDS as unknown as string[]).includes(col.key);
  92. if (canSort) {
  93. return (
  94. <SortLink
  95. align="right"
  96. title={col.name}
  97. direction={sort?.field === col.key ? sort.kind : undefined}
  98. canSort={canSort}
  99. generateSortLink={generateSortLink}
  100. />
  101. );
  102. }
  103. if (col.key === 'score') {
  104. return (
  105. <AlignCenter>
  106. <span>{col.name}</span>
  107. </AlignCenter>
  108. );
  109. }
  110. if (col.key === 'opportunity') {
  111. return (
  112. <Tooltip
  113. title={t(
  114. 'The biggest opportunities to improve your cumulative performance score.'
  115. )}
  116. >
  117. <OpportunityHeader>{col.name}</OpportunityHeader>
  118. </Tooltip>
  119. );
  120. }
  121. return <span>{col.name}</span>;
  122. }
  123. function renderBodyCell(col: Column, row: RowWithScoreAndOpportunity) {
  124. const {key} = col;
  125. if (key === 'score') {
  126. return (
  127. <AlignCenter>
  128. <PerformanceBadge score={row.score} />
  129. </AlignCenter>
  130. );
  131. }
  132. if (key === 'count()') {
  133. return <AlignRight>{formatAbbreviatedNumber(row['count()'])}</AlignRight>;
  134. }
  135. if (key === 'transaction') {
  136. return (
  137. <NoOverflow>
  138. {project && (
  139. <StyledProjectAvatar
  140. project={project}
  141. direction="left"
  142. size={16}
  143. hasTooltip
  144. tooltip={project.slug}
  145. />
  146. )}
  147. <Link
  148. to={{
  149. ...location,
  150. ...(organization.features.includes(
  151. 'starfish-browser-webvitals-pageoverview-v2'
  152. )
  153. ? {pathname: `${location.pathname}overview/`}
  154. : {}),
  155. query: {...location.query, transaction: row.transaction},
  156. }}
  157. >
  158. {row.transaction}
  159. </Link>
  160. </NoOverflow>
  161. );
  162. }
  163. if (
  164. [
  165. 'p75(measurements.fcp)',
  166. 'p75(measurements.lcp)',
  167. 'p75(measurements.ttfb)',
  168. 'p75(measurements.fid)',
  169. ].includes(key)
  170. ) {
  171. return <AlignRight>{getFormattedDuration((row[key] as number) / 1000)}</AlignRight>;
  172. }
  173. if (['p75(measurements.cls)', 'opportunity'].includes(key)) {
  174. return <AlignRight>{Math.round((row[key] as number) * 100) / 100}</AlignRight>;
  175. }
  176. return <NoOverflow>{row[key]}</NoOverflow>;
  177. }
  178. return (
  179. <span>
  180. <SearchBarContainer>
  181. <SearchBar
  182. placeholder={t('Search for Pages')}
  183. onSearch={query => {
  184. setSearch(query === '' ? undefined : `*${query}*`);
  185. }}
  186. />
  187. </SearchBarContainer>
  188. <GridContainer>
  189. <GridEditable
  190. isLoading={isProjectWebVitalsQueryLoading || isTransactionWebVitalsQueryLoading}
  191. columnOrder={columnOrder}
  192. columnSortBy={[]}
  193. data={tableData}
  194. grid={{
  195. renderHeadCell,
  196. renderBodyCell,
  197. }}
  198. location={location}
  199. />
  200. </GridContainer>
  201. </span>
  202. );
  203. }
  204. const NoOverflow = styled('span')`
  205. overflow: hidden;
  206. text-overflow: ellipsis;
  207. white-space: nowrap;
  208. `;
  209. const AlignRight = styled('span')<{color?: string}>`
  210. text-align: right;
  211. width: 100%;
  212. ${p => (p.color ? `color: ${p.color};` : '')}
  213. `;
  214. const AlignCenter = styled('span')`
  215. text-align: center;
  216. width: 100%;
  217. `;
  218. const StyledProjectAvatar = styled(ProjectAvatar)`
  219. top: ${space(0.25)};
  220. position: relative;
  221. padding-right: ${space(1)};
  222. `;
  223. const SearchBarContainer = styled('div')`
  224. margin-bottom: ${space(1)};
  225. `;
  226. const GridContainer = styled('div')`
  227. margin-bottom: ${space(1)};
  228. `;
  229. const OpportunityHeader = styled('span')`
  230. ${p => p.theme.tooltipUnderline()};
  231. `;