pagePerformanceTable.tsx 8.3 KB

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