From cd6a28ea4a7fafd1dc352447430ff1a00ba82933 Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Mon, 19 Jan 2026 12:46:21 -0800 Subject: [PATCH] improve revenue joins and event_data joins to website_event --- src/queries/sql/events/getEventDataEvents.ts | 20 +++++++--- src/queries/sql/events/getEventDataFields.ts | 10 +++-- .../sql/events/getEventDataProperties.ts | 10 +++-- src/queries/sql/events/getEventDataStats.ts | 10 +++-- src/queries/sql/events/getEventDataValues.ts | 10 +++-- src/queries/sql/reports/getRevenue.ts | 38 +++++++++++-------- 6 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/queries/sql/events/getEventDataEvents.ts b/src/queries/sql/events/getEventDataEvents.ts index 6c8f12c1..8ed6633e 100644 --- a/src/queries/sql/events/getEventDataEvents.ts +++ b/src/queries/sql/events/getEventDataEvents.ts @@ -93,11 +93,15 @@ async function clickhouseQuery( string_value as propertyValue, count(*) as total from event_data - join website_event + 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) website_event on website_event.event_id = event_data.event_id + and website_event.session_id = event_data.session_id and website_event.website_id = event_data.website_id - and website_event.website_id = {websiteId:UUID} - and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64} ${cohortQuery} where event_data.website_id = {websiteId:UUID} and event_data.created_at between {startDate:DateTime64} and {endDate:DateTime64} @@ -120,11 +124,15 @@ async function clickhouseQuery( data_type as dataType, count(*) as total from event_data - join website_event + 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) website_event on website_event.event_id = event_data.event_id + and website_event.session_id = event_data.session_id and website_event.website_id = event_data.website_id - and website_event.website_id = {websiteId:UUID} - and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64} ${cohortQuery} where event_data.website_id = {websiteId:UUID} and event_data.created_at between {startDate:DateTime64} and {endDate:DateTime64} diff --git a/src/queries/sql/events/getEventDataFields.ts b/src/queries/sql/events/getEventDataFields.ts index 93377690..70945492 100644 --- a/src/queries/sql/events/getEventDataFields.ts +++ b/src/queries/sql/events/getEventDataFields.ts @@ -65,11 +65,15 @@ async function clickhouseQuery( string_value) as "value", count(*) as "total" from event_data - join website_event + 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) website_event on website_event.event_id = event_data.event_id + and website_event.session_id = event_data.session_id and website_event.website_id = event_data.website_id - and website_event.website_id = {websiteId:UUID} - and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64} ${cohortQuery} where event_data.website_id = {websiteId:UUID} and event_data.created_at between {startDate:DateTime64} and {endDate:DateTime64} diff --git a/src/queries/sql/events/getEventDataProperties.ts b/src/queries/sql/events/getEventDataProperties.ts index 82c078f8..82d0b6b6 100644 --- a/src/queries/sql/events/getEventDataProperties.ts +++ b/src/queries/sql/events/getEventDataProperties.ts @@ -69,11 +69,15 @@ async function clickhouseQuery( data_key as propertyName, count(*) as total from event_data - join website_event + 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) website_event on website_event.event_id = event_data.event_id + and website_event.session_id = event_data.session_id and website_event.website_id = event_data.website_id - and website_event.website_id = {websiteId:UUID} - and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64} ${cohortQuery} where event_data.website_id = {websiteId:UUID} and event_data.created_at between {startDate:DateTime64} and {endDate:DateTime64} diff --git a/src/queries/sql/events/getEventDataStats.ts b/src/queries/sql/events/getEventDataStats.ts index 89e13582..9d178f67 100644 --- a/src/queries/sql/events/getEventDataStats.ts +++ b/src/queries/sql/events/getEventDataStats.ts @@ -72,11 +72,15 @@ async function clickhouseQuery( data_key, count(*) as "total" from event_data - join website_event + 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) website_event on website_event.event_id = event_data.event_id + and website_event.session_id = event_data.session_id and website_event.website_id = event_data.website_id - and website_event.website_id = {websiteId:UUID} - and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64} ${cohortQuery} where event_data.website_id = {websiteId:UUID} and event_data.created_at between {startDate:DateTime64} and {endDate:DateTime64} diff --git a/src/queries/sql/events/getEventDataValues.ts b/src/queries/sql/events/getEventDataValues.ts index 0426e646..7c1ae282 100644 --- a/src/queries/sql/events/getEventDataValues.ts +++ b/src/queries/sql/events/getEventDataValues.ts @@ -72,11 +72,15 @@ async function clickhouseQuery( string_value) as "value", count(*) as "total" from event_data - join website_event + 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) website_event on website_event.event_id = event_data.event_id + and website_event.session_id = event_data.session_id and website_event.website_id = event_data.website_id - and website_event.website_id = {websiteId:UUID} - and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64} ${cohortQuery} where event_data.website_id = {websiteId:UUID} and event_data.created_at between {startDate:DateTime64} and {endDate:DateTime64} diff --git a/src/queries/sql/reports/getRevenue.ts b/src/queries/sql/reports/getRevenue.ts index 2a4604fe..d3bff6c9 100644 --- a/src/queries/sql/reports/getRevenue.ts +++ b/src/queries/sql/reports/getRevenue.ts @@ -43,12 +43,14 @@ async function relationalQuery( const joinQuery = filterQuery || cohortQuery - ? `join website_event - on website_event.website_id = revenue.website_id - and website_event.session_id = revenue.session_id - and website_event.event_id = revenue.event_id - and website_event.website_id = {{websiteId::uuid}} - and website_event.created_at between {{startDate}} and {{endDate}}` + ? `join (select * + from website_event + where website_id = {{websiteId::uuid}} + and created_at between {{startDate}} and {{endDate}} + and event_type = 2) website_event + on website_event.website_id = revenue.website_id + and website_event.session_id = revenue.session_id + and website_event.event_id = revenue.event_id` : ''; const chart = await rawQuery( @@ -130,12 +132,15 @@ async function clickhouseQuery( }); const joinQuery = filterQuery - ? `join website_event - on website_event.website_id = website_revenue.website_id - and website_event.session_id = website_revenue.session_id - and website_event.event_id = website_revenue.event_id - and website_event.website_id = {websiteId:UUID} - and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}` + ? `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) website_event + on website_event.website_id = website_revenue.website_id + and website_event.session_id = website_revenue.session_id + and website_event.event_id = website_revenue.event_id` : ''; const chart = await rawQuery< @@ -174,12 +179,15 @@ async function clickhouseQuery( website_event.country as name, sum(website_revenue.revenue) as value from website_revenue - join website_event + 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) website_event on website_event.website_id = website_revenue.website_id and website_event.session_id = website_revenue.session_id and website_event.event_id = website_revenue.event_id - and website_event.website_id = {websiteId:UUID} - and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64} ${cohortQuery} where website_revenue.website_id = {websiteId:UUID} and website_revenue.created_at between {startDate:DateTime64} and {endDate:DateTime64}