- Created dashboard service with DB queries for users, economy, events - Added client stats provider with 30s caching for Discord metrics - Implemented /api/stats endpoint aggregating all dashboard data - Created useDashboardStats React hook with auto-refresh - Updated Dashboard.tsx to display real data with loading/error states - Added comprehensive test coverage (11 tests passing) - Replaced all mock values with live Discord and database metrics
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { AuroraClient } from "./BotClient";
|
|
import type { ClientStats } from "@shared/modules/dashboard/dashboard.types";
|
|
|
|
// Cache for client stats (30 second TTL)
|
|
let cachedStats: ClientStats | null = null;
|
|
let lastFetchTime: number = 0;
|
|
const CACHE_TTL_MS = 30 * 1000; // 30 seconds
|
|
|
|
/**
|
|
* Get Discord client statistics with caching
|
|
* Respects rate limits by caching for 30 seconds
|
|
*/
|
|
export function getClientStats(): ClientStats {
|
|
const now = Date.now();
|
|
|
|
// Return cached stats if still valid
|
|
if (cachedStats && (now - lastFetchTime) < CACHE_TTL_MS) {
|
|
return cachedStats;
|
|
}
|
|
|
|
// Fetch fresh stats
|
|
const stats: ClientStats = {
|
|
guilds: AuroraClient.guilds.cache.size,
|
|
ping: AuroraClient.ws.ping,
|
|
cachedUsers: AuroraClient.users.cache.size,
|
|
commandsRegistered: AuroraClient.commands.size,
|
|
uptime: process.uptime(),
|
|
lastCommandTimestamp: AuroraClient.lastCommandTimestamp,
|
|
};
|
|
|
|
// Update cache
|
|
cachedStats = stats;
|
|
lastFetchTime = now;
|
|
|
|
return stats;
|
|
}
|
|
|
|
/**
|
|
* Clear the stats cache (useful for testing)
|
|
*/
|
|
export function clearStatsCache(): void {
|
|
cachedStats = null;
|
|
lastFetchTime = 0;
|
|
}
|