107 lines
3.6 KiB
TypeScript
107 lines
3.6 KiB
TypeScript
export default defineEventHandler(async (event) => {
|
|
console.log('📊 Admin stats requested at:', new Date().toISOString());
|
|
|
|
try {
|
|
// Check if user is admin (middleware should handle this, but double-check)
|
|
const sessionManager = createSessionManager();
|
|
const cookieHeader = getHeader(event, 'cookie');
|
|
const session = sessionManager.getSession(cookieHeader);
|
|
|
|
if (!session || session.user.tier !== 'admin') {
|
|
console.warn('🚨 Unauthorized admin stats access attempt');
|
|
throw createError({
|
|
statusCode: 403,
|
|
statusMessage: 'Admin access required'
|
|
});
|
|
}
|
|
|
|
console.log('✅ Admin access verified for user:', session.user.email);
|
|
|
|
// Get real system metrics
|
|
const { getSystemMetrics, getSystemHealthStatus, formatBytes } = await import('~/server/utils/system-metrics');
|
|
const systemMetrics = await getSystemMetrics();
|
|
const systemHealth = getSystemHealthStatus(systemMetrics);
|
|
|
|
// Build comprehensive stats with real data
|
|
const stats = {
|
|
// Real system metrics
|
|
totalUsers: 156, // TODO: Get from Keycloak API
|
|
activeUsers: 45, // TODO: Get from session store
|
|
totalSessions: 67, // TODO: Get from session store
|
|
systemHealth,
|
|
lastBackup: new Date().toISOString(),
|
|
diskUsage: `${systemMetrics.disk.usagePercent}%`,
|
|
memoryUsage: `${systemMetrics.memory.usagePercent}%`,
|
|
|
|
// Enhanced system metrics with real data
|
|
systemMetrics: {
|
|
cpu: systemMetrics.cpu.usage,
|
|
memory: systemMetrics.memory.usagePercent,
|
|
disk: systemMetrics.disk.usagePercent,
|
|
uptime: systemMetrics.uptime,
|
|
processes: systemMetrics.processes.total,
|
|
cpuCores: systemMetrics.cpu.cores,
|
|
cpuModel: systemMetrics.cpu.model,
|
|
memoryTotal: formatBytes(systemMetrics.memory.total),
|
|
memoryUsed: formatBytes(systemMetrics.memory.used),
|
|
diskTotal: formatBytes(systemMetrics.disk.total),
|
|
diskUsed: formatBytes(systemMetrics.disk.used)
|
|
},
|
|
|
|
// Mock data for features not yet implemented
|
|
recentActivity: [
|
|
{
|
|
action: 'User login',
|
|
user: 'john@example.com',
|
|
timestamp: new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(),
|
|
type: 'info'
|
|
},
|
|
{
|
|
action: 'Password reset',
|
|
user: 'jane@example.com',
|
|
timestamp: new Date(Date.now() - 4 * 60 * 60 * 1000).toISOString(),
|
|
type: 'warning'
|
|
},
|
|
{
|
|
action: 'User created',
|
|
user: 'admin@monacousa.org',
|
|
timestamp: new Date(Date.now() - 6 * 60 * 60 * 1000).toISOString(),
|
|
type: 'success'
|
|
}
|
|
],
|
|
|
|
securityAlerts: [
|
|
{
|
|
id: 1,
|
|
title: 'Failed Login Attempts',
|
|
description: '3 failed login attempts detected',
|
|
severity: 'medium',
|
|
timestamp: new Date(Date.now() - 1 * 60 * 60 * 1000).toISOString()
|
|
},
|
|
{
|
|
id: 2,
|
|
title: 'System Update Available',
|
|
description: 'Security update available for Keycloak',
|
|
severity: 'low',
|
|
timestamp: new Date(Date.now() - 12 * 60 * 60 * 1000).toISOString()
|
|
}
|
|
]
|
|
};
|
|
|
|
console.log('✅ Admin stats retrieved successfully with real system metrics');
|
|
return stats;
|
|
|
|
} catch (error: any) {
|
|
console.error('❌ Admin stats error:', error);
|
|
|
|
if (error.statusCode) {
|
|
throw error;
|
|
}
|
|
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Failed to retrieve system statistics'
|
|
});
|
|
}
|
|
});
|