An atproto indexer and web app that scrapes domains from collections it sees
at-tracker CLAUDE.md
4.0 kB · 102 lines
Markdown
at main

AT Tracker — Claude context #

Project-level decisions and architecture notes for future Claude sessions.


Relay architecture #

The relay is a headless Node.js service that consumes the AT Proto Jetstream WebSocket firehose and writes aggregate counts to Postgres.

Hot path vs. background workers #

The WebSocket message handler must never block or do I/O. The pattern used throughout the relay:

  • Hot path (ws.on("message")) — only touches in-memory structures (Maps, Sets, arrays). Appends to buffers, returns immediately.
  • Flush worker — setInterval every 5 s (or on hitting FLUSH_BATCH_SIZE). Swaps the buffer out atomically, then does the Postgres upsert. If the upsert fails, the buffer is restored so no events are lost.
  • PDS resolver worker — separate setInterval, never touches the event loop. Drains the resolution queue in the background.

PDS resolution (relay/src/pds-resolver.ts) #

Each commit event carries a did but not the user's PDS host. Resolution is non-blocking:

  1. getPdsHost(did) — plain Map.get(), O(1), called on every event.
  2. Cache hit → attribute to collection_pds immediately.
  3. Cache miss → enqueue(did) drops it into a Set and the handler moves on.

Background worker (every 2 s):

  • Dequeues up to BATCH_SIZE = 100 DIDs.
  • Resolves them in parallel chunks of CONCURRENT = 10 with a CHUNK_GAP_MS = 50 pause between chunks (politeness to plc.directory).
  • did:web:* DIDs are inferred directly from the DID string — no HTTP needed.
  • did:plc:* DIDs hit https://plc.directory/{did} and parse services.atproto_pds.endpoint.

Throughput arithmetic:

10 concurrent × ~100 ms/fetch ≈ 100 ms/chunk
100 DIDs / 10 per chunk × ~100 ms + 50 ms gaps ≈ ~1 s/run
Running every 2 s → ~50 DIDs/s → ~4.3 M DIDs/day

Fills the active-user cache within a few hours on a fresh deploy.

DB persistence:

  • did_pds_cache — persisted DID → PDS mappings, loaded on startup (last 7 days only to keep warm-up fast), pruned monthly (entries older than 30 days).
  • collection_pds — per-collection PDS attribution counts, queried by the web frontend to show "N PDSs" per domain card.

Bootstrap escape hatch: if fill time is unacceptable after a data wipe, https://plc.directory/export streams newline-delimited JSON of all PLC operations ever. A one-time script processing this can populate did_pds_cache in full before the relay starts taking live traffic.

Batch upsert pattern #

Both batchUpsert (collection stats) and batchUpsertPds (PDS attribution) follow the same pattern:

  1. Aggregate duplicates locally into a Map — reduces DB round-trips.
  2. Build a single multi-row INSERT ... ON CONFLICT DO UPDATE — one query per flush regardless of how many distinct names/pairs were seen.
  3. On error, restore the buffer so events are not silently dropped.

Web frontend #

React Router 7 SSR app. All UI state is URL-driven (no client-side state for filters, sort, pagination, view mode).

Domain grouping #

Collections are grouped by their two-segment reverse-domain prefix: split_part(name, '.', 1) || '.' || split_part(name, '.', 2)

e.g. social.attic.post → domain social.attic → URL https://attic.social

Query pattern #

The main loader uses a CTE with window functions to avoid separate count queries:

  • COUNT(*) OVER () — total domain count (replaces a separate COUNT(*) query)
  • MAX(total_count) OVER () — global max for bar calibration

A second parallel query fetches all collections for the visible domain cards via = ANY($domains_array) in a single round-trip.

PDS counts are fetched as a third parallel query on collection_pds.

URL param defaults (clean URL convention) #

Param Default (omitted) Non-default
sort first_seen last_seen, count
dir desc asc
hide_bsky on (hidden) hide_bsky=0 to show
view list view=grid