Per-viewer action statistics: rewards, Power-ups, cheers, subs, gifts, follows, raids, tips
chat_action counts every logged viewer event (via dispatch.js) per viewer and item, with total spent (points / bits / months / subs / viewers / tip money). Keyed by Twitch user id, or name:<login> for events without one. Hidden chatters skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cd751e3a07
commit
862edf6d5d
3 changed files with 63 additions and 5 deletions
|
|
@ -48,5 +48,6 @@ Estimate: about half a day once chat is read server-side.
|
|||
|
||||
Status: **logging built 2026-11-22** (src/chatlog.js), with her choices: per-viewer counts (messages, per day,
|
||||
emotes incl. 7TV, !commands, first/last seen) parsed as messages arrive; raw messages kept **30 days** only;
|
||||
moderation removes stored text; hidden chatters (bots) not counted. Still to do: a statistics page, and a
|
||||
moderation removes stored text; hidden chatters (bots) not counted. Viewer actions are counted too
|
||||
(chat_action: rewards + points, Power-ups + bits, cheers, subs, gifts, follows, raids, watch streaks, tips). Still to do: a statistics page, and a
|
||||
"forget this viewer" button for removal requests (`chatlog.forgetUser()` exists).
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@
|
|||
// chat_emote per viewer per emote: uses (Twitch emotes from the message, 7TV channel + global by name)
|
||||
// chat_command per viewer per !command: uses, last used
|
||||
// chat_raw the messages themselves, deleted after RAW_DAYS
|
||||
// chat_action per viewer per action + item: uses, total spent (points / bits / subs / viewers / tip money),
|
||||
// from every event that goes through dispatch.js (rewards, Power-ups, cheers, subs, gifts,
|
||||
// follows, raids, watch streaks, tips). Keyed by Twitch user id, or 'name:<login>' when the
|
||||
// event has none (StreamElements tips, watch streaks).
|
||||
//
|
||||
// Moderation: a deleted message, a timeout/ban (clear_user_messages) or a chat clear removes the stored text at
|
||||
// once (Twitch developer rules); the counts stay, they hold no message content.
|
||||
|
|
@ -41,6 +45,15 @@ function init() {
|
|||
source TEXT, emote_id TEXT, uses INTEGER DEFAULT 0, last_used TEXT,
|
||||
PRIMARY KEY (user_id, emote)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chat_action (
|
||||
user_key TEXT NOT NULL, -- Twitch user id, or 'name:<login>'
|
||||
user_login TEXT, user_name TEXT,
|
||||
action TEXT NOT NULL, -- channel_points, custom_power_up, bits, sub, gift_sub, follow, raid, tip, ...
|
||||
item TEXT NOT NULL DEFAULT '', -- reward / Power-up title, sub tier, ...
|
||||
uses INTEGER DEFAULT 0, amount REAL DEFAULT 0, unit TEXT,
|
||||
first_at TEXT, last_at TEXT,
|
||||
PRIMARY KEY (user_key, action, item)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chat_command (
|
||||
user_id TEXT NOT NULL, command TEXT NOT NULL, uses INTEGER DEFAULT 0, last_used TEXT,
|
||||
PRIMARY KEY (user_id, command)
|
||||
|
|
@ -106,6 +119,46 @@ function onMessage(e) {
|
|||
})();
|
||||
}
|
||||
|
||||
// --- viewer actions (called by dispatch.js for every logged event) ---
|
||||
const TIER = { 1000: 'Tier 1', 2000: 'Tier 2', 3000: 'Tier 3' };
|
||||
function actionOf(eventType, detail, data, a) {
|
||||
const { AUTO_REWARDS, POWER_UPS } = require('./redeems');
|
||||
const d = data || {};
|
||||
switch (eventType) {
|
||||
case 'channel_points': return { item: d.reward?.title || detail, amount: d.reward?.cost, unit: 'points' };
|
||||
case 'auto_reward': return { item: AUTO_REWARDS[d.reward?.type] || d.reward?.type || detail, amount: d.reward?.channel_points, unit: 'points' };
|
||||
case 'power_up': return { item: POWER_UPS[d.power_up?.type] || d.power_up?.type || detail, amount: d.bits, unit: 'bits' };
|
||||
case 'custom_power_up': return { item: d.custom_power_up?.title || detail, amount: d.custom_power_up?.bits, unit: 'bits' };
|
||||
case 'bits': return { item: 'cheer', amount: d.bits ?? a?.bits, unit: 'bits' };
|
||||
case 'sub': return { item: a?.prime ? 'Prime' : TIER[a?.tier] || 'sub', amount: a?.duration || 1, unit: 'months' };
|
||||
case 'gift_sub': return { item: TIER[a?.tier] || 'gift subs', amount: a?.total || 1, unit: 'subs' };
|
||||
case 'follow': return { item: 'follow', amount: 0, unit: null };
|
||||
case 'raid': return { item: 'raid', amount: a?.viewers, unit: 'viewers' };
|
||||
case 'watch_streak': return { item: 'watch streak', amount: a?.streak, unit: 'streak', max: true };
|
||||
case 'tip': return { item: 'tip', amount: a?.amount, unit: a?.currency || null };
|
||||
default: return null; // polls, hype train, raid out, legacy commands: no single viewer
|
||||
}
|
||||
}
|
||||
function onAction(eventType, detail, username, data, replay) {
|
||||
init();
|
||||
const a = replay?.type === 'alert' ? replay.alert : null;
|
||||
const act = actionOf(eventType, detail, data, a);
|
||||
if (!act) return;
|
||||
const d = data || {};
|
||||
const id = d.user_id || d.chatter_user_id || null;
|
||||
const login = String(d.user_login || d.chatter_user_login || username || a?.user || '').toLowerCase();
|
||||
if (!id && !login) return; // anonymous gifts / cheers
|
||||
if (hiddenUsers().includes(login)) return;
|
||||
const key = id ? String(id) : `name:${login}`;
|
||||
const amount = Number(act.amount) || 0, at = new Date().toISOString();
|
||||
db.raw().prepare(`INSERT INTO chat_action (user_key, user_login, user_name, action, item, uses, amount, unit, first_at, last_at)
|
||||
VALUES (?, ?, ?, ?, ?, 1, ?, ?, ?, ?)
|
||||
ON CONFLICT(user_key, action, item) DO UPDATE SET uses = uses + 1,
|
||||
amount = ${act.max ? 'MAX(amount, excluded.amount)' : 'amount + excluded.amount'},
|
||||
user_login = excluded.user_login, user_name = excluded.user_name, last_at = excluded.last_at`)
|
||||
.run(key, login, d.user_name || d.chatter_user_name || username || login, eventType, String(act.item || ''), amount, act.unit, at, at);
|
||||
}
|
||||
|
||||
// moderation: remove stored message text (counts stay)
|
||||
function onDelete(e) { init(); db.raw().prepare('DELETE FROM chat_raw WHERE id = ?').run(e.message_id); }
|
||||
function onClearUser(e) { init(); db.raw().prepare('DELETE FROM chat_raw WHERE user_id = ?').run(e.target_user_id); }
|
||||
|
|
@ -122,9 +175,10 @@ function forgetUser(userIdOrLogin) {
|
|||
init();
|
||||
const d = db.raw();
|
||||
const u = d.prepare('SELECT user_id FROM chat_user WHERE user_id = ? OR user_login = ?').get(String(userIdOrLogin), String(userIdOrLogin).toLowerCase());
|
||||
if (!u) return 0;
|
||||
for (const t of ['chat_raw', 'chat_user', 'chat_user_day', 'chat_emote', 'chat_command']) d.prepare(`DELETE FROM ${t} WHERE user_id = ?`).run(u.user_id);
|
||||
return 1;
|
||||
const login = String(userIdOrLogin).toLowerCase();
|
||||
if (u) for (const t of ['chat_raw', 'chat_user', 'chat_user_day', 'chat_emote', 'chat_command']) d.prepare(`DELETE FROM ${t} WHERE user_id = ?`).run(u.user_id);
|
||||
const n = d.prepare('DELETE FROM chat_action WHERE user_key = ? OR user_key = ? OR user_login = ?').run(u ? u.user_id : '-', `name:${login}`, login).changes;
|
||||
return u || n ? 1 : 0;
|
||||
}
|
||||
|
||||
module.exports = { init, onMessage, onDelete, onClearUser, onClear, prune, forgetUser, RAW_DAYS };
|
||||
module.exports = { init, onMessage, onAction, onDelete, onClearUser, onClear, prune, forgetUser, RAW_DAYS };
|
||||
|
|
|
|||
|
|
@ -35,6 +35,9 @@ function dispatchEvent(eventType, eventDetail = null, username = null, data = nu
|
|||
// the log must never take the server down (a bad row once crashed it mid-stream)
|
||||
try { db.logActivity(eventType || 'unknown', eventDetail, username, data, fired, replay); }
|
||||
catch (e) { console.error(`[dispatch] activity log failed (${eventType}):`, e.message); }
|
||||
// per-viewer action statistics (chatlog.js)
|
||||
try { require('./chatlog').onAction(eventType, eventDetail, username, data, replay); }
|
||||
catch (e) { console.error(`[dispatch] action stats failed (${eventType}):`, e.message); }
|
||||
}
|
||||
|
||||
module.exports = { dispatchEvent };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue