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-11-08 07:35:51 +01:00
|
|
|
import cache from 'lib/cache';
|
2022-12-13 04:45:38 +01:00
|
|
|
import { Prisma } from '@prisma/client';
|
2022-07-12 23:14:36 +02:00
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
export async function createSession(args: Prisma.SessionCreateInput) {
|
2022-08-28 06:38:35 +02:00
|
|
|
return runQuery({
|
2022-12-13 04:45:38 +01:00
|
|
|
[PRISMA]: () => relationalQuery(args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
2022-11-08 01:22:49 +01:00
|
|
|
}).then(async data => {
|
2022-11-08 07:35:51 +01:00
|
|
|
if (cache.enabled) {
|
|
|
|
await cache.storeSession(data);
|
2022-11-08 01:22:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2022-07-22 23:43:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
async function relationalQuery(data: Prisma.SessionCreateInput) {
|
2022-11-08 07:35:51 +01:00
|
|
|
return prisma.client.session.create({ data });
|
2022-07-12 23:14:36 +02:00
|
|
|
}
|
2022-07-22 23:43:19 +02:00
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
async function clickhouseQuery(data: {
|
|
|
|
id: string;
|
|
|
|
websiteId: string;
|
|
|
|
hostname?: string;
|
|
|
|
browser?: string;
|
|
|
|
os?: string;
|
|
|
|
device?: string;
|
|
|
|
screen?: string;
|
|
|
|
language?: string;
|
|
|
|
country?: string;
|
|
|
|
}) {
|
2022-11-08 07:35:51 +01:00
|
|
|
const { id, websiteId, hostname, browser, os, device, screen, language, country } = data;
|
2022-08-28 06:38:35 +02:00
|
|
|
const { getDateFormat, sendMessage } = kafka;
|
2022-11-08 07:35:51 +01:00
|
|
|
const website = await cache.fetchWebsite(websiteId);
|
2022-09-12 18:55:34 +02:00
|
|
|
|
2022-11-08 07:35:51 +01:00
|
|
|
const msg = {
|
|
|
|
session_id: id,
|
2022-10-10 22:42:18 +02:00
|
|
|
website_id: websiteId,
|
2022-09-12 18:55:34 +02:00
|
|
|
hostname,
|
|
|
|
browser,
|
|
|
|
os,
|
|
|
|
device,
|
|
|
|
screen,
|
|
|
|
language,
|
2022-11-08 07:35:51 +01:00
|
|
|
country,
|
|
|
|
rev_id: website?.revId || 0,
|
|
|
|
created_at: getDateFormat(new Date()),
|
2022-08-01 09:28:38 +02:00
|
|
|
};
|
2022-07-22 23:43:19 +02:00
|
|
|
|
2022-11-08 07:35:51 +01:00
|
|
|
await sendMessage(msg, 'event');
|
2022-08-27 05:21:53 +02:00
|
|
|
|
2022-11-08 01:22:49 +01:00
|
|
|
return data;
|
2022-07-22 23:43:19 +02:00
|
|
|
}
|