Something went wrong. Try again.
pushes on tangled sites.wisp.place/zzstoatzz.io/punch
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051// build the leaderboard.json payload. one JOIN pulls pushes + handles from D1// in a single query — fast, no subrequest fan-out.
import type { Env, LeaderboardPayload, LeaderboardRow } from "./types.ts";
interface Joined { did: string; count: number; handle: string | null; avatar: string | null;}
export async function buildLeaderboard(env: Env, status: string | null): Promise<LeaderboardPayload> { const joined = await env.DB.prepare( `SELECT p.did, p.count, h.handle, h.avatar FROM pushes p LEFT JOIN handles h ON h.did = p.did ORDER BY p.count DESC`, ).all<Joined>();
const rows: LeaderboardRow[] = joined.results.map((r) => ({ did: r.did, handle: r.handle, avatar: r.avatar, pushes: r.count, }));
const knotRows = await env.DB.prepare("SELECT host FROM knots").all<{ host: string }>(); const knots = knotRows.results.map((r) => r.host).sort();
const meta = await env.DB.prepare( "SELECT key, value FROM meta WHERE key IN ('seen_ref_updates', 'last_published_at')", ).all<{ key: string; value: string }>(); const metaMap = new Map(meta.results.map((r) => [r.key, r.value])); const totalPushes = Number(metaMap.get("seen_ref_updates") ?? 0);
// updatedAt tracks when the DATA was last refreshed (end of last cron tick), // not when this JSON was serialized. fall back to now only if no cycle has // run yet. const lastCycle = metaMap.get("last_published_at"); const updatedAt = lastCycle && lastCycle.length > 0 ? lastCycle : new Date().toISOString();
return { rows, updatedAt, knots, totalPushes, status, };}