umami/queries/analytics/session/createSession.js

80 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-08-26 07:04:32 +02:00
import clickhouse from 'lib/clickhouse';
2022-08-27 05:21:53 +02:00
import { CLICKHOUSE, KAFKA, RELATIONAL } from 'lib/constants';
2022-08-26 07:23:19 +02:00
import { runAnalyticsQuery } from 'lib/db';
2022-08-27 05:21:53 +02:00
import kafka from 'lib/kafka';
import redis from 'lib/redis';
import { prisma, runQuery } from 'lib/relational';
2022-07-12 23:14:36 +02:00
2022-07-22 23:43:19 +02:00
export async function createSession(...args) {
return runAnalyticsQuery({
2022-07-25 18:47:11 +02:00
[RELATIONAL]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
2022-08-05 02:43:47 +02:00
[KAFKA]: () => kafkaQuery(...args),
2022-07-22 23:43:19 +02:00
});
}
async function relationalQuery(website_id, data) {
2022-07-12 23:14:36 +02:00
return runQuery(
prisma.session.create({
data: {
website_id,
...data,
},
select: {
session_id: true,
},
}),
2022-08-27 05:21:53 +02:00
).then(async res => {
if (process.env.REDIS_URL) {
await redis.set(`session:${res.session_uuid}`, '');
}
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-05 02:43:47 +02:00
) {
const params = [
session_uuid,
website_id,
hostname,
browser,
os,
device,
screen,
language,
country ? country : null,
];
2022-08-26 07:04:32 +02:00
await clickhouse.rawQuery(
2022-08-09 07:09:18 +02:00
`insert into umami.session (created_at, session_uuid, website_id, hostname, browser, os, device, screen, language, country)
2022-08-26 07:04:32 +02:00
values (${clickhouse.getDateFormat(new Date())}, $1, $2, $3, $4, $5, $6, $7, $8, $9);`,
2022-08-05 02:43:47 +02:00
params,
);
}
async function kafkaQuery(
website_id,
{ session_uuid, hostname, browser, os, screen, language, country, device },
2022-07-22 23:43:19 +02:00
) {
2022-08-01 09:28:38 +02:00
const params = {
session_uuid: session_uuid,
website_id: website_id,
2022-08-26 07:04:32 +02:00
created_at: kafka.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
await kafka.sendMessage(params, 'session');
2022-08-27 05:21:53 +02:00
await redis.set(`session:${session_uuid}`, '');
2022-07-22 23:43:19 +02:00
}