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

126
views/stats.ejs Normal file
View 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>