2022-08-29 19:47:01 +02:00
|
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
2022-08-27 05:21:53 +02:00
|
|
|
import kafka from 'lib/kafka';
|
2022-08-29 19:47:01 +02:00
|
|
|
import prisma from 'lib/prisma';
|
2022-08-27 05:21:53 +02:00
|
|
|
import redis from 'lib/redis';
|
2022-07-12 23:14:36 +02:00
|
|
|
|
2022-07-22 23:43:19 +02:00
|
|
|
export async function createSession(...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-07-22 23:43:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function relationalQuery(website_id, data) {
|
2022-08-28 06:38:35 +02:00
|
|
|
return prisma.client.session
|
|
|
|
.create({
|
2022-07-12 23:14:36 +02:00
|
|
|
data: {
|
|
|
|
website_id,
|
|
|
|
...data,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
session_id: true,
|
|
|
|
},
|
2022-08-28 06:38:35 +02:00
|
|
|
})
|
|
|
|
.then(async res => {
|
2022-08-29 22:04:58 +02:00
|
|
|
if (redis.client && res) {
|
2022-09-06 07:47:45 +02:00
|
|
|
await redis.client.set(`session:${res.session_uuid}`, res.session_id);
|
2022-08-28 06:38:35 +02:00
|
|
|
}
|
2022-08-27 05:21:53 +02:00
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
return res;
|
|
|
|
});
|
2022-07-12 23:14:36 +02:00
|
|
|
}
|
2022-07-22 23:43:19 +02:00
|
|
|
|
|
|
|
async function clickhouseQuery(
|
|
|
|
website_id,
|
|
|
|
{ session_uuid, hostname, browser, os, screen, language, country, device },
|
|
|
|
) {
|
2022-08-28 06:38:35 +02:00
|
|
|
const { getDateFormat, sendMessage } = kafka;
|
2022-08-01 09:28:38 +02:00
|
|
|
const params = {
|
|
|
|
session_uuid: session_uuid,
|
|
|
|
website_id: website_id,
|
2022-08-28 06:38:35 +02:00
|
|
|
created_at: getDateFormat(new Date()),
|
2022-08-01 09:28:38 +02:00
|
|
|
hostname: hostname,
|
|
|
|
browser: browser,
|
|
|
|
os: os,
|
|
|
|
device: device,
|
|
|
|
screen: screen,
|
|
|
|
language: language,
|
|
|
|
country: country ? country : null,
|
|
|
|
};
|
2022-07-22 23:43:19 +02:00
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
await sendMessage(params, 'session');
|
2022-08-27 05:21:53 +02:00
|
|
|
|
2022-08-29 22:04:58 +02:00
|
|
|
if (redis.client) {
|
2022-08-29 19:47:01 +02:00
|
|
|
await redis.client.set(`session:${session_uuid}`, 1);
|
|
|
|
}
|
2022-07-22 23:43:19 +02:00
|
|
|
}
|