umami/queries/analytics/session/createSession.js

65 lines
1.3 KiB
JavaScript
Raw Normal View History

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-08-27 03:21:53 +00:00
import redis from 'lib/redis';
2022-07-12 21:14:36 +00:00
2022-07-22 21:43:19 +00:00
export async function createSession(...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-22 21:43:19 +00:00
});
}
async function relationalQuery(websiteId, data) {
2022-08-28 04:38:35 +00:00
return prisma.client.session
.create({
2022-07-12 21:14:36 +00:00
data: {
websiteId,
2022-07-12 21:14:36 +00:00
...data,
},
select: {
2022-10-12 06:09:06 +00:00
id: true,
2022-09-12 16:55:34 +00:00
hostname: true,
browser: true,
os: true,
screen: true,
language: true,
country: true,
device: true,
2022-07-12 21:14:36 +00:00
},
2022-08-28 04:38:35 +00:00
})
.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-28 04:38:35 +00:00
}
2022-08-27 03:21:53 +00:00
2022-08-28 04:38:35 +00:00
return res;
});
2022-07-12 21:14:36 +00:00
}
2022-07-22 21:43:19 +00:00
async function clickhouseQuery(
websiteId,
2022-11-01 06:42:37 +00:00
{ sessionId, hostname, browser, os, screen, language, country, device },
2022-07-22 21:43:19 +00:00
) {
2022-08-28 04:38:35 +00:00
const { getDateFormat, sendMessage } = kafka;
2022-09-12 16:55:34 +00:00
2022-08-01 07:28:38 +00:00
const params = {
2022-11-01 06:42:37 +00:00
sessionId,
website_id: websiteId,
2022-08-28 04:38:35 +00:00
created_at: getDateFormat(new Date()),
2022-09-12 16:55:34 +00:00
hostname,
browser,
os,
device,
screen,
language,
2022-08-01 07:28:38 +00:00
country: country ? country : null,
};
2022-07-22 21:43:19 +00:00
2022-09-12 16:55:34 +00:00
await sendMessage(params, 'event');
2022-08-27 03:21:53 +00:00
2022-10-26 03:17:13 +00:00
if (redis.enabled) {
2022-11-01 06:42:37 +00:00
await redis.set(`session:${sessionId}`, 1);
2022-08-29 17:47:01 +00:00
}
2022-07-22 21:43:19 +00:00
}