2022-08-29 17:47:01 +00:00
|
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
2022-08-27 03:21:53 +00:00
|
|
|
import kafka from 'lib/kafka';
|
2022-08-29 17:47:01 +00:00
|
|
|
import prisma from 'lib/prisma';
|
2022-11-08 06:35:51 +00:00
|
|
|
import cache from 'lib/cache';
|
2022-12-13 03:45:38 +00:00
|
|
|
import { Prisma } from '@prisma/client';
|
2022-07-12 21:14:36 +00:00
|
|
|
|
2022-12-13 03:45:38 +00:00
|
|
|
export async function createSession(args: Prisma.SessionCreateInput) {
|
2022-08-28 04:38:35 +00:00
|
|
|
return runQuery({
|
2022-12-13 03:45:38 +00:00
|
|
|
[PRISMA]: () => relationalQuery(args),
|
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
2022-11-08 00:22:49 +00:00
|
|
|
}).then(async data => {
|
2022-11-08 06:35:51 +00:00
|
|
|
if (cache.enabled) {
|
|
|
|
|
await cache.storeSession(data);
|
2022-11-08 00:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
2022-07-22 21:43:19 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 03:45:38 +00:00
|
|
|
async function relationalQuery(data: Prisma.SessionCreateInput) {
|
2023-04-14 05:40:48 +00:00
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
websiteId,
|
|
|
|
|
hostname,
|
|
|
|
|
browser,
|
|
|
|
|
os,
|
|
|
|
|
device,
|
|
|
|
|
screen,
|
|
|
|
|
language,
|
|
|
|
|
country,
|
|
|
|
|
subdivision1,
|
|
|
|
|
subdivision2,
|
|
|
|
|
city,
|
|
|
|
|
} = data;
|
|
|
|
|
|
|
|
|
|
return prisma.client.session.create({
|
|
|
|
|
data: {
|
|
|
|
|
id,
|
|
|
|
|
websiteId,
|
|
|
|
|
hostname,
|
|
|
|
|
browser,
|
|
|
|
|
os,
|
|
|
|
|
device,
|
|
|
|
|
screen,
|
|
|
|
|
language,
|
|
|
|
|
country,
|
2023-04-14 06:32:28 +00:00
|
|
|
subdivision1: country && subdivision1 ? `${country}-${subdivision1}` : null,
|
2023-04-14 05:40:48 +00:00
|
|
|
subdivision2,
|
|
|
|
|
city,
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-07-12 21:14:36 +00:00
|
|
|
}
|
2022-07-22 21:43:19 +00:00
|
|
|
|
2022-12-13 03:45:38 +00:00
|
|
|
async function clickhouseQuery(data: {
|
|
|
|
|
id: string;
|
|
|
|
|
websiteId: string;
|
|
|
|
|
hostname?: string;
|
|
|
|
|
browser?: string;
|
|
|
|
|
os?: string;
|
|
|
|
|
device?: string;
|
|
|
|
|
screen?: string;
|
|
|
|
|
language?: string;
|
|
|
|
|
country?: string;
|
2023-02-20 17:04:20 +00:00
|
|
|
subdivision1?: string;
|
|
|
|
|
subdivision2?: string;
|
|
|
|
|
city?: string;
|
2022-12-13 03:45:38 +00:00
|
|
|
}) {
|
2023-02-20 17:04:20 +00:00
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
websiteId,
|
|
|
|
|
hostname,
|
|
|
|
|
browser,
|
|
|
|
|
os,
|
|
|
|
|
device,
|
|
|
|
|
screen,
|
|
|
|
|
language,
|
|
|
|
|
country,
|
|
|
|
|
subdivision1,
|
|
|
|
|
subdivision2,
|
|
|
|
|
city,
|
|
|
|
|
} = data;
|
2022-08-28 04:38:35 +00:00
|
|
|
const { getDateFormat, sendMessage } = kafka;
|
2022-09-12 16:55:34 +00:00
|
|
|
|
2022-11-08 06:35:51 +00:00
|
|
|
const msg = {
|
|
|
|
|
session_id: id,
|
2022-10-10 20:42:18 +00:00
|
|
|
website_id: websiteId,
|
2022-09-12 16:55:34 +00:00
|
|
|
hostname,
|
|
|
|
|
browser,
|
|
|
|
|
os,
|
|
|
|
|
device,
|
|
|
|
|
screen,
|
|
|
|
|
language,
|
2022-11-08 06:35:51 +00:00
|
|
|
country,
|
2023-02-20 17:04:20 +00:00
|
|
|
subdivision1,
|
|
|
|
|
subdivision2,
|
|
|
|
|
city,
|
2022-11-08 06:35:51 +00:00
|
|
|
created_at: getDateFormat(new Date()),
|
2022-08-01 07:28:38 +00:00
|
|
|
};
|
2022-07-22 21:43:19 +00:00
|
|
|
|
2022-11-08 06:35:51 +00:00
|
|
|
await sendMessage(msg, 'event');
|
2022-08-27 03:21:53 +00:00
|
|
|
|
2022-11-08 00:22:49 +00:00
|
|
|
return data;
|
2022-07-22 21:43:19 +00:00
|
|
|
}
|