umami/src/queries/analytics/sessions/getSessionMetrics.ts

98 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-08-26 05:04:32 +00:00
import clickhouse from 'lib/clickhouse';
2024-04-10 05:20:11 +00:00
import { EVENT_TYPE, FILTER_COLUMNS, SESSION_COLUMNS } from 'lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
2023-08-04 20:18:30 +00:00
import { QueryFilters } from 'lib/types';
2022-07-12 21:14:36 +00:00
export async function getSessionMetrics(
...args: [websiteId: string, type: string, filters: QueryFilters, limit?: number, offset?: number]
) {
2022-08-28 04:38:35 +00:00
return runQuery({
[PRISMA]: () => relationalQuery(...args),
2022-07-25 16:47:11 +00:00
[CLICKHOUSE]: () => clickhouseQuery(...args),
2022-07-21 04:31:26 +00:00
});
}
2023-12-13 03:00:44 +00:00
async function relationalQuery(
websiteId: string,
2024-04-10 05:20:11 +00:00
type: string,
2023-12-13 03:00:44 +00:00
filters: QueryFilters,
2024-02-14 23:13:53 +00:00
limit: number = 500,
2024-03-05 08:45:55 +00:00
offset: number = 0,
2023-12-13 03:00:44 +00:00
) {
2024-04-10 05:20:11 +00:00
const column = FILTER_COLUMNS[type] || type;
2023-07-25 06:06:16 +00:00
const { parseFilters, rawQuery } = prisma;
2023-08-04 21:23:03 +00:00
const { filterQuery, joinSession, params } = await parseFilters(
websiteId,
{
...filters,
eventType: EVENT_TYPE.pageView,
},
{
2024-04-10 05:20:11 +00:00
joinSession: SESSION_COLUMNS.includes(type),
2023-08-04 21:23:03 +00:00
},
);
const includeCountry = column === 'city' || column === 'subdivision1';
2022-07-12 21:14:36 +00:00
return rawQuery(
2023-08-04 21:00:37 +00:00
`
2023-08-26 19:46:27 +00:00
select
${column} x,
count(distinct website_event.session_id) y
${includeCountry ? ', country' : ''}
2023-08-04 21:00:37 +00:00
from website_event
2023-08-04 21:23:03 +00:00
${joinSession}
2023-08-04 21:00:37 +00:00
where website_event.website_id = {{websiteId::uuid}}
and website_event.created_at between {{startDate}} and {{endDate}}
and website_event.event_type = {{eventType}}
${filterQuery}
2023-08-26 19:46:27 +00:00
group by 1
${includeCountry ? ', 3' : ''}
2023-03-05 21:30:21 +00:00
order by 2 desc
2024-03-05 08:45:55 +00:00
limit ${limit}
offset ${offset}
`,
2023-08-04 20:18:30 +00:00
params,
2022-07-12 21:14:36 +00:00
);
}
2022-07-21 04:31:26 +00:00
2023-09-29 18:00:06 +00:00
async function clickhouseQuery(
websiteId: string,
2024-04-10 05:20:11 +00:00
type: string,
2023-09-29 18:00:06 +00:00
filters: QueryFilters,
2024-02-14 23:13:53 +00:00
limit: number = 500,
2024-03-05 08:45:55 +00:00
offset: number = 0,
2023-09-29 18:00:06 +00:00
): Promise<{ x: string; y: number }[]> {
2024-04-10 05:20:11 +00:00
const column = FILTER_COLUMNS[type] || type;
const { parseSessionFilters, rawQuery } = clickhouse;
const { filterQuery, params } = await parseSessionFilters(websiteId, {
2023-08-04 20:18:30 +00:00
...filters,
eventType: EVENT_TYPE.pageView,
});
const includeCountry = column === 'city' || column === 'subdivision1';
2022-07-21 04:31:26 +00:00
2022-08-28 04:38:35 +00:00
return rawQuery(
2023-07-25 06:06:16 +00:00
`
select
2023-08-26 19:46:27 +00:00
${column} x,
uniq(session_id) y
${includeCountry ? ', country' : ''}
from website_event_stats_hourly website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
2023-07-25 07:30:18 +00:00
and event_type = {eventType:UInt32}
${filterQuery}
2023-08-26 19:46:27 +00:00
group by x
${includeCountry ? ', country' : ''}
2023-03-05 21:30:21 +00:00
order by y desc
2023-12-13 03:00:44 +00:00
limit ${limit}
2024-03-05 08:45:55 +00:00
offset ${offset}
2023-07-25 06:06:16 +00:00
`,
2023-08-04 20:18:30 +00:00
params,
2023-09-29 18:00:06 +00:00
).then(a => {
return Object.values(a).map(a => {
2023-10-11 17:46:08 +00:00
return { x: a.x, y: Number(a.y), country: a.country };
2023-09-29 18:00:06 +00:00
});
});
2022-07-21 04:31:26 +00:00
}