umami/queries/analytics/session/getSessions.ts

53 lines
1.2 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
export async function getSessions(...args: [websites: 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
});
}
async function relationalQuery(websites: string[], startAt: Date) {
2022-08-29 17:47:01 +00:00
return prisma.client.session.findMany({
where: {
...(websites && websites.length > 0
? {
2022-11-08 19:55:02 +00:00
websiteId: {
in: websites,
2022-08-29 17:47:01 +00:00
},
}
: {}),
createdAt: {
2022-12-27 01:36:48 +00:00
gte: startAt,
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
async function clickhouseQuery(websites: string[], startAt: Date) {
const { rawQuery } = clickhouse;
2022-08-28 04:38:35 +00:00
return rawQuery(
2022-09-12 16:55:34 +00:00
`select distinct
2022-10-12 06:09:06 +00:00
session_id,
2022-07-21 04:31:26 +00:00
website_id,
created_at,
hostname,
browser,
os,
device,
screen,
2022-08-28 04:38:35 +00:00
language,
2022-07-21 04:31:26 +00:00
country
2022-09-12 16:55:34 +00:00
from event
where ${websites && websites.length > 0 ? `website_id in {websites:Array(UUID)}` : '0 = 0'}
and created_at >= {startAt:DateTime('UTC')}`,
{
websites,
startAt,
},
2022-07-21 04:31:26 +00:00
);
}