mirror of
https://github.com/BillyOutlast/drop.git
synced 2026-02-04 08:41:17 +01:00
* Adds new tile on the admin home page with system data. Also fixes the active users bug in the pie chart * Fixes missing parentheses * Updates user stats cache when signing in * Reads active number of users from session provider * Removes unused variable * Small improvements * Removes acl properties from system data websocket and performs initial push of data * fix: remove acl fetch --------- Co-authored-by: DecDuck <declanahofmeyr@gmail.com>
58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
/*
|
|
Handles managing collections
|
|
*/
|
|
|
|
import cacheHandler from "../cache";
|
|
import prisma from "../db/database";
|
|
import sessionHandler from "../session";
|
|
|
|
class UserStatsManager {
|
|
// Caches the user's core library
|
|
private userStatsCache = cacheHandler.createCache<number>("userStats");
|
|
|
|
async cacheUserSessions() {
|
|
const activeSessions = await sessionHandler.getNumberActiveSessions();
|
|
await this.userStatsCache.set("activeSessions", activeSessions);
|
|
}
|
|
|
|
private async cacheUserCount() {
|
|
const userCount =
|
|
(await prisma.user.count({
|
|
where: { id: { not: "system" } },
|
|
})) || 0;
|
|
await this.userStatsCache.set("userCount", userCount);
|
|
}
|
|
|
|
async cacheUserStats() {
|
|
await this.cacheUserSessions();
|
|
await this.cacheUserCount();
|
|
}
|
|
|
|
async getUserStats() {
|
|
let activeSessions = await this.userStatsCache.get("activeSessions");
|
|
let userCount = await this.userStatsCache.get("userCount");
|
|
|
|
if (activeSessions === null || userCount === null) {
|
|
await this.cacheUserStats();
|
|
activeSessions = (await this.userStatsCache.get("activeSessions")) || 0;
|
|
userCount = (await this.userStatsCache.get("userCount")) || 0;
|
|
}
|
|
|
|
return { activeSessions, userCount };
|
|
}
|
|
|
|
async addUser() {
|
|
const userCount = (await this.userStatsCache.get("userCount")) || 0;
|
|
await this.userStatsCache.set("userCount", userCount + 1);
|
|
}
|
|
|
|
async deleteUser() {
|
|
const userCount = (await this.userStatsCache.get("userCount")) || 1;
|
|
await this.userStatsCache.set("userCount", userCount - 1);
|
|
await this.cacheUserSessions();
|
|
}
|
|
}
|
|
|
|
export const manager = new UserStatsManager();
|
|
export default manager;
|