From e42fe5c9965f5b69c4e5849c0735e95763bb6919 Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Thu, 17 Aug 2023 13:39:53 -0700 Subject: [PATCH] fix event data stats --- pages/api/event-data/stats.ts | 17 +---- .../analytics/eventData/getEventDataStats.ts | 69 +++++++++++++++++++ queries/index.js | 1 + 3 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 queries/analytics/eventData/getEventDataStats.ts diff --git a/pages/api/event-data/stats.ts b/pages/api/event-data/stats.ts index d1ee396b..4ba843be 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 00000000..cf77ff89 --- /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 c3255795..452d85a6 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';