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

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-07-29 02:51:14 +00:00
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
2022-07-12 21:14:36 +00:00
export async function getSessionActivity(
...args: [websiteId: string, sessionId: string, startDate: Date, endDate: Date]
) {
2024-07-29 02:51:14 +00:00
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(
websiteId: string,
sessionId: string,
startDate: Date,
endDate: Date,
) {
2024-07-30 02:09:13 +00:00
return prisma.client.websiteEvent.findMany({
2023-07-30 05:03:34 +00:00
where: {
2024-07-29 02:51:14 +00:00
id: sessionId,
2024-07-30 02:09:13 +00:00
websiteId,
createdAt: { gte: startDate, lte: endDate },
2023-07-30 05:03:34 +00:00
},
});
2022-07-12 21:14:36 +00:00
}
2024-07-29 02:51:14 +00:00
async function clickhouseQuery(
websiteId: string,
sessionId: string,
startDate: Date,
endDate: Date,
) {
2024-07-29 02:51:14 +00:00
const { rawQuery } = clickhouse;
return rawQuery(
`
select
session_id as id,
website_id as websiteId,
2024-07-30 02:09:13 +00:00
created_at as createdAt,
url_path as urlPath,
url_query as urlQuery,
referrer_domain as referrerDomain,
event_id as eventId,
event_type as eventType,
event_name as eventName,
visit_id as visitId
2024-07-29 02:51:14 +00:00
from website_event
where website_id = {websiteId:UUID}
and session_id = {sessionId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
2024-07-30 02:09:13 +00:00
order by created_at desc
2024-07-29 02:51:14 +00:00
`,
{ websiteId, sessionId, startDate, endDate },
2024-07-30 02:09:13 +00:00
);
2024-07-29 02:51:14 +00:00
}