/** * Global Profile Plugin - Stats Routes * * Handles statistics API endpoints */ const sdk = require('../../../includes/plugins/sdk'); const { getAllProfilesFromDB } = require('../database'); /** * Handle stats-related routes */ async function handleStatsRoutes(req, res, path, query, method) { // GET /api/stats - Get profile statistics if (path === 'api/stats' && method === 'GET') { try { const allProfiles = await getAllProfilesFromDB(); const onlinePeerIds = sdk.state.peerIds || []; // Count online/offline profiles let onlineCount = 0; let offlineCount = 0; const recentActivity = []; for (const profile of allProfiles) { const isOnline = onlinePeerIds.includes(profile.peerId); if (isOnline) { onlineCount++; } else { offlineCount++; } // Track recent activity (last 24 hours) if (profile.lastUpdated) { const hoursAgo = (Date.now() - profile.lastUpdated) / (1000 * 60 * 60); if (hoursAgo <= 24) { recentActivity.push({ peerId: profile.peerId, displayName: profile.displayName || 'Unknown', hoursAgo: Math.round(hoursAgo * 10) / 10 }); } } } // Sort recent activity by most recent recentActivity.sort((a, b) => { const aProfile = allProfiles.find(p => p.peerId === a.peerId); const bProfile = allProfiles.find(p => p.peerId === b.peerId); return (bProfile?.lastUpdated || 0) - (aProfile?.lastUpdated || 0); }); return sdk.router.json(res, { total: allProfiles.length, online: onlineCount, offline: offlineCount, recentActivity: recentActivity.slice(0, 10), // Top 10 most recent timestamp: Date.now() }); } catch (err) { sdk.log.error('global.profile', `Error getting stats: ${err.message}`); return sdk.router.error(res, err.message, 500); } } return false; // Not handled by this module } module.exports = { handleStatsRoutes };