2022-08-26 05:04:32 +00:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
2022-08-29 17:47:01 +00:00
|
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|
|
|
|
import prisma from 'lib/prisma';
|
|
|
|
|
import redis from 'lib/redis';
|
2022-07-12 21:14:36 +00:00
|
|
|
|
2022-11-01 06:42:37 +00:00
|
|
|
export async function getSession(...args) {
|
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
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 06:42:37 +00:00
|
|
|
async function relationalQuery(where) {
|
2022-08-29 17:47:01 +00:00
|
|
|
return prisma.client.session
|
|
|
|
|
.findUnique({
|
2022-11-01 06:42:37 +00:00
|
|
|
where,
|
2022-08-29 17:47:01 +00:00
|
|
|
})
|
|
|
|
|
.then(async res => {
|
2022-10-26 03:17:13 +00:00
|
|
|
if (redis.enabled && res) {
|
|
|
|
|
await redis.set(`session:${res.sessionUuid}`, 1);
|
2022-08-29 17:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
});
|
2022-07-12 21:14:36 +00:00
|
|
|
}
|
2022-07-21 04:31:26 +00:00
|
|
|
|
2022-10-10 20:42:18 +00:00
|
|
|
async function clickhouseQuery(sessionUuid) {
|
2022-08-28 04:38:35 +00:00
|
|
|
const { rawQuery, findFirst } = clickhouse;
|
2022-10-10 20:42:18 +00:00
|
|
|
const params = [sessionUuid];
|
2022-07-21 04:31:26 +00:00
|
|
|
|
2022-08-28 04:38:35 +00:00
|
|
|
return rawQuery(
|
2022-09-12 16:55:34 +00:00
|
|
|
`select distinct
|
2022-10-12 06:09:06 +00:00
|
|
|
session_id,
|
2022-07-21 04:31:26 +00:00
|
|
|
website_id,
|
|
|
|
|
created_at,
|
|
|
|
|
hostname,
|
|
|
|
|
browser,
|
|
|
|
|
os,
|
|
|
|
|
device,
|
|
|
|
|
screen,
|
2022-08-28 04:38:35 +00:00
|
|
|
language,
|
2022-07-21 04:31:26 +00:00
|
|
|
country
|
2022-09-12 16:55:34 +00:00
|
|
|
from event
|
2022-10-12 06:09:06 +00:00
|
|
|
where session_id = $1`,
|
2022-08-28 04:38:35 +00:00
|
|
|
params,
|
2022-08-29 17:47:01 +00:00
|
|
|
)
|
|
|
|
|
.then(result => findFirst(result))
|
|
|
|
|
.then(async res => {
|
2022-10-26 03:17:13 +00:00
|
|
|
if (redis.enabled && res) {
|
2022-11-01 06:42:37 +00:00
|
|
|
await redis.set(`session:${res.id}`, 1);
|
2022-08-29 17:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
});
|
2022-07-21 04:31:26 +00:00
|
|
|
}
|