aboutsummaryrefslogtreecommitdiff
path: root/src/queries/sql/events/getEventDataUsage.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/queries/sql/events/getEventDataUsage.ts')
-rw-r--r--src/queries/sql/events/getEventDataUsage.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/queries/sql/events/getEventDataUsage.ts b/src/queries/sql/events/getEventDataUsage.ts
new file mode 100644
index 0000000..50613a7
--- /dev/null
+++ b/src/queries/sql/events/getEventDataUsage.ts
@@ -0,0 +1,38 @@
+import clickhouse from '@/lib/clickhouse';
+import { CLICKHOUSE, notImplemented, PRISMA, runQuery } from '@/lib/db';
+import type { QueryFilters } from '@/lib/types';
+
+const FUNCTION_NAME = 'getEventDataUsage';
+
+export function getEventDataUsage(...args: [websiteIds: string[], filters: QueryFilters]) {
+ return runQuery({
+ [PRISMA]: notImplemented,
+ [CLICKHOUSE]: () => clickhouseQuery(...args),
+ });
+}
+
+function clickhouseQuery(
+ websiteIds: string[],
+ filters: QueryFilters,
+): Promise<{ websiteId: string; count: number }[]> {
+ const { rawQuery } = clickhouse;
+ const { startDate, endDate } = filters;
+
+ return rawQuery(
+ `
+ select
+ website_id as websiteId,
+ count(*) as count
+ from event_data
+ where created_at between {startDate:DateTime64} and {endDate:DateTime64}
+ and website_id in {websiteIds:Array(UUID)}
+ group by website_id
+ `,
+ {
+ websiteIds,
+ startDate,
+ endDate,
+ },
+ FUNCTION_NAME,
+ );
+}