|
| 1 | +import { createLeaderboard, projectionStore } from '@profullstack/leaderboard'; |
| 2 | +import { brand, config } from '@tipoff/config'; |
| 3 | +import { sql } from '@tipoff/db'; |
| 4 | + |
| 5 | +/** |
| 6 | + * The public board over the crawler paywall. |
| 7 | + * |
| 8 | + * Two sides, never in one list. An agent that paid is a customer and is ranked |
| 9 | + * by money; an agent that keeps hitting the wall without paying is demand, and |
| 10 | + * is ranked by volume. Putting them in one table would say those are the same |
| 11 | + * kind of fact, and the difference between them is the entire business. |
| 12 | + * |
| 13 | + * Both tables are written by the gateway hooks in app.js, so the board keeps no |
| 14 | + * copy of its own and cannot drift from them. Badges are the exception: they |
| 15 | + * are awarded rather than derived. |
| 16 | + */ |
| 17 | + |
| 18 | +const ms = (v) => (v instanceof Date ? v.getTime() : new Date(v).getTime()); |
| 19 | + |
| 20 | +const badges = { |
| 21 | + async awardBadge(player, badge) { |
| 22 | + const rows = await sql` |
| 23 | + insert into leaderboard_badges (player, badge) values (${player}, ${badge}) |
| 24 | + on conflict (player, badge) do nothing |
| 25 | + returning player`; |
| 26 | + return rows.length > 0; |
| 27 | + }, |
| 28 | + async badges() { |
| 29 | + const rows = await sql`select player, badge, awarded_at from leaderboard_badges`; |
| 30 | + const out = {}; |
| 31 | + for (const r of rows) { |
| 32 | + out[r.player] ??= {}; |
| 33 | + out[r.player][r.badge] = ms(r.awarded_at); |
| 34 | + } |
| 35 | + return out; |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +const KNOWN_AGENTS = [ |
| 40 | + 'meta-externalagent', |
| 41 | + 'GPTBot', |
| 42 | + 'ClaudeBot', |
| 43 | + 'anthropic-ai', |
| 44 | + 'CCBot', |
| 45 | + 'Bytespider', |
| 46 | + 'Applebot', |
| 47 | + 'FacebookBot', |
| 48 | + 'Lightpanda', |
| 49 | + 'PerplexityBot', |
| 50 | + 'Google-Extended', |
| 51 | +]; |
| 52 | + |
| 53 | +/** |
| 54 | + * The name a reader recognises. An agent that says nothing useful is grouped |
| 55 | + * under one honest label rather than given a row per random string, which |
| 56 | + * would fill the board with noise and bury the agents that matter. |
| 57 | + */ |
| 58 | +export function agentName(userAgent) { |
| 59 | + const ua = String(userAgent ?? ''); |
| 60 | + const known = KNOWN_AGENTS.find((k) => ua.toLowerCase().includes(k.toLowerCase())); |
| 61 | + if (known) return known; |
| 62 | + const token = ua.match(/([A-Za-z][\w.-]{2,30})\/[\d.]+/)?.[1]; |
| 63 | + return token ?? 'unidentified'; |
| 64 | +} |
| 65 | + |
| 66 | +const shortWallet = (p) => (p.length > 14 ? `${p.slice(0, 6)}…${p.slice(-4)}` : p); |
| 67 | + |
| 68 | +async function events({ since }) { |
| 69 | + const from = new Date(since || 0); |
| 70 | + const [sales, demand] = await Promise.all([ |
| 71 | + sql`select payer, user_agent, total_cents, days, created_at |
| 72 | + from crawl_sales where created_at >= ${from}`, |
| 73 | + // crawl_demand.day is a DATE. Binding a JS Date here sends Postgres the |
| 74 | + // full "Thu Jan 01 1970 00:00:00 GMT+0000 (…)" string, which it refuses to |
| 75 | + // parse as a date, and the whole board 500s. Send the day itself. |
| 76 | + sql`select agent, day, hits from crawl_demand where day >= ${from.toISOString().slice(0, 10)}`, |
| 77 | + ]); |
| 78 | + |
| 79 | + const out = []; |
| 80 | + for (const s of sales) { |
| 81 | + const player = s.payer || agentName(s.user_agent); |
| 82 | + if (!player) continue; |
| 83 | + const at = ms(s.created_at); |
| 84 | + const name = agentName(s.user_agent) ?? shortWallet(String(player)); |
| 85 | + const each = (metric, delta) => out.push({ player: String(player), name, metric, delta, at }); |
| 86 | + each('spent', Number(s.total_cents) || 0); |
| 87 | + each('passes', 1); |
| 88 | + each('days', Number(s.days) || 1); |
| 89 | + } |
| 90 | + for (const d of demand) { |
| 91 | + // A day bucket lands at noon UTC, so it falls inside the day it describes |
| 92 | + // no matter which way a period boundary rounds. |
| 93 | + const at = ms(d.day) + 12 * 3600 * 1000; |
| 94 | + out.push({ |
| 95 | + player: `ua:${d.agent}`, |
| 96 | + name: d.agent, |
| 97 | + metric: 'blocked', |
| 98 | + delta: Number(d.hits) || 0, |
| 99 | + at, |
| 100 | + }); |
| 101 | + } |
| 102 | + return out; |
| 103 | +} |
| 104 | + |
| 105 | +export const leaderboard = createLeaderboard({ |
| 106 | + siteName: brand.name, |
| 107 | + siteUrl: config.siteUrl, |
| 108 | + basePath: '/leaderboard', |
| 109 | + store: projectionStore({ events, badges }), |
| 110 | + sides: { buy: 'Agents paying', use: 'Agents not paying' }, |
| 111 | + boards: { |
| 112 | + spenders: { |
| 113 | + label: 'Biggest spenders', |
| 114 | + metric: 'spent', |
| 115 | + format: 'usd', |
| 116 | + unit: 'Spent', |
| 117 | + side: 'buy', |
| 118 | + actor: 'Agent', |
| 119 | + }, |
| 120 | + passes: { |
| 121 | + label: 'Most passes bought', |
| 122 | + metric: 'passes', |
| 123 | + format: 'integer', |
| 124 | + unit: 'Passes', |
| 125 | + side: 'buy', |
| 126 | + actor: 'Agent', |
| 127 | + }, |
| 128 | + days: { |
| 129 | + label: 'Most days of access', |
| 130 | + metric: 'days', |
| 131 | + format: 'integer', |
| 132 | + unit: 'Days', |
| 133 | + side: 'buy', |
| 134 | + actor: 'Agent', |
| 135 | + }, |
| 136 | + blocked: { |
| 137 | + label: 'Most requests refused', |
| 138 | + metric: 'blocked', |
| 139 | + format: 'integer', |
| 140 | + unit: 'Refused', |
| 141 | + side: 'use', |
| 142 | + actor: 'Agent', |
| 143 | + }, |
| 144 | + }, |
| 145 | + // Nobody earns here: this site sells its own data rather than anyone else's. |
| 146 | + ladder: null, |
| 147 | + cacheMs: 60_000, |
| 148 | +}); |
0 commit comments