umami/src/queries/analytics/sessions/getWebsiteSessions.ts

93 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-08-26 05:04:32 +00:00
import clickhouse from 'lib/clickhouse';
2024-08-21 01:59:24 +00:00
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
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(
...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
});
}
async function relationalQuery(websiteId: string, filters: QueryFilters, pageParams: PageParams) {
2024-08-21 01:59:24 +00:00
const { pagedRawQuery, parseFilters } = prisma;
const { filterQuery, params } = await parseFilters(websiteId, {
...filters,
2024-08-21 01:59:24 +00:00
});
2024-08-21 01:59:24 +00:00
return pagedRawQuery(
`
with sessions as (
select
s.session_id as "id",
s.website_id as "websiteId",
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
city,
min(we.created_at) as "firstAt",
max(we.created_at) as "lastAt",
count(distinct we.visit_id) as "visits",
sum(case when we.event_type = 1 then 1 else 0 end) as "views",
max(we.created_at) as "createdAt"
from website_event we
join session s on s.session_id = we.session_id
where we.website_id = {{websiteId::uuid}}
and we.created_at between {{startDate}} and {{endDate}}
${filterQuery}
group by s.session_id, s.website_id, s.hostname, s.browser, s.os, s.device, s.screen, s.language, s.country, s.subdivision1, s.city
order by max(we.created_at) desc
limit 1000)
select * from sessions
`,
params,
pageParams,
);
2022-07-12 21:14:36 +00:00
}
2022-07-21 04:31:26 +00:00
async function clickhouseQuery(websiteId: string, filters: QueryFilters, pageParams?: PageParams) {
2024-08-16 07:35:08 +00:00
const { pagedQuery, parseFilters, getDateStringSQL } = clickhouse;
const { params, dateQuery, filterQuery } = await parseFilters(websiteId, filters);
2022-08-28 04:38:35 +00:00
return pagedQuery(
2023-07-25 06:06:16 +00:00
`
with sessions as (
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,
country,
subdivision1,
city,
2024-08-16 07:35:08 +00:00
${getDateStringSQL('min(min_time)')} as firstAt,
${getDateStringSQL('max(max_time)')} as lastAt,
uniq(visit_id) as visits,
2024-08-21 01:59:24 +00:00
sumIf(views, event_type = 1) as views,
lastAt as createdAt
from website_event_stats_hourly
2023-02-15 10:27:18 +00:00
where website_id = {websiteId:UUID}
${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
order by lastAt desc
limit 1000)
select * from sessions
2023-07-25 06:06:16 +00:00
`,
params,
pageParams,
2024-08-21 01:59:24 +00:00
);
2022-07-21 04:31:26 +00:00
}