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';
|
|
|
|
import { EVENT_TYPE } from 'lib/constants';
|
2023-04-02 02:38:35 +02:00
|
|
|
import { loadWebsite } from 'lib/query';
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
export async function getPageviewStats(
|
|
|
|
...args: [
|
|
|
|
websiteId: string,
|
2023-04-02 00:44:30 +02:00
|
|
|
criteria: {
|
2022-12-13 04:45:38 +01:00
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
timezone?: string;
|
|
|
|
unit?: string;
|
|
|
|
count?: string;
|
|
|
|
filters: object;
|
|
|
|
sessionKey?: string;
|
|
|
|
},
|
|
|
|
]
|
|
|
|
) {
|
|
|
|
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;
|
|
|
|
timezone?: string;
|
|
|
|
unit?: string;
|
|
|
|
count?: string;
|
|
|
|
filters: object;
|
|
|
|
sessionKey?: string;
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
const {
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
timezone = 'utc',
|
|
|
|
unit = 'day',
|
|
|
|
count = '*',
|
|
|
|
filters = {},
|
|
|
|
sessionKey = 'session_id',
|
2023-04-02 00:44:30 +02:00
|
|
|
} = criteria;
|
2023-05-15 08:49:21 +02:00
|
|
|
const { getDatabaseType, toUuid, getDateQuery, parseFilters, rawQuery, client } = prisma;
|
|
|
|
const db = getDatabaseType();
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-04-20 06:16:56 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
2023-03-27 20:25:16 +02:00
|
|
|
const params: any = [websiteId, resetDate, startDate, endDate];
|
2022-12-13 04:45:38 +01:00
|
|
|
const { filterQuery, joinSession } = parseFilters(filters, params);
|
|
|
|
|
2023-05-18 02:10:35 +02:00
|
|
|
let sessionInclude = '';
|
|
|
|
let sessionGroupAggregation: any = { $match: {} };
|
|
|
|
let sessionProjectAggregation: any = { $match: {} };
|
|
|
|
if (count !== '*') {
|
|
|
|
sessionInclude = 'session_id : 1';
|
|
|
|
sessionGroupAggregation = {
|
|
|
|
$group: {
|
|
|
|
_id: {
|
|
|
|
t: '$t',
|
|
|
|
session_id: '$session_id',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
sessionProjectAggregation = {
|
|
|
|
$project: {
|
|
|
|
t: '$_id.t',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2023-05-15 08:49:21 +02:00
|
|
|
if (db === 'mongodb') {
|
|
|
|
return await client.websiteEvent.aggregateRaw({
|
|
|
|
pipeline: [
|
2023-05-18 02:10:35 +02:00
|
|
|
{
|
|
|
|
$match: {
|
|
|
|
$expr: {
|
|
|
|
$and: [
|
|
|
|
{
|
|
|
|
$eq: ['$event_type', EVENT_TYPE.pageView],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$eq: ['$website_id', websiteId],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$gte: [
|
|
|
|
'$created_at',
|
|
|
|
{
|
|
|
|
$dateFromString: {
|
|
|
|
dateString: resetDate.toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$gte: [
|
|
|
|
'$created_at',
|
|
|
|
{
|
|
|
|
$dateFromString: {
|
|
|
|
dateString: startDate.toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lte: [
|
|
|
|
'$created_at',
|
|
|
|
{
|
|
|
|
$dateFromString: {
|
|
|
|
dateString: endDate.toISOString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
t: {
|
|
|
|
$dateTrunc: {
|
|
|
|
date: '$created_at',
|
|
|
|
unit: unit,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sessionInclude,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sessionGroupAggregation,
|
|
|
|
sessionProjectAggregation,
|
|
|
|
{
|
|
|
|
$group: {
|
|
|
|
_id: {
|
|
|
|
t: '$t',
|
|
|
|
session_id: '$session_id',
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
$sum: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-05-15 08:49:21 +02:00
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
x: {
|
|
|
|
$dateToString: {
|
2023-05-18 02:10:35 +02:00
|
|
|
date: '$_id.t',
|
|
|
|
format: getDateQuery('', unit, timezone),
|
2023-05-15 08:49:21 +02:00
|
|
|
timezone: timezone,
|
|
|
|
},
|
|
|
|
},
|
2023-05-18 02:10:35 +02:00
|
|
|
y: 1,
|
|
|
|
_id: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$sort: {
|
|
|
|
x: 1,
|
2023-05-15 08:49:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return rawQuery(
|
|
|
|
`select ${getDateQuery('website_event.created_at', unit, timezone)} x,
|
2022-12-13 04:45:38 +01:00
|
|
|
count(${count !== '*' ? `${count}${sessionKey}` : 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
|
2022-12-13 04:45:38 +01:00
|
|
|
and event_type = ${EVENT_TYPE.pageView}
|
|
|
|
${filterQuery}
|
|
|
|
group by 1`,
|
2023-05-15 08:49:21 +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;
|
|
|
|
timezone?: string;
|
|
|
|
unit?: string;
|
|
|
|
count?: string;
|
|
|
|
filters: object;
|
|
|
|
sessionKey?: string;
|
|
|
|
},
|
|
|
|
) {
|
2023-04-02 00:44:30 +02:00
|
|
|
const {
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
timezone = 'UTC',
|
|
|
|
unit = 'day',
|
|
|
|
count = '*',
|
|
|
|
filters = {},
|
|
|
|
} = criteria;
|
2023-03-27 20:25:16 +02:00
|
|
|
const {
|
|
|
|
parseFilters,
|
|
|
|
getDateFormat,
|
|
|
|
rawQuery,
|
|
|
|
getDateStringQuery,
|
|
|
|
getDateQuery,
|
|
|
|
getBetweenDates,
|
|
|
|
} = clickhouse;
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-04-20 06:16:56 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || website?.createdAt);
|
2023-03-27 20:25:16 +02:00
|
|
|
const params = { websiteId };
|
2022-12-13 04:45:38 +01:00
|
|
|
const { filterQuery } = parseFilters(filters, params);
|
|
|
|
|
|
|
|
return rawQuery(
|
|
|
|
`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
|
|
|
|
from
|
|
|
|
(select
|
|
|
|
${getDateQuery('created_at', unit, timezone)} t,
|
|
|
|
count(${count !== '*' ? 'distinct session_id' : 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}
|
2022-12-13 04:45:38 +01:00
|
|
|
and event_type = ${EVENT_TYPE.pageView}
|
2023-03-27 20:25:16 +02:00
|
|
|
and created_at >= ${getDateFormat(resetDate)}
|
2022-12-13 04:45:38 +01:00
|
|
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
|
|
${filterQuery}
|
|
|
|
group by t) g
|
|
|
|
order by t`,
|
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|