mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 15:47:13 +01:00
Refactored redis usage. Added lib/cache.
This commit is contained in:
parent
3485b6268b
commit
f118bc95c1
22 changed files with 236 additions and 221 deletions
|
|
@ -1,62 +1,45 @@
|
|||
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
||||
import kafka from 'lib/kafka';
|
||||
import prisma from 'lib/prisma';
|
||||
import redis from 'lib/redis';
|
||||
import cache from 'lib/cache';
|
||||
|
||||
export async function createSession(...args) {
|
||||
return runQuery({
|
||||
[PRISMA]: () => relationalQuery(...args),
|
||||
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
||||
}).then(async data => {
|
||||
if (redis.enabled && data) {
|
||||
await redis.set(`session:${data.id}`, data);
|
||||
if (cache.enabled) {
|
||||
await cache.storeSession(data);
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
async function relationalQuery(websiteId, data) {
|
||||
return prisma.client.session.create({
|
||||
data: {
|
||||
websiteId,
|
||||
...data,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
hostname: true,
|
||||
browser: true,
|
||||
os: true,
|
||||
screen: true,
|
||||
language: true,
|
||||
country: true,
|
||||
device: true,
|
||||
},
|
||||
});
|
||||
async function relationalQuery(data) {
|
||||
return prisma.client.session.create({ data });
|
||||
}
|
||||
|
||||
async function clickhouseQuery(
|
||||
websiteId,
|
||||
{ sessionId, hostname, browser, os, screen, language, country, device },
|
||||
) {
|
||||
async function clickhouseQuery(data) {
|
||||
const { id, websiteId, hostname, browser, os, device, screen, language, country } = data;
|
||||
const { getDateFormat, sendMessage } = kafka;
|
||||
const website = await redis.get(`website:${websiteId}`);
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
|
||||
const data = {
|
||||
sessionId,
|
||||
const msg = {
|
||||
session_id: id,
|
||||
website_id: websiteId,
|
||||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
device,
|
||||
screen,
|
||||
language,
|
||||
country: country ? country : null,
|
||||
country,
|
||||
rev_id: website?.revId || 0,
|
||||
created_at: getDateFormat(new Date()),
|
||||
};
|
||||
|
||||
await sendMessage(data, 'event');
|
||||
await sendMessage(msg, 'event');
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue