Some checks failed
ci/crow/cron/weekly-audit-missing/2 Pipeline was successful
ci/crow/cron/auto-apply-patches Pipeline failed
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/7 Pipeline was successful
ci/crow/cron/weekly-audit-missing/6 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/8 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/10 Pipeline was successful
ci/crow/cron/trial-build-registry/6 Pipeline was successful
ci/crow/cron/trial-build-registry/12 Pipeline was successful
ci/crow/cron/trial-build-registry/13 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/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/15 Pipeline was successful
ci/crow/cron/weekly-rebuild-reindex/6 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/6 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/22 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-missing/5 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-missing/2 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/23 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/24 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/20 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/30 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/14 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/28 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/29 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/1 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/35 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/33 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/37 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/34 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-reindex/11 Pipeline was successful
ci/crow/cron/weekly-rebuild-missing/38 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/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
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
139 lines
5.1 KiB
TypeScript
139 lines
5.1 KiB
TypeScript
/**
|
|
* 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()];
|
|
}
|