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:
parent
1c36e80f66
commit
c364e7079b
6 changed files with 383 additions and 0 deletions
|
|
@ -55,6 +55,7 @@ app.use('/ext', require('./routers/ext').router); // HUDFX / Blerp pages serve
|
||||||
// app.use('/nowplaying', require('./routers/nowplaying')); // key-protected push from the PC helper
|
// app.use('/nowplaying', require('./routers/nowplaying')); // key-protected push from the PC helper
|
||||||
app.use('/actions', requireAuth, require('./routers/actions'));
|
app.use('/actions', requireAuth, require('./routers/actions'));
|
||||||
app.use('/media', requireAuth, require('./routers/media'));
|
app.use('/media', requireAuth, require('./routers/media'));
|
||||||
|
app.use('/stats', requireAuth, require('./routers/stats'));
|
||||||
app.use('/settings', requireAuth, require('./routers/settings'));
|
app.use('/settings', requireAuth, require('./routers/settings'));
|
||||||
app.use('/alerts', requireAuth, require('./routers/alerts'));
|
app.use('/alerts', requireAuth, require('./routers/alerts'));
|
||||||
app.use('/redeems', requireAuth, require('./routers/redeems'));
|
app.use('/redeems', requireAuth, require('./routers/redeems'));
|
||||||
|
|
|
||||||
72
src/points.js
Normal file
72
src/points.js
Normal 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 };
|
||||||
106
src/routers/stats.js
Normal file
106
src/routers/stats.js
Normal file
|
|
@ -0,0 +1,106 @@
|
||||||
|
// Stats page: overview, activity per day, leaderboards, top emotes/commands, viewer lookup, forget a viewer.
|
||||||
|
// Bots (Settings → Hidden chatters) are counted but left out of everything shown here.
|
||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const db = require('../db');
|
||||||
|
const chatlog = require('../chatlog');
|
||||||
|
const watchtime = require('../watchtime');
|
||||||
|
const points = require('../points');
|
||||||
|
|
||||||
|
const has = t => !!db.raw().prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name = ?").get(t);
|
||||||
|
const all = (sql, ...a) => (has(sql.match(/FROM (\w+)/)[1]) ? db.raw().prepare(sql).all(...a) : []);
|
||||||
|
const one = (sql, ...a) => (has(sql.match(/FROM (\w+)/)[1]) ? db.raw().prepare(sql).get(...a) : null) || {};
|
||||||
|
|
||||||
|
function days(n) { // last n days, oldest first, as YYYY-MM-DD (UTC, like the stored days)
|
||||||
|
const out = [];
|
||||||
|
for (let i = n - 1; i >= 0; i--) out.push(new Date(Date.now() - i * 864e5).toISOString().slice(0, 10));
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
router.get('/', (req, res) => {
|
||||||
|
chatlog.init(); watchtime.init();
|
||||||
|
const hidden = new Set(chatlog.hiddenUsers());
|
||||||
|
const ok = login => !hidden.has(String(login || '').toLowerCase());
|
||||||
|
const viewers = [...points.everyone().values()].filter(x => ok(x.login));
|
||||||
|
|
||||||
|
const users = all('SELECT user_id, user_login, user_name, messages FROM chat_user').filter(r => ok(r.user_login));
|
||||||
|
const watchers = all('SELECT user_id, user_login, user_name, ticks FROM watch_user').filter(r => ok(r.user_login));
|
||||||
|
const actions = all('SELECT * FROM chat_action').filter(r => ok(r.user_login));
|
||||||
|
const loginOf = new Map([...users, ...watchers].map(r => [r.user_id, r.user_login]));
|
||||||
|
|
||||||
|
// per day (last 30 days), bots left out
|
||||||
|
const d30 = days(30), since = d30[0];
|
||||||
|
const msgDay = new Map(), watchDay = new Map();
|
||||||
|
for (const r of all('SELECT user_id, day, messages FROM chat_user_day WHERE day >= ?', since)) if (ok(loginOf.get(r.user_id))) msgDay.set(r.day, (msgDay.get(r.day) || 0) + r.messages);
|
||||||
|
for (const r of all('SELECT user_id, day, ticks FROM watch_user_day WHERE day >= ?', since)) if (ok(loginOf.get(r.user_id))) watchDay.set(r.day, (watchDay.get(r.day) || 0) + r.ticks * 5);
|
||||||
|
|
||||||
|
const sumAct = (types, f = r => r.amount) => {
|
||||||
|
const m = new Map();
|
||||||
|
for (const r of actions) if (types.includes(r.action)) m.set(r.user_key, { login: r.user_login, name: r.user_name, n: (m.get(r.user_key)?.n || 0) + f(r) });
|
||||||
|
return [...m.values()].sort((a, b) => b.n - a.n).slice(0, 10);
|
||||||
|
};
|
||||||
|
|
||||||
|
const emotes = new Map(), commands = new Map();
|
||||||
|
for (const r of all('SELECT user_id, emote, source, emote_id, uses FROM chat_emote')) if (ok(loginOf.get(r.user_id))) {
|
||||||
|
const e = emotes.get(r.emote) || { emote: r.emote, source: r.source, id: r.emote_id, uses: 0 }; e.uses += r.uses; emotes.set(r.emote, e);
|
||||||
|
}
|
||||||
|
for (const r of all('SELECT user_id, command, uses FROM chat_command')) if (ok(loginOf.get(r.user_id))) commands.set(r.command, (commands.get(r.command) || 0) + r.uses);
|
||||||
|
|
||||||
|
res.render('stats', {
|
||||||
|
live: watchtime.isLive(),
|
||||||
|
hiddenCount: hidden.size,
|
||||||
|
overview: {
|
||||||
|
viewers: viewers.length,
|
||||||
|
messages: users.reduce((s, r) => s + r.messages, 0),
|
||||||
|
watchHours: Math.round(watchers.reduce((s, r) => s + r.ticks, 0) * 5 / 60),
|
||||||
|
streams: one('SELECT COUNT(*) n FROM stream_session').n || 0,
|
||||||
|
checkins: one('SELECT COUNT(*) n FROM stream_checkin').n || 0,
|
||||||
|
since: one('SELECT MIN(first_seen) t FROM chat_user').t || null,
|
||||||
|
},
|
||||||
|
perDay: d30.map(day => ({ day, messages: msgDay.get(day) || 0, watchHours: +((watchDay.get(day) || 0) / 60).toFixed(1) })),
|
||||||
|
boards: {
|
||||||
|
points: viewers.sort((a, b) => b.total - a.total).slice(0, 10).map(x => ({ login: x.login, name: x.name, n: x.total, level: x.level.name })),
|
||||||
|
watch: watchers.sort((a, b) => b.ticks - a.ticks).slice(0, 10).map(r => ({ login: r.user_login, name: r.user_name, n: r.ticks * 5 })),
|
||||||
|
chat: users.sort((a, b) => b.messages - a.messages).slice(0, 10).map(r => ({ login: r.user_login, name: r.user_name, n: r.messages })),
|
||||||
|
bits: sumAct(['bits', 'power_up', 'custom_power_up']),
|
||||||
|
channelPoints: sumAct(['channel_points', 'auto_reward']),
|
||||||
|
},
|
||||||
|
emotes: [...emotes.values()].sort((a, b) => b.uses - a.uses).slice(0, 15),
|
||||||
|
commands: [...commands.entries()].map(([command, uses]) => ({ command, uses })).sort((a, b) => b.uses - a.uses).slice(0, 15),
|
||||||
|
q: '', viewer: null,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// one viewer: /stats/viewer?q=<login>
|
||||||
|
router.get('/viewer', (req, res) => {
|
||||||
|
chatlog.init(); watchtime.init();
|
||||||
|
const q = String(req.query.q || '').trim().replace(/^@/, '').toLowerCase();
|
||||||
|
if (!q) return res.redirect('/stats');
|
||||||
|
const v = [...points.everyone().values()].find(x => x.login === q || x.key === q);
|
||||||
|
if (!v) return res.render('stats_viewer', { q, viewer: null });
|
||||||
|
const id = v.user_id;
|
||||||
|
res.render('stats_viewer', {
|
||||||
|
q,
|
||||||
|
viewer: {
|
||||||
|
...v,
|
||||||
|
hidden: chatlog.isHidden(v.login),
|
||||||
|
chat: id ? one('SELECT * FROM chat_user WHERE user_id = ?', id) : {},
|
||||||
|
watch: id ? one('SELECT * FROM watch_user WHERE user_id = ?', id) : {},
|
||||||
|
checkins: id ? one('SELECT COUNT(*) n FROM stream_checkin WHERE user_id = ?', id).n || 0 : 0,
|
||||||
|
emotes: id ? all('SELECT emote, source, emote_id, uses FROM chat_emote WHERE user_id = ? ORDER BY uses DESC LIMIT 20', id) : [],
|
||||||
|
commands: id ? all('SELECT command, uses, last_used FROM chat_command WHERE user_id = ? ORDER BY uses DESC LIMIT 20', id) : [],
|
||||||
|
actions: all('SELECT action, item, uses, amount, unit, last_at FROM chat_action WHERE user_key = ? OR user_login = ? ORDER BY last_at DESC', v.key, v.login),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// removal request: forget a viewer completely (stats, watch time, raw chat; activity log entries too)
|
||||||
|
router.post('/viewer/forget', (req, res) => {
|
||||||
|
const login = String(req.body.login || '').toLowerCase(), id = String(req.body.user_id || '');
|
||||||
|
if (id) { chatlog.forgetUser(id); watchtime.forgetUser(id); } else if (login) chatlog.forgetUser(login);
|
||||||
|
if (login) db.raw().prepare('DELETE FROM activity_log WHERE lower(username) = ?').run(login);
|
||||||
|
console.log(`[stats] forgot viewer ${login || id}`);
|
||||||
|
res.redirect('/stats');
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
|
@ -27,6 +27,7 @@
|
||||||
<li><a href="/tts">TTS</a></li>
|
<li><a href="/tts">TTS</a></li>
|
||||||
<li><a href="/actions">Actions</a></li>
|
<li><a href="/actions">Actions</a></li>
|
||||||
<li><a href="/media">Media</a></li>
|
<li><a href="/media">Media</a></li>
|
||||||
|
<li><a href="/stats">Stats</a></li>
|
||||||
<li><a href="/settings">Settings</a></li>
|
<li><a href="/settings">Settings</a></li>
|
||||||
<% if (locals.user) { %>
|
<% if (locals.user) { %>
|
||||||
<li><a href="/auth/logout">Logout (<%= user.login %>)</a></li>
|
<li><a href="/auth/logout">Logout (<%= user.login %>)</a></li>
|
||||||
|
|
|
||||||
126
views/stats.ejs
Normal file
126
views/stats.ejs
Normal file
|
|
@ -0,0 +1,126 @@
|
||||||
|
<style>
|
||||||
|
.meta { color:var(--pico-muted-color); font-size:.85rem; }
|
||||||
|
.tiles { display:grid; grid-template-columns:repeat(auto-fit, minmax(150px, 1fr)); gap:.6rem; margin:.6rem 0 1.2rem; }
|
||||||
|
.tile { border:1px solid var(--pico-muted-border-color); border-radius:12px; padding:.7rem .9rem; }
|
||||||
|
.tile .v { font-size:1.7rem; font-weight:700; line-height:1.1; }
|
||||||
|
.tile .k { font-size:.8rem; color:var(--pico-muted-color); }
|
||||||
|
.charts { display:grid; grid-template-columns:repeat(auto-fit, minmax(320px, 1fr)); gap:1rem; margin-bottom:1.2rem; }
|
||||||
|
.chart { border:1px solid var(--pico-muted-border-color); border-radius:12px; padding:.7rem .9rem; position:relative; }
|
||||||
|
.chart h4 { margin:0 0 .4rem; font-size:.95rem; }
|
||||||
|
.chart svg { width:100%; height:auto; display:block; }
|
||||||
|
.chart .bar { fill:var(--pico-primary); }
|
||||||
|
.chart .bar:hover, .chart .hit:hover + .bar { fill:var(--pico-primary-hover); }
|
||||||
|
.chart .grid { stroke:var(--pico-muted-border-color); stroke-width:1; }
|
||||||
|
.chart .ax { fill:var(--pico-muted-color); font-size:10px; }
|
||||||
|
.tip { position:absolute; pointer-events:none; background:var(--pico-card-background-color); border:1px solid var(--pico-muted-border-color);
|
||||||
|
border-radius:8px; padding:.25rem .5rem; font-size:.8rem; box-shadow:0 4px 12px rgba(0,0,0,.35); white-space:nowrap; display:none; z-index:5; }
|
||||||
|
.boards { display:grid; grid-template-columns:repeat(auto-fit, minmax(260px, 1fr)); gap:1rem; }
|
||||||
|
.board { border:1px solid var(--pico-muted-border-color); border-radius:12px; padding:.6rem .8rem; }
|
||||||
|
.board h4 { margin:0 0 .3rem; font-size:.95rem; }
|
||||||
|
.board table { margin:0; font-size:.85rem; }
|
||||||
|
.board td, .board th { padding:.2rem .35rem; }
|
||||||
|
.board td.n { text-align:right; font-variant-numeric:tabular-nums; }
|
||||||
|
.board td.r { width:1.6em; color:var(--pico-muted-color); }
|
||||||
|
.board a { text-decoration:none; }
|
||||||
|
.emotes { display:flex; flex-wrap:wrap; gap:.5rem; }
|
||||||
|
.emote { display:flex; align-items:center; gap:.35rem; border:1px solid var(--pico-muted-border-color); border-radius:99px; padding:.15rem .6rem .15rem .25rem; font-size:.8rem; }
|
||||||
|
.emote img { width:26px; height:26px; object-fit:contain; }
|
||||||
|
.search { display:flex; gap:.5rem; max-width:420px; }
|
||||||
|
.search input, .search button { margin:0; }
|
||||||
|
.search button { width:auto; }
|
||||||
|
.empty { color:var(--pico-muted-color); font-size:.85rem; font-style:italic; }
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<div style="display:flex; align-items:center; gap:1rem; flex-wrap:wrap;">
|
||||||
|
<h2 style="margin:0;">Stats</h2>
|
||||||
|
<% if (live) { %><span class="badge badge-green">live · counting</span><% } %>
|
||||||
|
<form class="search" method="GET" action="/stats/viewer" style="margin-left:auto;">
|
||||||
|
<input type="search" name="q" placeholder="Look up a viewer…" required>
|
||||||
|
<button type="submit" class="outline">Find</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<p class="meta" title="Bots (Settings → Hidden chatters) are counted but left out of everything shown here. Chat is counted from the first restart after 2026-11-22; watch time from the first stream after the chatter-list permission was approved.">
|
||||||
|
<%= overview.since ? `Counting since ${overview.since.slice(0, 10)}` : 'Nothing counted yet: stats start with the next stream.' %>
|
||||||
|
· <%= hiddenCount %> hidden bot<%= hiddenCount === 1 ? '' : 's' %> left out ⓘ
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="tiles">
|
||||||
|
<div class="tile"><div class="v"><%= overview.viewers.toLocaleString() %></div><div class="k">viewers seen</div></div>
|
||||||
|
<div class="tile"><div class="v"><%= overview.messages.toLocaleString() %></div><div class="k">chat messages</div></div>
|
||||||
|
<div class="tile"><div class="v"><%= overview.watchHours.toLocaleString() %></div><div class="k">hours watched</div></div>
|
||||||
|
<div class="tile"><div class="v"><%= overview.streams %></div><div class="k">streams</div></div>
|
||||||
|
<div class="tile"><div class="v"><%= overview.checkins.toLocaleString() %></div><div class="k">check-ins</div></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<%
|
||||||
|
// one single-series bar chart (no legend: the title names it); x = last 30 days
|
||||||
|
const chart = (title, key, unit) => {
|
||||||
|
const W = 600, H = 150, L = 34, B = 18, T = 8, n = perDay.length, max = Math.max(1, ...perDay.map(d => d[key]));
|
||||||
|
const nice = (() => { const p = Math.pow(10, Math.floor(Math.log10(max))); return [1, 2, 2.5, 5, 10].map(m => m * p).find(v => v >= max); })();
|
||||||
|
const slot = (W - L) / n, bw = Math.max(2, slot - 2), y = v => T + (H - T - B) * (1 - v / nice);
|
||||||
|
let s = `<svg viewBox="0 0 ${W} ${H}" role="img" aria-label="${title}">`;
|
||||||
|
for (const g of [0, nice / 2, nice]) s += `<line class="grid" x1="${L}" x2="${W}" y1="${y(g)}" y2="${y(g)}"/><text class="ax" x="${L - 4}" y="${y(g) + 3}" text-anchor="end">${g}</text>`;
|
||||||
|
perDay.forEach((d, i) => {
|
||||||
|
const x = L + i * slot + 1, v = d[key], top = y(v), h = (H - B) - top;
|
||||||
|
const label = `${d.day.slice(5)} · ${v} ${unit}`;
|
||||||
|
s += `<rect class="hit" x="${x - 1}" y="${T}" width="${slot}" height="${H - T - B}" fill="transparent" data-tip="${label}"/>`;
|
||||||
|
if (v > 0) s += `<path class="bar" d="M${x},${H - B} V${top + Math.min(4, h)} Q${x},${top} ${x + Math.min(4, bw / 2)},${top} H${x + bw - Math.min(4, bw / 2)} Q${x + bw},${top} ${x + bw},${top + Math.min(4, h)} V${H - B} Z"/>`;
|
||||||
|
if (i % 7 === 0 && i < n - 4) s += `<text class="ax" x="${x + bw / 2}" y="${H - 4}" text-anchor="middle">${d.day.slice(5)}</text>`;
|
||||||
|
if (i === n - 1) s += `<text class="ax" x="${x + bw}" y="${H - 4}" text-anchor="end">${d.day.slice(5)}</text>`;
|
||||||
|
});
|
||||||
|
return `<div class="chart"><h4>${title}</h4>${s}</svg><div class="tip"></div></div>`;
|
||||||
|
};
|
||||||
|
%>
|
||||||
|
<div class="charts">
|
||||||
|
<%- chart('Chat messages per day', 'messages', 'messages') %>
|
||||||
|
<%- chart('Watch hours per day', 'watchHours', 'h') %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3>Leaderboards</h3>
|
||||||
|
<%
|
||||||
|
const board = (title, rows, fmt, extra) => {
|
||||||
|
let s = `<div class="board"><h4>${title}</h4>`;
|
||||||
|
if (!rows.length) return s + '<p class="empty">nothing yet</p></div>';
|
||||||
|
s += '<table>';
|
||||||
|
rows.forEach((r, i) => {
|
||||||
|
s += `<tr><td class="r">${i + 1}</td><td><a href="/stats/viewer?q=${encodeURIComponent(r.login || '')}">${(r.name || r.login || '?').replace(/[<>&"]/g, '')}</a>${extra ? `<br><span class="meta" style="font-size:.75rem;">${extra(r)}</span>` : ''}</td><td class="n">${fmt(r.n)}</td></tr>`;
|
||||||
|
});
|
||||||
|
return s + '</table></div>';
|
||||||
|
};
|
||||||
|
const hm = m => m >= 60 ? `${Math.floor(m / 60)} h ${m % 60} m` : `${m} m`;
|
||||||
|
%>
|
||||||
|
<div class="boards">
|
||||||
|
<%- board('Points', boards.points, n => n.toLocaleString(), r => r.level) %>
|
||||||
|
<%- board('Watch time', boards.watch, hm) %>
|
||||||
|
<%- board('Chat messages', boards.chat, n => n.toLocaleString()) %>
|
||||||
|
<%- board('Bits', boards.bits, n => n.toLocaleString()) %>
|
||||||
|
<%- board('Channel points spent', boards.channelPoints, n => n.toLocaleString()) %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3 style="margin-top:1.4rem;">Top emotes</h3>
|
||||||
|
<% if (!emotes.length) { %><p class="empty">nothing yet</p><% } else { %>
|
||||||
|
<div class="emotes">
|
||||||
|
<% for (const e of emotes) {
|
||||||
|
const src = e.source === 'twitch' && e.id ? `https://static-cdn.jtvnw.net/emoticons/v2/${e.id}/default/dark/1.0`
|
||||||
|
: e.source === '7tv' && e.id ? `https://cdn.7tv.app/emote/${e.id}/1x.webp` : null; %>
|
||||||
|
<span class="emote" title="<%= e.emote %> · <%= e.source %>"><% if (src) { %><img src="<%= src %>" alt="" loading="lazy"><% } %><%= e.emote %> <strong><%= e.uses.toLocaleString() %></strong></span>
|
||||||
|
<% } %>
|
||||||
|
</div>
|
||||||
|
<% } %>
|
||||||
|
|
||||||
|
<h3 style="margin-top:1.4rem;">Top commands</h3>
|
||||||
|
<% if (!commands.length) { %><p class="empty">nothing yet</p><% } else { %>
|
||||||
|
<div class="emotes"><% for (const c of commands) { %><span class="emote" style="padding-left:.6rem;"><code><%= c.command %></code> <strong><%= c.uses.toLocaleString() %></strong></span><% } %></div>
|
||||||
|
<% } %>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// bar hover tooltips (per day)
|
||||||
|
document.querySelectorAll('.chart').forEach(ch => {
|
||||||
|
const tip = ch.querySelector('.tip');
|
||||||
|
ch.querySelectorAll('.hit').forEach(h => {
|
||||||
|
h.addEventListener('mouseenter', () => { tip.textContent = h.dataset.tip; tip.style.display = 'block'; });
|
||||||
|
h.addEventListener('mousemove', e => { const r = ch.getBoundingClientRect(); tip.style.left = (e.clientX - r.left + 12) + 'px'; tip.style.top = (e.clientY - r.top - 28) + 'px'; });
|
||||||
|
h.addEventListener('mouseleave', () => { tip.style.display = 'none'; });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
77
views/stats_viewer.ejs
Normal file
77
views/stats_viewer.ejs
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
<style>
|
||||||
|
.meta { color:var(--pico-muted-color); font-size:.85rem; }
|
||||||
|
.card { border:1px solid var(--pico-muted-border-color); border-radius:12px; padding:.8rem 1rem; margin-bottom:1rem; }
|
||||||
|
.tiles { display:grid; grid-template-columns:repeat(auto-fit, minmax(140px, 1fr)); gap:.6rem; }
|
||||||
|
.tile { border:1px solid var(--pico-muted-border-color); border-radius:12px; padding:.6rem .8rem; }
|
||||||
|
.tile .v { font-size:1.5rem; font-weight:700; line-height:1.1; }
|
||||||
|
.tile .k { font-size:.8rem; color:var(--pico-muted-color); }
|
||||||
|
.parts { display:flex; flex-wrap:wrap; gap:.4rem; margin-top:.6rem; }
|
||||||
|
.part { border:1px solid var(--pico-muted-border-color); border-radius:99px; padding:.1rem .6rem; font-size:.8rem; }
|
||||||
|
.emotes { display:flex; flex-wrap:wrap; gap:.4rem; }
|
||||||
|
.emote { display:flex; align-items:center; gap:.3rem; border:1px solid var(--pico-muted-border-color); border-radius:99px; padding:.1rem .55rem .1rem .25rem; font-size:.8rem; }
|
||||||
|
.emote img { width:24px; height:24px; object-fit:contain; }
|
||||||
|
table { font-size:.85rem; }
|
||||||
|
.btn-sm { margin:0; padding:.25rem .7rem; font-size:.8rem; width:auto; }
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<p><a href="/stats">← Stats</a></p>
|
||||||
|
<% if (!viewer) { %>
|
||||||
|
<h2><%= q %></h2>
|
||||||
|
<p class="meta">No statistics for this viewer (yet). Names are Twitch logins, e.g. <code>channel</code>.</p>
|
||||||
|
<% } else {
|
||||||
|
const hm = m => m >= 60 ? `${Math.floor(m / 60)} h ${m % 60} m` : `${m} m`;
|
||||||
|
const minutes = (viewer.watch.ticks || 0) * 5; %>
|
||||||
|
<h2 style="margin-bottom:.2rem;"><%= viewer.name %> <span class="meta">@<%= viewer.login %></span></h2>
|
||||||
|
<p class="meta"><%= viewer.level.name %>
|
||||||
|
<% if (viewer.hidden) { %> · <span class="badge badge-grey" title="In Settings → Hidden chatters: counted, but not shown in leaderboards">hidden bot</span><% } %>
|
||||||
|
<% if (viewer.chat.first_seen) { %> · first chat <%= viewer.chat.first_seen.slice(0, 10) %>, last <%= viewer.chat.last_seen.slice(0, 10) %><% } %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<div class="tiles">
|
||||||
|
<div class="tile"><div class="v"><%= viewer.total.toLocaleString() %></div><div class="k">points</div></div>
|
||||||
|
<div class="tile"><div class="v"><%= hm(minutes) %></div><div class="k">watched</div></div>
|
||||||
|
<div class="tile"><div class="v"><%= (viewer.chat.messages || 0).toLocaleString() %></div><div class="k">chat messages</div></div>
|
||||||
|
<div class="tile"><div class="v"><%= viewer.checkins %></div><div class="k">check-ins</div></div>
|
||||||
|
</div>
|
||||||
|
<div class="parts" title="Where the points come from (values in docs/PLANS.md)">
|
||||||
|
<% for (const [k, v] of Object.entries(viewer.parts).sort((a, b) => b[1] - a[1])) { %><span class="part"><%= k %>: <strong><%= v.toLocaleString() %></strong></span><% } %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<% if (viewer.emotes.length) { %>
|
||||||
|
<h4>Emotes</h4>
|
||||||
|
<div class="emotes" style="margin-bottom:1rem;">
|
||||||
|
<% for (const e of viewer.emotes) {
|
||||||
|
const src = e.source === 'twitch' && e.emote_id ? `https://static-cdn.jtvnw.net/emoticons/v2/${e.emote_id}/default/dark/1.0`
|
||||||
|
: e.source === '7tv' && e.emote_id ? `https://cdn.7tv.app/emote/${e.emote_id}/1x.webp` : null; %>
|
||||||
|
<span class="emote"><% if (src) { %><img src="<%= src %>" alt="" loading="lazy"><% } %><%= e.emote %> <strong><%= e.uses %></strong></span>
|
||||||
|
<% } %>
|
||||||
|
</div>
|
||||||
|
<% } %>
|
||||||
|
|
||||||
|
<% if (viewer.commands.length) { %>
|
||||||
|
<h4>Commands</h4>
|
||||||
|
<div class="emotes" style="margin-bottom:1rem;"><% for (const c of viewer.commands) { %><span class="emote" style="padding-left:.55rem;"><code><%= c.command %></code> <strong><%= c.uses %></strong></span><% } %></div>
|
||||||
|
<% } %>
|
||||||
|
|
||||||
|
<% if (viewer.actions.length) { %>
|
||||||
|
<h4>Actions</h4>
|
||||||
|
<table>
|
||||||
|
<thead><tr><th>Action</th><th>What</th><th>Times</th><th>Total</th><th>Last</th></tr></thead>
|
||||||
|
<tbody>
|
||||||
|
<% for (const a of viewer.actions) { %>
|
||||||
|
<tr><td><%= a.action.replace(/_/g, ' ') %></td><td><%= a.item %></td><td><%= a.uses %></td>
|
||||||
|
<td><%= a.unit ? `${(+a.amount).toLocaleString()} ${a.unit}` : '' %></td><td><small><%= (a.last_at || '').slice(0, 10) %></small></td></tr>
|
||||||
|
<% } %>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<% } %>
|
||||||
|
|
||||||
|
<form method="POST" action="/stats/viewer/forget" style="margin-top:1.5rem;">
|
||||||
|
<input type="hidden" name="login" value="<%= viewer.login %>">
|
||||||
|
<input type="hidden" name="user_id" value="<%= viewer.user_id || '' %>">
|
||||||
|
<button type="button" data-confirm class="outline btn-sm" style="color:#f88; border-color:#f88;"
|
||||||
|
title="For removal requests: deletes all of this viewer's statistics, watch time, stored chat and activity-log entries. Cannot be undone.">Forget this viewer</button>
|
||||||
|
</form>
|
||||||
|
<% } %>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue