pagePerformanceTable.tsx 7.1 KB

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