expanded metrics performance updates.

This commit is contained in:
Francis Cao committed 2026-08-13 10:16:29 -07:00
1 parent 6f5d0d2bc2
commit 5ca2be7598
3 files changed
+283 -140

No files matched your search

+177 -96
View File
@@ -19,128 +19,209 @@ export interface MetricLabelProps {
}
export function MetricLabel({ type, data }: MetricLabelProps) {
const { t, labels } = useMessages();
const { formatValue, formatCity } = useFormat();
const { locale } = useLocale();
const { countryNames } = useCountryNames(locale);
const { getRegionName } = useRegionNames(locale);
const { label, country, domain } = data;
switch (type) {
case 'browser':
case 'os':
return (
<FilterLink
type={type}
value={label}
label={formatValue(label, type)}
icon={<TypeIcon type={type} value={label} />}
/>
);
return <BrowserOsMetricLabel type={type} data={data} />;
case 'channel':
return t(labels[label]);
return <ChannelMetricLabel data={data} />;
case 'city':
return (
<FilterLink
type="city"
value={label}
label={formatCity(label, country)}
icon={
country && (
<img
src={`${process.env.basePath || ''}/images/country/${
country?.toLowerCase() || 'xx'
}.png`}
alt={country}
/>
)
}
/>
);
return <CityMetricLabel data={data} />;
case 'region':
return (
<FilterLink
type="region"
value={label}
label={getRegionName(label, country)}
icon={<TypeIcon type="country" value={country} />}
/>
);
return <RegionMetricLabel data={data} />;
case 'country':
return (
<FilterLink
type="country"
value={(countryNames[label] && label) || label}
label={formatValue(label, 'country')}
icon={<TypeIcon type="country" value={label} />}
/>
);
return <CountryMetricLabel data={data} />;
case 'path':
case 'entry':
case 'exit':
return (
<FilterLink
type={type === 'entry' || type === 'exit' ? 'path' : type}
value={label}
label={!label && t(labels.none)}
externalUrl={
domain ? `${domain?.startsWith('http') ? domain : `https://${domain}`}${label}` : null
}
/>
);
return <PathMetricLabel type={type} data={data} />;
case 'fullPath':
return label || `(${t(labels.none)})`;
return <FullPathMetricLabel data={data} />;
case 'device':
return (
<FilterLink
type="device"
value={labels[label] && label}
label={formatValue(label, 'device')}
icon={<TypeIcon type="device" value={label} />}
/>
);
return <DeviceMetricLabel data={data} />;
case 'referrer':
return (
<FilterLink
type="referrer"
value={label}
externalUrl={`https://${label}`}
label={!label ? t(labels.none) : decodePunycodeDomain(label)}
icon={<Favicon domain={label} />}
/>
);
return <ReferrerMetricLabel data={data} />;
case 'domain':
if (label === 'Other') {
return `(${t(labels.other)})`;
} else {
const name = GROUPED_DOMAINS.find(({ domain }) => domain === label)?.name;
if (!name) {
return null;
}
return (
<Row alignItems="center" gap="3">
<Favicon domain={label} />
{name}
</Row>
);
}
return <DomainMetricLabel data={data} />;
case 'language':
return formatValue(label, 'language');
return <LanguageMetricLabel data={data} />;
default:
return <FilterLink type={type} value={label} />;
return <DefaultMetricLabel type={type} data={data} />;
}
}
function BrowserOsMetricLabel({
type,
data,
}: {
type: 'browser' | 'os';
data: MetricLabelProps['data'];
}) {
const { formatValue } = useFormat();
const { label } = data;
return (
<FilterLink
type={type}
value={label}
label={formatValue(label, type)}
icon={<TypeIcon type={type} value={label} />}
/>
);
}
function ChannelMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { t, labels } = useMessages();
const { label } = data;
return t(labels[label]);
}
function CityMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { formatCity } = useFormat();
const { label, country } = data;
return (
<FilterLink
type="city"
value={label}
label={formatCity(label, country)}
icon={
country && (
<img
src={`${process.env.basePath || ''}/images/country/${country?.toLowerCase() || 'xx'}.png`}
alt={country}
/>
)
}
/>
);
}
function RegionMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { locale } = useLocale();
const { getRegionName } = useRegionNames(locale);
const { label, country } = data;
return (
<FilterLink
type="region"
value={label}
label={getRegionName(label, country)}
icon={<TypeIcon type="country" value={country} />}
/>
);
}
function CountryMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { formatValue } = useFormat();
const { locale } = useLocale();
const { countryNames } = useCountryNames(locale);
const { label } = data;
return (
<FilterLink
type="country"
value={(countryNames[label] && label) || label}
label={formatValue(label, 'country')}
icon={<TypeIcon type="country" value={label} />}
/>
);
}
function PathMetricLabel({ type, data }: MetricLabelProps) {
const { t, labels } = useMessages();
const { label, domain } = data;
return (
<FilterLink
type={type === 'entry' || type === 'exit' ? 'path' : type}
value={label}
label={!label && t(labels.none)}
externalUrl={domain ? `${domain?.startsWith('http') ? domain : `https://${domain}`}${label}` : null}
/>
);
}
function FullPathMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { t, labels } = useMessages();
const { label } = data;
return label || `(${t(labels.none)})`;
}
function DeviceMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { formatValue } = useFormat();
const { labels } = useMessages();
const { label } = data;
return (
<FilterLink
type="device"
value={labels[label] && label}
label={formatValue(label, 'device')}
icon={<TypeIcon type="device" value={label} />}
/>
);
}
function ReferrerMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { t, labels } = useMessages();
const { label } = data;
return (
<FilterLink
type="referrer"
value={label}
externalUrl={`https://${label}`}
label={!label ? t(labels.none) : decodePunycodeDomain(label)}
icon={<Favicon domain={label} />}
/>
);
}
function DomainMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { t, labels } = useMessages();
const { label } = data;
if (label === 'Other') {
return `(${t(labels.other)})`;
}
const name = GROUPED_DOMAINS.find(({ domain }) => domain === label)?.name;
if (!name) {
return null;
}
return (
<Row alignItems="center" gap="3">
<Favicon domain={label} />
{name}
</Row>
);
}
function LanguageMetricLabel({ data }: Pick<MetricLabelProps, 'data'>) {
const { formatValue } = useFormat();
const { label } = data;
return formatValue(label, 'language');
}
function DefaultMetricLabel({ type, data }: MetricLabelProps) {
const { label } = data;
return <FilterLink type={type} value={label} />;
}
@@ -0,0 +1,99 @@
import { afterEach, describe, expect, test, vi } from 'vitest';
const parseFiltersResult = {
queryParams: { websiteId: 'website-1' },
filterQuery: 'and website_event.url_path = {{path}}',
joinSessionQuery: 'join session on session.session_id = website_event.session_id',
cohortQuery: 'join cohort on cohort.session_id = website_event.session_id',
excludeBounceQuery: 'join excludeBounce on excludeBounce.visit_id = website_event.visit_id',
};
async function loadModule({
mode,
}: {
mode: 'prisma' | 'clickhouse';
}) {
vi.resetModules();
const state = { mode };
const prismaRawQuery = vi.fn().mockResolvedValue([{}]);
const prismaParseFilters = vi.fn().mockReturnValue(parseFiltersResult);
const getTimestampDiffSQL = vi.fn().mockReturnValue('ts_diff(t.min_time, t.max_time)');
const clickhouseRawQuery = vi.fn().mockResolvedValue([{}]);
const clickhouseParseFilters = vi.fn().mockReturnValue(parseFiltersResult);
vi.doMock('@/lib/db', () => ({
CLICKHOUSE: 'clickhouse',
PRISMA: 'prisma',
runQuery: vi.fn((queries: Record<string, () => unknown>) => queries[state.mode]()),
}));
vi.doMock('@/lib/prisma', () => ({
default: {
rawQuery: prismaRawQuery,
parseFilters: prismaParseFilters,
getTimestampDiffSQL,
},
}));
vi.doMock('@/lib/clickhouse', () => ({
default: {
rawQuery: clickhouseRawQuery,
parseFilters: clickhouseParseFilters,
},
}));
const mod = await import('./getPageviewExpandedMetrics');
return {
getPageviewExpandedMetrics: mod.getPageviewExpandedMetrics,
prismaRawQuery,
clickhouseRawQuery,
getTimestampDiffSQL,
};
}
afterEach(() => {
vi.resetModules();
vi.clearAllMocks();
});
describe('getPageviewExpandedMetrics postgres branch', () => {
test('skips bounce and duration work for expanded pageview rows', async () => {
const { getPageviewExpandedMetrics, prismaRawQuery, getTimestampDiffSQL } = await loadModule({
mode: 'prisma',
});
await getPageviewExpandedMetrics('website-1', { type: 'referrer' }, { path: '/pricing' } as any);
const [query] = prismaRawQuery.mock.calls[0];
expect(query).toContain('0 as "bounces"');
expect(query).toContain('0 as "totaltime"');
expect(query).not.toContain('left join (');
expect(query).not.toContain('min(website_event.created_at) as "min_time"');
expect(query).not.toContain('max(website_event.created_at) as "max_time"');
expect(query).not.toContain('event_type = 2');
expect(getTimestampDiffSQL).not.toHaveBeenCalled();
});
});
describe('getPageviewExpandedMetrics clickhouse branch', () => {
test('skips bounce and duration work for expanded pageview rows', async () => {
const { getPageviewExpandedMetrics, clickhouseRawQuery } = await loadModule({
mode: 'clickhouse',
});
await getPageviewExpandedMetrics('website-1', { type: 'referrer' }, { path: '/pricing' } as any);
const [query] = clickhouseRawQuery.mock.calls[0];
expect(query).toContain('0 as "bounces"');
expect(query).toContain('0 as "totaltime"');
expect(query).not.toContain('left join (');
expect(query).not.toContain('min(created_at) min_time');
expect(query).not.toContain('max(created_at) max_time');
expect(query).not.toContain('sum(max_time-min_time)');
expect(query).not.toContain('event_type = 2');
});
});
@@ -1,6 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import {
EVENT_TYPE,
FILTER_COLUMNS,
GROUPED_DOMAINS,
SESSION_COLUMNS,
@@ -42,7 +41,7 @@ async function relationalQuery(
): Promise<PageviewExpandedMetricsData[]> {
const { type, limit = 500, offset = 0 } = parameters;
let column = getPageviewColumn(type);
const { rawQuery, parseFilters, getTimestampDiffSQL } = prisma;
const { rawQuery, parseFilters } = prisma;
const { filterQuery, joinSessionQuery, cohortQuery, excludeBounceQuery, queryParams } =
parseFilters(
{
@@ -51,7 +50,6 @@ async function relationalQuery(
},
{ joinSession: SESSION_COLUMNS.includes(type) },
);
const needsBounceEvents = filters.excludeBounce !== true;
const fullPathSearchQuery =
type === 'fullPath' && filters.search
? `and (case when website_event.url_query != '' then website_event.url_path || '?' || website_event.url_query else website_event.url_path end) ilike {{fullPathSearch}}`
@@ -94,21 +92,6 @@ async function relationalQuery(
type === 'fullPath'
? `case when website_event.url_query != '' then website_event.url_path || '?' || website_event.url_query else website_event.url_path end`
: column;
const bounceQuery = needsBounceEvents
? `sum(case when t.c = 1 and coalesce(e.has_custom_event, 0) = 0 then 1 else 0 end) as "bounces",`
: '0 as "bounces",';
const visitEventsJoin = needsBounceEvents
? `left join (
select session_id, visit_id, 1 as "has_custom_event"
from website_event
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and event_type = ${EVENT_TYPE.customEvent}
group by 1, 2
) as e
on e.session_id = t.session_id
and e.visit_id = t.visit_id`
: '';
return rawQuery(
`
@@ -117,16 +100,14 @@ async function relationalQuery(
sum(t.c) as "pageviews",
count(distinct t.session_id) as "visitors",
count(distinct t.visit_id) as "visits",
${bounceQuery}
sum(${getTimestampDiffSQL('t.min_time', 't.max_time')}) as "totaltime"
0 as "bounces",
0 as "totaltime"
from (
select
${selectColumn} as "name",
website_event.session_id,
website_event.visit_id,
count(*) as "c",
min(website_event.created_at) as "min_time",
max(website_event.created_at) as "max_time"
count(*) as "c"
from website_event
${cohortQuery}
${excludeBounceQuery}
@@ -140,7 +121,6 @@ async function relationalQuery(
${filterQuery}
group by ${groupByColumn}, website_event.session_id, website_event.visit_id
) as t
${visitEventsJoin}
where name != ''
group by name
order by visitors desc, visits desc
@@ -167,7 +147,6 @@ async function clickhouseQuery(
...filters,
websiteId,
});
const needsBounceEvents = filters.excludeBounce !== true;
const fullPathSearchQuery =
type === 'fullPath' && filters.search
? `and positionCaseInsensitive(if(url_query != '', concat(url_path, '?', url_query), url_path), {fullPathSearch:String}) > 0`
@@ -203,19 +182,6 @@ async function clickhouseQuery(
} else if (type === 'fullPath') {
selectColumn = `if(url_query != '', concat(url_path, '?', url_query), url_path)`;
}
const bounceQuery = needsBounceEvents
? `sumIf(1, t.c = 1 and ifNull(e.has_custom_event, 0) = 0) as "bounces",`
: '0 as "bounces",';
const visitEventsJoin = needsBounceEvents
? `left join (
select session_id, visit_id, toUInt8(1) as has_custom_event
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = ${EVENT_TYPE.customEvent}
group by session_id, visit_id
) as e using (session_id, visit_id)`
: '';
return rawQuery(
`
@@ -224,16 +190,14 @@ async function clickhouseQuery(
sum(t.c) as "pageviews",
uniq(t.session_id) as "visitors",
uniq(t.visit_id) as "visits",
${bounceQuery}
sum(max_time-min_time) as "totaltime"
0 as "bounces",
0 as "totaltime"
from (
select
${selectColumn} name,
session_id,
visit_id,
count(*) c,
min(created_at) min_time,
max(created_at) max_time
count(*) c
from website_event
${cohortQuery}
${excludeBounceQuery}
@@ -247,7 +211,6 @@ async function clickhouseQuery(
${filterQuery}
group by name, session_id, visit_id
) as t
${visitEventsJoin}
group by name
order by visitors desc, visits desc
limit ${limit}