update queries for event / session properties and values screens

This commit is contained in:
Francis Cao 2024-08-21 13:51:28 -07:00
parent 2c0ba65ba0
commit 917916c224
5 changed files with 59 additions and 40 deletions

View File

@ -13,7 +13,7 @@ export function TypeIcon({
<>
<img
src={`${process.env.basePath || ''}/images/${type}/${
value.replaceAll(' ', '-').toLowerCase() || 'unknown'
value?.replaceAll(' ', '-').toLowerCase() || 'unknown'
}.png`}
alt={value}
width={type === 'country' ? undefined : 16}

View File

@ -24,15 +24,15 @@ async function relationalQuery(
return rawQuery(
`
select
we.event_name as "eventName",
ed.data_key as "propertyName",
website_event.event_name as "eventName",
event_data.data_key as "propertyName",
count(*) as "total"
from event_data ed
join website_event we on we.event_id = ed.website_event_id
where ed.website_id = {{websiteId::uuid}}
and ed.created_at between {{startDate}} and {{endDate}}
from event_data
join website_event on website_event.event_id = event_data.website_event_id
where event_data.website_id = {{websiteId::uuid}}
and event_data.created_at between {{startDate}} and {{endDate}}
${filterQuery}
group by we.event_name, ed.data_key
group by website_event.event_name, event_data.data_key
order by 3 desc
limit 500
`,

View File

@ -19,21 +19,26 @@ async function relationalQuery(
websiteId: string,
filters: QueryFilters & { eventName?: string; propertyName?: string },
) {
const { rawQuery, parseFilters } = prisma;
const { rawQuery, parseFilters, getDateSQL } = prisma;
const { filterQuery, params } = await parseFilters(websiteId, filters);
return rawQuery(
`
select
string_value as "value",
case
when data_type = 2 then replace(string_value, '.0000', '')
when data_type = 4 then ${getDateSQL('date_value', 'hour')}
else string_value
end as "value",
count(*) as "total"
from event_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and data_key = {{propertyName}}
and event_key = {{eventName}}
join website_event on website_event.event_id = event_data.website_event_id
where event_data.website_id = {{websiteId::uuid}}
and event_data.created_at between {{startDate}} and {{endDate}}
and event_data.data_key = {{propertyName}}
and website_event.event_name = {{eventName}}
${filterQuery}
group by string_value
group by value
order by 2 desc
limit 100
`,

View File

@ -16,20 +16,24 @@ async function relationalQuery(
websiteId: string,
filters: QueryFilters & { propertyName?: string },
) {
const { rawQuery, parseFilters } = prisma;
const { rawQuery, parseFilters, getDateSQL } = prisma;
const { filterQuery, params } = await parseFilters(websiteId, filters);
return rawQuery(
`
select
string_value as "value",
case
when data_type = 2 then replace(string_value, '.0000', '')
when data_type = 4 then ${getDateSQL('date_value', 'hour')}
else string_value
end as "value",
count(*) as "total"
from session_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
and data_key = {{propertyName}}
${filterQuery}
group by string_value
group by value
order by 2 desc
limit 100
`,

View File

@ -22,29 +22,39 @@ async function relationalQuery(websiteId: string, filters: QueryFilters, pagePar
`
with sessions as (
select
s.session_id as "id",
s.website_id as "websiteId",
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
city,
min(we.created_at) as "firstAt",
max(we.created_at) as "lastAt",
count(distinct we.visit_id) as "visits",
sum(case when we.event_type = 1 then 1 else 0 end) as "views",
max(we.created_at) as "createdAt"
from website_event we
join session s on s.session_id = we.session_id
where we.website_id = {{websiteId::uuid}}
and we.created_at between {{startDate}} and {{endDate}}
session.session_id as "id",
session.website_id as "websiteId",
session.hostname,
session.browser,
session.os,
session.device,
session.screen,
session.language,
session.country,
session.subdivision1,
session.city,
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",
max(website_event.created_at) as "createdAt"
from website_event
join session on session.session_id = website_event.session_id
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
${filterQuery}
group by s.session_id, s.website_id, s.hostname, s.browser, s.os, s.device, s.screen, s.language, s.country, s.subdivision1, s.city
order by max(we.created_at) desc
group by session.session_id,
session.website_id,
session.hostname,
session.browser,
session.os,
session.device,
session.screen,
session.language,
session.country,
session.subdivision1,
session.city
order by max(website_event.created_at) desc
limit 1000)
select * from sessions
`,