Add collect limits.

This commit is contained in:
Brian Cao 2022-12-31 13:42:03 -08:00
parent f42cab8d83
commit e487da72c3
13 changed files with 217 additions and 61 deletions

View file

@ -0,0 +1,38 @@
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
export async function getViewTotals(...args: [websites: string[], startDate: Date]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websites: string[], startDate: Date) {
return prisma.client.websiteEvent.count({
where: {
websiteId: {
in: websites,
},
createdAt: {
gte: startDate,
},
},
});
}
async function clickhouseQuery(websiteIds: string[], startDate: Date) {
const { rawQuery, getDateFormat, getCommaSeparatedStringFormat, findFirst } = clickhouse;
return rawQuery(
`
select
count(*) as views
from event
where
website_id in (${getCommaSeparatedStringFormat(websiteIds)})
and created_at >= ${getDateFormat(startDate)}
`,
).then(data => findFirst(data));
}

View file

@ -18,7 +18,7 @@ async function relationalQuery(
) {
const { startDate, endDate, filters = {} } = data;
const { getDateQuery, getTimestampInterval, parseFilters, rawQuery } = prisma;
const params = [startDate, endDate];
const params: any = [startDate, endDate];
const { filterQuery, joinSession } = parseFilters(filters, params);
return rawQuery(