2022-07-12 21:14:36 +00:00
|
|
|
import { subMinutes } from 'date-fns';
|
2022-08-28 04:38:35 +00:00
|
|
|
import prisma from 'lib/prisma';
|
2022-08-26 05:04:32 +00:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
2022-08-28 04:38:35 +00:00
|
|
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
2022-07-12 21:14:36 +00:00
|
|
|
|
2022-12-13 03:45:38 +00:00
|
|
|
export async function getActiveVisitors(...args: [websiteId: string]) {
|
2022-08-28 04:38:35 +00:00
|
|
|
return runQuery({
|
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
2022-07-25 16:47:11 +00:00
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
2022-07-21 04:31:26 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 03:45:38 +00:00
|
|
|
async function relationalQuery(websiteId: string) {
|
2023-07-25 06:06:16 +00:00
|
|
|
const { rawQuery } = prisma;
|
2022-07-12 21:14:36 +00:00
|
|
|
|
2023-01-11 19:01:44 +00:00
|
|
|
return rawQuery(
|
2023-07-25 06:06:16 +00:00
|
|
|
`
|
|
|
|
|
select count(distinct session_id) x
|
2023-01-11 19:01:44 +00:00
|
|
|
from website_event
|
2023-08-08 22:29:59 +00:00
|
|
|
where website_id = {{websiteId::uuid}}
|
|
|
|
|
and created_at >= {{startAt}}
|
2023-07-25 06:06:16 +00:00
|
|
|
`,
|
|
|
|
|
{ websiteId, startAt: subMinutes(new Date(), 5) },
|
2022-07-12 21:14:36 +00:00
|
|
|
);
|
|
|
|
|
}
|
2022-07-21 04:31:26 +00:00
|
|
|
|
2022-12-13 03:45:38 +00:00
|
|
|
async function clickhouseQuery(websiteId: string) {
|
2023-01-12 08:02:12 +00:00
|
|
|
const { rawQuery } = clickhouse;
|
2022-07-21 04:31:26 +00:00
|
|
|
|
2022-08-28 04:38:35 +00:00
|
|
|
return rawQuery(
|
2023-07-25 06:06:16 +00:00
|
|
|
`
|
|
|
|
|
select
|
|
|
|
|
count(distinct session_id) x
|
2023-03-29 18:16:02 +00:00
|
|
|
from website_event
|
2023-01-12 08:02:12 +00:00
|
|
|
where website_id = {websiteId:UUID}
|
2023-07-25 06:06:16 +00:00
|
|
|
and created_at >= {startAt:DateTime}
|
|
|
|
|
`,
|
|
|
|
|
{ websiteId, startAt: subMinutes(new Date(), 5) },
|
2022-07-21 04:31:26 +00:00
|
|
|
);
|
|
|
|
|
}
|