From 1d31ca9672de1061b8943b393d288151a6d3356b Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 22 Apr 2026 09:54:53 +0200 Subject: [PATCH] Refactor user statistics reporting in broadcast.js - Updated the logic for retrieving unique users today to ensure only the latest records are considered, improving accuracy in user engagement metrics. - Enhanced the command table to display user age alongside their name and login count, providing more comprehensive insights into user demographics. These changes collectively improve the reporting of user statistics and enhance the clarity of the displayed data. --- server/broadcast.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/server/broadcast.js b/server/broadcast.js index 0d0ffee..25dc1a2 100644 --- a/server/broadcast.js +++ b/server/broadcast.js @@ -419,8 +419,18 @@ export function setupBroadcast(io, __dirname) { if (sub === 'today') { const day = new Date().toISOString().slice(0, 10); const dayRecords = records.filter((r) => r.day === day); - const uniqueUsersToday = Array.from(new Set(dayRecords.map((r) => r.userName))) - .sort((a, b) => a.localeCompare(b, 'de')); + const latestByUserToday = new Map(); + dayRecords + .slice() + .sort((a, b) => b.date - a.date) + .forEach((record) => { + if (!latestByUserToday.has(record.userName)) { + latestByUserToday.set(record.userName, record); + } + }); + const uniqueUsersToday = Array.from(latestByUserToday.values()) + .sort((a, b) => a.userName.localeCompare(b.userName, 'de')) + .map((r) => `${r.userName} (${r.age})`); sendCommandTable(socket, 'Statistik: Heute', ['Metrik', 'Wert'], [ ['Tag', day], ['Logins', dayRecords.length], @@ -473,12 +483,12 @@ export function setupBroadcast(io, __dirname) { sendCommandTable( socket, `Statistik: Namen (gesamt verschieden: ${new Set(records.map((r) => r.userName)).size})`, - ['Name', 'Anzahl', 'Letzter Login'], + ['Name', 'Alter', 'Anzahl', 'Letzter Login'], topNames.map(([name, count]) => { const latestRecord = records .filter((r) => r.userName === name) .sort((a, b) => b.date - a.date)[0]; - return [name, count, latestRecord ? latestRecord.timestamp : '-']; + return [name, latestRecord ? latestRecord.age : '-', count, latestRecord ? latestRecord.timestamp : '-']; }) ); return;