use functions to convert dates to string

This commit is contained in:
Francis Cao 2023-08-14 22:18:21 -07:00
parent 8f542ed998
commit e45d014042

View file

@ -25,7 +25,7 @@ async function relationalQuery(
}, },
): Promise< ): Promise<
{ {
date: Date; date: string;
day: number; day: number;
visitors: number; visitors: number;
returnVisitors: number; returnVisitors: number;
@ -33,13 +33,15 @@ async function relationalQuery(
}[] }[]
> { > {
const { startDate, endDate } = dateRange; const { startDate, endDate } = dateRange;
const { rawQuery } = prisma; const { getDateQuery, rawQuery } = prisma;
const timezone = 'utc';
const unit = 'day';
return rawQuery( return rawQuery(
` `
WITH cohort_items AS ( WITH cohort_items AS (
select session_id, select session_id,
date_trunc('day', created_at)::date as cohort_date ${getDateQuery('created_at', unit, timezone)} as cohort_date
from session from session
where website_id = {{websiteId::uuid}} where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}} and created_at between {{startDate}} and {{endDate}}
@ -47,7 +49,7 @@ async function relationalQuery(
user_activities AS ( user_activities AS (
select distinct select distinct
w.session_id, w.session_id,
(date_trunc('day', w.created_at)::date - c.cohort_date::date) as day_number (${getDateQuery('created_at', unit, timezone)}::date - c.cohort_date::date) as day_number
from website_event w from website_event w
join cohort_items c join cohort_items c
on w.session_id = c.session_id on w.session_id = c.session_id
@ -98,7 +100,7 @@ async function clickhouseQuery(
}, },
): Promise< ): Promise<
{ {
date: Date; date: string;
day: number; day: number;
visitors: number; visitors: number;
returnVisitors: number; returnVisitors: number;
@ -106,13 +108,15 @@ async function clickhouseQuery(
}[] }[]
> { > {
const { startDate, endDate } = dateRange; const { startDate, endDate } = dateRange;
const { rawQuery } = clickhouse; const { getDateQuery, getDateStringQuery, rawQuery } = clickhouse;
const timezone = 'UTC';
const unit = 'day';
return rawQuery( return rawQuery(
` `
WITH cohort_items AS ( WITH cohort_items AS (
select select
min(date_trunc('day', created_at)) as cohort_date, min(${getDateQuery('created_at', unit, timezone)}) as cohort_date,
session_id session_id
from website_event from website_event
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
@ -122,7 +126,7 @@ async function clickhouseQuery(
user_activities AS ( user_activities AS (
select distinct select distinct
w.session_id, w.session_id,
(date_trunc('day', w.created_at) - c.cohort_date) / 86400 as day_number (${getDateQuery('created_at', unit, timezone)} - c.cohort_date) / 86400 as day_number
from website_event w from website_event w
join cohort_items c join cohort_items c
on w.session_id = c.session_id on w.session_id = c.session_id
@ -147,7 +151,7 @@ async function clickhouseQuery(
group by 1, 2 group by 1, 2
) )
select select
c.cohort_date as date, ${getDateStringQuery('c.cohort_date', unit)} as date,
c.day_number as day, c.day_number as day,
s.visitors as visitors, s.visitors as visitors,
c.visitors returnVisitors, c.visitors returnVisitors,