2022-08-28 06:38:35 +02:00
|
|
|
import prisma from 'lib/prisma';
|
2022-08-26 07:04:32 +02:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
2022-08-28 06:38:35 +02:00
|
|
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
2023-08-04 09:51:52 +02:00
|
|
|
import { EVENT_TYPE } from 'lib/constants';
|
2023-08-04 22:18:30 +02:00
|
|
|
import { QueryFilters } from 'lib/types';
|
2022-07-12 23:14:36 +02:00
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
export async function getSessionMetrics(
|
2023-08-04 22:18:30 +02:00
|
|
|
...args: [websiteId: string, column: string, filters: QueryFilters]
|
2022-12-13 04:45:38 +01:00
|
|
|
) {
|
2022-08-28 06:38:35 +02:00
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
2022-07-25 18:47:11 +02:00
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
2022-07-21 06:31:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-04 22:18:30 +02:00
|
|
|
async function relationalQuery(websiteId: string, column: string, filters: QueryFilters) {
|
2023-07-25 08:06:16 +02:00
|
|
|
const { parseFilters, rawQuery } = prisma;
|
2023-08-04 22:18:30 +02:00
|
|
|
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
|
|
|
|
...filters,
|
|
|
|
eventType: EVENT_TYPE.pageView,
|
|
|
|
});
|
2022-07-12 23:14:36 +02:00
|
|
|
|
|
|
|
return rawQuery(
|
2023-04-02 00:44:30 +02:00
|
|
|
`select ${column} x, count(*) y
|
2023-01-11 20:01:44 +01:00
|
|
|
from website_event
|
2023-08-04 09:51:52 +02:00
|
|
|
${joinSession}
|
|
|
|
where website_event.website_id = {{websiteId::uuid}}
|
2023-07-25 08:06:16 +02:00
|
|
|
and website_event.created_at between {{startDate}} and {{endDate}}
|
2023-08-04 22:18:30 +02:00
|
|
|
and website_event.event_type = {{eventType}}
|
2022-12-13 04:45:38 +01:00
|
|
|
${filterQuery}
|
2023-08-02 23:21:13 +02:00
|
|
|
) as t
|
2022-07-12 23:14:36 +02:00
|
|
|
group by 1
|
2023-03-05 22:30:21 +01:00
|
|
|
order by 2 desc
|
2023-03-30 08:29:37 +02:00
|
|
|
limit 100`,
|
2023-08-04 22:18:30 +02:00
|
|
|
params,
|
2022-07-12 23:14:36 +02:00
|
|
|
);
|
|
|
|
}
|
2022-07-21 06:31:26 +02:00
|
|
|
|
2023-08-04 22:18:30 +02:00
|
|
|
async function clickhouseQuery(websiteId: string, column: string, filters: QueryFilters) {
|
2023-07-25 08:06:16 +02:00
|
|
|
const { parseFilters, rawQuery } = clickhouse;
|
2023-08-04 22:18:30 +02:00
|
|
|
const { filterQuery, params } = await parseFilters(websiteId, {
|
|
|
|
...filters,
|
|
|
|
eventType: EVENT_TYPE.pageView,
|
|
|
|
});
|
2022-07-21 06:31:26 +02:00
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
return rawQuery(
|
2023-07-25 08:06:16 +02:00
|
|
|
`
|
|
|
|
select
|
|
|
|
${column} x, count(distinct session_id) y
|
2023-08-02 23:21:13 +02:00
|
|
|
from website_event
|
2023-01-12 09:02:12 +01:00
|
|
|
where website_id = {websiteId:UUID}
|
2023-07-25 08:06:16 +02:00
|
|
|
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
2023-07-25 09:30:18 +02:00
|
|
|
and event_type = {eventType:UInt32}
|
2022-12-13 04:45:38 +01:00
|
|
|
${filterQuery}
|
2022-07-21 06:31:26 +02:00
|
|
|
group by x
|
2023-03-05 22:30:21 +01:00
|
|
|
order by y desc
|
2023-07-25 08:06:16 +02:00
|
|
|
limit 100
|
|
|
|
`,
|
2023-08-04 22:18:30 +02:00
|
|
|
params,
|
2022-07-21 06:31:26 +02:00
|
|
|
);
|
|
|
|
}
|