umami/queries/analytics/pageview/getPageviewMetrics.ts

100 lines
2.7 KiB
TypeScript
Raw Normal View History

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';
export async function getPageviewMetrics(
...args: [
websiteId: string,
data: {
startDate: Date;
endDate: Date;
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
filters: object;
type: string;
},
]
) {
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;
type: string;
},
) {
const { startDate, endDate, column, filters = {}, type } = data;
const { rawQuery, parseFilters, toUuid } = prisma;
2023-03-27 21:11:06 +02:00
const website = await getWebsite({ id: websiteId });
const resetDate = website?.resetAt || website?.createdAt;
const params: any = [
websiteId,
resetDate,
startDate,
endDate,
type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
];
const { filterQuery, joinSession } = parseFilters(filters, params);
return rawQuery(
`select ${column} x, count(*) y
from website_event
${joinSession}
where website_event.website_id = $1${toUuid()}
and website_event.created_at >= $2
and website_event.created_at between $3 and $4
and event_type = $5
${filterQuery}
group by 1
2023-03-05 22:30:21 +01:00
order by 2 desc
limit 200`,
params,
);
}
async function clickhouseQuery(
websiteId: string,
data: {
startDate: Date;
endDate: Date;
column: Prisma.WebsiteEventScalarFieldEnum | Prisma.SessionScalarFieldEnum;
filters: object;
type: string;
},
) {
const { startDate, endDate, column, filters = {}, type } = data;
const { rawQuery, getDateFormat, parseFilters, getBetweenDates } = clickhouse;
const website = await cache.fetchWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;
const params = {
websiteId,
eventType: type === 'event' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
};
const { filterQuery } = parseFilters(filters, params);
return rawQuery(
`select ${column} x, count(*) y
2023-03-29 20:06:12 +02:00
from website_event
where website_id = {websiteId:UUID}
and event_type = {eventType:UInt32}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
${filterQuery}
group by x
2023-03-05 22:30:21 +01:00
order by y desc
limit 200`,
params,
);
}