This commit is contained in:
Mike Cao committed 2026-05-07 14:54:25 -07:00
commit 6c2155d496
157 files changed
+1389 -643

No files matched your search

+27 -105
View File
@@ -1,5 +1,4 @@
import clickhouse from '@/lib/clickhouse';
import { DEFAULT_PAGE_SIZE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { EventPropertyFilter, QueryFilters } from '@/lib/types';
@@ -27,10 +26,7 @@ async function relationalQuery(
eventFilters: EventPropertyFilter[] = [],
) {
const { timezone = 'utc' } = filters;
const { rawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = prisma;
const { page = 1, pageSize } = filters;
const size = +pageSize || DEFAULT_PAGE_SIZE;
const offset = +size * (+page - 1);
const { pagedRawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = prisma;
const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({
...filters,
@@ -39,30 +35,10 @@ async function relationalQuery(
});
const { sql: pfSQL, params: pfParams } = getPropertyFilterQuery(eventFilters, 'event', timezone);
const countResult = await rawQuery(
`
select count(distinct website_event.event_id) as num
from website_event
join event_data on event_data.website_event_id = website_event.event_id
and event_data.website_id = {{websiteId::uuid}}
and event_data.created_at between {{startDate}} and {{endDate}}
${cohortQuery}
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_name = {{eventName}}
${filterQuery}
${pfSQL}
`,
{ ...queryParams, eventName, ...pfParams },
);
const count = countResult[0].num;
const rows = await rawQuery(
return pagedRawQuery(
`
with paged_events as (
select website_event.event_id
select website_event.event_id, max(website_event.created_at) as sort_created_at
from website_event
join event_data on event_data.website_event_id = website_event.event_id
and event_data.website_id = {{websiteId::uuid}}
@@ -75,68 +51,43 @@ async function relationalQuery(
${filterQuery}
${pfSQL}
group by website_event.event_id
order by max(website_event.created_at) desc
limit ${size} offset ${offset}
)
select
website_event.event_id as "eventId",
website_event.session_id as "sessionId",
website_event.event_name as "eventName",
website_event.url_path as "urlPath",
website_event.created_at as "createdAt",
event_data.data_key as "dataKey",
coalesce(
case when event_data.data_type = 1 then event_data.string_value end,
case when event_data.data_type = 2 then cast(event_data.number_value as varchar) end,
case when event_data.data_type = 3 then event_data.string_value end,
case when event_data.data_type = 4 then ${getDateStringSQL('event_data.date_value', 'second', timezone)} end,
case when event_data.data_type = 5 then event_data.string_value end,
''
) as "value",
event_data.data_type as "dataType"
max(website_event.created_at) as "createdAt",
array_agg(event_data.data_key order by event_data.data_key asc) as "propertyKeys",
array_agg(
coalesce(
case when event_data.data_type = 1 then event_data.string_value end,
case when event_data.data_type = 2 then cast(event_data.number_value as varchar) end,
case when event_data.data_type = 3 then event_data.string_value end,
case when event_data.data_type = 4 then ${getDateStringSQL('event_data.date_value', 'second', timezone)} end,
case when event_data.data_type = 5 then event_data.string_value end,
''
)
order by event_data.data_key asc
) as "propertyValues"
from event_data
join website_event on website_event.event_id = event_data.website_event_id
and website_event.website_id = {{websiteId::uuid}}
join paged_events on paged_events.event_id = event_data.website_event_id
where event_data.website_id = {{websiteId::uuid}}
and event_data.created_at between {{startDate}} and {{endDate}}
order by website_event.created_at desc
group by
website_event.event_id,
website_event.session_id,
website_event.event_name,
website_event.url_path,
paged_events.sort_created_at
order by paged_events.sort_created_at desc
`,
{ ...queryParams, eventName, ...pfParams },
filters,
FUNCTION_NAME,
);
// Pivot flat rows into one record per event
const eventMap = new Map<
string,
{
eventId: string;
sessionId: string;
eventName: string;
urlPath: string;
createdAt: Date;
propertyKeys: string[];
propertyValues: string[];
}
>();
for (const { eventId, sessionId, eventName: name, urlPath, createdAt, dataKey, value } of rows) {
if (!eventMap.has(eventId)) {
eventMap.set(eventId, {
eventId,
sessionId,
eventName: name,
urlPath,
createdAt,
propertyKeys: [],
propertyValues: [],
});
}
const entry = eventMap.get(eventId);
entry.propertyKeys.push(dataKey);
entry.propertyValues.push(value ?? '');
}
return { data: [...eventMap.values()], count, page: +page, pageSize: size };
}
async function clickhouseQuery(
@@ -146,10 +97,7 @@ async function clickhouseQuery(
eventFilters: EventPropertyFilter[] = [],
) {
const { timezone = 'UTC' } = filters;
const { rawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = clickhouse;
const { page = 1, pageSize } = filters;
const size = +pageSize || DEFAULT_PAGE_SIZE;
const offset = +size * (+page - 1);
const { pagedRawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = clickhouse;
const { filterQuery, cohortQuery, queryParams } = parseFilters({
...filters,
@@ -158,31 +106,7 @@ async function clickhouseQuery(
});
const { sql: pfSQL, params: pfParams } = getPropertyFilterQuery(eventFilters, 'event', timezone);
const count = await rawQuery(
`
select count() as num
from umami.event_data_pivot
any left join (
select *
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type = 2
and event_name = {eventName:String}) website_event
on website_event.event_id = event_data_pivot.event_id
and website_event.session_id = event_data_pivot.session_id
and website_event.website_id = event_data_pivot.website_id
${cohortQuery}
where event_data_pivot.website_id = {websiteId:UUID}
and event_data_pivot.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_data_pivot.event_name = {eventName:String}
${filterQuery}
${pfSQL}
`,
{ ...queryParams, eventName, ...pfParams },
).then((res: any) => res[0].num);
const data = await rawQuery(
return pagedRawQuery(
`
select
event_data_pivot.event_id as eventId,
@@ -224,11 +148,9 @@ async function clickhouseQuery(
event_data_pivot.url_path,
event_data_pivot.created_at
order by event_data_pivot.created_at desc
limit ${size} offset ${offset}
`,
{ ...queryParams, eventName, ...pfParams },
filters,
FUNCTION_NAME,
);
return { data, count, page: +page, pageSize: size };
}
@@ -1,4 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';
@@ -55,6 +56,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
join session on session.session_id = website_event.session_id
and session.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.event_type != ${EVENT_TYPE.performance}
${dateQuery}
${filterQuery}
${searchQuery}
@@ -107,6 +109,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
from website_event
${cohortQuery}
where website_id = {websiteId:UUID}
and event_type != ${EVENT_TYPE.performance}
${dateQuery}
${filterQuery}
${searchQuery}
+3
View File
@@ -1,4 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';
@@ -38,6 +39,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
on session.session_id = website_event.session_id
and session.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${dateQuery}
order by website_event.created_at desc
@@ -71,6 +73,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters): Promis
from website_event
${cohortQuery}
where website_id = {websiteId:UUID}
and event_type != ${EVENT_TYPE.performance}
${filterQuery}
${dateQuery}
order by createdAt desc
-4
View File
@@ -96,10 +96,6 @@ async function clickhouseQuery(websiteId: string, column: string, filters: Query
excludeDomain = `and referrer_domain != hostname and referrer_domain != ''`;
}
if (search) {
searchQuery = `and positionCaseInsensitive(${column}, {search:String}) > 0`;
}
if (search) {
if (decodeURIComponent(search).includes(',')) {
searchQuery = `AND (${decodeURIComponent(search)
@@ -1,4 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';
@@ -64,6 +65,7 @@ async function relationalQuery(websiteId: string, currency: string, filters: Que
and upper(currency) = {{currency}}
) rev on rev.session_id = website_event.session_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.event_type != ${EVENT_TYPE.performance}
${dateQuery}
${filterQuery}
${searchQuery}
@@ -126,6 +128,7 @@ async function clickhouseQuery(websiteId: string, currency: string, filters: Que
from website_event
${cohortQuery}
where website_id = {websiteId:UUID}
and event_type != ${EVENT_TYPE.performance}
${dateQuery}
${filterQuery}
${searchQuery}
@@ -1,4 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';
@@ -37,6 +38,7 @@ async function relationalQuery(websiteId: string, sessionId: string, filters: Qu
from website_event
where website_id = {{websiteId::uuid}}
and session_id = {{sessionId::uuid}}
and event_type != ${EVENT_TYPE.performance}
and created_at between {{startDate}} and {{endDate}}
order by created_at desc
limit 500
@@ -70,6 +72,7 @@ async function clickhouseQuery(websiteId: string, sessionId: string, filters: Qu
from website_event
where website_id = {websiteId:UUID}
and session_id = {sessionId:UUID}
and event_type != ${EVENT_TYPE.performance}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
order by created_at desc
limit 500
@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { DATA_TYPE } from '@/lib/constants';
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { PropertyFilter, PropertyLeaderboardRow, QueryFilters } from '@/lib/types';
@@ -22,7 +22,7 @@ async function relationalQuery(
propertyFilters: PropertyFilter[] = [],
) {
const { timezone = 'utc' } = filters;
const { rawQuery, parseFilters, getPropertyFilterQuery, getTimestampDiffSQL } = prisma;
const { rawQuery, parseFilters, getPropertyFilterQuery } = prisma;
const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({
...filters,
websiteId,
@@ -39,6 +39,7 @@ async function relationalQuery(
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
),
@@ -57,6 +58,7 @@ async function relationalQuery(
and filtered_sessions.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
group by website_event.session_id, website_event.visit_id
),
session_stats as (
@@ -65,8 +67,7 @@ async function relationalQuery(
count(*) as visits,
sum(activity) as activity,
sum(views) as views,
sum(events) as events,
sum(${getTimestampDiffSQL('min_time', 'max_time')}) as totaltime
sum(events) as events
from session_rollup
group by session_id
),
@@ -89,8 +90,7 @@ async function relationalQuery(
count(distinct property_values.session_id) as sessions,
coalesce(sum(session_stats.visits), 0) as visits,
coalesce(sum(session_stats.views), 0) as views,
coalesce(sum(session_stats.events), 0) as events,
coalesce(sum(session_stats.totaltime), 0) as totaltime
coalesce(sum(session_stats.events), 0) as events
from property_values
join session_stats on session_stats.session_id = property_values.session_id
group by property_values.value
@@ -121,6 +121,7 @@ async function clickhouseQuery(
${cohortQuery}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
),
@@ -137,6 +138,7 @@ async function clickhouseQuery(
join filtered_sessions on filtered_sessions.session_id = website_event.session_id
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
group by website_event.session_id, website_event.visit_id
),
session_stats as (
@@ -145,8 +147,7 @@ async function clickhouseQuery(
count() as visits,
sum(activity) as activity,
sum(views) as views,
sum(events) as events,
sum(max_time - min_time) as totaltime
sum(events) as events
from session_rollup
group by session_id
),
@@ -167,8 +168,7 @@ async function clickhouseQuery(
uniq(property_values.session_id) as sessions,
ifNull(sum(session_stats.visits), 0) as visits,
ifNull(sum(session_stats.views), 0) as views,
ifNull(sum(session_stats.events), 0) as events,
ifNull(sum(session_stats.totaltime), 0) as totaltime
ifNull(sum(session_stats.events), 0) as events
from property_values
join session_stats on session_stats.session_id = property_values.session_id
group by property_values.value
@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { DATA_TYPE } from '@/lib/constants';
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { EventDataSeriesPoint, PropertyFilter, QueryFilters } from '@/lib/types';
@@ -50,6 +50,7 @@ async function relationalQuery(
cross join lateral jsonb_array_elements_text(coalesce(session_data.string_value, '[]')::jsonb) as array_item(value)
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {{propertyName}}
and session_data.data_type = ${DATA_TYPE.array}
${filterQuery}
@@ -86,6 +87,7 @@ async function clickhouseQuery(
and session_data.website_id = {websiteId:UUID}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {propertyName:String}
and session_data.data_type = ${DATA_TYPE.array}
${filterQuery}
@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { DATA_TYPE } from '@/lib/constants';
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { EventDataDateSeriesPoint, PropertyFilter, QueryFilters } from '@/lib/types';
@@ -48,6 +48,7 @@ async function relationalQuery(
and session_data.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {{propertyName}}
and session_data.data_type = ${DATA_TYPE.date}
${filterQuery}
@@ -83,6 +84,7 @@ async function clickhouseQuery(
and session_data.website_id = {websiteId:UUID}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {propertyName:String}
and session_data.data_type = ${DATA_TYPE.date}
${filterQuery}
@@ -1,4 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { PropertyFilter, QueryFilters } from '@/lib/types';
@@ -51,6 +52,7 @@ async function relationalQuery(
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
)
@@ -98,6 +100,7 @@ async function clickhouseQuery(
${cohortQuery}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
)
@@ -1,4 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { EventDataNumericStats, PropertyFilter, QueryFilters } from '@/lib/types';
@@ -43,6 +44,7 @@ async function relationalQuery(
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
)
@@ -84,6 +86,7 @@ async function clickhouseQuery(
${cohortQuery}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
)
+13 -94
View File
@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { DEFAULT_PAGE_SIZE } from '@/lib/constants';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { PageResult, PropertyFilter, QueryFilters, SessionDataPivotRow } from '@/lib/types';
@@ -25,12 +25,9 @@ async function relationalQuery(
propertyName: string,
filters: QueryFilters,
propertyFilters: PropertyFilter[] = [],
): Promise<PageResult<SessionDataPivotRow[]>> {
) {
const { timezone = 'utc' } = filters;
const { rawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = prisma;
const { page = 1, pageSize } = filters;
const size = +pageSize || DEFAULT_PAGE_SIZE;
const offset = +size * (+page - 1);
const { pagedRawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = prisma;
const { filterQuery, cohortQuery, joinSessionQuery, queryParams } = parseFilters({
...filters,
@@ -43,47 +40,7 @@ async function relationalQuery(
timezone,
);
const countResult = (await rawQuery(
`
with filtered_sessions as (
select distinct website_event.session_id
from website_event
${cohortQuery}
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
${filterQuery}
${pfSQL}
),
latest_session_properties as (
select
ranked.session_id,
ranked.data_key
from (
select
session_data.session_id,
session_data.data_key,
row_number() over (
partition by session_data.session_id, session_data.data_key
order by session_data.created_at desc, session_data.session_data_id desc
) as row_num
from session_data
join filtered_sessions
on filtered_sessions.session_id = session_data.session_id
where session_data.website_id = {{websiteId::uuid}}
) ranked
where ranked.row_num = 1
)
select count(*) as num
from latest_session_properties
where latest_session_properties.data_key = {{propertyName}}
`,
{ ...queryParams, websiteId, propertyName, ...pfParams },
)) as { num: number }[];
const count = countResult[0].num;
const rows = (await rawQuery(
return pagedRawQuery(
`
with filtered_sessions as (
select distinct website_event.session_id
@@ -92,6 +49,7 @@ async function relationalQuery(
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
),
@@ -132,8 +90,6 @@ async function relationalQuery(
latest_session_properties.created_at as sort_created_at
from latest_session_properties
where latest_session_properties.data_key = {{propertyName}}
order by latest_session_properties.created_at desc
limit ${size} offset ${offset}
)
select
latest_session_properties.session_id as "sessionId",
@@ -158,10 +114,9 @@ async function relationalQuery(
order by paged_sessions.sort_created_at desc
`,
{ ...queryParams, websiteId, propertyName, ...pfParams },
filters,
FUNCTION_NAME,
)) as SessionDataPivotRow[];
return { data: rows, count, page: +page, pageSize: size };
);
}
async function clickhouseQuery(
@@ -169,12 +124,9 @@ async function clickhouseQuery(
propertyName: string,
filters: QueryFilters,
propertyFilters: PropertyFilter[] = [],
): Promise<PageResult<SessionDataPivotRow[]>> {
) {
const { timezone = 'UTC' } = filters;
const { rawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = clickhouse;
const { page = 1, pageSize } = filters;
const size = +pageSize || DEFAULT_PAGE_SIZE;
const offset = +size * (+page - 1);
const { pagedRawQuery, parseFilters, getPropertyFilterQuery, getDateStringSQL } = clickhouse;
const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId, timezone });
const { sql: pfSQL, params: pfParams } = getPropertyFilterQuery(
@@ -183,38 +135,7 @@ async function clickhouseQuery(
timezone,
);
const countResult = (await rawQuery(
`
with filtered_sessions as (
select distinct website_event.session_id
from website_event
${cohortQuery}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
${filterQuery}
${pfSQL}
),
latest_session_properties as (
select
session_data.session_id as session_id,
session_data.data_key as data_key
from session_data final
join filtered_sessions
on filtered_sessions.session_id = session_data.session_id
where session_data.website_id = {websiteId:UUID}
group by
session_data.session_id,
session_data.data_key
)
select count() as num
from latest_session_properties
where latest_session_properties.data_key = {propertyName:String}
`,
{ ...queryParams, websiteId, propertyName, ...pfParams },
)) as { num: number }[];
const count = countResult[0].num;
const data = (await rawQuery(
return pagedRawQuery(
`
with filtered_sessions as (
select distinct website_event.session_id
@@ -222,6 +143,7 @@ async function clickhouseQuery(
${cohortQuery}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
),
@@ -249,8 +171,6 @@ async function clickhouseQuery(
latest_session_properties.created_at as sort_created_at
from latest_session_properties
where latest_session_properties.data_key = {propertyName:String}
order by latest_session_properties.created_at desc
limit ${size} offset ${offset}
)
select
latest_session_properties.session_id as sessionId,
@@ -274,8 +194,7 @@ async function clickhouseQuery(
order by paged_sessions.sort_created_at desc
`,
{ ...queryParams, websiteId, propertyName, ...pfParams },
filters,
FUNCTION_NAME,
)) as SessionDataPivotRow[];
return { data, count, page: +page, pageSize: size };
);
}
@@ -1,4 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { PropertyFilter, QueryFilters } from '@/lib/types';
@@ -10,7 +11,7 @@ export async function getSessionDataProperties(
websiteId: string,
filters: QueryFilters,
propertyFilters?: PropertyFilter[],
selectedPropertyName?: string,
propertyName?: string,
]
) {
return runQuery({
@@ -23,7 +24,7 @@ async function relationalQuery(
websiteId: string,
filters: QueryFilters,
propertyFilters: PropertyFilter[] = [],
selectedPropertyName?: string,
propertyName?: string,
) {
const { timezone = 'utc' } = filters;
const { rawQuery, parseFilters, getPropertyFilterQuery } = prisma;
@@ -47,6 +48,7 @@ async function relationalQuery(
${joinSessionQuery}
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
),
@@ -56,7 +58,7 @@ async function relationalQuery(
join filtered_sessions
on filtered_sessions.session_id = session_data.session_id
and filtered_sessions.website_id = session_data.website_id
${selectedPropertyName ? 'where session_data.data_key = {{selectedPropertyName}}' : ''}
${propertyName ? 'where session_data.data_key = {{propertyName}}' : ''}
)
select
data_key as "propertyName",
@@ -70,7 +72,7 @@ async function relationalQuery(
order by 3 desc, 1 asc
limit 500
`,
{ ...queryParams, websiteId, selectedPropertyName, ...pfParams },
{ ...queryParams, websiteId, propertyName, ...pfParams },
FUNCTION_NAME,
);
}
@@ -79,7 +81,7 @@ async function clickhouseQuery(
websiteId: string,
filters: QueryFilters,
propertyFilters: PropertyFilter[] = [],
selectedPropertyName?: string,
propertyName?: string,
): Promise<{ propertyName: string; dataType: number; total: number }[]> {
const { timezone = 'UTC' } = filters;
const { rawQuery, parseFilters, getPropertyFilterQuery } = clickhouse;
@@ -98,6 +100,7 @@ async function clickhouseQuery(
${cohortQuery}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
${pfSQL}
),
@@ -107,7 +110,7 @@ async function clickhouseQuery(
join filtered_sessions
on filtered_sessions.session_id = session_data.session_id
and filtered_sessions.website_id = session_data.website_id
${selectedPropertyName ? 'where session_data.data_key = {selectedPropertyName:String}' : ''}
${propertyName ? 'where session_data.data_key = {propertyName:String}' : ''}
)
select
data_key as propertyName,
@@ -123,7 +126,7 @@ async function clickhouseQuery(
order by 3 desc, 1 asc
limit 500
`,
{ ...queryParams, websiteId, selectedPropertyName, ...pfParams },
{ ...queryParams, websiteId, propertyName, ...pfParams },
FUNCTION_NAME,
);
}
@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { DATA_TYPE } from '@/lib/constants';
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { EventDataSeriesPoint, PropertyFilter, QueryFilters } from '@/lib/types';
@@ -49,6 +49,7 @@ async function relationalQuery(
and session_data.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {{propertyName}}
and session_data.data_type in (${DATA_TYPE.string}, ${DATA_TYPE.boolean})
${filterQuery}
@@ -85,6 +86,7 @@ async function clickhouseQuery(
and session_data.website_id = {websiteId:UUID}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {propertyName:String}
and session_data.data_type in (${DATA_TYPE.string}, ${DATA_TYPE.boolean})
${filterQuery}
@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { DATA_TYPE } from '@/lib/constants';
import { DATA_TYPE, EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';
@@ -41,6 +41,7 @@ async function relationalQuery(
cross join lateral jsonb_array_elements_text(coalesce(session_data.string_value, '[]')::jsonb) as array_item(value)
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {{propertyName}}
and session_data.data_type = ${DATA_TYPE.array}
${filterQuery}
@@ -70,6 +71,7 @@ async function relationalQuery(
and session_data.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {{propertyName}}
${dataType ? `and session_data.data_type = ${dataType}` : ''}
${filterQuery}
@@ -103,6 +105,7 @@ async function clickhouseQuery(
and session_data.website_id = {websiteId:UUID}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {propertyName:String}
and session_data.data_type = ${DATA_TYPE.array}
${filterQuery}
@@ -129,6 +132,7 @@ async function clickhouseQuery(
and session_data.website_id = {websiteId:UUID}
where website_event.website_id = {websiteId:UUID}
and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_event.event_type != ${EVENT_TYPE.performance}
and session_data.data_key = {propertyName:String}
${dataType ? `and session_data.data_type = ${dataType}` : ''}
${filterQuery}
@@ -1,4 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
@@ -54,6 +55,7 @@ async function relationalQuery(websiteId: string, sessionId: string) {
join website_event on website_event.session_id = session.session_id
where session.website_id = {{websiteId::uuid}}
and session.session_id = {{sessionId::uuid}}
and website_event.event_type != ${EVENT_TYPE.performance}
group by session.session_id, session.distinct_id, visit_id, session.website_id, session.browser, session.os, session.device, session.screen, session.language, session.country, session.region, session.city) t
group by id, distinct_id, website_id, browser, os, device, screen, language, country, region, city;
`,
@@ -104,6 +106,7 @@ async function clickhouseQuery(websiteId: string, sessionId: string) {
from website_event_stats_hourly
where website_id = {websiteId:UUID}
and session_id = {sessionId:UUID}
and event_type != ${EVENT_TYPE.performance}
group by session_id, distinct_id, visit_id, website_id, browser, os, device, screen, language, country, region, city) t
group by id, websiteId, distinctId, browser, os, device, screen, language, country, region, city;
`,
@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_COLUMNS } from '@/lib/constants';
import { EVENT_COLUMNS, EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';
@@ -36,7 +36,7 @@ async function relationalQuery(
return rawQuery(
`
select
count(*) as "pageviews",
sum(case when website_event.event_type = 1 then 1 else 0 end) as "pageviews",
count(distinct website_event.session_id) as "visitors",
count(distinct website_event.visit_id) as "visits",
count(distinct session.country) as "countries",
@@ -47,6 +47,7 @@ async function relationalQuery(
and website_event.website_id = session.website_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type != ${EVENT_TYPE.performance}
${filterQuery}
`,
queryParams,
@@ -75,6 +76,7 @@ async function clickhouseQuery(
${cohortQuery}
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type != ${EVENT_TYPE.performance}
${filterQuery}
`;
} else {
@@ -89,6 +91,7 @@ async function clickhouseQuery(
${cohortQuery}
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and event_type != ${EVENT_TYPE.performance}
${filterQuery}
`;
}
@@ -1,5 +1,5 @@
import clickhouse from '@/lib/clickhouse';
import { EVENT_COLUMNS } from '@/lib/constants';
import { EVENT_COLUMNS, EVENT_TYPE } from '@/lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';
@@ -55,6 +55,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
join session on session.session_id = website_event.session_id
and session.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.event_type != ${EVENT_TYPE.performance}
${dateQuery}
${filterQuery}
${searchQuery}
@@ -118,6 +119,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
from website_event
${cohortQuery}
where website_id = {websiteId:UUID}
and event_type != ${EVENT_TYPE.performance}
${dateQuery}
${filterQuery}
${searchQuery}
@@ -147,6 +149,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
from website_event_stats_hourly as website_event
${cohortQuery}
where website_id = {websiteId:UUID}
and event_type != ${EVENT_TYPE.performance}
${dateQuery}
${filterQuery}
${searchQuery}