mirror of
https://github.com/umami-software/umami.git
synced 2026-02-19 20:15:41 +01:00
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:
parent
1b6da0aaa0
commit
da511ec76a
5 changed files with 23 additions and 16 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import clickhouse from '@/lib/clickhouse';
|
import clickhouse from '@/lib/clickhouse';
|
||||||
import { CLICKHOUSE, getDatabaseType, POSTGRESQL, PRISMA, runQuery } from '@/lib/db';
|
import { CLICKHOUSE, getDatabaseType, POSTGRESQL, PRISMA, runQuery } from '@/lib/db';
|
||||||
|
import { EVENT_TYPE } from '@/lib/constants';
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from '@/lib/prisma';
|
||||||
import { PageParams, QueryFilters } from '@/lib/types';
|
import { PageParams, QueryFilters } from '@/lib/types';
|
||||||
|
|
||||||
|
|
@ -44,8 +45,8 @@ async function relationalQuery(websiteId: string, filters: QueryFilters, pagePar
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
${
|
${
|
||||||
search
|
search
|
||||||
? `and ((event_name ${like} {{search}} and event_type = 2)
|
? `and ((event_name ${like} {{search}} and event_type = ${EVENT_TYPE.customEvent})
|
||||||
or (url_path ${like} {{search}} and event_type = 1))`
|
or (url_path ${like} {{search}} and event_type = ${EVENT_TYPE.pageView}))`
|
||||||
: ''
|
: ''
|
||||||
}
|
}
|
||||||
order by created_at desc
|
order by created_at desc
|
||||||
|
|
@ -82,8 +83,8 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
|
||||||
${filterQuery}
|
${filterQuery}
|
||||||
${
|
${
|
||||||
search
|
search
|
||||||
? `and ((positionCaseInsensitive(event_name, {search:String}) > 0 and event_type = 2)
|
? `and ((positionCaseInsensitive(event_name, {search:String}) > 0 and event_type = ${EVENT_TYPE.customEvent})
|
||||||
or (positionCaseInsensitive(url_path, {search:String}) > 0 and event_type = 1))`
|
or (positionCaseInsensitive(url_path, {search:String}) > 0 and event_type = ${EVENT_TYPE.pageView}))`
|
||||||
: ''
|
: ''
|
||||||
}
|
}
|
||||||
order by created_at desc
|
order by created_at desc
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import clickhouse from '@/lib/clickhouse';
|
import clickhouse from '@/lib/clickhouse';
|
||||||
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from '@/lib/prisma';
|
||||||
|
import { EVENT_TYPE } from '@/lib/constants';
|
||||||
|
|
||||||
export async function getUTM(
|
export async function getUTM(
|
||||||
...args: [
|
...args: [
|
||||||
|
|
@ -36,7 +37,7 @@ async function relationalQuery(
|
||||||
where website_id = {{websiteId::uuid}}
|
where website_id = {{websiteId::uuid}}
|
||||||
and created_at between {{startDate}} and {{endDate}}
|
and created_at between {{startDate}} and {{endDate}}
|
||||||
and coalesce(url_query, '') != ''
|
and coalesce(url_query, '') != ''
|
||||||
and event_type = 1
|
and event_type = ${EVENT_TYPE.pageView}
|
||||||
group by 1
|
group by 1
|
||||||
`,
|
`,
|
||||||
{
|
{
|
||||||
|
|
@ -65,7 +66,7 @@ async function clickhouseQuery(
|
||||||
where website_id = {websiteId:UUID}
|
where website_id = {websiteId:UUID}
|
||||||
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
||||||
and url_query != ''
|
and url_query != ''
|
||||||
and event_type = 1
|
and event_type = ${EVENT_TYPE.pageView}
|
||||||
group by 1
|
group by 1
|
||||||
`,
|
`,
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from '@/lib/prisma';
|
||||||
import clickhouse from '@/lib/clickhouse';
|
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]) {
|
export async function getWebsiteSession(...args: [websiteId: string, sessionId: string]) {
|
||||||
return runQuery({
|
return runQuery({
|
||||||
|
|
@ -46,8 +47,12 @@ async function relationalQuery(websiteId: string, sessionId: string) {
|
||||||
session.city,
|
session.city,
|
||||||
min(website_event.created_at) as min_time,
|
min(website_event.created_at) as min_time,
|
||||||
max(website_event.created_at) as max_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 = ${
|
||||||
sum(case when website_event.event_type = 2 then 1 else 0 end) as events
|
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
|
from session
|
||||||
join website_event on website_event.session_id = session.session_id
|
join website_event on website_event.session_id = session.session_id
|
||||||
where session.website_id = {{websiteId::uuid}}
|
where session.website_id = {{websiteId::uuid}}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import clickhouse from '@/lib/clickhouse';
|
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 { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db';
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from '@/lib/prisma';
|
||||||
import { QueryFilters } from '@/lib/types';
|
import { QueryFilters } from '@/lib/types';
|
||||||
|
|
@ -33,7 +33,7 @@ async function relationalQuery(
|
||||||
count(distinct website_event.session_id) as "visitors",
|
count(distinct website_event.session_id) as "visitors",
|
||||||
count(distinct website_event.visit_id) as "visits",
|
count(distinct website_event.visit_id) as "visits",
|
||||||
count(distinct session.country) as "countries",
|
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
|
from website_event
|
||||||
${cohortQuery}
|
${cohortQuery}
|
||||||
join session on website_event.session_id = session.session_id
|
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))) {
|
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item))) {
|
||||||
sql = `
|
sql = `
|
||||||
select
|
select
|
||||||
sumIf(1, event_type = 1) as "pageviews",
|
sumIf(1, event_type = ${EVENT_TYPE.pageView}) as "pageviews",
|
||||||
uniq(session_id) as "visitors",
|
uniq(session_id) as "visitors",
|
||||||
uniq(visit_id) as "visits",
|
uniq(visit_id) as "visits",
|
||||||
uniq(country) as "countries",
|
uniq(country) as "countries",
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import clickhouse from '@/lib/clickhouse';
|
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 { CLICKHOUSE, getDatabaseType, POSTGRESQL, PRISMA, runQuery } from '@/lib/db';
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from '@/lib/prisma';
|
||||||
import { PageParams, QueryFilters } from '@/lib/types';
|
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",
|
min(website_event.created_at) as "firstAt",
|
||||||
max(website_event.created_at) as "lastAt",
|
max(website_event.created_at) as "lastAt",
|
||||||
count(distinct website_event.visit_id) as "visits",
|
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"
|
max(website_event.created_at) as "createdAt"
|
||||||
from website_event
|
from website_event
|
||||||
${cohortQuery}
|
${cohortQuery}
|
||||||
|
|
@ -96,7 +96,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
|
||||||
${getDateStringSQL('min(created_at)')} as firstAt,
|
${getDateStringSQL('min(created_at)')} as firstAt,
|
||||||
${getDateStringSQL('max(created_at)')} as lastAt,
|
${getDateStringSQL('max(created_at)')} as lastAt,
|
||||||
uniq(visit_id) as visits,
|
uniq(visit_id) as visits,
|
||||||
sumIf(views, event_type = 1) as views,
|
sumIf(views, event_type = ${EVENT_TYPE.pageView}) as views,
|
||||||
lastAt as createdAt
|
lastAt as createdAt
|
||||||
from website_event
|
from website_event
|
||||||
${cohortQuery}
|
${cohortQuery}
|
||||||
|
|
@ -131,7 +131,7 @@ async function clickhouseQuery(websiteId: string, filters: QueryFilters, pagePar
|
||||||
${getDateStringSQL('min(min_time)')} as firstAt,
|
${getDateStringSQL('min(min_time)')} as firstAt,
|
||||||
${getDateStringSQL('max(max_time)')} as lastAt,
|
${getDateStringSQL('max(max_time)')} as lastAt,
|
||||||
uniq(visit_id) as visits,
|
uniq(visit_id) as visits,
|
||||||
sumIf(views, event_type = 1) as views,
|
sumIf(views, event_type = ${EVENT_TYPE.pageView}) as views,
|
||||||
lastAt as createdAt
|
lastAt as createdAt
|
||||||
from website_event_stats_hourly website_event
|
from website_event_stats_hourly website_event
|
||||||
${cohortQuery}
|
${cohortQuery}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue