2022-12-13 04:45:38 +01:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
|
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|
|
|
import prisma from 'lib/prisma';
|
2023-07-26 18:55:54 +02:00
|
|
|
import { EVENT_TYPE } from 'lib/constants';
|
2023-07-26 08:59:08 +02:00
|
|
|
import { loadWebsite } from 'lib/load';
|
|
|
|
import { maxDate } from 'lib/date';
|
2022-12-13 04:45:38 +01:00
|
|
|
|
2023-07-26 18:55:54 +02:00
|
|
|
export interface PageviewStatsCriteria {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
timezone?: string;
|
|
|
|
unit?: string;
|
|
|
|
count?: string;
|
|
|
|
filters: object;
|
|
|
|
sessionKey?: string;
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
export async function getPageviewStats(
|
2023-07-26 18:55:54 +02:00
|
|
|
...args: [websiteId: string, criteria: PageviewStatsCriteria]
|
2022-12-13 04:45:38 +01:00
|
|
|
) {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-26 18:55:54 +02:00
|
|
|
async function relationalQuery(websiteId: string, criteria: PageviewStatsCriteria) {
|
2022-12-13 04:45:38 +01:00
|
|
|
const {
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
timezone = 'utc',
|
|
|
|
unit = 'day',
|
|
|
|
count = '*',
|
|
|
|
filters = {},
|
|
|
|
sessionKey = 'session_id',
|
2023-04-02 00:44:30 +02:00
|
|
|
} = criteria;
|
2023-07-25 08:06:16 +02:00
|
|
|
const { getDateQuery, parseFilters, rawQuery } = prisma;
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-07-25 08:06:16 +02:00
|
|
|
const { filterQuery, joinSession } = parseFilters(filters);
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
return rawQuery(
|
2023-07-25 08:06:16 +02:00
|
|
|
`
|
|
|
|
select
|
|
|
|
${getDateQuery('website_event.created_at', unit, timezone)} x,
|
|
|
|
count(${count !== '*' ? `${count}${sessionKey}` : count}) y
|
|
|
|
from website_event
|
|
|
|
${joinSession}
|
|
|
|
where website_event.website_id = {{websiteId::uuid}}
|
|
|
|
and website_event.created_at between {{startDate}} and {{endDate}}
|
2023-07-25 18:43:51 +02:00
|
|
|
and event_type = {{eventType}}
|
2023-07-25 08:06:16 +02:00
|
|
|
${filterQuery}
|
|
|
|
group by 1
|
|
|
|
`,
|
2023-07-26 00:17:50 +02:00
|
|
|
{
|
|
|
|
websiteId,
|
2023-07-26 08:59:08 +02:00
|
|
|
startDate: maxDate(startDate, website.resetAt),
|
2023-07-26 00:17:50 +02:00
|
|
|
endDate,
|
|
|
|
eventType: EVENT_TYPE.pageView,
|
2023-07-31 20:42:47 +02:00
|
|
|
...filters,
|
2023-07-26 00:17:50 +02:00
|
|
|
},
|
2022-12-13 04:45:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-26 18:55:54 +02:00
|
|
|
async function clickhouseQuery(websiteId: string, criteria: PageviewStatsCriteria) {
|
2023-04-02 00:44:30 +02:00
|
|
|
const {
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
timezone = 'UTC',
|
|
|
|
unit = 'day',
|
|
|
|
count = '*',
|
|
|
|
filters = {},
|
|
|
|
} = criteria;
|
2023-07-25 08:06:16 +02:00
|
|
|
const { parseFilters, rawQuery, getDateStringQuery, getDateQuery } = clickhouse;
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-07-25 08:06:16 +02:00
|
|
|
const { filterQuery } = parseFilters(filters);
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
return rawQuery(
|
2023-07-25 08:06:16 +02:00
|
|
|
`
|
|
|
|
select
|
2023-03-15 06:37:50 +01:00
|
|
|
${getDateStringQuery('g.t', unit)} as x,
|
2022-12-13 04:45:38 +01:00
|
|
|
g.y as y
|
2023-07-25 08:06:16 +02:00
|
|
|
from (
|
|
|
|
select
|
|
|
|
${getDateQuery('created_at', unit, timezone)} as t,
|
|
|
|
count(${count !== '*' ? 'distinct session_id' : count}) as y
|
2023-03-29 20:06:12 +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}
|
2023-07-25 08:06:16 +02:00
|
|
|
group by t
|
|
|
|
) as g
|
|
|
|
order by t
|
|
|
|
`,
|
2023-07-26 00:17:50 +02:00
|
|
|
{
|
|
|
|
...filters,
|
|
|
|
websiteId,
|
2023-07-26 08:59:08 +02:00
|
|
|
startDate: maxDate(startDate, website.resetAt),
|
2023-07-26 00:17:50 +02:00
|
|
|
endDate,
|
|
|
|
eventType: EVENT_TYPE.pageView,
|
|
|
|
},
|
2022-12-13 04:45:38 +01:00
|
|
|
);
|
|
|
|
}
|