Refactored queries.

This commit is contained in:
Mike Cao 2023-07-24 23:06:16 -07:00
parent e4bd314bd6
commit 4bd3ef8e12
19 changed files with 330 additions and 408 deletions

View file

@ -45,26 +45,27 @@ async function relationalQuery(
};
},
) {
const { toUuid, rawQuery, getDateQuery, getFilterQuery } = prisma;
const { rawQuery, getDateQuery, getFilterQuery } = prisma;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params: any = [websiteId, resetDate, startDate, endDate];
const filterQuery = getFilterQuery(filters, params);
const filterQuery = getFilterQuery(filters);
return rawQuery(
`select
`
select
event_name x,
${getDateQuery('created_at', unit, timezone)} t,
count(*) y
from website_event
where website_id = $1${toUuid()}
and created_at >= $2
and created_at between $3 and $4
where website_id = {{websiteId::uuid}}
and created_at >= {{resetDate}}
and created_at between {{startDate}} and {{endDate}}
and event_type = ${EVENT_TYPE.customEvent}
${filterQuery}
group by 1, 2
order by 2`,
params,
order by 2
`,
{ ...filters, websiteId, resetDate, startDate, endDate },
);
}
@ -87,24 +88,26 @@ async function clickhouseQuery(
};
},
) {
const { rawQuery, getDateQuery, getDateFormat, getFilterQuery } = clickhouse;
const { rawQuery, getDateQuery, getFilterQuery } = clickhouse;
const website = await loadWebsite(websiteId);
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
const params = { websiteId };
const filterQuery = getFilterQuery(filters);
return rawQuery(
`select
`
select
event_name x,
${getDateQuery('created_at', unit, timezone)} t,
count(*) y
from website_event
where website_id = {websiteId:UUID}
and event_type = ${EVENT_TYPE.customEvent}
and created_at >= ${getDateFormat(resetDate)}
and created_at between ${getDateFormat(startDate)} and ${getDateFormat(endDate)}
${getFilterQuery(filters, params)}
and created_at >= {resetDate:DateTIme}
and created_at between {startDate:DateTime} and {endDate:DateTime}
${filterQuery}
group by x, t
order by t`,
params,
order by t
`,
{ ...filters, websiteId, resetDate, startDate, endDate },
);
}

View file

@ -1,28 +1,26 @@
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { CLICKHOUSE, PRISMA, runQuery, notImplemented } from 'lib/db';
export function getEventUsage(...args: [websiteIds: string[], startDate: Date, endDate: Date]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[PRISMA]: notImplemented,
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
function relationalQuery(websiteIds: string[], startDate: Date, endDate: Date) {
throw new Error('Not implemented.');
}
function clickhouseQuery(websiteIds: string[], startDate: Date, endDate: Date) {
const { rawQuery } = clickhouse;
return rawQuery(
`select
website_id as websiteId,
count(*) as count
`
select
website_id as websiteId,
count(*) as count
from website_event
where created_at between {startDate:DateTime64} and {endDate:DateTime64}
and website_id in {websiteIds:Array(UUID)}
group by website_id`,
where website_id in {websiteIds:Array(UUID)}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
group by website_id
`,
{
websiteIds,
startDate,

View file

@ -25,7 +25,8 @@ function clickhouseQuery(websiteId: string, startAt: Date, eventType: number) {
const { rawQuery } = clickhouse;
return rawQuery(
`select
`
select
event_id as id,
website_id as websiteId,
session_id as sessionId,
@ -37,7 +38,8 @@ function clickhouseQuery(websiteId: string, startAt: Date, eventType: number) {
from website_event
where event_type = {eventType:UInt32}
and website_id = {websiteId:UUID}
and created_at >= {startAt:DateTime('UTC')}`,
and created_at >= {startAt:DateTime('UTC')}
`,
{
websiteId,
startAt,