umami/queries/analytics/sessions/getSessions.ts

53 lines
1.1 KiB
TypeScript
Raw Normal View History

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, PRISMA, CLICKHOUSE } from 'lib/db';
2022-07-12 21:14:36 +00:00
2023-02-15 10:27:18 +00:00
export async function getSessions(...args: [websiteId: string, startAt: Date]) {
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
});
}
2023-07-26 16:55:54 +00:00
async function relationalQuery(websiteId: string, startDate: Date) {
2022-08-29 17:47:01 +00:00
return prisma.client.session.findMany({
where: {
2023-02-15 10:27:18 +00:00
websiteId,
createdAt: {
2023-07-26 16:55:54 +00:00
gte: startDate,
2022-07-12 21:14:36 +00:00
},
2022-08-29 17:47:01 +00:00
},
});
2022-07-12 21:14:36 +00:00
}
2022-07-21 04:31:26 +00:00
2023-07-26 16:55:54 +00:00
async function clickhouseQuery(websiteId: string, startDate: Date) {
const { rawQuery } = clickhouse;
2022-08-28 04:38:35 +00:00
return rawQuery(
2023-07-25 06:06:16 +00:00
`
select distinct
2023-04-20 19:07:04 +00:00
session_id as id,
2023-02-18 05:42:42 +00:00
website_id as websiteId,
created_at as createdAt,
toUnixTimestamp(created_at) as timestamp,
2022-07-21 04:31:26 +00:00
hostname,
browser,
os,
device,
screen,
2022-08-28 04:38:35 +00:00
language,
country,
subdivision1,
subdivision2,
city
2023-03-29 18:06:12 +00:00
from website_event
2023-02-15 10:27:18 +00:00
where website_id = {websiteId:UUID}
2023-07-26 16:55:54 +00:00
and created_at >= {startDate:DateTime}
2023-07-25 06:06:16 +00:00
`,
{
2023-02-15 10:27:18 +00:00
websiteId,
2023-07-26 16:55:54 +00:00
startDate,
},
2022-07-21 04:31:26 +00:00
);
}