umami/queries/analytics/pageview/getPageviewMetrics.ts

114 lines
2.9 KiB
TypeScript
Raw Normal View History

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';
export async function getPageviewMetrics(
...args: [
websiteId: string,
2023-04-02 00:44:30 +02:00
criteria: {
startDate: Date;
endDate: Date;
2023-04-02 00:44:30 +02:00
column: string;
filters: object;
},
]
) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(
websiteId: string,
2023-04-02 00:44:30 +02:00
criteria: {
startDate: Date;
endDate: Date;
2023-04-02 00:44:30 +02:00
column: string;
filters: object;
},
) {
2023-04-02 00:44:30 +02:00
const { startDate, endDate, filters = {}, column } = criteria;
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);
const params: any = [
websiteId,
resetDate,
startDate,
endDate,
2023-04-02 00:44:30 +02:00
column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
];
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);
}
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
2023-04-02 19:00:28 +02:00
${excludeDomain}
${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`,
params,
);
}
async function clickhouseQuery(
websiteId: string,
2023-04-02 00:44:30 +02:00
criteria: {
startDate: Date;
endDate: Date;
2023-04-02 00:44:30 +02:00
column: string;
filters: object;
},
) {
2023-04-02 00:44:30 +02:00
const { startDate, endDate, filters = {}, column } = criteria;
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);
const params = {
websiteId,
2023-04-02 00:44:30 +02:00
eventType: column === 'event_name' ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
domain: undefined,
};
2023-04-02 00:44:30 +02:00
let excludeDomain = '';
if (column === 'referrer_domain') {
excludeDomain = 'and referrer_domain != {domain:String}';
params.domain = website.domain;
}
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)}
2023-04-02 00:44:30 +02:00
and ${getBetweenDates('created_at', startDate, endDate)}
${excludeDomain}
${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`,
params,
);
}