umami/queries/analytics/session/getSessionMetrics.ts

77 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-08-28 06:38:35 +02:00
import prisma from 'lib/prisma';
2022-08-26 07:04:32 +02:00
import clickhouse from 'lib/clickhouse';
2022-08-28 06:38:35 +02:00
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import cache from 'lib/cache';
import { EVENT_TYPE } from 'lib/constants';
2023-03-27 21:11:06 +02:00
import { getWebsite } from 'queries';
2022-07-12 23:14:36 +02:00
export async function getSessionMetrics(
...args: [
websiteId: string,
data: { startDate: Date; endDate: Date; field: string; filters: object },
]
) {
2022-08-28 06:38:35 +02:00
return runQuery({
[PRISMA]: () => relationalQuery(...args),
2022-07-25 18:47:11 +02:00
[CLICKHOUSE]: () => clickhouseQuery(...args),
2022-07-21 06:31:26 +02:00
});
}
async function relationalQuery(
websiteId: string,
data: { startDate: Date; endDate: Date; field: string; filters: object },
) {
2023-03-27 21:11:06 +02:00
const website = await getWebsite({ id: websiteId });
const resetDate = website?.resetAt || website?.createdAt;
const { startDate, endDate, field, filters = {} } = data;
const { toUuid, parseFilters, rawQuery } = prisma;
const params: any = [websiteId, resetDate, startDate, endDate];
const { filterQuery, joinSession } = parseFilters(filters, params);
2022-07-12 23:14:36 +02:00
return rawQuery(
2022-08-28 06:38:35 +02:00
`select ${field} x, count(*) y
2022-07-12 23:14:36 +02:00
from session as x
where x.session_id in (
select website_event.session_id
from website_event
2022-10-12 04:37:38 +02:00
join website
on website_event.website_id = website.website_id
2022-07-12 23:14:36 +02:00
${joinSession}
where website.website_id = $1${toUuid()}
and website_event.created_at >= $2
and website_event.created_at between $3 and $4
${filterQuery}
2022-07-12 23:14:36 +02:00
)
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`,
2022-07-12 23:14:36 +02:00
params,
);
}
2022-07-21 06:31:26 +02:00
async function clickhouseQuery(
websiteId: string,
data: { startDate: Date; endDate: Date; field: string; filters: object },
) {
const { startDate, endDate, field, filters = {} } = data;
const { getDateFormat, parseFilters, getBetweenDates, rawQuery } = clickhouse;
const website = await cache.fetchWebsite(websiteId);
const resetDate = website?.resetAt || website?.createdAt;
const params = { websiteId };
const { filterQuery } = parseFilters(filters, params);
2022-07-21 06:31:26 +02:00
2022-08-28 06:38:35 +02:00
return rawQuery(
`select ${field} x, count(distinct session_id) y
2023-03-29 20:06:12 +02:00
from website_event as x
where website_id = {websiteId:UUID}
and event_type = ${EVENT_TYPE.pageView}
and created_at >= ${getDateFormat(resetDate)}
2022-09-12 18:55:34 +02:00
and ${getBetweenDates('created_at', startDate, endDate)}
${filterQuery}
2022-07-21 06:31:26 +02:00
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-07-21 06:31:26 +02:00
params,
);
}