fix(analytics): align flag counts and violation score math

- Count only flagged messages in the active users Flag column
- Coerce PostgreSQL count results to numbers before calculating violation_score
- Prevent string concatenation in flagged*3 + warned scoring

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-01 19:23:46 +07:00
co-authored by Claude Opus 4.8
parent 80f1069e2f
commit bda8304bb9
+16 -12
View File
@@ -483,7 +483,7 @@ export async function getUserLeaderboard(input: {
count(*) as message_count, count(*) as message_count,
count(case when type = 'edited' then 1 end) as edited_count, count(case when type = 'edited' then 1 end) as edited_count,
count(case when type = 'deleted' then 1 end) as deleted_count, count(case when type = 'deleted' then 1 end) as deleted_count,
count(case when ai_status in ('flagged', 'warn') then 1 end) as flagged_count, count(case when ai_status = 'flagged' then 1 end) as flagged_count,
max(created_at) as last_active max(created_at) as last_active
FROM messages FROM messages
WHERE guild_id = ? WHERE guild_id = ?
@@ -682,17 +682,21 @@ export async function getTopViolators(input: {
: [guildId, since, limit], : [guildId, since, limit],
); );
const violators: ViolatorStat[] = rows.map((row: any) => ({ const violators: ViolatorStat[] = rows.map((row: any) => {
user_id: row.user_id, const flaggedCount = Number(row.flagged_count ?? 0);
username: row.username, const warnedCount = Number(row.warned_count ?? 0);
avatar_url: row.avatar_url, return {
total_messages: row.total_messages, user_id: row.user_id,
flagged_count: row.flagged_count, username: row.username,
warned_count: row.warned_count, avatar_url: row.avatar_url,
violation_score: row.flagged_count * 3 + row.warned_count, total_messages: Number(row.total_messages ?? 0),
worst_flags: [], flagged_count: flaggedCount,
last_violation: row.last_violation, warned_count: warnedCount,
})); violation_score: flaggedCount * 3 + warnedCount,
worst_flags: [],
last_violation: Number(row.last_violation ?? 0),
};
});
setCache(cacheKey, violators, AGGREGATE_CACHE_TTL_MS); setCache(cacheKey, violators, AGGREGATE_CACHE_TTL_MS);
return violators; return violators;