update schema and queries to implement reset_at

This commit is contained in:
Francis Cao 2023-03-27 11:25:16 -07:00
parent 43ef6884df
commit 14e4a090bb
15 changed files with 74 additions and 59 deletions

View file

@ -50,7 +50,6 @@ async function clickhouseQuery(data: {
city,
} = data;
const { getDateFormat, sendMessage } = kafka;
const website = await cache.fetchWebsite(websiteId);
const msg = {
session_id: id,
@ -65,7 +64,6 @@ async function clickhouseQuery(data: {
subdivision1,
subdivision2,
city,
rev_id: website?.revId || 0,
created_at: getDateFormat(new Date()),
};

View file

@ -20,9 +20,11 @@ async function relationalQuery(
websiteId: string,
data: { startDate: Date; endDate: Date; field: string; filters: object },
) {
const website = await cache.fetchWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;
const { startDate, endDate, field, filters = {} } = data;
const { toUuid, parseFilters, rawQuery } = prisma;
const params: any = [websiteId, startDate, endDate];
const params: any = [websiteId, resetDate, startDate, endDate];
const { filterQuery, joinSession } = parseFilters(filters, params);
return rawQuery(
@ -35,7 +37,8 @@ async function relationalQuery(
on website_event.website_id = website.website_id
${joinSession}
where website.website_id = $1${toUuid()}
and website_event.created_at between $2 and $3
and website_event.created_at >= $2
and website_event.created_at between $3 and $4
${filterQuery}
)
group by 1
@ -50,17 +53,18 @@ async function clickhouseQuery(
data: { startDate: Date; endDate: Date; field: string; filters: object },
) {
const { startDate, endDate, field, filters = {} } = data;
const { parseFilters, getBetweenDates, rawQuery } = clickhouse;
const { getDateFormat, parseFilters, getBetweenDates, rawQuery } = clickhouse;
const website = await cache.fetchWebsite(websiteId);
const params = { websiteId, revId: website?.revId || 0 };
const resetDate = website?.resetAt || website?.createdAt;
const params = { websiteId };
const { filterQuery } = parseFilters(filters, params);
return rawQuery(
`select ${field} x, count(distinct session_id) y
from event as x
where website_id = {websiteId:UUID}
and rev_id = {revId:UInt32}
and event_type = ${EVENT_TYPE.pageView}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
${filterQuery}
group by x