Stats per stream and period, public stats page, dashboard settings, Card alert style

Statistics
- Per day and per stream statistics (src/periodstats.js, one generic stat_day table), backfilled
  once from the older tables; Stream / Week / Month / Year / All time views, Community and Streams tabs.
- Viewer count every 5 minutes and title / category history per stream (EventSub channel.update):
  viewer graph, games played (donut), games in the streams list.
- Twitch sync (src/twitchsync.js): followers with follow dates, current subs, mods, VIPs, all-time
  bits, channel emotes, past streams from VODs. Twitch analytics CSV import (src/twitchcsv.js).
- Public stats page on its own host (Settings -> Stats and data): no message text, no bots,
  !hideme / !showme opt-out. Viewer page: roles, follow age, sub status, bits, a menu for rarely used actions.
- StreamElements watch-time importer removed (already imported minutes stay as a baseline).

Dashboard
- Settings and Actions with a left sidebar; each settings page saves only its own fields.
  Connections (Twitch, StreamElements token now set here, HUDFX), OBS links (hidden behind an eye
  toggle) with a link builder, Chat (sound picked from the media library), TTS, Stats and data,
  Backup (export / import the whole setup), Advanced. Time zone setting for the stats pages.
- Actions -> Alerts: alert style, pictures, sounds, bits tiers and card colours, stored in the
  database (src/theme.js); an old file-based theme is imported once. Alert preview page
  (/actions/alerts/preview, tools/preview/export.js for a static copy).
- Old /actions page removed; Actions = Redeems / Alerts / TTS voices / Media.

Alerts
- New plain Card style for everyone (public/alerts/card.js); Polaroid, Cassette and the TTS letter
  stay as optional looks. Redeem variations can be Card, Cassette, Sound only or Video.

Repo
- No channel data in git: the channel's art, stickers and sounds stay on disk (gitignored) and are
  picked in the dashboard; examples for the server config (apache.example.sh, .env.example).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-23 11:22:49 +02:00
parent 5bf4bffaa0
commit c481ca92cb
57 changed files with 3080 additions and 774 deletions

View file

@ -1,44 +1,11 @@
<style>
/* colours for the shared stats body (_statsbody.ejs), from Pico */
main { --muted:var(--pico-muted-color); --line:var(--pico-muted-border-color); --accent:var(--pico-primary-background);
--accent-hover:var(--pico-primary-hover-background); --card:transparent; --card-solid:var(--pico-card-background-color); }
.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-fill, minmax(320px, 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 ol { list-style:none; margin:0; padding:0; }
.board li { list-style:none; display:grid; grid-template-columns:1.6rem minmax(0, 1fr) var(--nw, 4.6rem); align-items:center; gap:.5rem;
height:2rem; margin:0; padding:0 .2rem; border-bottom:1px solid var(--pico-muted-border-color); font-size:.88rem; }
.board li:last-child { border-bottom:0; }
.board .r { color:var(--pico-muted-color); text-align:right; font-variant-numeric:tabular-nums; }
.board .who { display:flex; align-items:center; gap:.4rem; min-width:0; }
.board .who a { text-decoration:none; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; min-width:0; }
.board .lvl { flex:none; font-size:.66rem; padding:.02rem .45rem; border-radius:99px; border:1px solid var(--pico-muted-border-color);
color:var(--pico-muted-color); white-space:nowrap; }
.board .n { text-align:right; font-variant-numeric:tabular-nums; white-space:nowrap; }
/* points: the main board, twice as wide, level in its own column so the pills line up */
.board.wide { grid-column:span 2; }
.board.wide li { grid-template-columns:1.6rem minmax(0, 1fr) 5.5rem var(--nw, 4.6rem); }
.board.wide .lvl { justify-self:start; }
@media (max-width: 700px) { .board.wide { grid-column:auto; } }
.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; }
.warn { color:#e6b800; }
</style>
@ -50,100 +17,23 @@
<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 class="meta">
<span title="Bots (Settings → Hidden chatters) are counted but left out of everything shown here. Days are UTC. Watch time from before HeyStreamer counted comes from StreamElements (or Twitch's analytics export) and only shows under All time."><%= since ? `Counting since ${since}` : 'Nothing counted yet: stats start with the next stream.' %>
· <%= hiddenCount %> hidden bot<%= hiddenCount === 1 ? '' : 's' %> left out ⓘ</span>
· <% if (publicUrl) { %><a href="<%= publicUrl %>" target="_blank" rel="noopener" title="What viewers see: no message text, bots and !hideme viewers not named">Public page ↗</a><% } else { %><a href="/public" target="_blank" rel="noopener" title="Public page is off (Settings → Public stats page). This preview works while you are logged in.">Public page preview ↗</a><% } %>
</p>
<%
const syncBad = Object.entries(sync.parts || {}).filter(([, v]) => !String(v).startsWith('ok'));
const syncTip = sync.at ? `Last sync ${sync.at.slice(0, 16).replace('T', ' ')} UTC: ` + Object.entries(sync.parts).map(([k, v]) => `${k} ${v}`).join(', ') : 'Not synced yet';
%>
<p class="meta" style="margin-top:-.4rem;">
<span title="<%= syncTip %>">Twitch sync: <%= sync.at ? `${sync.at.slice(0, 16).replace('T', ' ')} UTC` : 'never' %><% if (syncBad.length) { %> · <span class="warn"><%= syncBad.map(([k]) => k).join(', ') %> failed</span><% } %> ⓘ</span>
· analytics: <%= csv.days ? `${csv.days} days imported` : 'none imported' %>
· <a href="/settings/stats">Sync, import and the public page: Settings → Stats and data</a>
</p>
<% if (msg) { %><p class="<%= /failed/i.test(msg) ? 'warn' : '' %>" style="font-weight:600;"><%= msg %></p><% } %>
<% if (se.configured) { %>
<form method="POST" action="/stats/import-se" style="display:flex; gap:.6rem; align-items:center; flex-wrap:wrap; margin:.2rem 0 .8rem;">
<button type="submit" class="outline" style="width:auto; margin:0; padding:.3rem .9rem; font-size:.85rem;"
title="Copies StreamElements' watch time (per viewer) into HeyStreamer once. It counts like our own watch time (5 points per 5 minutes). Importing again replaces the previous import.<%= se.ownTicks ? ' HeyStreamer is already counting watch time itself: StreamElements has counted those streams too, so importing again now counts them twice.' : '' %>">
<%= se.n ? 'Import again from StreamElements' : 'Import watch time from StreamElements' %></button>
<span class="meta"><%= se.n ? `Imported ${se.n} viewers · ${Math.round(se.m / 60)} h on ${String(se.at).slice(0, 10)}` : 'Not imported yet: best done before the first stream HeyStreamer counts.' %>
<% if (se.n && se.ownTicks) { %><span class="warn"> · importing again would double-count streams since then</span><% } %></span>
</form>
<% } %>
<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, nw) => {
let s = `<div class="board${extra ? ' wide' : ''}"${nw ? ` style="--nw:${nw}"` : ''}><h4>${title}</h4>`;
if (!rows.length) return s + '<p class="empty">nothing yet</p></div>';
s += '<ol>';
rows.forEach((r, i) => {
const name = (r.name || r.login || '?').replace(/[<>&"]/g, '');
s += `<li><span class="r">${i + 1}</span><span class="who"><a href="/stats/viewer?q=${encodeURIComponent(r.login || '')}" title="${name}">${name}</a></span>`
+ `${extra ? `<span class="lvl">${extra(r)}</span>` : ''}<span class="n">${fmt(r.n)}</span></li>`;
});
return s + '</ol></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, null, '6.4rem') %>
<%- 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>
<%- include('_statsbody', { s, q, base: '/stats', viewerHref: '/stats/viewer?q=', mode: 'admin', view, ranks, tz,
extraBoards: [{ title: 'Points · all time', rows: pointsBoard, level: x => x.level, wide: true }] }) %>