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';
|
2022-11-15 22:21:14 +01:00
|
|
|
import { Prisma } from '@prisma/client';
|
2022-07-12 23:14:36 +02:00
|
|
|
|
2022-11-15 22:21:14 +01:00
|
|
|
export async function getSession(args: { id: string }) {
|
2022-08-28 06:38:35 +02:00
|
|
|
return runQuery({
|
2022-11-15 22:21:14 +01:00
|
|
|
[PRISMA]: () => relationalQuery(args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
2022-07-21 06:31:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-15 22:21:14 +01:00
|
|
|
async function relationalQuery(where: Prisma.SessionWhereUniqueInput) {
|
2022-11-08 01:22:49 +01:00
|
|
|
return prisma.client.session.findUnique({
|
|
|
|
where,
|
|
|
|
});
|
2022-07-12 23:14:36 +02:00
|
|
|
}
|
2022-07-21 06:31:26 +02:00
|
|
|
|
2022-11-15 22:21:14 +01:00
|
|
|
async function clickhouseQuery({ id: sessionId }: { id: string }) {
|
2022-08-28 06:38:35 +02:00
|
|
|
const { rawQuery, findFirst } = clickhouse;
|
2022-11-08 01:22:49 +01:00
|
|
|
const params = [sessionId];
|
2022-07-21 06:31:26 +02:00
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
return rawQuery(
|
2022-11-08 07:35:51 +01:00
|
|
|
`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,
|
2022-07-21 06:31:26 +02:00
|
|
|
country
|
2022-09-12 18:55:34 +02:00
|
|
|
from event
|
2022-11-09 00:42:46 +01:00
|
|
|
where session_id = $1
|
2022-11-08 07:35:51 +01:00
|
|
|
limit 1`,
|
2022-08-28 06:38:35 +02:00
|
|
|
params,
|
2022-11-08 01:22:49 +01:00
|
|
|
).then(result => findFirst(result));
|
2022-07-21 06:31:26 +02:00
|
|
|
}
|