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 redis from 'lib/redis';
|
2022-07-12 23:14:36 +02:00
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
export async function getSession(...args) {
|
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-11-08 01:22:49 +01:00
|
|
|
}).then(async data => {
|
|
|
|
if (redis.enabled && data) {
|
|
|
|
await redis.set(`session:${data.id}`, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2022-07-21 06:31:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
async function relationalQuery(where) {
|
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-08 01:22:49 +01:00
|
|
|
async function clickhouseQuery(sessionId) {
|
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-09-12 18:55:34 +02:00
|
|
|
`select distinct
|
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-10-12 08:09:06 +02:00
|
|
|
where session_id = $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
|
|
|
}
|