devxy/build-cran-binaries
Watch
1
0
Fork
You've already forked build-cran-binaries
0

feat(stats): publish per-package download statistics

Write one JSON file per package to stats/packages/<name>.json with its
daily series and its versions, distributions, R versions and
architectures for 7 days, 30 days and all time, for the package view
of the rpkgs.com dashboard.

- Add distinct package and byte counts to each window in downloads.json.
- Add breakdowns for every window as breakdownsByWindow, keeping
  breakdowns as the 30-day set for existing readers.
- Upload package files before the main file and purge them with one
  wildcard purge.
This commit is contained in:
Patrick Schratz 2026-11-14 13:29:58 +01:00
commit 06afded1fe

139
stats/packages.ts Normal file
View file

@ -0,0 +1,139 @@
/**
* Per-package statistics, one file per package at `stats/packages/<name>.json`.
*
* Kept out of `downloads.json` so the main file stays small however many
* packages are served: the dashboard only fetches a package's file when that
* package is opened.
*
* The same rules as the repository numbers apply: our own runners are left out,
* and a download is a distinct (client, package, version) on one UTC day.
*/
import type { SQL } from 'bun';
import type { Breakdown } from './publish';
export interface PackageWindow {
downloads: number;
clients: number;
versions: Breakdown[];
os: Breakdown[];
rMinor: Breakdown[];
arch: Breakdown[];
}
export interface PackageFile {
schema: 1;
name: string;
generatedAt: string;
/** Highest version seen downloaded, by `numeric_version` ordering. */
latestVersion: string | null;
/** Position by downloads in the last 30 days, 1-based; null without downloads in that window. */
rank30d: number | null;
firstDay: string | null;
daily: { date: string; downloads: number; clients: number }[];
windows: { last7: PackageWindow; last30: PackageWindow; all: PackageWindow };
}
const WINDOWS = { last7: 7, last30: 30, all: null } as const;
type Dimension = 'versions' | 'os' | 'rMinor' | 'arch';
export function compareVersions(a: string, b: string): number {
const pa = a.split(/[.-]/).map(Number);
const pb = b.split(/[.-]/).map(Number);
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
if (d !== 0) return d;
}
return 0;
}
const emptyWindow = (): PackageWindow => ({ downloads: 0, clients: 0, versions: [], os: [], rMinor: [], arch: [] });
export async function buildPackageFiles(sql: SQL, generatedAt: string): Promise<PackageFile[]> {
const files = new Map<string, PackageFile>();
const file = (name: string): PackageFile => {
let f = files.get(name);
if (!f) {
f = {
schema: 1,
name,
generatedAt,
latestVersion: null,
rank30d: null,
firstDay: null,
daily: [],
windows: { last7: emptyWindow(), last30: emptyWindow(), all: emptyWindow() },
};
files.set(name, f);
}
return f;
};
const daily: { package: string; date: string; downloads: number; clients: number }[] = await sql`
with dl as (
select distinct client, package, version, os, r_minor, arch, (ts at time zone 'UTC')::date as day
from download_stats.events
where network <> 'own' and outcome = 'served' and kind = 'download' and package is not null
)
select package, day::text as date, count(distinct (client, version))::int as downloads,
count(distinct client)::int as clients
from dl group by package, day order by package, day`;
for (const row of daily) {
const f = file(row.package);
f.daily.push({ date: row.date, downloads: row.downloads, clients: row.clients });
f.firstDay ??= row.date;
}
for (const [windowId, days] of Object.entries(WINDOWS) as [keyof typeof WINDOWS, number | null][]) {
const since = days === null ? new Date(0) : new Date(Date.now() - days * 86400000);
const rows: {
package: string;
dimension: Dimension | null;
key: string | null;
downloads: number;
clients: number;
}[] = await sql`
with dl as (
select distinct client, package, version, os, r_minor, arch, (ts at time zone 'UTC')::date as day
from download_stats.events
where network <> 'own' and outcome = 'served' and kind = 'download' and package is not null
and ts >= ${since}
)
select package,
case
when grouping(version) = 0 then 'versions'
when grouping(os) = 0 then 'os'
when grouping(r_minor) = 0 then 'rMinor'
when grouping(arch) = 0 then 'arch'
end as dimension,
coalesce(version, os, r_minor, arch) as key,
count(distinct (client, version, day))::int as downloads,
count(distinct client)::int as clients
from dl
group by package, grouping sets ((), (version), (os), (r_minor), (arch))`;
for (const row of rows) {
const w = file(row.package).windows[windowId];
if (row.dimension === null) {
w.downloads = row.downloads;
w.clients = row.clients;
} else {
w[row.dimension].push({ key: row.key ?? 'unknown', clients: row.clients, downloads: row.downloads });
}
}
}
const ranked = [...files.values()]
.filter((f) => f.windows.last30.downloads > 0)
.sort((a, b) => b.windows.last30.downloads - a.windows.last30.downloads || a.name.localeCompare(b.name));
ranked.forEach((f, i) => (f.rank30d = i + 1));
for (const f of files.values()) {
const versions = f.windows.all.versions.map((v) => v.key).filter((v) => v !== 'unknown');
f.latestVersion = versions.sort(compareVersions).at(-1) ?? null;
for (const w of Object.values(f.windows)) {
w.versions.sort((a, b) => compareVersions(b.key, a.key));
for (const dim of ['os', 'rMinor', 'arch'] as const) w[dim].sort((a, b) => b.clients - a.clients);
}
}
return [...files.values()];
}