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';
|
2023-06-15 07:32:06 +02:00
|
|
|
import { DEFAULT_CREATED_AT, EVENT_TYPE } from 'lib/constants';
|
2023-04-02 00:44:30 +02:00
|
|
|
import { loadWebsite } from 'lib/query';
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
export async function getPageviewMetrics(
|
|
|
|
...args: [
|
|
|
|
websiteId: string,
|
2023-04-02 00:44:30 +02:00
|
|
|
criteria: {
|
2022-12-13 04:45:38 +01:00
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
2023-04-02 00:44:30 +02:00
|
|
|
column: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
filters: object;
|
|
|
|
},
|
|
|
|
]
|
|
|
|
) {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function relationalQuery(
|
|
|
|
websiteId: string,
|
2023-04-02 00:44:30 +02:00
|
|
|
criteria: {
|
2022-12-13 04:45:38 +01:00
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
2023-04-02 00:44:30 +02:00
|
|
|
column: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
filters: object;
|
|
|
|
},
|
|
|
|
) {
|
2023-04-02 00:44:30 +02:00
|
|
|
const { startDate, endDate, filters = {}, column } = criteria;
|
2023-01-11 20:01:44 +01:00
|
|
|
const { rawQuery, parseFilters, toUuid } = prisma;
|
2023-04-02 00:44:30 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-06-15 07:32:06 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
|
2023-01-06 04:39:29 +01:00
|
|
|
const params: any = [
|
2023-01-11 20:01:44 +01:00
|
|
|
websiteId,
|
2023-03-27 20:25:16 +02:00
|
|
|
resetDate,
|
2023-01-06 04:39:29 +01:00
|
|
|
startDate,
|
|
|
|
endDate,
|
2023-04-02 00:44:30 +02:00
|
|
|
column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
2023-01-06 04:39:29 +01:00
|
|
|
];
|
2023-04-02 00:44:30 +02:00
|
|
|
|
2023-04-02 19:00:28 +02:00
|
|
|
let excludeDomain = '';
|
2023-04-02 00:44:30 +02:00
|
|
|
|
|
|
|
if (column === 'referrer_domain') {
|
2023-04-02 19:00:28 +02:00
|
|
|
excludeDomain = 'and website_event.referrer_domain != $6';
|
2023-04-02 00:44:30 +02:00
|
|
|
params.push(website.domain);
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
const { filterQuery, joinSession } = parseFilters(filters, params);
|
|
|
|
|
|
|
|
return rawQuery(
|
|
|
|
`select ${column} x, count(*) y
|
|
|
|
from website_event
|
|
|
|
${joinSession}
|
2023-01-11 20:01:44 +01:00
|
|
|
where website_event.website_id = $1${toUuid()}
|
2023-03-27 20:25:16 +02:00
|
|
|
and website_event.created_at >= $2
|
|
|
|
and website_event.created_at between $3 and $4
|
|
|
|
and event_type = $5
|
2023-04-02 19:00:28 +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-03-30 08:29:37 +02:00
|
|
|
limit 100`,
|
2022-12-13 04:45:38 +01:00
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clickhouseQuery(
|
|
|
|
websiteId: string,
|
2023-04-02 00:44:30 +02:00
|
|
|
criteria: {
|
2022-12-13 04:45:38 +01:00
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
2023-04-02 00:44:30 +02:00
|
|
|
column: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
filters: object;
|
|
|
|
},
|
|
|
|
) {
|
2023-04-02 00:44:30 +02:00
|
|
|
const { startDate, endDate, filters = {}, column } = criteria;
|
2023-03-27 20:25:16 +02:00
|
|
|
const { rawQuery, getDateFormat, parseFilters, getBetweenDates } = clickhouse;
|
2023-04-02 00:44:30 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-06-15 07:32:06 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
|
2023-01-12 09:02:12 +01:00
|
|
|
const params = {
|
2023-01-06 04:39:29 +01:00
|
|
|
websiteId,
|
2023-04-02 00:44:30 +02:00
|
|
|
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
|
|
|
domain: undefined,
|
2023-01-12 09:02:12 +01:00
|
|
|
};
|
2023-04-02 00:44:30 +02:00
|
|
|
|
|
|
|
let excludeDomain = '';
|
|
|
|
|
|
|
|
if (column === 'referrer_domain') {
|
|
|
|
excludeDomain = 'and referrer_domain != {domain:String}';
|
|
|
|
params.domain = website.domain;
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
const { filterQuery } = parseFilters(filters, params);
|
|
|
|
|
|
|
|
return rawQuery(
|
|
|
|
`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}
|
|
|
|
and event_type = {eventType:UInt32}
|
2023-03-27 20:25:16 +02:00
|
|
|
and created_at >= ${getDateFormat(resetDate)}
|
2023-04-02 00:44:30 +02:00
|
|
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
|
|
${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-03-30 08:29:37 +02:00
|
|
|
limit 100`,
|
2022-12-13 04:45:38 +01:00
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|