umami/queries/analytics/session/getSessionMetrics.ts

76 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';
2023-06-15 07:32:06 +02:00
import { DEFAULT_CREATED_AT, EVENT_TYPE } from 'lib/constants';
2023-04-02 02:38:35 +02:00
import { loadWebsite } from 'lib/query';
2022-07-12 23:14:36 +02:00
export async function getSessionMetrics(
...args: [
websiteId: string,
2023-04-02 00:44:30 +02:00
criteria: { startDate: Date; endDate: Date; column: 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,
2023-04-02 00:44:30 +02:00
criteria: { startDate: Date; endDate: Date; column: string; filters: object },
) {
2023-04-02 02:38:35 +02:00
const website = await loadWebsite(websiteId);
2023-06-15 07:32:06 +02:00
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
2023-04-02 00:44:30 +02:00
const { startDate, endDate, column, filters = {} } = criteria;
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(
2023-04-02 00:44:30 +02:00
`select ${column} 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,
2023-04-02 00:44:30 +02:00
data: { startDate: Date; endDate: Date; column: string; filters: object },
) {
2023-04-02 00:44:30 +02:00
const { startDate, endDate, column, filters = {} } = data;
const { getDateFormat, parseFilters, getBetweenDates, rawQuery } = clickhouse;
2023-04-02 02:38:35 +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
const { filterQuery } = parseFilters(filters, params);
2022-07-21 06:31:26 +02:00
2022-08-28 06:38:35 +02:00
return rawQuery(
2023-04-02 00:44:30 +02:00
`select ${column} 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,
);
}