2022-12-13 04:45:38 +01:00
|
|
|
import prisma from 'lib/prisma';
|
|
|
|
import clickhouse from 'lib/clickhouse';
|
|
|
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
2024-03-26 06:51:10 +01:00
|
|
|
import { EVENT_TYPE, SESSION_COLUMNS } from 'lib/constants';
|
2023-08-04 22:18:30 +02:00
|
|
|
import { QueryFilters } from 'lib/types';
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
export async function getPageviewMetrics(
|
2024-03-05 09:45:55 +01:00
|
|
|
...args: [
|
|
|
|
websiteId: string,
|
|
|
|
column: string,
|
|
|
|
filters: QueryFilters,
|
|
|
|
limit?: number,
|
|
|
|
offset?: number,
|
|
|
|
]
|
2022-12-13 04:45:38 +01:00
|
|
|
) {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-13 04:00:44 +01:00
|
|
|
async function relationalQuery(
|
|
|
|
websiteId: string,
|
|
|
|
column: string,
|
|
|
|
filters: QueryFilters,
|
2024-02-15 00:13:53 +01:00
|
|
|
limit: number = 500,
|
2024-03-05 09:45:55 +01:00
|
|
|
offset: number = 0,
|
2023-12-13 04:00:44 +01:00
|
|
|
) {
|
2023-07-25 08:06:16 +02:00
|
|
|
const { rawQuery, parseFilters } = prisma;
|
2024-03-26 06:51:10 +01:00
|
|
|
|
2023-08-04 23:23:03 +02:00
|
|
|
const { filterQuery, joinSession, params } = await parseFilters(
|
|
|
|
websiteId,
|
|
|
|
{
|
|
|
|
...filters,
|
|
|
|
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
|
|
|
},
|
|
|
|
{ joinSession: SESSION_COLUMNS.includes(column) },
|
|
|
|
);
|
2022-12-13 04:45:38 +01:00
|
|
|
|
2023-09-05 21:22:51 +02:00
|
|
|
let excludeDomain = '';
|
|
|
|
if (column === 'referrer_domain') {
|
|
|
|
excludeDomain =
|
|
|
|
'and (website_event.referrer_domain != {{websiteDomain}} or website_event.referrer_domain is null)';
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
return rawQuery(
|
2023-07-25 08:06:16 +02:00
|
|
|
`
|
|
|
|
select ${column} x, count(*) y
|
2022-12-13 04:45:38 +01:00
|
|
|
from website_event
|
2023-08-04 23:23:03 +02:00
|
|
|
${joinSession}
|
2023-07-25 08:06:16 +02:00
|
|
|
where website_event.website_id = {{websiteId::uuid}}
|
|
|
|
and website_event.created_at between {{startDate}} and {{endDate}}
|
|
|
|
and event_type = {{eventType}}
|
2023-09-05 21:22:51 +02:00
|
|
|
${excludeDomain}
|
2022-12-13 04:45:38 +01:00
|
|
|
${filterQuery}
|
|
|
|
group by 1
|
2023-03-05 22:30:21 +01:00
|
|
|
order by 2 desc
|
2023-12-13 04:00:44 +01:00
|
|
|
limit ${limit}
|
2024-03-05 09:45:55 +01:00
|
|
|
offset ${offset}
|
2023-07-25 08:06:16 +02:00
|
|
|
`,
|
2023-08-04 22:18:30 +02:00
|
|
|
params,
|
2022-12-13 04:45:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-29 20:00:06 +02:00
|
|
|
async function clickhouseQuery(
|
|
|
|
websiteId: string,
|
|
|
|
column: string,
|
|
|
|
filters: QueryFilters,
|
2024-02-15 00:13:53 +01:00
|
|
|
limit: number = 500,
|
2024-03-05 09:45:55 +01:00
|
|
|
offset: number = 0,
|
2023-09-29 20:00:06 +02:00
|
|
|
): Promise<{ x: string; y: number }[]> {
|
2023-07-25 08:06:16 +02:00
|
|
|
const { rawQuery, parseFilters } = clickhouse;
|
2024-03-26 06:51:10 +01:00
|
|
|
|
2023-08-04 22:18:30 +02:00
|
|
|
const { filterQuery, params } = await parseFilters(websiteId, {
|
|
|
|
...filters,
|
|
|
|
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
|
|
|
});
|
2022-12-13 04:45:38 +01:00
|
|
|
|
2023-09-05 21:22:51 +02:00
|
|
|
let excludeDomain = '';
|
|
|
|
if (column === 'referrer_domain') {
|
|
|
|
excludeDomain = 'and referrer_domain != {websiteDomain:String}';
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
return rawQuery(
|
2023-07-25 08:06:16 +02:00
|
|
|
`
|
|
|
|
select ${column} x, count(*) 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-09-29 20:00:06 +02:00
|
|
|
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
|
2023-01-12 09:02:12 +01:00
|
|
|
and event_type = {eventType:UInt32}
|
2023-09-05 21:22:51 +02:00
|
|
|
${excludeDomain}
|
2022-12-13 04:45:38 +01:00
|
|
|
${filterQuery}
|
|
|
|
group by x
|
2023-03-05 22:30:21 +01:00
|
|
|
order by y desc
|
2023-12-13 04:00:44 +01:00
|
|
|
limit ${limit}
|
2024-03-05 09:45:55 +01:00
|
|
|
offset ${offset}
|
2023-07-25 08:06:16 +02:00
|
|
|
`,
|
2023-08-04 22:18:30 +02:00
|
|
|
params,
|
2023-09-29 20:00:06 +02:00
|
|
|
).then(a => {
|
|
|
|
return Object.values(a).map(a => {
|
|
|
|
return { x: a.x, y: Number(a.y) };
|
|
|
|
});
|
|
|
|
});
|
2022-12-13 04:45:38 +01:00
|
|
|
}
|