devxy/build-cran-binaries
Watch
1
0
Fork
You've already forked build-cran-binaries
0
build-cran-binaries/stats/README.md
pat-s fa1e8e8428
Some checks failed
ci/crow/cron/auto-apply-patches Pipeline failed
ci/crow/cron/weekly-audit-missing/2 Pipeline was successful
ci/crow/cron/weekly-audit-missing/1 Pipeline was successful
ci/crow/cron/weekly-audit-missing/4 Pipeline was successful
ci/crow/cron/weekly-audit-missing/5 Pipeline was successful
ci/crow/cron/weekly-audit-missing/6 Pipeline was successful
ci/crow/cron/weekly-audit-missing/7 Pipeline was successful
ci/crow/cron/weekly-audit-missing/8 Pipeline was successful
ci/crow/cron/weekly-audit-missing/9 Pipeline was successful
ci/crow/cron/weekly-audit-missing/10 Pipeline was successful
ci/crow/cron/weekly-audit-missing/3 Pipeline was successful
ci/crow/cron/weekly-audit-missing/11 Pipeline was successful
ci/crow/cron/weekly-audit-missing/12 Pipeline was successful
ci/crow/cron/weekly-audit-missing/13 Pipeline was successful
ci/crow/cron/weekly-audit-missing/14 Pipeline was successful
ci/crow/cron/trial-build-registry/10 Pipeline was successful
ci/crow/cron/trial-build-registry/6 Pipeline was successful
ci/crow/cron/trial-build-registry/8 Pipeline was successful
ci/crow/cron/trial-build-registry/14 Pipeline was successful
ci/crow/cron/trial-build-registry/2 Pipeline was successful
ci/crow/cron/trial-build-registry/12 Pipeline was successful
ci/crow/cron/trial-build-registry/16 Pipeline failed
ci/crow/cron/trial-build-registry/4 Pipeline failed
ci/crow/cron/trial-build-registry/1 Pipeline was successful
ci/crow/cron/trial-build-registry/3 Pipeline was successful
ci/crow/cron/trial-build-registry/5 Pipeline was successful
ci/crow/cron/trial-build-registry/7 Pipeline was successful
ci/crow/cron/trial-build-registry/9 Pipeline was successful
ci/crow/cron/trial-build-registry/11 Pipeline was successful
ci/crow/cron/trial-build-registry/13 Pipeline was successful
ci/crow/cron/trial-build-registry/15 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/17 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/13 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/18 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/3 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/16 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/6 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/2 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/6 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/23 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/24 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/5 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/15 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/4 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/22 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/14 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/8 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/2 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/5 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/19 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/30 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/20 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/1 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/28 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/29 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/10 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/1 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/36 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/21 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/34 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/35 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/7 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/12 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/25 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/26 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/27 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/41 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/9 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/40 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/42 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/14 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/31 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/32 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/33 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/11 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/37 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/38 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/39 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/13 Pipeline was successful
ci/crow/cron/process-updates/2 Pipeline was successful
ci/crow/cron/process-updates/8 Pipeline was successful
ci/crow/cron/process-updates/11 Pipeline was successful
ci/crow/cron/process-updates/12 Pipeline was successful
ci/crow/cron/process-updates/16 Pipeline was successful
ci/crow/cron/process-updates/9 Pipeline was successful
ci/crow/cron/process-updates/13 Pipeline was successful
ci/crow/cron/process-updates/10 Pipeline was successful
ci/crow/cron/process-updates/15 Pipeline was successful
ci/crow/cron/process-updates/14 Pipeline was successful
ci/crow/cron/process-updates/4 Pipeline was successful
ci/crow/cron/process-updates/3 Pipeline was successful
ci/crow/cron/process-updates/5 Pipeline was successful
ci/crow/cron/process-updates/6 Pipeline was successful
ci/crow/cron/download-stats Pipeline was successful
ci/crow/cron/process-updates/7 Pipeline was successful
ci/crow/cron/process-updates/1 Pipeline was successful
feat(stats): publish per-package download statistics (#253)
Adds the data for the package view of the rpkgs.com download dashboard (rpkgs/website#98).

- `stats/packages.ts` writes `stats/packages/<name>.json` per package: daily downloads and clients, latest version, 30-day rank, and versions, distributions, R versions and architectures for 7 days, 30 days and all time.
- `downloads.json` gains `packages` and `bytes` per window and `breakdownsByWindow`; `breakdowns` stays as the 30-day set, so the current page keeps working.
- Package files are uploaded before the main file and purged with one wildcard purge (`/stats/packages/*`, checked against the Bunny API).

Same counting rules as before: own runners excluded, a download is a distinct client, package version and UTC day.

## Verification

- `bun test stats`: 16 tests pass, including version ordering.
- `publish.ts --out` against the live database wrote the main file and 106 package files; `cli` matches a direct SQL count (5 downloads, 2 clients, 3.6.6).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed-on: #253
2026-11-14 13:12:20 +00:00

4.4 KiB

Download statistics

This directory turns the cran.rpkgs.com CDN access logs into the public download numbers on rpkgs.com/stats. The download-stats pipeline runs it every hour.

Data flow

  1. collect.ts reads the last 70 hours of access logs from the Bunny Logging API v2 and inserts each request into download_stats.events in the build metadata database.
  2. classify.ts derives what each request was (package download, index fetch, redirect, probe, noise) from its path, status and User-Agent.
  3. publish.ts aggregates the events into stats/downloads.json in the binaries bucket and purges its CDN copy, so it is served at https://cran.rpkgs.com/stats/downloads.json.
  4. packages.ts writes one file per package, stats/packages/<name>.json, with its daily series and its versions, distributions, R versions and architectures for each window. The dashboard fetches one only when that package is opened, so the main file does not grow with the number of packages.

Bunny keeps access logs for three days. Each hourly run re-reads almost all of that window, and events are keyed by Bunny's request id, so re-reading never double counts and up to two days of failed runs lose nothing. Every run is recorded in download_stats.runs, and the dashboard marks any hour that no run covered.

Privacy

Bunny anonymizes IPs at the edge to /24 (IPv4) or /64 (IPv6) before they reach the logs. Not even that prefix is stored: each event keeps only client, an HMAC (keyed with STATS_CLIENT_SALT) of the prefix, the network (ASN) and the client platform from the User-Agent. The public JSON contains counts only, never a client hash, IP or User-Agent. Countries seen from fewer than three clients are folded into "other".

What counts

Metric Definition
Download A distinct (client, package, version) served on one UTC day. Raw tarball requests are reported separately, because one CI job can re-download the same package many times a day.
Client A distinct client hash that fetched an index or a package. Several machines behind one /24 count once, and one laptop on two networks counts twice, so this is an estimate.
Client-day A client active on a given day, roughly one install session.

The same R session seen with and without renv's User-Agent prefix counts as one client, since clientPlatform() keys on the R part of the User-Agent.

What is separated or excluded

  • Cloud and CI networks are kept but reported separately: Microsoft/GitHub, AWS, Google, Hetzner, OVH and other hosting ASNs, plus any network whose name says hosting, cloud or data centre.
  • Our own CI runners (STATS_OWN_NETWORKS) are excluded from every public number and only counted as excluded requests.
  • Internal redirects (minor_routing, slot_resolution, https) are not counted, only the request they lead to.
  • Redirects away from us are counted as their own metrics: unsupported_r_minor (demand for R versions we do not build), archive_fallback (old versions we have no binary for), unknown_distro and macos.
  • Probes: renv and pak request __api__/status to check for Posit Package Manager.
  • Noise: vulnerability scanners, crawlers and anything outside the repository layout.

Changing the rules

Change classify.ts, add a test case to classify.test.ts and bump RULESET. Then recompute every stored event:

bun stats/collect.ts --reclassify

The raw path, status, User-Agent and network are stored for exactly this reason. Whether a request came from our own runners is decided at ingest, so changing STATS_OWN_NETWORKS only affects new events.

Running locally

The secrets are in OpenBao: the Bunny key at development/agent-dev/bunny, the database password at development/agent-dev/rpkgs-metadata-db and the salt and own networks at development/agent-dev/rpkgs-download-stats.

bun test stats
bun stats/collect.ts
# write the JSON locally instead of uploading it
bun stats/publish.ts --out downloads.json