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

@ -46,7 +46,9 @@ async function relationalQuery(
},
) {
const { toUuid, rawQuery, getDateQuery, getFilterQuery } = prisma;
const params: any = [websiteId, startDate, endDate];
const website = await cache.fetchWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;
const params: any = [websiteId, resetDate, startDate, endDate];
return rawQuery(
`select
@ -55,7 +57,8 @@ async function relationalQuery(
count(*) y
from website_event
where website_id = $1${toUuid()}
and created_at between $2 and $3
and created_at >= $2
and created_at between $3 and $4
and event_type = ${EVENT_TYPE.customEvent}
${getFilterQuery(filters, params)}
group by 1, 2
@ -83,9 +86,10 @@ async function clickhouseQuery(
};
},
) {
const { rawQuery, getDateQuery, getBetweenDates, getFilterQuery } = clickhouse;
const { rawQuery, getDateQuery, getDateFormat, getBetweenDates, getFilterQuery } = clickhouse;
const website = await cache.fetchWebsite(websiteId);
const params = { websiteId, revId: website?.revId || 0 };
const resetDate = website?.resetAt || website?.createdAt;
const params = { websiteId };
return rawQuery(
`select
@ -94,8 +98,8 @@ async function clickhouseQuery(
count(*) y
from event
where website_id = {websiteId:UUID}
and rev_id = {revId:UInt32}
and event_type = ${EVENT_TYPE.customEvent}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
${getFilterQuery(filters, params)}
group by x, t

View file

@ -3,7 +3,6 @@ import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import kafka from 'lib/kafka';
import prisma from 'lib/prisma';
import { uuid } from 'lib/crypto';
import cache from 'lib/cache';
import { saveEventData } from '../eventData/saveEventData';
export async function saveEvent(args: {
@ -41,7 +40,6 @@ async function relationalQuery(data: {
eventData?: any;
}) {
const { websiteId, id: sessionId, urlPath, urlQuery, eventName, eventData, pageTitle } = data;
const website = await cache.fetchWebsite(websiteId);
const websiteEventId = uuid();
const websiteEvent = prisma.client.websiteEvent.create({
@ -62,7 +60,6 @@ async function relationalQuery(data: {
websiteId,
sessionId,
eventId: websiteEventId,
revId: website?.revId,
urlPath: urlPath?.substring(0, URL_LENGTH),
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData,
@ -106,7 +103,6 @@ async function clickhouseQuery(data: {
...args
} = data;
const { getDateFormat, sendMessage } = kafka;
const website = await cache.fetchWebsite(websiteId);
const eventId = uuid();
const createdAt = getDateFormat(new Date());
@ -123,7 +119,6 @@ async function clickhouseQuery(data: {
page_title: pageTitle,
event_type: EVENT_TYPE.customEvent,
event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
rev_id: website?.revId || 0,
created_at: createdAt,
...args,
};
@ -135,7 +130,6 @@ async function clickhouseQuery(data: {
websiteId,
sessionId,
eventId,
revId: website?.revId,
urlPath: urlPath?.substring(0, URL_LENGTH),
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData,