mirror of
https://github.com/umami-software/umami.git
synced 2026-02-08 14:47:14 +01:00
Merge Session/Event/Pageview CH
This commit is contained in:
parent
d377ef86e7
commit
52e036964b
18 changed files with 237 additions and 294 deletions
|
|
@ -4,9 +4,12 @@ import { uuid } from 'lib/crypto';
|
|||
import redis, { DELETED } from 'lib/redis';
|
||||
import { getClientInfo, getJsonBody } from 'lib/request';
|
||||
import { createSession, getSessionByUuid, getWebsiteByUuid } from 'queries';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
|
||||
export async function getSession(req) {
|
||||
const { payload } = getJsonBody(req);
|
||||
const isRedis = redis.client;
|
||||
const isClickhouse = clickhouse.client;
|
||||
|
||||
if (!payload) {
|
||||
throw new Error('Invalid request');
|
||||
|
|
@ -31,11 +34,11 @@ export async function getSession(req) {
|
|||
let websiteId = null;
|
||||
|
||||
// Check if website exists
|
||||
if (redis.client) {
|
||||
if (isRedis) {
|
||||
websiteId = Number(await redis.client.get(`website:${website_uuid}`));
|
||||
}
|
||||
|
||||
// Check database if redis does not have
|
||||
// Check database if does not exists in Redis
|
||||
if (!websiteId) {
|
||||
const website = await getWebsiteByUuid(website_uuid);
|
||||
websiteId = website ? website.website_id : null;
|
||||
|
|
@ -46,47 +49,57 @@ export async function getSession(req) {
|
|||
}
|
||||
|
||||
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
|
||||
|
||||
const session_uuid = uuid(websiteId, hostname, ip, userAgent);
|
||||
|
||||
let sessionId = null;
|
||||
let session = null;
|
||||
|
||||
// Check if session exists
|
||||
if (redis.client) {
|
||||
sessionId = Number(await redis.client.get(`session:${session_uuid}`));
|
||||
}
|
||||
|
||||
// Check database if redis does not have
|
||||
if (!sessionId) {
|
||||
session = await getSessionByUuid(session_uuid);
|
||||
sessionId = session ? session.session_id : null;
|
||||
}
|
||||
|
||||
if (!sessionId) {
|
||||
try {
|
||||
session = await createSession(websiteId, {
|
||||
session_uuid,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
screen,
|
||||
language,
|
||||
country,
|
||||
device,
|
||||
});
|
||||
if (!isClickhouse) {
|
||||
// Check if session exists
|
||||
if (isRedis) {
|
||||
sessionId = Number(await redis.client.get(`session:${session_uuid}`));
|
||||
}
|
||||
|
||||
// Check database if does not exists in Redis
|
||||
if (!sessionId) {
|
||||
session = await getSessionByUuid(session_uuid);
|
||||
sessionId = session ? session.session_id : null;
|
||||
} catch (e) {
|
||||
if (!e.message.toLowerCase().includes('unique constraint')) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (!sessionId) {
|
||||
try {
|
||||
session = await createSession(websiteId, {
|
||||
session_uuid,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
screen,
|
||||
language,
|
||||
country,
|
||||
device,
|
||||
});
|
||||
} catch (e) {
|
||||
if (!e.message.toLowerCase().includes('unique constraint')) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
session = {
|
||||
session_id: sessionId,
|
||||
session_uuid,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
screen,
|
||||
language,
|
||||
country,
|
||||
device,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
website_id: websiteId,
|
||||
session_id: sessionId,
|
||||
session_uuid,
|
||||
session,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue