add date param to values

This commit is contained in:
Francis Cao 2023-10-13 09:31:53 -07:00
parent c18daf4845
commit 88aa341821
3 changed files with 29 additions and 7 deletions

View file

@ -2,14 +2,16 @@ import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
export async function getValues(...args: [websiteId: string, column: string]) {
export async function getValues(
...args: [websiteId: string, column: string, startDate: Date, endDate: Date]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websiteId: string, column: string) {
async function relationalQuery(websiteId: string, column: string, startDate: Date, endDate: Date) {
const { rawQuery } = prisma;
return rawQuery(
@ -19,12 +21,17 @@ async function relationalQuery(websiteId: string, column: string) {
inner join session
on session.session_id = website_event.session_id
where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
`,
{ websiteId },
{
websiteId,
startDate,
endDate,
},
);
}
async function clickhouseQuery(websiteId: string, column: string) {
async function clickhouseQuery(websiteId: string, column: string, startDate: Date, endDate: Date) {
const { rawQuery } = clickhouse;
return rawQuery(
@ -32,7 +39,12 @@ async function clickhouseQuery(websiteId: string, column: string) {
select distinct ${column} as value
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
`,
{ websiteId },
{
websiteId,
startDate,
endDate,
},
);
}