umami/src/queries/analytics/eventData/getEventDataUsage.ts

39 lines
967 B
TypeScript
Raw Normal View History

2023-05-04 00:17:57 +00:00
import clickhouse from 'lib/clickhouse';
2023-07-25 06:06:16 +00:00
import { CLICKHOUSE, PRISMA, runQuery, notImplemented } from 'lib/db';
2023-05-04 00:17:57 +00:00
export function getEventDataUsage(...args: [websiteIds: string[], startDate: Date, endDate: Date]) {
return runQuery({
2023-07-25 06:06:16 +00:00
[PRISMA]: notImplemented,
2023-05-04 00:17:57 +00:00
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
2023-10-20 19:34:02 +00:00
function clickhouseQuery(
websiteIds: string[],
startDate: Date,
endDate: Date,
): Promise<{ websiteId: string; count: number }[]> {
2023-05-04 00:17:57 +00:00
const { rawQuery } = clickhouse;
return rawQuery(
2023-07-25 06:06:16 +00:00
`
select
website_id as websiteId,
count(*) as count
2023-05-04 00:17:57 +00:00
from event_data
where created_at between {startDate:DateTime64} and {endDate:DateTime64}
2023-07-25 06:06:16 +00:00
and website_id in {websiteIds:Array(UUID)}
group by website_id
`,
2023-05-04 00:17:57 +00:00
{
websiteIds,
startDate,
endDate,
},
2023-10-20 19:34:02 +00:00
).then(a => {
return Object.values(a).map(a => {
return { websiteId: a.websiteId, count: Number(a.count) };
});
});
2023-05-04 00:17:57 +00:00
}