diff --git a/src/components/metrics/MetricLabel.tsx b/src/components/metrics/MetricLabel.tsx index c0b9857d5..425f2f54f 100644 --- a/src/components/metrics/MetricLabel.tsx +++ b/src/components/metrics/MetricLabel.tsx @@ -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 ( - } - /> - ); + return ; case 'channel': - return t(labels[label]); + return ; case 'city': - return ( - - ) - } - /> - ); + return ; case 'region': - return ( - } - /> - ); + return ; case 'country': - return ( - } - /> - ); + return ; case 'path': case 'entry': case 'exit': - return ( - - ); + return ; case 'fullPath': - return label || `(${t(labels.none)})`; + return ; case 'device': - return ( - } - /> - ); + return ; case 'referrer': - return ( - } - /> - ); + return ; case 'domain': - if (label === 'Other') { - return `(${t(labels.other)})`; - } else { - const name = GROUPED_DOMAINS.find(({ domain }) => domain === label)?.name; - - if (!name) { - return null; - } - - return ( - - - {name} - - ); - } + return ; case 'language': - return formatValue(label, 'language'); + return ; default: - return ; + return ; } } + +function BrowserOsMetricLabel({ + type, + data, +}: { + type: 'browser' | 'os'; + data: MetricLabelProps['data']; +}) { + const { formatValue } = useFormat(); + const { label } = data; + + return ( + } + /> + ); +} + +function ChannelMetricLabel({ data }: Pick) { + const { t, labels } = useMessages(); + const { label } = data; + + return t(labels[label]); +} + +function CityMetricLabel({ data }: Pick) { + const { formatCity } = useFormat(); + const { label, country } = data; + + return ( + + ) + } + /> + ); +} + +function RegionMetricLabel({ data }: Pick) { + const { locale } = useLocale(); + const { getRegionName } = useRegionNames(locale); + const { label, country } = data; + + return ( + } + /> + ); +} + +function CountryMetricLabel({ data }: Pick) { + const { formatValue } = useFormat(); + const { locale } = useLocale(); + const { countryNames } = useCountryNames(locale); + const { label } = data; + + return ( + } + /> + ); +} + +function PathMetricLabel({ type, data }: MetricLabelProps) { + const { t, labels } = useMessages(); + const { label, domain } = data; + + return ( + + ); +} + +function FullPathMetricLabel({ data }: Pick) { + const { t, labels } = useMessages(); + const { label } = data; + + return label || `(${t(labels.none)})`; +} + +function DeviceMetricLabel({ data }: Pick) { + const { formatValue } = useFormat(); + const { labels } = useMessages(); + const { label } = data; + + return ( + } + /> + ); +} + +function ReferrerMetricLabel({ data }: Pick) { + const { t, labels } = useMessages(); + const { label } = data; + + return ( + } + /> + ); +} + +function DomainMetricLabel({ data }: Pick) { + 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 ( + + + {name} + + ); +} + +function LanguageMetricLabel({ data }: Pick) { + const { formatValue } = useFormat(); + const { label } = data; + + return formatValue(label, 'language'); +} + +function DefaultMetricLabel({ type, data }: MetricLabelProps) { + const { label } = data; + + return ; +} diff --git a/src/queries/sql/pageviews/getPageviewExpandedMetrics.test.ts b/src/queries/sql/pageviews/getPageviewExpandedMetrics.test.ts new file mode 100644 index 000000000..a6f616d11 --- /dev/null +++ b/src/queries/sql/pageviews/getPageviewExpandedMetrics.test.ts @@ -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 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'); + }); +}); diff --git a/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts b/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts index d66b912f0..23294124d 100644 --- a/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts +++ b/src/queries/sql/pageviews/getPageviewExpandedMetrics.ts @@ -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 { 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}