umami/queries/analytics/session/getSession.ts

44 lines
998 B
TypeScript
Raw Normal View History

2022-08-26 07:04:32 +02:00
import clickhouse from 'lib/clickhouse';
2022-08-29 19:47:01 +02:00
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
import { Prisma } from '@prisma/client';
2022-07-12 23:14:36 +02:00
export async function getSession(args: { id: string }) {
2022-08-28 06:38:35 +02:00
return runQuery({
[PRISMA]: () => relationalQuery(args),
[CLICKHOUSE]: () => clickhouseQuery(args),
2022-07-21 06:31:26 +02:00
});
}
async function relationalQuery(where: Prisma.SessionWhereUniqueInput) {
return prisma.client.session.findUnique({
where,
});
2022-07-12 23:14:36 +02:00
}
2022-07-21 06:31:26 +02:00
async function clickhouseQuery({ id: sessionId }: { id: string }) {
2022-08-28 06:38:35 +02:00
const { rawQuery, findFirst } = clickhouse;
const params = { sessionId };
2022-07-21 06:31:26 +02:00
2022-08-28 06:38:35 +02:00
return rawQuery(
`select
2022-10-12 08:09:06 +02:00
session_id,
2022-07-21 06:31:26 +02:00
website_id,
created_at,
hostname,
browser,
os,
device,
screen,
2022-08-28 06:38:35 +02:00
language,
country,
subdivision1,
subdivision2,
city
2022-09-12 18:55:34 +02:00
from event
where session_id = {sessionId:UUID}
limit 1`,
2022-08-28 06:38:35 +02:00
params,
).then(result => findFirst(result));
2022-07-21 06:31:26 +02:00
}