Refactored realtime.
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import prisma from 'lib/prisma';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { runQuery, PRISMA, CLICKHOUSE } from 'lib/db';
|
||||
import { QueryFilters } from 'lib/types';
|
||||
|
||||
export async function getSessions(...args: [websiteId: string, startAt: Date]) {
|
||||
export async function getSessions(...args: [websiteId: string, filters: QueryFilters]) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId: string, startDate: Date) {
|
||||
async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
||||
const { startDate } = filters;
|
||||
|
||||
return prisma.client.session.findMany({
|
||||
where: {
|
||||
websiteId,
|
||||
@@ -18,13 +21,14 @@ async function relationalQuery(websiteId: string, startDate: Date) {
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'asc',
|
||||
createdAt: 'desc',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function clickhouseQuery(websiteId: string, startDate: Date) {
|
||||
async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
|
||||
const { rawQuery } = clickhouse;
|
||||
const { startDate } = filters;
|
||||
|
||||
return rawQuery(
|
||||
`
|
||||
@@ -46,7 +50,7 @@ async function clickhouseQuery(websiteId: string, startDate: Date) {
|
||||
from website_event
|
||||
where website_id = {websiteId:UUID}
|
||||
and created_at >= {startDate:DateTime64}
|
||||
order by created_at asc
|
||||
order by created_at desc
|
||||
`,
|
||||
{
|
||||
websiteId,
|
||||
|
||||
Reference in New Issue
Block a user