123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- import {useMemo, useState} from 'react';
- import {Link} from 'react-router';
- import styled from '@emotion/styled';
- import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
- import GridEditable, {
- COL_WIDTH_UNDEFINED,
- GridColumnHeader,
- GridColumnOrder,
- } from 'sentry/components/gridEditable';
- import SortLink from 'sentry/components/gridEditable/sortLink';
- import SearchBar from 'sentry/components/searchBar';
- import {Tooltip} from 'sentry/components/tooltip';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {Sort} from 'sentry/utils/discover/fields';
- import {formatAbbreviatedNumber, getDuration} from 'sentry/utils/formatters';
- import {useLocation} from 'sentry/utils/useLocation';
- import useOrganization from 'sentry/utils/useOrganization';
- import useProjects from 'sentry/utils/useProjects';
- import {PerformanceBadge} from 'sentry/views/performance/browser/webVitals/components/performanceBadge';
- import {calculateOpportunity} from 'sentry/views/performance/browser/webVitals/utils/calculateOpportunity';
- import {calculatePerformanceScore} from 'sentry/views/performance/browser/webVitals/utils/calculatePerformanceScore';
- import {
- Row,
- SORTABLE_FIELDS,
- } from 'sentry/views/performance/browser/webVitals/utils/types';
- import {useProjectWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/useProjectWebVitalsQuery';
- import {useTransactionWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/useTransactionWebVitalsQuery';
- import {useWebVitalsSort} from 'sentry/views/performance/browser/webVitals/utils/useWebVitalsSort';
- type RowWithScoreAndOpportunity = Row & {opportunity: number; score: number};
- type Column = GridColumnHeader<keyof RowWithScoreAndOpportunity>;
- const columnOrder: GridColumnOrder<keyof RowWithScoreAndOpportunity>[] = [
- {key: 'transaction', width: COL_WIDTH_UNDEFINED, name: 'Pages'},
- {key: 'count()', width: COL_WIDTH_UNDEFINED, name: 'Pageloads'},
- {key: 'p75(measurements.lcp)', width: COL_WIDTH_UNDEFINED, name: 'LCP'},
- {key: 'p75(measurements.fcp)', width: COL_WIDTH_UNDEFINED, name: 'FCP'},
- {key: 'p75(measurements.fid)', width: COL_WIDTH_UNDEFINED, name: 'FID'},
- {key: 'p75(measurements.cls)', width: COL_WIDTH_UNDEFINED, name: 'CLS'},
- {key: 'p75(measurements.ttfb)', width: COL_WIDTH_UNDEFINED, name: 'TTFB'},
- {key: 'score', width: COL_WIDTH_UNDEFINED, name: 'Score'},
- {key: 'opportunity', width: COL_WIDTH_UNDEFINED, name: 'Opportunity'},
- ];
- export function PagePerformanceTable() {
- const organization = useOrganization();
- const location = useLocation();
- const {projects} = useProjects();
- const [search, setSearch] = useState<string | undefined>(undefined);
- const project = useMemo(
- () => projects.find(p => p.id === String(location.query.project)),
- [projects, location.query.project]
- );
- const sort = useWebVitalsSort();
- const {data: projectData, isLoading: isProjectWebVitalsQueryLoading} =
- useProjectWebVitalsQuery({transaction: search});
- const projectScore = calculatePerformanceScore({
- lcp: projectData?.data[0]['p75(measurements.lcp)'] as number,
- fcp: projectData?.data[0]['p75(measurements.fcp)'] as number,
- cls: projectData?.data[0]['p75(measurements.cls)'] as number,
- ttfb: projectData?.data[0]['p75(measurements.ttfb)'] as number,
- fid: projectData?.data[0]['p75(measurements.fid)'] as number,
- });
- const {data, isLoading: isTransactionWebVitalsQueryLoading} =
- useTransactionWebVitalsQuery({limit: 10, transaction: search});
- const count = projectData?.data[0]['count()'] as number;
- const tableData: RowWithScoreAndOpportunity[] = data.map(row => ({
- ...row,
- opportunity: calculateOpportunity(
- projectScore.totalScore,
- count,
- row.score,
- row['count()']
- ),
- }));
- const getFormattedDuration = (value: number) => {
- return getDuration(value, value < 1 ? 0 : 2, true);
- };
- function renderHeadCell(col: Column) {
- function generateSortLink() {
- let newSortDirection: Sort['kind'] = 'desc';
- if (sort?.field === col.key) {
- if (sort.kind === 'desc') {
- newSortDirection = 'asc';
- }
- }
- const newSort = `${newSortDirection === 'desc' ? '-' : ''}${col.key}`;
- return {
- ...location,
- query: {...location.query, sort: newSort},
- };
- }
- const canSort = (SORTABLE_FIELDS as unknown as string[]).includes(col.key);
- if (canSort) {
- return (
- <SortLink
- align="right"
- title={col.name}
- direction={sort?.field === col.key ? sort.kind : undefined}
- canSort={canSort}
- generateSortLink={generateSortLink}
- />
- );
- }
- if (col.key === 'score') {
- return (
- <AlignCenter>
- <span>{col.name}</span>
- </AlignCenter>
- );
- }
- if (col.key === 'opportunity') {
- return (
- <Tooltip
- title={t(
- 'The biggest opportunities to improve your cumulative performance score.'
- )}
- >
- <OpportunityHeader>{col.name}</OpportunityHeader>
- </Tooltip>
- );
- }
- return <span>{col.name}</span>;
- }
- function renderBodyCell(col: Column, row: RowWithScoreAndOpportunity) {
- const {key} = col;
- if (key === 'score') {
- return (
- <AlignCenter>
- <PerformanceBadge score={row.score} />
- </AlignCenter>
- );
- }
- if (key === 'count()') {
- return <AlignRight>{formatAbbreviatedNumber(row['count()'])}</AlignRight>;
- }
- if (key === 'transaction') {
- return (
- <NoOverflow>
- {project && (
- <StyledProjectAvatar
- project={project}
- direction="left"
- size={16}
- hasTooltip
- tooltip={project.slug}
- />
- )}
- <Link
- to={{
- ...location,
- ...(organization.features.includes(
- 'starfish-browser-webvitals-pageoverview-v2'
- )
- ? {pathname: `${location.pathname}overview/`}
- : {}),
- query: {...location.query, transaction: row.transaction},
- }}
- >
- {row.transaction}
- </Link>
- </NoOverflow>
- );
- }
- if (
- [
- 'p75(measurements.fcp)',
- 'p75(measurements.lcp)',
- 'p75(measurements.ttfb)',
- 'p75(measurements.fid)',
- ].includes(key)
- ) {
- return <AlignRight>{getFormattedDuration((row[key] as number) / 1000)}</AlignRight>;
- }
- if (['p75(measurements.cls)', 'opportunity'].includes(key)) {
- return <AlignRight>{Math.round((row[key] as number) * 100) / 100}</AlignRight>;
- }
- return <NoOverflow>{row[key]}</NoOverflow>;
- }
- return (
- <span>
- <SearchBarContainer>
- <SearchBar
- placeholder={t('Search for Pages')}
- onSearch={query => {
- setSearch(query === '' ? undefined : `*${query}*`);
- }}
- />
- </SearchBarContainer>
- <GridContainer>
- <GridEditable
- isLoading={isProjectWebVitalsQueryLoading || isTransactionWebVitalsQueryLoading}
- columnOrder={columnOrder}
- columnSortBy={[]}
- data={tableData}
- grid={{
- renderHeadCell,
- renderBodyCell,
- }}
- location={location}
- />
- </GridContainer>
- </span>
- );
- }
- const NoOverflow = styled('span')`
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- `;
- const AlignRight = styled('span')<{color?: string}>`
- text-align: right;
- width: 100%;
- ${p => (p.color ? `color: ${p.color};` : '')}
- `;
- const AlignCenter = styled('span')`
- text-align: center;
- width: 100%;
- `;
- const StyledProjectAvatar = styled(ProjectAvatar)`
- top: ${space(0.25)};
- position: relative;
- padding-right: ${space(1)};
- `;
- const SearchBarContainer = styled('div')`
- margin-bottom: ${space(1)};
- `;
- const GridContainer = styled('div')`
- margin-bottom: ${space(1)};
- `;
- const OpportunityHeader = styled('span')`
- ${p => p.theme.tooltipUnderline()};
- `;
|