refactor(queries): replace hardcoded event types with constants

Replaced hardcoded `event_type` values with named constants from `EVENT_TYPE` for improved readability and to align these files to the named constant usage seen across the project.
This commit is contained in:
Federico D'Eredità 2025-09-09 07:57:59 +02:00
parent 1b6da0aaa0
commit da511ec76a
No known key found for this signature in database
GPG key ID: 87AF255298D68522
5 changed files with 23 additions and 16 deletions

View file

@ -1,5 +1,6 @@
import clickhouse from '@/lib/clickhouse';
import { CLICKHOUSE, getDatabaseType, POSTGRESQL, PRISMA, runQuery } from '@/lib/db';
import { EVENT_TYPE } from '@/lib/constants';
import prisma from '@/lib/prisma';
import { PageParams, QueryFilters } from '@/lib/types';
@ -44,8 +45,8 @@ async function relationalQuery(websiteId: string, filters: QueryFilters, pagePar
${filterQuery}
${
search
? `and ((event_name ${like} {{search}} and event_type = 2)
or (url_path ${like} {{search}} and event_type = 1))`
? `and ((event_name ${like} {{search}} and event_type = ${EVENT_TYPE.customEvent})
or (url_path ${like} {{search}} and event_type = ${EVENT_TYPE.pageView}))`
: ''
}
order by created_at desc
@ -82,8 +83,8 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
${filterQuery}
${
search
? `and ((positionCaseInsensitive(event_name, {search:String}) > 0 and event_type = 2)
or (positionCaseInsensitive(url_path, {search:String}) > 0 and event_type = 1))`
? `and ((positionCaseInsensitive(event_name, {search:String}) > 0 and event_type = ${EVENT_TYPE.customEvent})
or (positionCaseInsensitive(url_path, {search:String}) > 0 and event_type = ${EVENT_TYPE.pageView}))`
: ''
}
order by created_at desc

View file

@ -1,6 +1,7 @@
import clickhouse from '@/lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import { EVENT_TYPE } from '@/lib/constants';
export async function getUTM(
...args: [
@ -36,7 +37,7 @@ async function relationalQuery(
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and coalesce(url_query, '') != ''
and event_type = 1
and event_type = ${EVENT_TYPE.pageView}
group by 1
`,
{
@ -65,7 +66,7 @@ async function clickhouseQuery(
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
and url_query != ''
and event_type = 1
and event_type = ${EVENT_TYPE.pageView}
group by 1
`,
{

View file

@ -1,6 +1,7 @@
import prisma from '@/lib/prisma';
import clickhouse from '@/lib/clickhouse';
import { runQuery, PRISMA, CLICKHOUSE } from '@/lib/db';
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
import { EVENT_TYPE } from '@/lib/constants';
export async function getWebsiteSession(...args: [websiteId: string, sessionId: string]) {
return runQuery({
@ -46,8 +47,12 @@ async function relationalQuery(websiteId: string, sessionId: string) {
session.city,
min(website_event.created_at) as min_time,
max(website_event.created_at) as max_time,
sum(case when website_event.event_type = 1 then 1 else 0 end) as views,
sum(case when website_event.event_type = 2 then 1 else 0 end) as events
sum(case when website_event.event_type = ${
EVENT_TYPE.pageView
} then 1 else 0 end) as views,
sum(case when website_event.event_type = ${
EVENT_TYPE.customEvent
} then 1 else 0 end) as events
from session
join website_event on website_event.session_id = session.session_id
where session.website_id = {{websiteId::uuid}}

View file

@ -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 { QueryFilters } from '@/lib/types';
@ -33,7 +33,7 @@ async function relationalQuery(
count(distinct website_event.session_id) as "visitors",
count(distinct website_event.visit_id) as "visits",
count(distinct session.country) as "countries",
sum(case when website_event.event_type = 2 then 1 else 0 end) as "events"
sum(case when website_event.event_type = ${EVENT_TYPE.customEvent} then 1 else 0 end) as "events"
from website_event
${cohortQuery}
join session on website_event.session_id = session.session_id
@ -61,7 +61,7 @@ async function clickhouseQuery(
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item))) {
sql = `
select
sumIf(1, event_type = 1) as "pageviews",
sumIf(1, event_type = ${EVENT_TYPE.pageView}) as "pageviews",
uniq(session_id) as "visitors",
uniq(visit_id) as "visits",
uniq(country) as "countries",

View file

@ -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, getDatabaseType, POSTGRESQL, PRISMA, runQuery } from '@/lib/db';
import prisma from '@/lib/prisma';
import { PageParams, QueryFilters } from '@/lib/types';
@ -39,7 +39,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters, pagePar
min(website_event.created_at) as "firstAt",
max(website_event.created_at) as "lastAt",
count(distinct website_event.visit_id) as "visits",
sum(case when website_event.event_type = 1 then 1 else 0 end) as "views",
sum(case when website_event.event_type = ${EVENT_TYPE.pageView} then 1 else 0 end) as "views",
max(website_event.created_at) as "createdAt"
from website_event
${cohortQuery}
@ -96,7 +96,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
${getDateStringSQL('min(created_at)')} as firstAt,
${getDateStringSQL('max(created_at)')} as lastAt,
uniq(visit_id) as visits,
sumIf(views, event_type = 1) as views,
sumIf(views, event_type = ${EVENT_TYPE.pageView}) as views,
lastAt as createdAt
from website_event
${cohortQuery}
@ -131,7 +131,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
${getDateStringSQL('min(min_time)')} as firstAt,
${getDateStringSQL('max(max_time)')} as lastAt,
uniq(visit_id) as visits,
sumIf(views, event_type = 1) as views,
sumIf(views, event_type = ${EVENT_TYPE.pageView}) as views,
lastAt as createdAt
from website_event_stats_hourly website_event
${cohortQuery}