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';
|
2024-07-08 08:45:54 +00:00
|
|
|
import { PageParams, QueryFilters } from 'lib/types';
|
2022-07-12 21:14:36 +00:00
|
|
|
|
2024-07-30 02:09:13 +00:00
|
|
|
export async function getWebsiteSessions(
|
2024-07-08 08:45:54 +00:00
|
|
|
...args: [websiteId: string, filters?: QueryFilters, pageParams?: PageParams]
|
|
|
|
|
) {
|
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
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 08:45:54 +00:00
|
|
|
async function relationalQuery(websiteId: string, filters: QueryFilters, pageParams: PageParams) {
|
|
|
|
|
const { pagedQuery } = prisma;
|
2024-06-20 04:47:27 +00:00
|
|
|
|
2024-07-08 08:45:54 +00:00
|
|
|
const where = {
|
|
|
|
|
...filters,
|
|
|
|
|
id: websiteId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return pagedQuery('session', { where }, pageParams);
|
2022-07-12 21:14:36 +00:00
|
|
|
}
|
2022-07-21 04:31:26 +00:00
|
|
|
|
2024-07-08 08:45:54 +00:00
|
|
|
async function clickhouseQuery(websiteId: string, filters: QueryFilters, pageParams?: PageParams) {
|
2024-07-29 02:51:14 +00:00
|
|
|
const { pagedQuery, parseFilters } = clickhouse;
|
2024-07-08 08:45:54 +00:00
|
|
|
const { params, dateQuery, filterQuery } = await parseFilters(websiteId, filters);
|
2022-08-28 04:38:35 +00:00
|
|
|
|
2024-07-08 08:45:54 +00:00
|
|
|
return pagedQuery(
|
2023-07-25 06:06:16 +00:00
|
|
|
`
|
2023-12-10 04:55:50 +00:00
|
|
|
select
|
2023-04-20 19:07:04 +00:00
|
|
|
session_id as id,
|
2023-02-18 05:42:42 +00:00
|
|
|
website_id as websiteId,
|
2022-07-21 04:31:26 +00:00
|
|
|
hostname,
|
|
|
|
|
browser,
|
|
|
|
|
os,
|
|
|
|
|
device,
|
|
|
|
|
screen,
|
2022-08-28 04:38:35 +00:00
|
|
|
language,
|
2023-02-20 17:04:20 +00:00
|
|
|
country,
|
|
|
|
|
subdivision1,
|
2024-07-29 08:38:36 +00:00
|
|
|
city,
|
|
|
|
|
min(created_at) as firstAt,
|
2024-07-31 04:22:19 +00:00
|
|
|
max(created_at) as lastAt,
|
|
|
|
|
uniq(visit_id) as visits
|
2023-03-29 18:06:12 +00:00
|
|
|
from website_event
|
2023-02-15 10:27:18 +00:00
|
|
|
where website_id = {websiteId:UUID}
|
2024-07-08 08:45:54 +00:00
|
|
|
${dateQuery}
|
|
|
|
|
${filterQuery}
|
2024-07-29 02:51:14 +00:00
|
|
|
group by session_id, website_id, hostname, browser, os, device, screen, language, country, subdivision1, city
|
2024-07-29 08:38:36 +00:00
|
|
|
order by lastAt desc
|
2023-07-25 06:06:16 +00:00
|
|
|
`,
|
2024-07-08 08:45:54 +00:00
|
|
|
params,
|
|
|
|
|
pageParams,
|
2024-07-31 04:22:19 +00:00
|
|
|
).then((result: any) => ({
|
|
|
|
|
...result,
|
|
|
|
|
visits: Number(result.visits),
|
|
|
|
|
}));
|
2022-07-21 04:31:26 +00:00
|
|
|
}
|