Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

fix event data stats

Francis Caoe42fe5c9965f
diff --git a/pages/api/event-data/stats.ts b/pages/api/event-data/stats.ts
index d1ee396bc..4ba843bef 100644
--- a/pages/api/event-data/stats.ts
+++ b/pages/api/event-data/stats.ts
@@ -3,7 +3,7 @@ import { useCors, useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
-import { getEventDataFields } from 'queries';
+import { getEventDataStats } from 'queries';
export interface EventDataStatsRequestQuery {
websiteId: string;
@@ -11,7 +11,6 @@ export interface EventDataStatsRequestQuery {
startDate: string;
endDate: string;
};
- field?: string;
}
export default async (
@@ -31,19 +30,9 @@ export default async (
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
- const results = await getEventDataFields(websiteId, { startDate, endDate });
- const fields = new Set();
+ const data = await getEventDataStats(websiteId, { startDate, endDate });
- const data = results.reduce(
- (obj, row) => {
- fields.add(row.fieldName);
- obj.records += Number(row.total);
- return obj;
- },
- { events: results.length, records: 0 },
- );
-
- return ok(res, { ...data, fields: fields.size });
+ return ok(res, data);
}
return methodNotAllowed(res);
diff --git a/queries/analytics/eventData/getEventDataStats.ts b/queries/analytics/eventData/getEventDataStats.ts
new file mode 100644
index 000000000..cf77ff89a
--- /dev/null
+++ b/queries/analytics/eventData/getEventDataStats.ts
@@ -0,0 +1,69 @@
+import prisma from 'lib/prisma';
+import clickhouse from 'lib/clickhouse';
+import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
+import { QueryFilters } from 'lib/types';
+
+export async function getEventDataStats(
+ ...args: [websiteId: string, filters: QueryFilters]
+): Promise<{
+ events: number;
+ fields: 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, params } = await parseFilters(websiteId, filters);
+
+ return rawQuery(
+ `
+ select
+ count(distinct t.website_event_id) as "events",
+ count(distinct t.event_key) as "fields",
+ sum(t.total) as "records"
+ from (
+ select
+ website_event_id,
+ event_key,
+ count(*) as "total"
+ from event_data
+ where website_id = {{websiteId::uuid}}
+ and created_at between {{startDate}} and {{endDate}}
+ ${filterQuery}
+ group by website_event_id, event_key
+ ) as t
+ `,
+ params,
+ );
+}
+
+async function clickhouseQuery(websiteId: string, filters: QueryFilters) {
+ const { rawQuery, parseFilters } = clickhouse;
+ const { filterQuery, params } = await parseFilters(websiteId, filters);
+
+ return rawQuery(
+ `
+ select
+ count(distinct t.event_id) as "events",
+ count(distinct t.event_key) as "fields",
+ sum(t.total) as "records"
+ from (
+ select
+ event_id,
+ event_key,
+ count(*) as "total"
+ from event_data
+ where website_id = {websiteId:UUID}
+ and created_at between {startDate:DateTime} and {endDate:DateTime}
+ ${filterQuery}
+ group by event_id, event_key
+ ) as t
+ `,
+ params,
+ );
+}
diff --git a/queries/index.js b/queries/index.js
index c3255795b..452d85a64 100644
--- a/queries/index.js
+++ b/queries/index.js
@@ -9,6 +9,7 @@ export * from './analytics/events/getEventUsage';
export * from './analytics/events/getEvents';
export * from './analytics/eventData/getEventDataEvents';
export * from './analytics/eventData/getEventDataFields';
+export * from './analytics/eventData/getEventDataStats';
export * from './analytics/eventData/getEventDataUsage';
export * from './analytics/events/saveEvent';
export * from './analytics/reports/getFunnel';