Stats page: overview, activity per day, leaderboards, top emotes/commands, viewer lookup

/stats (dashboard nav): totals, chat messages and watch hours per day (30 days,
single-series bar charts with hover), leaderboards for points (with level), watch
time, chat, bits, channel points spent; top emotes (with images) and commands.
/stats/viewer?q=: one viewer's points breakdown, watch time, messages, check-ins,
emotes, commands, actions, and 'Forget this viewer' (removal requests: stats, watch
time, raw chat, activity log). Bots (Hidden chatters) left out of everything shown.
src/points.js: the agreed point values in one place, calculated from the stats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-22 17:40:05 +02:00
parent 1c36e80f66
commit c364e7079b
6 changed files with 383 additions and 0 deletions

72
src/points.js Normal file
View file

@ -0,0 +1,72 @@
// Points (planned point system, values agreed 2026-11-22, see docs/PLANS.md), calculated from the statistics
// tables, so changing a value here recalculates everyone. Also used by the Stats page; later by !points.
const db = require('./db');
const VALUE = {
watchTick: 5, // per 5-minute watch tick
chat: 10, // per points-eligible message (max 1 per 5 min, counted in chatlog.js)
checkin: 25, // first message of a stream
intro: 50, // first message ever on the channel
follow: 50, // once
bit: 1, // cheers, Power-ups, Custom Power-ups
per100ChannelPoints: 1,
subMonth: { 'Tier 1': 500, Prime: 500, 'Tier 2': 1000, 'Tier 3': 2500 },
tipPerUnit: 100, // per 1 EUR (or whatever currency the tip was in)
raid: 100, // flat, no reward for raid size
streakPer: 5, streakMax: 100,
};
const LEVELS = [ // cassette theme
{ min: 0, name: 'Newcomer' }, { min: 500, name: 'Regular' }, { min: 2000, name: 'Tape Collector' },
{ min: 6000, name: 'Mixtape Maker' }, { min: 15000, name: 'Side B Legend' },
];
const levelOf = p => [...LEVELS].reverse().find(l => p >= l.min);
const has = t => !!db.raw().prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name = ?").get(t);
const all = (sql, ...a) => db.raw().prepare(sql).all(...a);
// -> Map key -> viewer { key, user_id, login, name, parts: {watch, chat, ...}, total, level }
// key = Twitch user id; viewers known only by name (tips, watch streaks) are merged by login when possible
function everyone() {
const v = new Map(), byLogin = new Map();
const get = (id, login, name) => {
let key = id ? String(id) : null;
if (!key && login && byLogin.has(login)) key = byLogin.get(login);
if (!key) key = `name:${login}`;
if (!v.has(key)) v.set(key, { key, user_id: id || null, login, name: name || login, parts: {}, total: 0 });
const x = v.get(key);
if (login) { byLogin.set(login, key); x.login = x.login || login; }
if (name) x.name = name;
return x;
};
const add = (x, part, pts) => { if (!pts) return; x.parts[part] = (x.parts[part] || 0) + pts; x.total += pts; };
if (has('chat_user')) for (const r of all('SELECT user_id, user_login, user_name, scored, intro FROM chat_user')) {
const x = get(r.user_id, r.user_login, r.user_name);
add(x, 'chat', (r.scored || 0) * VALUE.chat); add(x, 'first message', r.intro ? VALUE.intro : 0);
}
if (has('watch_user')) for (const r of all('SELECT user_id, user_login, user_name, ticks FROM watch_user')) {
add(get(r.user_id, r.user_login, r.user_name), 'watching', (r.ticks || 0) * VALUE.watchTick);
}
if (has('stream_checkin')) for (const r of all('SELECT user_id, COUNT(*) n FROM stream_checkin GROUP BY user_id')) {
add(get(r.user_id, null, null), 'check-ins', r.n * VALUE.checkin);
}
if (has('chat_action')) for (const r of all('SELECT * FROM chat_action ORDER BY user_key LIKE \'name:%\'')) {
const id = r.user_key.startsWith('name:') ? null : r.user_key;
const x = get(id, r.user_login, r.user_name);
switch (r.action) {
case 'follow': add(x, 'follow', VALUE.follow); break;
case 'bits': case 'power_up': case 'custom_power_up': add(x, 'bits', Math.round(r.amount * VALUE.bit)); break;
case 'channel_points': case 'auto_reward': add(x, 'channel points', Math.floor(r.amount / 100) * VALUE.per100ChannelPoints); break;
case 'sub': add(x, 'subs', Math.round(r.amount * (VALUE.subMonth[r.item] || VALUE.subMonth['Tier 1']))); break;
case 'gift_sub': add(x, 'gift subs', Math.round(r.amount * (VALUE.subMonth[r.item] || VALUE.subMonth['Tier 1']))); break;
case 'tip': add(x, 'tips', Math.round(r.amount * VALUE.tipPerUnit)); break;
case 'raid': add(x, 'raids', r.uses * VALUE.raid); break;
case 'watch_streak': add(x, 'watch streak', Math.min(VALUE.streakMax, r.amount * VALUE.streakPer)); break;
}
}
for (const x of v.values()) x.level = levelOf(x.total);
return v;
}
module.exports = { VALUE, LEVELS, levelOf, everyone };