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.
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user