2023-08-02 21:21:13 +00:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
2024-08-02 05:57:54 +00:00
|
|
|
import { EVENT_COLUMNS, EVENT_TYPE } from 'lib/constants';
|
2023-08-02 21:21:13 +00:00
|
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|
|
|
|
import prisma from 'lib/prisma';
|
2023-08-04 20:18:30 +00:00
|
|
|
import { QueryFilters } from 'lib/types';
|
2023-08-02 21:21:13 +00:00
|
|
|
|
2023-08-04 20:18:30 +00:00
|
|
|
export async function getSessionStats(...args: [websiteId: string, filters: QueryFilters]) {
|
2023-08-02 21:21:13 +00:00
|
|
|
return runQuery({
|
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 20:18:30 +00:00
|
|
|
async function relationalQuery(websiteId: string, filters: QueryFilters) {
|
|
|
|
|
const { timezone = 'utc', unit = 'day' } = filters;
|
2024-07-08 08:45:54 +00:00
|
|
|
const { getDateSQL, parseFilters, rawQuery } = prisma;
|
2023-08-04 21:00:37 +00:00
|
|
|
const { filterQuery, joinSession, params } = await parseFilters(websiteId, {
|
2023-08-04 20:18:30 +00:00
|
|
|
...filters,
|
|
|
|
|
eventType: EVENT_TYPE.pageView,
|
|
|
|
|
});
|
2023-08-02 21:21:13 +00:00
|
|
|
|
|
|
|
|
return rawQuery(
|
|
|
|
|
`
|
|
|
|
|
select
|
2024-08-22 20:42:22 +00:00
|
|
|
${getDateSQL('website_event.created_at', unit, timezone)} x,
|
|
|
|
|
count(distinct website_event.session_id) y
|
|
|
|
|
from website_event
|
2023-08-04 21:00:37 +00:00
|
|
|
${joinSession}
|
2024-08-22 20:42:22 +00:00
|
|
|
where website_event.website_id = {{websiteId::uuid}}
|
2023-08-02 21:21:13 +00:00
|
|
|
and website_event.created_at between {{startDate}} and {{endDate}}
|
2024-08-22 20:42:22 +00:00
|
|
|
and event_type = {{eventType}}
|
2023-08-02 21:21:13 +00:00
|
|
|
${filterQuery}
|
|
|
|
|
group by 1
|
2024-08-23 23:43:38 +00:00
|
|
|
order by 1
|
2023-08-02 21:21:13 +00:00
|
|
|
`,
|
2023-08-04 20:18:30 +00:00
|
|
|
params,
|
2023-08-02 21:21:13 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-29 18:00:06 +00:00
|
|
|
async function clickhouseQuery(
|
|
|
|
|
websiteId: string,
|
|
|
|
|
filters: QueryFilters,
|
|
|
|
|
): Promise<{ x: string; y: number }[]> {
|
2024-08-26 17:36:07 +00:00
|
|
|
const { timezone = 'utc', unit = 'day' } = filters;
|
2024-08-27 19:02:23 +00:00
|
|
|
const { parseFilters, rawQuery, getDateSQL } = clickhouse;
|
2024-08-02 05:57:54 +00:00
|
|
|
const { filterQuery, params } = await parseFilters(websiteId, {
|
2023-08-04 20:18:30 +00:00
|
|
|
...filters,
|
|
|
|
|
eventType: EVENT_TYPE.pageView,
|
|
|
|
|
});
|
2023-08-02 21:21:13 +00:00
|
|
|
|
2024-08-02 05:57:54 +00:00
|
|
|
let sql = '';
|
2024-07-24 05:44:10 +00:00
|
|
|
|
2024-08-02 05:57:54 +00:00
|
|
|
if (EVENT_COLUMNS.some(item => Object.keys(filters).includes(item)) || unit === 'minute') {
|
|
|
|
|
sql = `
|
2023-08-02 21:21:13 +00:00
|
|
|
select
|
2024-08-27 19:02:23 +00:00
|
|
|
g.t as x,
|
2023-08-02 21:21:13 +00:00
|
|
|
g.y as y
|
|
|
|
|
from (
|
2024-08-21 06:58:20 +00:00
|
|
|
select
|
2024-08-27 19:02:23 +00:00
|
|
|
${getDateSQL('website_event.created_at', unit, timezone)} as t,
|
2024-08-02 05:57:54 +00:00
|
|
|
count(distinct session_id) as y
|
|
|
|
|
from website_event
|
2023-08-02 21:21:13 +00:00
|
|
|
where website_id = {websiteId:UUID}
|
2023-09-29 18:00:06 +00:00
|
|
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
2023-08-02 21:21:13 +00:00
|
|
|
and event_type = {eventType:UInt32}
|
|
|
|
|
${filterQuery}
|
|
|
|
|
group by t
|
|
|
|
|
) as g
|
|
|
|
|
order by t
|
2024-08-02 05:57:54 +00:00
|
|
|
`;
|
|
|
|
|
} else {
|
|
|
|
|
sql = `
|
|
|
|
|
select
|
2024-08-27 19:02:23 +00:00
|
|
|
g.t as x,
|
2024-08-02 05:57:54 +00:00
|
|
|
g.y as y
|
|
|
|
|
from (
|
2024-08-21 06:58:20 +00:00
|
|
|
select
|
2024-08-27 19:02:23 +00:00
|
|
|
${getDateSQL('website_event.created_at', unit, timezone)} as t,
|
2024-08-02 05:57:54 +00:00
|
|
|
uniq(session_id) as y
|
|
|
|
|
from website_event_stats_hourly website_event
|
|
|
|
|
where website_id = {websiteId:UUID}
|
|
|
|
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
|
|
|
|
and event_type = {eventType:UInt32}
|
|
|
|
|
${filterQuery}
|
|
|
|
|
group by t
|
|
|
|
|
) as g
|
|
|
|
|
order by t
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 21:53:53 +00:00
|
|
|
return rawQuery(sql, params);
|
2023-08-02 21:21:13 +00:00
|
|
|
}
|