mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
use uuid
This commit is contained in:
parent
246e4e5f4f
commit
17041efaae
73 changed files with 491 additions and 874 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { parseSecureToken, parseToken } from 'next-basics';
|
||||
import { getAccount, getWebsite } from 'queries';
|
||||
import { getUser, getWebsite } from 'queries';
|
||||
import { SHARE_TOKEN_HEADER, TYPE_ACCOUNT, TYPE_WEBSITE } from 'lib/constants';
|
||||
import { secret } from 'lib/crypto';
|
||||
|
||||
|
|
@ -50,13 +50,13 @@ export async function allowQuery(req, type) {
|
|||
|
||||
if (userId) {
|
||||
if (type === TYPE_WEBSITE) {
|
||||
const website = await getWebsite({ websiteUuid: id });
|
||||
const website = await getWebsite({ id });
|
||||
|
||||
return website && website.userId === userId;
|
||||
} else if (type === TYPE_ACCOUNT) {
|
||||
const account = await getAccount({ accountUuid: id });
|
||||
const user = await getUser({ id });
|
||||
|
||||
return account && account.accountUuid === id;
|
||||
return user && user.id === id;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ export const REALTIME_RANGE = 30;
|
|||
export const REALTIME_INTERVAL = 3000;
|
||||
|
||||
export const TYPE_WEBSITE = 'website';
|
||||
export const TYPE_ACCOUNT = 'account';
|
||||
export const TYPE_ACCOUNT = 'user';
|
||||
|
||||
export const THEME_COLORS = {
|
||||
light: {
|
||||
|
|
|
|||
31
lib/redis.js
31
lib/redis.js
|
|
@ -1,11 +1,8 @@
|
|||
import Redis from 'ioredis';
|
||||
import { startOfMonth } from 'date-fns';
|
||||
import debug from 'debug';
|
||||
import { getSessions, getAllWebsites } from 'queries';
|
||||
import Redis from 'ioredis';
|
||||
import { REDIS } from 'lib/db';
|
||||
|
||||
const log = debug('umami:redis');
|
||||
const INITIALIZED = 'redis:initialized';
|
||||
export const DELETED = 'deleted';
|
||||
|
||||
let redis;
|
||||
|
|
@ -32,30 +29,6 @@ function getClient() {
|
|||
return redis;
|
||||
}
|
||||
|
||||
async function stageData() {
|
||||
const sessions = await getSessions([], startOfMonth(new Date()));
|
||||
const websites = await getAllWebsites();
|
||||
|
||||
const sessionUuids = sessions.map(a => {
|
||||
return { key: `session:${a.sessionUuid}`, value: 1 };
|
||||
});
|
||||
const websiteIds = websites.map(a => {
|
||||
return { key: `website:${a.websiteUuid}`, value: Number(a.websiteId) };
|
||||
});
|
||||
|
||||
await addSet(sessionUuids);
|
||||
await addSet(websiteIds);
|
||||
|
||||
await redis.set(INITIALIZED, 1);
|
||||
}
|
||||
|
||||
async function addSet(ids) {
|
||||
for (let i = 0; i < ids.length; i++) {
|
||||
const { key, value } = ids[i];
|
||||
await redis.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
async function get(key) {
|
||||
await connect();
|
||||
|
||||
|
|
@ -76,4 +49,4 @@ async function connect() {
|
|||
return redis;
|
||||
}
|
||||
|
||||
export default { enabled, client: redis, log, connect, get, set, stageData };
|
||||
export default { enabled, client: redis, log, connect, get, set };
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { secret, uuid } from 'lib/crypto';
|
|||
import redis, { DELETED } from 'lib/redis';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { getClientInfo, getJsonBody } from 'lib/request';
|
||||
import { createSession, getSessionByUuid, getWebsite } from 'queries';
|
||||
import { createSession, getSession as getSessionPrisma, getWebsite } from 'queries';
|
||||
|
||||
export async function getSession(req) {
|
||||
const { payload } = getJsonBody(req);
|
||||
|
|
@ -23,51 +23,51 @@ export async function getSession(req) {
|
|||
}
|
||||
}
|
||||
|
||||
const { website: websiteUuid, hostname, screen, language } = payload;
|
||||
const { website: websiteId, hostname, screen, language } = payload;
|
||||
|
||||
if (!validate(websiteUuid)) {
|
||||
if (!validate(websiteId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let websiteId = null;
|
||||
let isValidWebsite = null;
|
||||
|
||||
// Check if website exists
|
||||
if (redis.enabled) {
|
||||
websiteId = Number(await redis.get(`website:${websiteUuid}`));
|
||||
isValidWebsite = await redis.get(`website:${websiteId}`);
|
||||
}
|
||||
|
||||
// Check database if does not exists in Redis
|
||||
if (!websiteId) {
|
||||
const website = await getWebsite({ websiteUuid });
|
||||
websiteId = website ? website.id : null;
|
||||
if (!isValidWebsite) {
|
||||
const website = await getWebsite({ id: websiteId });
|
||||
isValidWebsite = !!website;
|
||||
}
|
||||
|
||||
if (!websiteId || websiteId === DELETED) {
|
||||
throw new Error(`Website not found: ${websiteUuid}`);
|
||||
if (!isValidWebsite || isValidWebsite === DELETED) {
|
||||
throw new Error(`Website not found: ${websiteId}`);
|
||||
}
|
||||
|
||||
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
|
||||
const sessionUuid = uuid(websiteUuid, hostname, ip, userAgent);
|
||||
const sessionId = uuid(websiteId, hostname, ip, userAgent);
|
||||
|
||||
let sessionId = null;
|
||||
let isValidSession = null;
|
||||
let session = null;
|
||||
|
||||
if (!clickhouse.enabled) {
|
||||
// Check if session exists
|
||||
if (redis.enabled) {
|
||||
sessionId = Number(await redis.get(`session:${sessionUuid}`));
|
||||
isValidSession = await redis.get(`session:${sessionId}`);
|
||||
}
|
||||
|
||||
// Check database if does not exists in Redis
|
||||
if (!sessionId) {
|
||||
session = await getSessionByUuid(sessionUuid);
|
||||
sessionId = session ? session.id : null;
|
||||
if (!isValidSession) {
|
||||
session = await getSessionPrisma({ id: sessionId });
|
||||
isValidSession = !!session;
|
||||
}
|
||||
|
||||
if (!sessionId) {
|
||||
if (!isValidSession) {
|
||||
try {
|
||||
session = await createSession(websiteId, {
|
||||
sessionUuid,
|
||||
id: sessionId,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
|
|
@ -84,8 +84,7 @@ export async function getSession(req) {
|
|||
}
|
||||
} else {
|
||||
session = {
|
||||
sessionId,
|
||||
sessionUuid,
|
||||
id: sessionId,
|
||||
hostname,
|
||||
browser,
|
||||
os,
|
||||
|
|
@ -97,10 +96,7 @@ export async function getSession(req) {
|
|||
}
|
||||
|
||||
return {
|
||||
website: {
|
||||
websiteId,
|
||||
websiteUuid,
|
||||
},
|
||||
websiteId,
|
||||
session,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue