Refactored realtime.

This commit is contained in:
Mike Cao 2024-06-19 21:47:27 -07:00
parent cda3ba345b
commit 5108b91f80
16 changed files with 205 additions and 227 deletions

View file

@ -1,31 +1,34 @@
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
import { QueryFilters } from 'lib/types';
export function getEvents(...args: [websiteId: string, startDate: Date, eventType: number]) {
export function getEvents(...args: [websiteId: string, filters: QueryFilters]) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
function relationalQuery(websiteId: string, startDate: Date, eventType: number) {
function relationalQuery(websiteId: string, filters: QueryFilters) {
const { startDate } = filters;
return prisma.client.websiteEvent.findMany({
where: {
websiteId,
eventType,
createdAt: {
gte: startDate,
},
},
orderBy: {
createdAt: 'asc',
createdAt: 'desc',
},
});
}
function clickhouseQuery(websiteId: string, startDate: Date, eventType: number) {
function clickhouseQuery(websiteId: string, filters: QueryFilters) {
const { rawQuery } = clickhouse;
const { startDate } = filters;
return rawQuery(
`
@ -41,13 +44,11 @@ function clickhouseQuery(websiteId: string, startDate: Date, eventType: number)
from website_event
where website_id = {websiteId:UUID}
and created_at >= {startDate:DateTime64}
and event_type = {eventType:UInt32}
order by created_at asc
order by created_at desc
`,
{
websiteId,
startDate,
eventType,
},
);
}