8dbac9379f
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
107 lines
7.9 KiB
JavaScript
107 lines
7.9 KiB
JavaScript
// Daily snapshot (Marty, 2026-11-02): once a day, a short "how LinkSpin is doing" card in the LinkSpin Support topic
|
|
// (and any other target listed), the LinkSpin counterpart of InstantAdPay's growth snapshot. The card speaks LinkSpin:
|
|
// spin links served, Banner Spin impressions, zone views, page views and leads, then sign-ups, purchases and POL paid.
|
|
// Every number comes from the same stores the Reports screen reads. Settings: snapshotEnabled (1/0), snapshotHourUtc
|
|
// (default 14 = 9 AM Central), snapshotTargets (feed, support, echo). State: snapshot-state.json (lastDay), so a
|
|
// restart never double-posts.
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
let X = {};
|
|
const DAY = 86400000;
|
|
function init(opts) { X = opts || {}; }
|
|
const STATE = () => path.join(X.dataDir, 'snapshot-state.json');
|
|
function state() { try { return JSON.parse(fs.readFileSync(STATE(), 'utf8')); } catch (e) { return {}; } }
|
|
function setState(s) { try { fs.writeFileSync(STATE(), JSON.stringify(s)); } catch (e) {} }
|
|
const n = v => Number(v || 0).toLocaleString('en-US');
|
|
const pol = w => { try { return (Number(BigInt(w) / (10n ** 16n)) / 100).toLocaleString('en-US', { maximumFractionDigits: 0 }); } catch (e) { return '0'; } };
|
|
const safe = (fn, dflt) => { try { const v = fn(); return v == null ? dflt : v; } catch (e) { return dflt; } };
|
|
|
|
async function gather(now = Date.now()) {
|
|
const since = now - DAY, prior = now - 2 * DAY;
|
|
const s = { signups: 0, signupsPrior: 0, members: 0, wallets: 0, payoutsOn: 0, seen24h: 0,
|
|
buys: 0, buysPrior: 0, usd: 0, activations: 0, paidWei: 0n, lifeBuys: 0, lifePaidWei: 0n, lifePayouts: 0,
|
|
links: null, spins: null, zones: null, pages: null };
|
|
if (X.db && X.db.enabled()) {
|
|
const q = (a, b) => X.db.q(a, b || []);
|
|
const a = (await q('SELECT COUNT(*) total, SUM(created>=?) s24, SUM(created>=? AND created<?) sPrior, SUM(address IS NOT NULL) wallet, SUM(member_id>0) payouts, SUM(last_seen>=?) seen FROM accounts', [since, prior, since, since]))[0] || {};
|
|
s.members = Number(a.total || 0); s.signups = Number(a.s24 || 0); s.signupsPrior = Number(a.sPrior || 0); s.wallets = Number(a.wallet || 0); s.payoutsOn = Number(a.payouts || 0); s.seen24h = Number(a.seen || 0);
|
|
} else if (X.accounts && X.accounts.listAll) {
|
|
// JSON store (production LinkSpin runs without MySQL): the same counts from the account records
|
|
const all = await X.accounts.listAll(1e6);
|
|
s.members = all.length; s.signups = all.filter(a => (a.created || 0) >= since).length; s.signupsPrior = all.filter(a => (a.created || 0) >= prior && (a.created || 0) < since).length;
|
|
s.wallets = all.filter(a => a.address).length; s.payoutsOn = all.filter(a => Number(a.memberId) > 0).length; s.seen24h = all.filter(a => (a.lastSeen || 0) >= since).length;
|
|
}
|
|
try {
|
|
for (const ev of X.chain.recentEvents(1e9)) {
|
|
if (ev.type === 'Purchase') { s.lifeBuys++; if (ev.ts >= since) { s.buys++; s.usd += ev.priceCents / 100; } else if (ev.ts >= prior) s.buysPrior++; }
|
|
else if (ev.type === 'MemberActivated' && ev.ts >= since) s.activations++;
|
|
else if (ev.type === 'TierPaid' || ev.type === 'AwardPaid') { s.lifePayouts++; s.lifePaidWei += BigInt(ev.amountWei); if (ev.ts >= since) s.paidWei += BigInt(ev.amountWei); }
|
|
}
|
|
const t = X.chain.totals ? X.chain.totals() : null;
|
|
if (t) { if (t.purchases) s.lifeBuys = Number(t.purchases); if (t.payoutWei) s.lifePaidWei = BigInt(t.payoutWei); if (t.payouts) s.lifePayouts = Number(t.payouts); }
|
|
} catch (e) {}
|
|
// the LinkSpin tools, each module answers for every owner at once
|
|
s.links = safe(() => X.rotator && X.rotator.window24(since), null);
|
|
s.spins = safe(() => X.bannerspin && X.bannerspin.window24(since), null);
|
|
s.zones = safe(() => X.zones && X.zones.window24(since), null);
|
|
s.pages = safe(() => X.pages && X.pages.window24(since), null);
|
|
return s;
|
|
}
|
|
|
|
function compose(s, cta) {
|
|
const trend = (cur, prev, what) => cur > prev ? ' (up from ' + n(prev) + ' ' + what + ')' : cur < prev ? ' (' + n(prev) + ' ' + what + ')' : '';
|
|
const plural = (k, one, many) => n(k) + ' ' + (k === 1 ? one : many);
|
|
const L = [];
|
|
L.push('\u{1F300} <b>LinkSpin</b> · 24-hour snapshot');
|
|
if (s.links) L.push('\u{1F517} Spin links: <b>' + n(s.links.served) + '</b> visitors sent through ' + plural(s.links.links, 'link', 'links') + ' · ' + n(s.links.unique) + ' unique · ' + n(s.links.countries) + ' countries' + (s.links.created ? ' · ' + plural(s.links.created, 'new link', 'new links') : ''));
|
|
if (s.spins) L.push('\u{1F5BC} Banner Spin: <b>' + n(s.spins.impsToday) + '</b> banner views today · ' + n(s.spins.clicks) + ' clicks · ' + n(s.spins.banners) + ' banners in ' + plural(s.spins.spins, 'spin', 'spins'));
|
|
if (s.zones) L.push('\u{1F4E1} Zones: <b>' + n(s.zones.views) + '</b> ad views on ' + plural(s.zones.zones, 'zone', 'zones') + ' · ' + n(s.zones.viewable) + ' viewable');
|
|
if (s.pages) L.push('\u{1F4C4} Pages: <b>' + n(s.pages.views) + '</b> views on ' + plural(s.pages.published, 'published page', 'published pages') + ' · ' + n(s.pages.leads) + ' leads' + (s.pages.created ? ' · ' + plural(s.pages.created, 'new page', 'new pages') : ''));
|
|
L.push('\u{1F465} Sign-ups: <b>' + n(s.signups) + '</b>' + trend(s.signups, s.signupsPrior, 'the day before') + ' · ' + n(s.activations) + ' switched on payouts · ' + n(s.seen24h) + ' signed in');
|
|
L.push('\u{1F9FE} Purchases: <b>' + n(s.buys) + '</b> for $' + n(Math.round(s.usd)) + trend(s.buys, s.buysPrior, 'the day before') + ' · <b>' + pol(s.paidWei) + ' POL</b> paid to members in the same transactions');
|
|
L.push('\u{1F3C1} So far: <b>' + n(s.members) + '</b> accounts · ' + n(s.wallets) + ' wallets linked · ' + n(s.payoutsOn) + ' with payouts on · ' + n(s.lifeBuys) + ' purchases · <b>' + pol(s.lifePaidWei) + ' POL</b> paid out in ' + n(s.lifePayouts) + ' payouts');
|
|
if (cta) L.push('\u{1F449} ' + cta);
|
|
return L.join('\n');
|
|
}
|
|
|
|
function targetMap(sc) {
|
|
return { feed: [sc.telegramChatId, sc.telegramTopicId], support: [sc.telegramSupportChatId, sc.telegramSupportTopicId], echo: [sc.telegramEchoChatId, sc.telegramEchoTopicId] };
|
|
}
|
|
async function post() {
|
|
const sc = X.siteConfig();
|
|
if (!sc.telegramBotToken) return { error: 'No Telegram bot token set.' };
|
|
const s = await gather(); const text = compose(s, sc.telegramCtaUrl);
|
|
const targets = String(sc.snapshotTargets || 'support').split(',').map(x => x.trim()).filter(Boolean);
|
|
const map = targetMap(sc); let sent = 0;
|
|
for (const name of targets) {
|
|
const t = map[name]; if (!t) continue;
|
|
if (!t[0]) { console.warn('snapshot: target "' + name + '" is listed but has no chat id, nothing sent'); continue; }
|
|
if (await X.send(t[0], text, t[1])) sent++;
|
|
}
|
|
const st = state(); st.lastPost = Date.now(); st.lastDay = new Date().toISOString().slice(0, 10); setState(st);
|
|
return { ok: true, sent, text };
|
|
}
|
|
// one destination by name, for a one-off post (admin: POST /api/admin/snapshot/send with {target})
|
|
async function postTo(name) {
|
|
const sc = X.siteConfig(); if (!sc.telegramBotToken) return { error: 'No Telegram bot token set.' };
|
|
const t = targetMap(sc)[name]; if (!t || !t[0]) return { error: 'No chat id for target ' + name + '.' };
|
|
const text = compose(await gather(), sc.telegramCtaUrl);
|
|
const ok = await X.send(t[0], text, t[1]); return { ok: !!ok, sent: ok ? 1 : 0, target: name, text };
|
|
}
|
|
|
|
// every 10 minutes: post once a day at or after snapshotHourUtc (a restart never double-posts)
|
|
async function tick() {
|
|
const sc = X.siteConfig();
|
|
if (String(sc.snapshotEnabled || '0') !== '1' || !sc.telegramBotToken) return;
|
|
const hour = Number(sc.snapshotHourUtc == null || sc.snapshotHourUtc === '' ? 14 : sc.snapshotHourUtc);
|
|
const now = new Date(); const day = now.toISOString().slice(0, 10);
|
|
if (now.getUTCHours() < hour) return;
|
|
if (state().lastDay === day) return;
|
|
await post();
|
|
}
|
|
|
|
async function preview() { const sc = X.siteConfig(); return compose(await gather(), sc.telegramCtaUrl); }
|
|
|
|
module.exports = { init, gather, compose, post, postTo, tick, preview };
|