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-04-02 19:00:28 +02:00
|
|
|
import { 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-05-18 02:10:35 +02:00
|
|
|
const { getDatabaseType, rawQuery, parseFilters, toUuid, client } = prisma;
|
|
|
|
const db = getDatabaseType();
|
2023-04-02 00:44:30 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-04-20 06:16:56 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
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-05-18 02:10:35 +02:00
|
|
|
let excludeDomainMongo = {};
|
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-05-18 02:10:35 +02:00
|
|
|
excludeDomainMongo = {
|
|
|
|
$ne: ['$referrer_domain', website.domain],
|
|
|
|
};
|
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);
|
|
|
|
|
2023-05-18 02:10:35 +02:00
|
|
|
if (db === 'mongodb') {
|
|
|
|
return await client.websiteEvent.aggregateRaw({
|
|
|
|
pipeline: [
|
|
|
|
{
|
|
|
|
$match: {
|
|
|
|
$expr: {
|
|
|
|
$and: [
|
|
|
|
{
|
|
|
|
$eq: ['$event_type', params[4]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$eq: ['$website_id', websiteId],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$gte: [
|
|
|
|
'$created_at',
|
|
|
|
{
|
|
|
|
$dateFromString: {
|
|
|
|
dateString: resetDate.toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$gte: [
|
|
|
|
'$created_at',
|
|
|
|
{
|
|
|
|
$dateFromString: {
|
|
|
|
dateString: startDate.toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2023-05-18 03:35:24 +02:00
|
|
|
$lte: [
|
2023-05-18 02:10:35 +02:00
|
|
|
'$created_at',
|
|
|
|
{
|
|
|
|
$dateFromString: {
|
|
|
|
dateString: endDate.toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
excludeDomainMongo,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$group: {
|
|
|
|
_id: '$' + column,
|
|
|
|
y: {
|
|
|
|
$sum: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
x: '$_id',
|
|
|
|
y: 1,
|
|
|
|
_id: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$sort: {
|
|
|
|
x: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$sort: {
|
|
|
|
y: -1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$limit: 100,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return rawQuery(
|
|
|
|
`select ${column} x, count(*) y
|
2022-12-13 04:45:38 +01:00
|
|
|
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`,
|
2023-05-18 02:10:35 +02:00
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|
2022-12-13 04:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-20 06:16:56 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
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,
|
|
|
|
);
|
|
|
|
}
|