v3 prisma queries update

This commit is contained in:
Francis Cao 2025-10-27 11:48:35 -07:00
parent 61b667c587
commit 2ffde37f5b
21 changed files with 119 additions and 108 deletions

View file

@ -118,6 +118,7 @@ function getCohortQuery(filters: QueryFilters = {}) {
(select distinct website_event.session_id (select distinct website_event.session_id
from website_event from website_event
join session on session.session_id = website_event.session_id join session on session.session_id = website_event.session_id
and session.website_id = website_event.website_id
where website_event.website_id = {{websiteId}} where website_event.website_id = {{websiteId}}
and website_event.created_at between {{cohort_startDate}} and {{cohort_endDate}} and website_event.created_at between {{cohort_startDate}} and {{cohort_endDate}}
${filterQuery} ${filterQuery}
@ -165,7 +166,7 @@ function parseFilters(filters: Record<string, any>, options?: QueryOptions) {
return { return {
joinSessionQuery: joinSessionQuery:
options?.joinSession || joinSession options?.joinSession || joinSession
? `inner join session on website_event.session_id = session.session_id` ? `inner join session on website_event.session_id = session.session_id and website_event.website_id = session.website_id`
: '', : '',
dateQuery: getDateQuery(filters), dateQuery: getDateQuery(filters),
filterQuery: getFilterQuery(filters, options), filterQuery: getFilterQuery(filters, options),
@ -225,8 +226,8 @@ async function pagedQuery<T>(model: string, criteria: T, filters?: QueryFilters)
async function pagedRawQuery( async function pagedRawQuery(
query: string, query: string,
filters: QueryFilters,
queryParams: Record<string, any>, queryParams: Record<string, any>,
filters: QueryFilters,
name?: string, name?: string,
) { ) {
const { page = 1, pageSize, orderBy, sortDescending = false } = filters; const { page = 1, pageSize, orderBy, sortDescending = false } = filters;

View file

@ -19,17 +19,17 @@ async function relationalQuery(websiteId: string, eventId: string) {
return rawQuery( return rawQuery(
` `
select website_id as websiteId, select website_id as "websiteId",
session_id as sessionId, session_id as "sessionId",
event_id as eventId, event_id as "eventId",
url_path as urlPath, url_path as "urlPath",
event_name as eventName, event_name as "eventName",
data_key as dataKey, data_key as "dataKey",
string_value as stringValue, string_value as "stringValue",
number_value as numberValue, number_value as "numberValue",
date_value as dateValue, date_value as "dateValue",
data_type as dataType, data_type as "dataType",
created_at as createdAt created_at as "createdAt"
from event_data from event_data
website_id = {{websiteId::uuid}} website_id = {{websiteId::uuid}}
event_id = {{eventId::uuid}} event_id = {{eventId::uuid}}

View file

@ -18,7 +18,6 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
const { filterQuery, dateQuery, cohortQuery, queryParams } = parseFilters({ const { filterQuery, dateQuery, cohortQuery, queryParams } = parseFilters({
...filters, ...filters,
websiteId, websiteId,
search: `%${search}%`,
}); });
const searchQuery = search const searchQuery = search
@ -29,32 +28,33 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
return pagedRawQuery( return pagedRawQuery(
` `
select select
event_id as "id", website_event.event_id as "id",
website_id as "websiteId", website_event.website_id as "websiteId",
session_id as "sessionId", website_event.session_id as "sessionId",
created_at as "createdAt", website_event.created_at as "createdAt",
hostname, website_event.hostname,
url_path as "urlPath", website_event.url_path as "urlPath",
url_query as "urlQuery", website_event.url_query as "urlQuery",
referrer_path as "referrerPath", website_event.referrer_path as "referrerPath",
referrer_query as "referrerQuery", website_event.referrer_query as "referrerQuery",
referrer_domain as "referrerDomain", website_event.referrer_domain as "referrerDomain",
country as country, session.country as country,
city as city, city as city,
device as device, device as device,
os as os, os as os,
browser as browser, browser as browser,
page_title as "pageTitle", page_title as "pageTitle",
event_type as "eventType", website_event.event_type as "eventType",
event_name as "eventName" website_event.event_name as "eventName"
from website_event from website_event
${cohortQuery} ${cohortQuery}
join session on website_event.session_id = session.session_id join session on session.session_id = website_event.session_id
where website_id = {{websiteId::uuid}} and session.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}}
${dateQuery} ${dateQuery}
${filterQuery} ${filterQuery}
${searchQuery} ${searchQuery}
order by created_at desc order by website_event.created_at desc
`, `,
queryParams, queryParams,
filters, filters,

View file

@ -18,7 +18,7 @@ async function relationalQuery(websiteId: string) {
const result = await rawQuery( const result = await rawQuery(
` `
select count(distinct session_id) as visitors select count(distinct session_id) as "visitors"
from website_event from website_event
where website_id = {{websiteId::uuid}} where website_id = {{websiteId::uuid}}
and created_at >= {{startDate}} and created_at >= {{startDate}}

View file

@ -49,19 +49,19 @@ async function relationalQuery(
return rawQuery( return rawQuery(
` `
WITH prefix AS ( WITH prefix AS (
select case when utm_medium LIKE 'p%' OR select case when website_event.utm_medium LIKE 'p%' OR
utm_medium LIKE '%ppc%' OR website_event.utm_medium LIKE '%ppc%' OR
utm_medium LIKE '%retargeting%' OR website_event.utm_medium LIKE '%retargeting%' OR
utm_medium LIKE '%paid%' then 'paid' else 'organic' end prefix, website_event.utm_medium LIKE '%paid%' then 'paid' else 'organic' end prefix,
referrer_domain, website_event.referrer_domain,
url_query, website_event.url_query,
utm_medium, website_event.utm_medium,
utm_source, website_event.utm_source,
session_id, website_event.session_id,
visit_id, website_event.visit_id,
count(*) c, count(*) c,
min(created_at) min_time, min(website_event.created_at) min_time,
max(created_at) max_time max(website_event.created_at) max_time
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}
@ -70,12 +70,12 @@ async function relationalQuery(
${dateQuery} ${dateQuery}
${filterQuery} ${filterQuery}
group by prefix, group by prefix,
referrer_domain, website_event.referrer_domain,
url_query, website_event.url_query,
utm_medium, website_event.utm_medium,
utm_source, website_event.utm_source,
session_id, website_event.session_id,
visit_id), website_event.visit_id),
channels as ( channels as (
select case select case

View file

@ -30,15 +30,15 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
return rawQuery( return rawQuery(
` `
WITH prefix AS ( WITH prefix AS (
select case when utm_medium LIKE 'p%' OR select case when website_event.utm_medium LIKE 'p%' OR
utm_medium LIKE '%ppc%' OR website_event.utm_medium LIKE '%ppc%' OR
utm_medium LIKE '%retargeting%' OR website_event.utm_medium LIKE '%retargeting%' OR
utm_medium LIKE '%paid%' then 'paid' else 'organic' end prefix, website_event.utm_medium LIKE '%paid%' then 'paid' else 'organic' end prefix,
referrer_domain, website_event.referrer_domain,
url_query, website_event.url_query,
utm_medium, website_event.utm_medium,
utm_source, website_event.utm_source,
session_id website_event.session_id
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}

View file

@ -35,6 +35,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
${cohortQuery} ${cohortQuery}
inner join session inner join session
on session.session_id = website_event.session_id on session.session_id = website_event.session_id
and session.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
${filterQuery} ${filterQuery}
${dateQuery} ${dateQuery}

View file

@ -51,6 +51,7 @@ async function relationalQuery(websiteId: string, column: string, filters: Query
from website_event from website_event
inner join session inner join session
on session.session_id = website_event.session_id on session.session_id = website_event.session_id
and session.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
${searchQuery} ${searchQuery}

View file

@ -20,8 +20,8 @@ async function relationalQuery(websiteId: string) {
const result = await rawQuery( const result = await rawQuery(
` `
select select
min(created_at) as startDate, min(created_at) as "startDate",
max(created_at) as endDate max(created_at) as "endDate"
from website_event from website_event
where website_id = {{websiteId::uuid}} where website_id = {{websiteId::uuid}}
and created_at >= {{startDate}} and created_at >= {{startDate}}

View file

@ -24,13 +24,13 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
return rawQuery( return rawQuery(
` `
select select
${getDateWeeklySQL('created_at', timezone)} as time, ${getDateWeeklySQL('website_event.created_at', timezone)} as time,
count(distinct session_id) as value count(distinct website_event.session_id) as value
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
${filterQuery} ${filterQuery}
group by time group by time
order by 2 order by 2

View file

@ -103,6 +103,7 @@ async function relationalQuery(
${filterQuery} ${filterQuery}
group by name, website_event.session_id, website_event.visit_id group by name, website_event.session_id, website_event.visit_id
) as t ) as t
where name != ''
group by name group by name
order by visitors desc, visits desc order by visitors desc, visits desc
limit ${limit} limit ${limit}

View file

@ -69,14 +69,14 @@ async function relationalQuery(
const eventQuery = `WITH events AS ( const eventQuery = `WITH events AS (
select distinct select distinct
session_id, website_event.session_id,
max(created_at) max_dt max(website_event.created_at) max_dt
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
and ${column} = {{step}} and website_event.${column} = {{step}}
${filterQuery} ${filterQuery}
group by 1),`; group by 1),`;
@ -234,14 +234,14 @@ async function relationalQuery(
` `
select select
count(*) as "pageviews", count(*) as "pageviews",
count(distinct session_id) as "visitors", count(distinct website_event.session_id) as "visitors",
count(distinct visit_id) as "visits" count(distinct website_event.visit_id) as "visits"
from website_event from website_event
${joinSessionQuery} ${joinSessionQuery}
${cohortQuery} ${cohortQuery}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
and ${column} = {{step}} and website_event.${column} = {{step}}
${filterQuery} ${filterQuery}
`, `,
queryParams, queryParams,

View file

@ -67,12 +67,12 @@ async function relationalQuery(
if (levelNumber === 1) { if (levelNumber === 1) {
pv.levelOneQuery = ` pv.levelOneQuery = `
WITH level1 AS ( WITH level1 AS (
select distinct session_id, created_at select distinct website_event.session_id, website_event.created_at
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
and ${column} ${operator} {{${i}}} and ${column} ${operator} {{${i}}}
${filterQuery} ${filterQuery}
)`; )`;

View file

@ -42,26 +42,26 @@ async function relationalQuery(
return rawQuery( return rawQuery(
` `
select count(*) as num, select count(distinct website_event.session_id) as num,
( (
select count(distinct session_id) select count(distinct website_event.session_id)
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
${dateQuery} ${dateQuery}
${filterQuery} ${filterQuery}
) as total ) as total
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and ${column} = {{value}} and ${column} = {{value}}
${dateQuery} ${dateQuery}
${filterQuery} ${filterQuery}
`, `,
queryParams, queryParams,
); ).then(results => results?.[0]);
} }
async function clickhouseQuery( async function clickhouseQuery(
@ -84,7 +84,7 @@ async function clickhouseQuery(
return rawQuery( return rawQuery(
` `
select count(*) as num, select count(distinct session_id) as num,
( (
select count(distinct session_id) select count(distinct session_id)
from website_event from website_event

View file

@ -117,16 +117,16 @@ async function relationalQuery(
` `
WITH events AS ( WITH events AS (
select distinct select distinct
visit_id, website_event.visit_id,
referrer_path, website_event.referrer_path,
coalesce(nullIf(event_name, ''), url_path) event, coalesce(nullIf(website_event.event_name, ''), website_event.url_path) event,
row_number() OVER (PARTITION BY visit_id ORDER BY created_at) AS event_number row_number() OVER (PARTITION BY visit_id ORDER BY website_event.created_at) AS event_number
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}), and website_event.created_at between {{startDate}} and {{endDate}}
${filterQuery} ${filterQuery}),
${sequenceQuery} ${sequenceQuery}
select * select *
from sequences from sequences

View file

@ -37,13 +37,13 @@ async function relationalQuery(
return rawQuery( return rawQuery(
` `
select ${column} utm, count(*) as views select website_event.${column} utm, count(*) as views
from website_event from website_event
${cohortQuery} ${cohortQuery}
${joinSessionQuery} ${joinSessionQuery}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
and coalesce(${column}, '') != '' and coalesce(website_event.${column}, '') != ''
${filterQuery} ${filterQuery}
group by 1 group by 1
order by 2 desc order by 2 desc

View file

@ -21,20 +21,23 @@ async function relationalQuery(websiteId: string, sessionId: string, filters: Qu
return rawQuery( return rawQuery(
` `
select select
created_at as createdAt, created_at as "createdAt",
url_path as urlPath, url_path as "urlPath",
url_query as urlQuery, url_query as "urlQuery",
referrer_domain as referrerDomain, referrer_domain as "referrerDomain",
event_id as eventId, event_id as "eventId",
event_type as eventType, event_type as "eventType",
event_name as eventName, event_name as "eventName",
visit_id as visitId, visit_id as "visitId",
event_id IN (SELECT event_id FROM event_data) AS hasData event_id IN (select event_id
from website_event e from event_data
where e.website_id = {websiteId:UUID} where website_id = {{websiteId::uuid}}
and e.session_id = {sessionId:UUID} and session_id = {{sessionId::uuid}}) AS "hasData"
and e.created_at between {startDate:DateTime64} and {endDate:DateTime64} from website_event
order by e.created_at desc where website_id = {{websiteId::uuid}}
and session_id = {{sessionId::uuid}}
and created_at between {{startDate}} and {{endDate}}
order by created_at desc
limit 500 limit 500
`, `,
{ websiteId, sessionId, startDate, endDate }, { websiteId, sessionId, startDate, endDate },

View file

@ -31,6 +31,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
${joinSessionQuery} ${joinSessionQuery}
join session_data join session_data
on session_data.session_id = website_event.session_id on session_data.session_id = website_event.session_id
and session_data.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
${filterQuery} ${filterQuery}

View file

@ -38,6 +38,7 @@ async function relationalQuery(
${joinSessionQuery} ${joinSessionQuery}
join session_data join session_data
on session_data.session_id = website_event.session_id on session_data.session_id = website_event.session_id
and session_data.website_id = website_event.website_id
where website_event.website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
and session_data.data_key = {{propertyName}} and session_data.data_key = {{propertyName}}

View file

@ -44,6 +44,7 @@ async function relationalQuery(
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
and website_event.website_id = session.website_id
where website_event.website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
${filterQuery} ${filterQuery}

View file

@ -52,6 +52,7 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
from website_event from website_event
${cohortQuery} ${cohortQuery}
join session on session.session_id = website_event.session_id 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}} where website_event.website_id = {{websiteId::uuid}}
${dateQuery} ${dateQuery}
${filterQuery} ${filterQuery}