LinkSpin data in MySQL (Marty 2026-11-13: 'everything database-driven'). docstore.js: every .json/.jsonl data document under DATA_DIR lives in MySQL table docs (DOCSTORE_URL), served to the modules through their own fs calls from an in-memory copy, written through in order; one writer at a time (docs_writer), so an older copy during a deploy can never overwrite newer data; first boot imports each file and sets it aside (.imported-<date>). Spin-link clicks are rows in rot_hits (one insert per click instead of rewriting a 20 MB document; every click kept). Kept as files on purpose: chain-index cache, uploads, geo db, kits, marks, queues. Admin sessions load after the store. Rehearsed on a copy: 52/54 documents byte-identical (rotator.json minus hits, kb.json seeds refreshed), 97,579 clicks moved, links + clicks work after restart.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+6
-2
@@ -15,6 +15,7 @@ let DATA_DIR = null, geo = null, OWN_HOSTS = () => [], SOURCES = null; // SOURCE
|
||||
let WANTS_CLICK_ID = () => false; // conversion pixel (2026-10-23): owner -> true when they have a goal, so their destinations get ?lsclk=
|
||||
let IS_NETWORK = () => false, GOAL_ROWS = () => []; // 2026-11-07: a page on a network host always gets the click id (InstantAdPay reports joins, wallets, purchases against it); goal conversions as rotator-shaped rows for per-link counts
|
||||
const DAY = 86400000;
|
||||
const docstore = require('./docstore'); // MySQL (2026-11-13): this document in table docs, each click a row in rot_hits
|
||||
const J = {
|
||||
db: null,
|
||||
FILE: () => path.join(DATA_DIR, 'rotator.json'),
|
||||
@@ -28,8 +29,10 @@ const J = {
|
||||
}
|
||||
if (last) throw new Error('rotator.json exists but cannot be read (' + last.message + '); refusing to start with no spin links');
|
||||
if (!this.db.conv) this.db.conv = [];
|
||||
const rows = docstore.isOn() ? docstore.rows('rot_hits') : null; if (rows) this.db.hits = rows.slice(-200000); // clicks live in MySQL
|
||||
if (!Array.isArray(this.db.hits)) this.db.hits = [];
|
||||
},
|
||||
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} }
|
||||
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(docstore.isOn() ? Object.assign({}, this.db, { hits: [] }) : this.db)); } catch (e) {} }
|
||||
};
|
||||
function init(opts) { DATA_DIR = opts.dataDir; geo = opts.geo || null; OWN_HOSTS = opts.ownHosts || (() => []); SOURCES = opts.sources || null; if (opts.wantsClickId) WANTS_CLICK_ID = opts.wantsClickId; if (opts.isNetworkHost) IS_NETWORK = opts.isNetworkHost; if (opts.goalRows) GOAL_ROWS = opts.goalRows; J.load(); }
|
||||
const convOf = (rid) => J.db.conv.filter(c => c.r === rid).concat(GOAL_ROWS().filter(c => c.r === rid)); // a link's conversions: site and network events, and goals
|
||||
@@ -197,6 +200,7 @@ async function resolve(code, req) {
|
||||
// sr = the traffic source attached to the link at click time, so the network that sent this click is the one told about its conversion
|
||||
J.db.hits.push({ id, r: r.id, d: d ? d.id : 0, ts: Date.now(), v, c: country, s: src, dv: device, bot: bot ? 1 : 0, oh: off ? 1 : undefined, q: Object.keys(q).length ? q : undefined, sr: r.sourceId || undefined, ac: ac || undefined });
|
||||
if (HIT_IX) HIT_IX.set(id, J.db.hits.length - 1);
|
||||
if (docstore.isOn()) { const h = J.db.hits[J.db.hits.length - 1]; docstore.rowPut('rot_hits', h.id, h.ts, h); } // memory keeps the newest 200k, MySQL keeps every click
|
||||
if (J.db.hits.length > 200000) { J.db.hits = J.db.hits.slice(-150000); HIT_IX = null; }
|
||||
J.save();
|
||||
let out = url;
|
||||
@@ -317,7 +321,7 @@ async function claim(demoOwner, newOwner) {
|
||||
function sweepDemos() {
|
||||
const now = Date.now(); const gone = new Set();
|
||||
J.db.rotations = J.db.rotations.filter(r => { if (r.demo && r.expires && r.expires < now) { gone.add(r.id); return false; } return true; });
|
||||
if (gone.size) { J.db.hits = J.db.hits.filter(h => !gone.has(h.r)); J.db.conv = J.db.conv.filter(c => !gone.has(c.r)); J.save(); }
|
||||
if (gone.size) { if (docstore.isOn()) docstore.rowDel('rot_hits', J.db.hits.filter(h => gone.has(h.r)).map(h => h.id)); J.db.hits = J.db.hits.filter(h => !gone.has(h.r)); J.db.conv = J.db.conv.filter(c => !gone.has(c.r)); J.save(); }
|
||||
return gone.size;
|
||||
}
|
||||
// site-wide figures for the daily snapshot (snapshot.js): the last `since` window, every owner, bots left out
|
||||
|
||||
Reference in New Issue
Block a user