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';
|
|
|
|
import cache from 'lib/cache';
|
|
|
|
import { Prisma } from '@prisma/client';
|
|
|
|
import { EVENT_TYPE } from 'lib/constants';
|
2023-03-27 21:11:06 +02:00
|
|
|
import { getWebsite } from 'queries';
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
export async function getPageviewMetrics(
|
|
|
|
...args: [
|
|
|
|
websiteId: string,
|
|
|
|
data: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
|
|
|
filters: object;
|
2023-01-06 04:39:29 +01:00
|
|
|
type: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
) {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function relationalQuery(
|
|
|
|
websiteId: string,
|
|
|
|
data: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
|
|
|
filters: object;
|
2023-01-06 04:39:29 +01:00
|
|
|
type: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
},
|
|
|
|
) {
|
2023-01-06 04:39:29 +01:00
|
|
|
const { startDate, endDate, column, filters = {}, type } = data;
|
2023-01-11 20:01:44 +01:00
|
|
|
const { rawQuery, parseFilters, toUuid } = prisma;
|
2023-03-27 21:11:06 +02:00
|
|
|
const website = await getWebsite({ id: websiteId });
|
2023-03-27 20:25:16 +02:00
|
|
|
const resetDate = 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,
|
|
|
|
type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
|
|
|
];
|
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
|
2022-12-13 04:45:38 +01:00
|
|
|
${filterQuery}
|
|
|
|
group by 1
|
2023-03-05 22:30:21 +01:00
|
|
|
order by 2 desc
|
|
|
|
limit 200`,
|
2022-12-13 04:45:38 +01:00
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clickhouseQuery(
|
|
|
|
websiteId: string,
|
|
|
|
data: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
|
|
|
|
filters: object;
|
2023-01-06 04:39:29 +01:00
|
|
|
type: string;
|
2022-12-13 04:45:38 +01:00
|
|
|
},
|
|
|
|
) {
|
2023-01-06 04:39:29 +01:00
|
|
|
const { startDate, endDate, column, filters = {}, type } = data;
|
2023-03-27 20:25:16 +02:00
|
|
|
const { rawQuery, getDateFormat, parseFilters, getBetweenDates } = clickhouse;
|
2022-12-13 04:45:38 +01:00
|
|
|
const website = await cache.fetchWebsite(websiteId);
|
2023-03-27 20:25:16 +02:00
|
|
|
const resetDate = website?.resetAt || website?.createdAt;
|
2023-01-12 09:02:12 +01:00
|
|
|
const params = {
|
2023-01-06 04:39:29 +01:00
|
|
|
websiteId,
|
2023-01-12 09:02:12 +01:00
|
|
|
eventType: type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
|
|
|
|
};
|
2022-12-13 04:45:38 +01:00
|
|
|
const { filterQuery } = parseFilters(filters, params);
|
|
|
|
|
|
|
|
return rawQuery(
|
|
|
|
`select ${column} x, count(*) y
|
|
|
|
from 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)}
|
2022-12-13 04:45:38 +01:00
|
|
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
|
|
${filterQuery}
|
|
|
|
group by x
|
2023-03-05 22:30:21 +01:00
|
|
|
order by y desc
|
|
|
|
limit 200`,
|
2022-12-13 04:45:38 +01:00
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|