diff options
| author | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
| commit | 396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b (patch) | |
| tree | b9df4ca6a70db45cfffbae6fdd7252e20fb8e93c /src/queries/sql/sessions/getSessionActivity.ts | |
| download | umami-main.tar.xz umami-main.zip | |
Created from https://vercel.com/new
Diffstat (limited to 'src/queries/sql/sessions/getSessionActivity.ts')
| -rw-r--r-- | src/queries/sql/sessions/getSessionActivity.ts | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/queries/sql/sessions/getSessionActivity.ts b/src/queries/sql/sessions/getSessionActivity.ts new file mode 100644 index 0000000..af31fca --- /dev/null +++ b/src/queries/sql/sessions/getSessionActivity.ts @@ -0,0 +1,78 @@ +import clickhouse from '@/lib/clickhouse'; +import { CLICKHOUSE, PRISMA, runQuery } from '@/lib/db'; +import prisma from '@/lib/prisma'; +import type { QueryFilters } from '@/lib/types'; + +const FUNCTION_NAME = 'getSessionActivity'; + +export async function getSessionActivity( + ...args: [websiteId: string, sessionId: string, filters: QueryFilters] +) { + return runQuery({ + [PRISMA]: () => relationalQuery(...args), + [CLICKHOUSE]: () => clickhouseQuery(...args), + }); +} + +async function relationalQuery(websiteId: string, sessionId: string, filters: QueryFilters) { + const { rawQuery } = prisma; + const { startDate, endDate } = filters; + + return rawQuery( + ` + select + 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", + event_id IN (select website_event_id + from event_data + where website_id = {{websiteId::uuid}} + and created_at between {{startDate}} and {{endDate}}) AS "hasData" + from website_event + where website_id = {{websiteId::uuid}} + and session_id = {{sessionId::uuid}} + and created_at between {{startDate}} and {{endDate}} + order by created_at desc + limit 500 + `, + { websiteId, sessionId, startDate, endDate }, + FUNCTION_NAME, + ); +} + +async function clickhouseQuery(websiteId: string, sessionId: string, filters: QueryFilters) { + const { rawQuery } = clickhouse; + const { startDate, endDate } = filters; + + return rawQuery( + ` + select + 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, + event_id IN (select event_id + from event_data + where website_id = {websiteId:UUID} + and session_id = {sessionId:UUID} + and created_at between {startDate:DateTime64} and {endDate:DateTime64}) AS hasData + from website_event + where website_id = {websiteId:UUID} + and session_id = {sessionId:UUID} + and created_at between {startDate:DateTime64} and {endDate:DateTime64} + order by created_at desc + limit 500 + `, + { websiteId, sessionId, startDate, endDate }, + FUNCTION_NAME, + ); +} |