mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
redis checkpoint
This commit is contained in:
parent
10cc6616c5
commit
818f8721e9
10 changed files with 187 additions and 34 deletions
59
lib/redis.js
Normal file
59
lib/redis.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { createClient } from 'redis';
|
||||
import { startOfMonth } from 'date-fns';
|
||||
import { getSessions, getAllWebsites } from '/queries';
|
||||
|
||||
async function getClient() {
|
||||
const redis = new createClient({
|
||||
url: process.env.REDIS_URL,
|
||||
});
|
||||
|
||||
await redis.connect();
|
||||
|
||||
if (process.env.LOG_QUERY) {
|
||||
redis.on('error', err => console.log('Redis Client Error', err));
|
||||
}
|
||||
|
||||
return redis;
|
||||
}
|
||||
|
||||
let redis = null;
|
||||
|
||||
(async () => {
|
||||
redis = global.redis || (await getClient());
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
global.redis = redis;
|
||||
}
|
||||
|
||||
const value = await redis.get('initialized');
|
||||
|
||||
if (!value) {
|
||||
await stageData();
|
||||
}
|
||||
})();
|
||||
|
||||
export default redis;
|
||||
|
||||
async function stageData() {
|
||||
const sessions = await getSessions([], startOfMonth(new Date()).toUTCString());
|
||||
const websites = await getAllWebsites();
|
||||
|
||||
const sessionUuids = sessions.map(a => {
|
||||
return { key: `session:${a.session_uuid}`, value: '' };
|
||||
});
|
||||
const websiteIds = websites.map(a => {
|
||||
return { key: `website:${a.website_uuid}`, value: Number(a.website_id) };
|
||||
});
|
||||
|
||||
await addRedis(sessionUuids);
|
||||
await addRedis(websiteIds);
|
||||
|
||||
await redis.set('initialized', 'initialized');
|
||||
}
|
||||
|
||||
async function addRedis(ids) {
|
||||
for (let i = 0; i < ids.length; i++) {
|
||||
const { key, value } = ids[i];
|
||||
await redis.set(key, value);
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ function logQuery(e) {
|
|||
console.log(chalk.yellow(e.params), '->', e.query, chalk.greenBright(`${e.duration}ms`));
|
||||
}
|
||||
|
||||
function getPrismaClient(options) {
|
||||
function getClient(options) {
|
||||
const prisma = new PrismaClient(options);
|
||||
|
||||
if (process.env.LOG_QUERY) {
|
||||
|
|
@ -32,7 +32,7 @@ function getPrismaClient(options) {
|
|||
|
||||
return prisma;
|
||||
}
|
||||
const prisma = global.prisma || getPrismaClient(options);
|
||||
const prisma = global.prisma || getClient(options);
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
global.prisma = prisma;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue