aboutsummaryrefslogtreecommitdiff
path: root/src/queries/sql/events/getEventDataStats.ts
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-01-24 13:09:50 +0000
committerFuwn <[email protected]>2026-01-24 13:09:50 +0000
commit396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b (patch)
treeb9df4ca6a70db45cfffbae6fdd7252e20fb8e93c /src/queries/sql/events/getEventDataStats.ts
downloadumami-396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b.tar.xz
umami-396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b.zip
Initial commitHEADmain
Created from https://vercel.com/new
Diffstat (limited to 'src/queries/sql/events/getEventDataStats.ts')
-rw-r--r--src/queries/sql/events/getEventDataStats.ts90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/queries/sql/events/getEventDataStats.ts b/src/queries/sql/events/getEventDataStats.ts
new file mode 100644
index 0000000..89e1358
--- /dev/null
+++ b/src/queries/sql/events/getEventDataStats.ts
@@ -0,0 +1,90 @@
+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 = 'getEventDataStats';
+
+export async function getEventDataStats(
+ ...args: [websiteId: string, filters: QueryFilters]
+): Promise<{
+ events: number;
+ properties: number;
+ records: number;
+}> {
+ return runQuery({
+ [PRISMA]: () => relationalQuery(...args),
+ [CLICKHOUSE]: () => clickhouseQuery(...args),
+ }).then(results => results?.[0]);
+}
+
+async function relationalQuery(websiteId: string, filters: QueryFilters) {
+ const { rawQuery, parseFilters } = prisma;
+ const { filterQuery, joinSessionQuery, cohortQuery, queryParams } = parseFilters({
+ ...filters,
+ websiteId,
+ });
+
+ return rawQuery(
+ `
+ select
+ count(distinct t.website_event_id) as "events",
+ count(distinct t.data_key) as "properties",
+ sum(t.total) as "records"
+ from (
+ select
+ website_event_id,
+ data_key,
+ count(*) as "total"
+ from event_data
+ join website_event on website_event.event_id = event_data.website_event_id
+ and website_event.website_id = {{websiteId::uuid}}
+ and website_event.created_at between {{startDate}} and {{endDate}}
+ ${cohortQuery}
+ ${joinSessionQuery}
+ where event_data.website_id = {{websiteId::uuid}}
+ and event_data.created_at between {{startDate}} and {{endDate}}
+ ${filterQuery}
+ group by website_event_id, data_key
+ ) as t
+ `,
+ queryParams,
+ FUNCTION_NAME,
+ );
+}
+
+async function clickhouseQuery(
+ websiteId: string,
+ filters: QueryFilters,
+): Promise<{ events: number; properties: number; records: number }[]> {
+ const { rawQuery, parseFilters } = clickhouse;
+ const { filterQuery, cohortQuery, queryParams } = parseFilters({ ...filters, websiteId });
+
+ return rawQuery(
+ `
+ select
+ count(distinct t.event_id) as "events",
+ count(distinct t.data_key) as "properties",
+ sum(t.total) as "records"
+ from (
+ select
+ event_id,
+ data_key,
+ count(*) as "total"
+ from event_data
+ join website_event
+ on website_event.event_id = event_data.event_id
+ and website_event.website_id = event_data.website_id
+ and website_event.website_id = {websiteId:UUID}
+ and website_event.created_at between {startDate:DateTime64} and {endDate:DateTime64}
+ ${cohortQuery}
+ where event_data.website_id = {websiteId:UUID}
+ and event_data.created_at between {startDate:DateTime64} and {endDate:DateTime64}
+ ${filterQuery}
+ group by event_id, data_key
+ ) as t
+ `,
+ queryParams,
+ FUNCTION_NAME,
+ );
+}