x12i-downloads 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +72 -57
  2. package/app/admin/layout.tsx +11 -0
  3. package/app/admin/page.tsx +362 -0
  4. package/app/globals.css +201 -26
  5. package/app/page.tsx +9 -18
  6. package/components/downloads-dashboard.tsx +6 -47
  7. package/lib/admin-overview.ts +148 -0
  8. package/lib/insights.mjs +524 -0
  9. package/lib/npm-client.ts +199 -0
  10. package/lib/npm-stats-live.ts +440 -425
  11. package/lib/npm-stats-shared.ts +21 -0
  12. package/lib/sqlite-d1.mjs +50 -0
  13. package/migrations/0001_stats_store.sql +22 -0
  14. package/migrations/0002_known_packages.sql +10 -0
  15. package/migrations/0003_package_daily_by_day.sql +2 -0
  16. package/package.json +85 -77
  17. package/public/x12i-stats.json +113 -17
  18. package/scripts/remote-d1.mjs +43 -0
  19. package/scripts/serve-local.mjs +134 -0
  20. package/scripts/stats-local.mjs +184 -0
  21. package/scripts/stats-report.mjs +68 -0
  22. package/tests/admin-overview.test.mjs +74 -0
  23. package/tests/insights.test.mjs +133 -0
  24. package/tests/stats-refresh.test.mjs +296 -0
  25. package/tests/worker-cron-auth.test.mjs +5 -8
  26. package/tsconfig.json +1 -0
  27. package/vite.config.ts +24 -4
  28. package/worker/index.ts +61 -177
  29. package/wrangler.jsonc +12 -10
  30. package/.vinext/fonts/geist-8ac0455e797f/geist-001175b1.woff2 +0 -0
  31. package/.vinext/fonts/geist-8ac0455e797f/geist-52306abf.woff2 +0 -0
  32. package/.vinext/fonts/geist-8ac0455e797f/geist-875ccdd4.woff2 +0 -0
  33. package/.vinext/fonts/geist-8ac0455e797f/geist-98bbbccb.woff2 +0 -0
  34. package/.vinext/fonts/geist-8ac0455e797f/geist-ff2310f5.woff2 +0 -0
  35. package/.vinext/fonts/geist-8ac0455e797f/style.css +0 -45
  36. package/.vinext/fonts/geist-mono-00e989178794/geist-mono-013b2f2f.woff2 +0 -0
  37. package/.vinext/fonts/geist-mono-00e989178794/geist-mono-0638449e.woff2 +0 -0
  38. package/.vinext/fonts/geist-mono-00e989178794/geist-mono-44745446.woff2 +0 -0
  39. package/.vinext/fonts/geist-mono-00e989178794/geist-mono-44e03052.woff2 +0 -0
  40. package/.vinext/fonts/geist-mono-00e989178794/geist-mono-971fb274.woff2 +0 -0
  41. package/.vinext/fonts/geist-mono-00e989178794/geist-mono-f6b33328.woff2 +0 -0
  42. package/.vinext/fonts/geist-mono-00e989178794/style.css +0 -54
  43. package/app/api/cron/update-stats/route.ts +0 -23
  44. package/scripts/update-stats.mjs +0 -258
package/README.md CHANGED
@@ -1,91 +1,106 @@
1
1
  # @x12i Total Downloads
2
2
 
3
- A React dashboard that discovers every public package in the `@x12i` npm scope and calculates cumulative downloads since March 20, 2026.
3
+ A React dashboard that counts cumulative npm downloads of every package the `@x12i` org has ever published, since March 20, 2026.
4
4
 
5
- Live: https://x12i-downloads.ami-3ad.workers.dev
5
+ Live: https://stats.x12i.com (also https://x12i-downloads.ami-3ad.workers.dev)
6
6
 
7
- ## What it does
7
+ ## How the numbers are kept
8
8
 
9
- - Discovers public `@x12i/*` packages anonymously through npm's public registry.
10
- - Filters npm's fuzzy search results to exact `@x12i/` package names.
11
- - Removes duplicates before calculating downloads.
12
- - Serves a generated verified snapshot immediately, then upgrades to the live Worker-computed stats when they are fresher.
13
- - Refreshes live stats on Cloudflare in bounded batches (free-plan subrequest limits) persisted to KV.
14
- - Aggregates exact daily package ranges into cumulative 14-day chart points.
15
- - Verifies the chart total against the independently accumulated package total.
16
- - Replaces the published snapshot only after all package ranges load and the integrity check passes.
17
- - Omits all horizontal-axis labels as requested.
9
+ - Stats live in a SQLite database: Cloudflare D1 in production, `.stats-cache/stats.sqlite` locally. Both run the same code (`lib/npm-stats-live.ts`).
10
+ - `packages` remembers every package ever discovered. Each run counts the union of known packages and npm's current org listing, so a package that leaves the listing keeps counting.
11
+ - `package_daily` stores per-package daily downloads. A stored day is only ever raised, never lowered or deleted, so the total cannot go down when npm revises or drops data.
12
+ - A new total is refused (the published one is held) if it would be lower; `stats_history` keeps one row per published day.
13
+ - npm publishes day D on D+1 at the earliest, sometimes stalls for days, and sometimes skips days for every package. Days npm reports as 0 for every package are estimated, through yesterday: each package gets the average of its last 7 days with downloads (within the 30 days before npm's last counted day), as an integer. "Skipped" days are ones npm moved past; "pending" days are after npm's latest counted day. Estimates are computed at publish time and never stored in `package_daily`, so npm's real counts replace them as soon as npm publishes or backfills those days. Because estimates skip zero days they run high, so when real counts arrive the total may be held for a while until real downloads catch up.
18
14
 
19
- ## Run locally
15
+ ## Refresh schedule
20
16
 
21
- Requirements: Node.js 22.13 or newer. On Windows, the `build`, `lint` and `test` scripts need Git Bash on `PATH`.
17
+ npm is checked once a day at 18:00 UTC with a single request ("what is your latest counted day?"). If npm has a newer day, a full calculation runs. If the check fails (npm errors, or still has no newer day), it is retried at the next 6-hour slot (00, 06, 12, 18 UTC), and so on until it succeeds; then it goes back to once a day. A check that finds no newer day still re-publishes the total with estimates through yesterday (no extra npm requests). No check runs while the data already includes yesterday.
18
+
19
+ A full crawl is ~460 npm requests. The Worker cron fires every 2 minutes, but only during 00, 06, 12 and 18 UTC (the hours a check can be due), and a tick only does work while a check is due or a calculation is in progress, crawling ~40 packages per tick (free-plan subrequest cap, npm rate limits). Idle ticks cost a single D1 row read. `npm run update:stats` locally asks npm once, immediately, and recalculates only if npm has a newer day.
20
+
21
+ ## Local first, then Cloudflare
22
+
23
+ Requirements: Node.js 22.18 or newer. On Windows, `build`, `lint` and `test` need Git Bash on `PATH`.
22
24
 
23
25
  ```bash
24
26
  npm install
25
- npm run dev
27
+ npm run local # fully local: build, calculate, then serve the site + /admin on http://localhost:8788
28
+ npm run local:serve # serve only (uses the existing build and .stats-cache/stats.sqlite); --no-cron to skip the local cron
29
+ npm run update:stats # ask npm once; recalculate locally if it has a newer day (--force to redo), writes public/x12i-stats.json
30
+ npm run stats:push # same, then merge into remote D1 (counts only rise; refuses if remote is higher)
31
+ npm run deploy # build + wrangler deploy (ships the refreshed snapshot)
26
32
  ```
27
33
 
28
- Open the local URL printed by the development server.
34
+ Schema changes:
29
35
 
30
- ## Configuration
36
+ ```bash
37
+ npx wrangler d1 migrations apply x12i-stats --remote --config wrangler.jsonc
38
+ ```
31
39
 
32
- All values are optional; defaults are shown. Set them in `.env` (ignored by git).
40
+ ## Internal insights (not shown in the UI)
33
41
 
34
- | Variable | Default |
35
- | --- | --- |
36
- | `NEXT_PUBLIC_BRAND_NAME` | `x12i` |
37
- | `NEXT_PUBLIC_PACKAGE_SCOPE` | `@x12i/` |
38
- | `NEXT_PUBLIC_NPM_PACKAGE_URL` | `https://www.npmjs.com/package/x12i-downloads` |
39
- | `NEXT_PUBLIC_START_DATE` | `2026-03-20` |
42
+ ```bash
43
+ npm run stats:report # report from the local database -> reports/stats-report-<day>.md
44
+ npm run stats:report -- --remote # same, from production D1
45
+ npm run stats:report -- --json # also write the raw report data as JSON
46
+ npm run stats:report -- --package @x12i/logxer # one package's detail as JSON
47
+ ```
40
48
 
41
- Changing the scope or start date invalidates the per-package KV cache, so the next live refresh does a full cold fetch.
49
+ The report covers leading packages (all time, 30 and 7 days), concentration, weekly risers and fallers, and anomalies. Anomalies include npm data gaps, org-wide spike and sweep days, package spikes scored against a 28-day robust baseline, and packages that went quiet. It also covers lifecycle: new, dormant and never-downloaded packages. It ends with a table of every package (all time, 30 and 7 days, first and last download), which is also written to `reports/packages-<day>.csv`. The script checks that the per-package totals add up to the npm-counted part of the published total.
42
50
 
43
- ## Production build and tests
51
+ The same analytics are exported as an SDK that works on any D1-compatible database (a Worker's `env.DB`, or local SQLite):
44
52
 
45
- ```bash
46
- npm run build
47
- npm test
53
+ ```js
54
+ import { loadStats, allPackages, topPackages, movers, findAnomalies, packageDetail } from "x12i-downloads/insights";
55
+ import { createSqliteD1 } from "x12i-downloads/sqlite";
56
+
57
+ const data = await loadStats(createSqliteD1(".stats-cache/stats.sqlite"));
58
+ topPackages(data, { window: 30, limit: 10 });
59
+ findAnomalies(data, { minScore: 5, minDownloads: 25 });
48
60
  ```
49
61
 
50
- ## Deploy to Cloudflare
62
+ ## Worker endpoints
51
63
 
52
- ```bash
53
- npx wrangler login
54
- npm run deploy
55
- ```
64
+ - `GET /api/stats` — latest published result (falls back to the baked snapshot before D1 has one).
65
+ - `GET /api/cron/update-status` — current job, latest result and recent history.
66
+ - `POST /api/cron/update-stats` — run one tick now; requires `Authorization: Bearer <CRON_SECRET>` when the secret is set, and always for `?force=1`.
67
+ - `GET /api/admin/overview` — data for the admin view.
56
68
 
57
- The Worker (`worker/index.ts`) serves the app and owns the live stats pipeline:
69
+ ## Admin view
58
70
 
59
- - A cron trigger runs every 10 minutes. Each tick refreshes one batch of packages; once the day's refresh completes, remaining ticks exit after two KV reads.
60
- - `GET /api/stats` returns the latest KV result (falls back to the baked snapshot).
61
- - `GET /api/cron/update-status` shows the current job and freshness.
62
- - `POST /api/cron/update-stats` starts a batch. Set a secret to require `Authorization: Bearer <secret>`; `?force=1` always requires it:
71
+ `/admin` (not linked from the dashboard, not indexed) shows everything known about the latest day and week: published total split into npm-counted and estimated, npm's latest counted day and lag, the refresh job (last and next npm check, notes, errors), the last 14 days with their source (npm or estimate), every estimated day, new packages, all packages ranked by the week (latest day, week, previous week, all time, last download), and the publish history. The public dashboard shows only the total and chart.
63
72
 
64
- ```bash
65
- npx wrangler secret put CRON_SECRET
66
- ```
73
+ ## Configuration
74
+
75
+ All values are optional; defaults are shown. Set them in `.env` (ignored by git).
67
76
 
68
- ## Refresh the baked snapshot
77
+ | Variable | Default |
78
+ | --- | --- |
79
+ | `NEXT_PUBLIC_BRAND_NAME` | `x12i` |
80
+ | `NEXT_PUBLIC_PACKAGE_SCOPE` | `@x12i/` |
81
+ | `NEXT_PUBLIC_NPM_PACKAGE_URL` | `https://www.npmjs.com/package/x12i-downloads` |
82
+ | `NEXT_PUBLIC_START_DATE` | `2026-03-20` |
69
83
 
70
- Run this before publishing a new snapshot or from a scheduled workflow:
84
+ ## Tests
71
85
 
72
86
  ```bash
73
- npm run update:stats
87
+ npm test
74
88
  ```
75
89
 
76
- The updater discovers all current public packages, downloads every daily range, verifies the aggregate, and atomically replaces `public/x12i-stats.json` only after success. Daily package-range work is resumable through an ignored local cache, so an interrupted or throttled run continues instead of starting over.
90
+ `tests/insights.test.mjs` checks the analytics on a synthetic org with known leaders, spikes, a sweep, an npm gap and quiet/new/unused packages. `tests/stats-refresh.test.mjs` runs the refresh pipeline against in-memory SQLite and a fake npm (budgeted ticks, once-a-day scheduling with 6-hour retry slots, estimates for days npm reports as 0, counts only rising, removed/new packages, rate limits, failing packages, refusal to publish lower totals).
77
91
 
78
92
  ## Main source files
79
93
 
80
- - `app/page.tsx` — page entry point; loads the baked snapshot, then live stats
81
- - `components/downloads-dashboard.tsx` — dashboard and empty-state UI
82
- - `components/cumulative-chart.tsx` — responsive accessible SVG chart
83
- - `lib/config.ts` — environment-driven configuration
84
- - `lib/npm-stats-live.ts` — batched, KV-backed live refresh used by the Worker
94
+ - `lib/npm-stats-live.ts` — refresh state machine and SQL (shared by Worker and local runner)
95
+ - `lib/npm-client.ts` — npm registry / downloads API client with fetch budget and retries
96
+ - `migrations/` — D1 / SQLite schema
97
+ - `lib/insights.mjs` — analytics SDK (leaders, movers, concentration, anomalies, lifecycle, report rendering)
98
+ - `lib/sqlite-d1.mjs` — D1-compatible adapter over `node:sqlite`
99
+ - `scripts/stats-local.mjs` — local runner, snapshot writer and D1 push
100
+ - `scripts/stats-report.mjs` — internal report generator
101
+ - `scripts/serve-local.mjs` — runs the built Worker locally against `.stats-cache/stats.sqlite`, with the production cron schedule
102
+ - `scripts/remote-d1.mjs` — wrangler helpers for production D1 (query, push, pull)
85
103
  - `worker/index.ts` — Cloudflare Worker entry, cron and stats endpoints
86
- - `public/x12i-stats.json` — generated verified snapshot rendered by React
87
- - `scripts/update-stats.mjs` — anonymous npm discovery and snapshot calculation
88
- - `lib/npm-stats-shared.ts` — shared result types and runtime validation
89
- - `app/globals.css` — responsive visual styling
90
-
91
- No npm login, token or database is required to run the dashboard.
104
+ - `lib/admin-overview.ts` — data for the admin view
105
+ - `app/page.tsx`, `components/` — dashboard UI; `app/admin/` — admin view
106
+ - `public/x12i-stats.json` — baked snapshot shown before live stats load
@@ -0,0 +1,11 @@
1
+ import type { Metadata } from "next";
2
+ import { PACKAGE_SCOPE } from "@/lib/config";
3
+
4
+ export const metadata: Metadata = {
5
+ title: `${PACKAGE_SCOPE.replace(/\/$/, "")} downloads · admin`,
6
+ robots: { index: false, follow: false },
7
+ };
8
+
9
+ export default function AdminLayout({ children }: Readonly<{ children: React.ReactNode }>) {
10
+ return children;
11
+ }
@@ -0,0 +1,362 @@
1
+ "use client";
2
+
3
+ import { useCallback, useEffect, useMemo, useState } from "react";
4
+ import type { AdminDayRow, AdminOverview, AdminPackageRow } from "@/lib/admin-overview";
5
+ import { PACKAGE_SCOPE } from "@/lib/config";
6
+
7
+ const TOP_WEEKLY = 15;
8
+ const number = new Intl.NumberFormat("en-US");
9
+ const dayFormat = new Intl.DateTimeFormat("en-US", { weekday: "short", month: "short", day: "numeric", timeZone: "UTC" });
10
+ const timeFormat = new Intl.DateTimeFormat("en-US", {
11
+ month: "short",
12
+ day: "numeric",
13
+ hour: "2-digit",
14
+ minute: "2-digit",
15
+ hour12: false,
16
+ timeZone: "UTC",
17
+ });
18
+
19
+ const fmt = (n: number | null | undefined) => (n === null || n === undefined ? "—" : number.format(n));
20
+ const fmtDay = (day: string | null | undefined) => (day ? dayFormat.format(new Date(`${day}T00:00:00Z`)) : "—");
21
+
22
+ function fmtTime(iso: string | null | undefined, now: number): string {
23
+ if (!iso) return "—";
24
+ const minutes = Math.round((Date.parse(iso) - now) / 60_000);
25
+ const abs = Math.abs(minutes);
26
+ const span = abs < 60 ? `${abs}m` : abs < 48 * 60 ? `${Math.round(abs / 60)}h` : `${Math.round(abs / 1440)}d`;
27
+ return `${timeFormat.format(new Date(iso))} UTC (${minutes >= 0 ? `in ${span}` : `${span} ago`})`;
28
+ }
29
+
30
+ function change(current: number, previous: number): string {
31
+ if (previous === 0) return current > 0 ? "new" : "—";
32
+ const pct = Math.round(((current - previous) / previous) * 100);
33
+ return `${pct > 0 ? "+" : ""}${pct}%`;
34
+ }
35
+
36
+ const SOURCE_LABEL: Record<AdminDayRow["source"], string> = {
37
+ npm: "npm",
38
+ skipped: "estimate · npm skipped",
39
+ pending: "estimate · npm pending",
40
+ };
41
+
42
+ const OUTCOME_LABEL: Record<string, string> = {
43
+ "new-day": "npm had a new day",
44
+ "no-new-day": "npm had nothing new",
45
+ error: "check failed",
46
+ };
47
+
48
+ function Card({ label, value, detail }: { label: string; value: string; detail?: string }) {
49
+ return (
50
+ <div className="admin-card">
51
+ <div className="admin-card-label">{label}</div>
52
+ <div className="admin-card-value">{value}</div>
53
+ {detail && <div className="admin-card-detail">{detail}</div>}
54
+ </div>
55
+ );
56
+ }
57
+
58
+ function PackagesTable({ rows, filter }: { rows: AdminPackageRow[]; filter: string }) {
59
+ const visible = filter ? rows.filter((r) => r.name.toLowerCase().includes(filter.toLowerCase())) : rows;
60
+ return (
61
+ <div className="admin-table-wrap admin-table-tall">
62
+ <table className="admin-table">
63
+ <thead>
64
+ <tr>
65
+ <th>#</th>
66
+ <th>Package</th>
67
+ <th className="num">Latest day</th>
68
+ <th className="num">Week</th>
69
+ <th className="num">Previous week</th>
70
+ <th className="num">Change</th>
71
+ <th className="num">All time</th>
72
+ <th>First download</th>
73
+ <th>Last download</th>
74
+ </tr>
75
+ </thead>
76
+ <tbody>
77
+ {visible.map((r) => (
78
+ <tr key={r.name}>
79
+ <td className="muted">{rows.indexOf(r) + 1}</td>
80
+ <td className="mono">{r.name}</td>
81
+ <td className="num">{fmt(r.latestDay)}</td>
82
+ <td className="num strong">{fmt(r.week)}</td>
83
+ <td className="num">{fmt(r.previousWeek)}</td>
84
+ <td className="num">{change(r.week, r.previousWeek)}</td>
85
+ <td className="num">{fmt(r.allTime)}</td>
86
+ <td>{fmtDay(r.firstDownload)}</td>
87
+ <td>{fmtDay(r.lastDownload)}</td>
88
+ </tr>
89
+ ))}
90
+ </tbody>
91
+ </table>
92
+ </div>
93
+ );
94
+ }
95
+
96
+ export default function AdminPage() {
97
+ const [data, setData] = useState<AdminOverview | null>(null);
98
+ const [error, setError] = useState<string | null>(null);
99
+ const [filter, setFilter] = useState("");
100
+
101
+ const load = useCallback(async () => {
102
+ try {
103
+ const res = await fetch("/api/admin/overview", { cache: "no-store" });
104
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
105
+ setData((await res.json()) as AdminOverview);
106
+ setError(null);
107
+ } catch (e) {
108
+ setError(e instanceof Error ? e.message : String(e));
109
+ }
110
+ }, []);
111
+
112
+ useEffect(() => {
113
+ load();
114
+ }, [load]);
115
+
116
+ const derived = useMemo(() => {
117
+ if (!data) return null;
118
+ const sum = (key: "latestDay" | "week" | "previousWeek") => data.packages.reduce((s, p) => s + p[key], 0);
119
+ const weekAgo = new Date(Date.parse(data.now) - 7 * 86_400_000).toISOString().slice(0, 10);
120
+ const startedOn = (p: AdminPackageRow) => p.firstDownload ?? p.firstSeen;
121
+ return {
122
+ latestDay: sum("latestDay"),
123
+ latestDayPackages: data.packages.filter((p) => p.latestDay > 0).length,
124
+ week: sum("week"),
125
+ previousWeek: sum("previousWeek"),
126
+ weekActive: data.packages.filter((p) => p.week > 0).length,
127
+ // data.packages is already sorted by weekly downloads.
128
+ topWeekly: data.packages.filter((p) => p.week > 0).slice(0, TOP_WEEKLY),
129
+ weekSkipped: data.week
130
+ ? data.days.filter((d) => d.source === "skipped" && d.day >= data.week!.from && d.day <= data.week!.to).length
131
+ : 0,
132
+ newPackages: data.packages
133
+ .filter((p) => startedOn(p) >= weekAgo)
134
+ .sort((a, b) => startedOn(b).localeCompare(startedOn(a)) || a.name.localeCompare(b.name)),
135
+ };
136
+ }, [data]);
137
+
138
+ const scope = PACKAGE_SCOPE.replace(/\/$/, "");
139
+ const now = data ? Date.parse(data.now) : Date.now();
140
+ const latest = data?.latest;
141
+ const job = data?.job;
142
+ const estimatedDays = latest?.estimated?.days ?? [];
143
+
144
+ return (
145
+ <main className="admin-shell">
146
+ <header className="admin-header">
147
+ <div>
148
+ <h1>{scope} downloads · admin</h1>
149
+ <p className="muted">{data ? `As of ${fmtTime(data.now, now)}` : "Loading…"}</p>
150
+ </div>
151
+ <button type="button" className="admin-button" onClick={load}>
152
+ Refresh
153
+ </button>
154
+ </header>
155
+
156
+ {error && <p className="admin-error">Could not load the overview: {error}</p>}
157
+
158
+ {data && derived && (
159
+ <>
160
+ <section className="admin-cards">
161
+ <Card
162
+ label="Published total"
163
+ value={fmt(latest?.total)}
164
+ detail={`npm counted ${fmt(latest?.countedTotal ?? latest?.total)} + estimated ${fmt(latest?.estimated?.total ?? 0)}`}
165
+ />
166
+ <Card
167
+ label="npm data through"
168
+ value={fmtDay(data.npm.dataThrough)}
169
+ detail={
170
+ data.npm.lagDays ? `${data.npm.lagDays} day${data.npm.lagDays === 1 ? "" : "s"} behind yesterday` : "up to date"
171
+ }
172
+ />
173
+ <Card label="Total includes days through" value={fmtDay(latest?.calculatedThrough)} detail={`updated ${fmtTime(latest?.updatedAt, now)}`} />
174
+ <Card label="Packages" value={fmt(latest?.packageCount ?? data.packages.length)} detail={`${derived.newPackages.length} new in the last 7 days`} />
175
+ <Card
176
+ label={`Latest npm day (${fmtDay(data.npm.dataThrough)})`}
177
+ value={fmt(derived.latestDay)}
178
+ detail={`${derived.latestDayPackages} packages downloaded`}
179
+ />
180
+ <Card
181
+ label={data.week ? `Week ${fmtDay(data.week.from)} – ${fmtDay(data.week.to)}` : "Week"}
182
+ value={fmt(derived.week)}
183
+ detail={`npm-counted only · ${change(derived.week, derived.previousWeek)} vs previous week (${fmt(derived.previousWeek)}) · ${derived.weekActive} packages${derived.weekSkipped ? ` · npm skipped ${derived.weekSkipped} of these days` : ""}`}
184
+ />
185
+ </section>
186
+
187
+ <section className="admin-section">
188
+ <h2>Top {TOP_WEEKLY} packages · weekly downloads</h2>
189
+ <p className="muted small">
190
+ {data.week
191
+ ? `Week ${fmtDay(data.week.from)} – ${fmtDay(data.week.to)} (npm's latest week) · ${fmt(derived.week)} downloads across all packages${derived.weekSkipped ? ` · npm skipped ${derived.weekSkipped} of these days` : ""}.`
192
+ : "No npm data yet."}
193
+ </p>
194
+ <div className="admin-table-wrap">
195
+ <table className="admin-table">
196
+ <thead>
197
+ <tr>
198
+ <th>#</th>
199
+ <th>Package</th>
200
+ <th className="num">Weekly downloads</th>
201
+ <th className="num">Share</th>
202
+ <th className="admin-share-col" aria-hidden="true" />
203
+ <th className="num">Previous week</th>
204
+ <th className="num">Change</th>
205
+ </tr>
206
+ </thead>
207
+ <tbody>
208
+ {derived.topWeekly.map((p, i) => (
209
+ <tr key={p.name}>
210
+ <td className="muted">{i + 1}</td>
211
+ <td className="mono">{p.name}</td>
212
+ <td className="num strong">{fmt(p.week)}</td>
213
+ <td className="num">{derived.week ? `${Math.round((p.week / derived.week) * 1000) / 10}%` : "—"}</td>
214
+ <td className="admin-share-col">
215
+ <div
216
+ className="admin-share-bar"
217
+ style={{ width: `${Math.max(2, (p.week / (derived.topWeekly[0]?.week || 1)) * 100)}%` }}
218
+ />
219
+ </td>
220
+ <td className="num">{fmt(p.previousWeek)}</td>
221
+ <td className="num">{change(p.week, p.previousWeek)}</td>
222
+ </tr>
223
+ ))}
224
+ </tbody>
225
+ </table>
226
+ </div>
227
+ </section>
228
+
229
+ <section className="admin-section">
230
+ <h2>Refresh job</h2>
231
+ <dl className="admin-facts">
232
+ <dt>Status</dt>
233
+ <dd>{job?.status === "running" ? `running (${job.cursor}/${job.packageCount} packages)` : (job?.status ?? "—")}</dd>
234
+ <dt>Last npm check</dt>
235
+ <dd>
236
+ {fmtTime(job?.lastCheckedAt, now)}
237
+ {job?.lastCheckOutcome && ` · ${OUTCOME_LABEL[job.lastCheckOutcome] ?? job.lastCheckOutcome}`}
238
+ </dd>
239
+ <dt>Next npm check</dt>
240
+ <dd>{job?.status === "running" ? "after the current run" : fmtTime(job?.nextCheckAt, now)}</dd>
241
+ <dt>Last full calculation</dt>
242
+ <dd>{fmtTime(job?.lastCompletedAt, now)}</dd>
243
+ <dt>Last run notes</dt>
244
+ <dd className="mono small">{job?.lastRunNotes ?? "—"}</dd>
245
+ <dt>Last error</dt>
246
+ <dd className={job?.lastError ? "admin-error-text" : ""}>{job?.lastError ?? "none"}</dd>
247
+ </dl>
248
+ </section>
249
+
250
+ <section className="admin-section">
251
+ <h2>Last 14 days</h2>
252
+ <div className="admin-table-wrap">
253
+ <table className="admin-table">
254
+ <thead>
255
+ <tr>
256
+ <th>Day</th>
257
+ <th className="num">Downloads</th>
258
+ <th className="num">Packages</th>
259
+ <th>Source</th>
260
+ </tr>
261
+ </thead>
262
+ <tbody>
263
+ {data.days.map((d) => (
264
+ <tr key={d.day} className={d.source === "npm" ? "" : "estimated"}>
265
+ <td>{fmtDay(d.day)}</td>
266
+ <td className="num strong">{fmt(d.downloads)}</td>
267
+ <td className="num">{fmt(d.packages)}</td>
268
+ <td>
269
+ <span className={`admin-badge ${d.source}`}>{SOURCE_LABEL[d.source]}</span>
270
+ </td>
271
+ </tr>
272
+ ))}
273
+ </tbody>
274
+ </table>
275
+ </div>
276
+ </section>
277
+
278
+ <section className="admin-section">
279
+ <h2>Estimated days ({estimatedDays.length})</h2>
280
+ <p className="muted small">
281
+ Days npm reports as 0 for every package. Each package is estimated as the average of its last 7 days
282
+ with downloads (within 30 days); npm&apos;s real counts replace these whenever npm publishes them.
283
+ </p>
284
+ <div className="admin-table-wrap">
285
+ <table className="admin-table">
286
+ <thead>
287
+ <tr>
288
+ <th>Day</th>
289
+ <th>Why</th>
290
+ <th className="num">Estimated downloads</th>
291
+ <th className="num">Packages</th>
292
+ </tr>
293
+ </thead>
294
+ <tbody>
295
+ {[...estimatedDays].reverse().map((d) => (
296
+ <tr key={d.day}>
297
+ <td>{fmtDay(d.day)}</td>
298
+ <td>{d.reason === "pending" ? "npm hasn't counted it yet" : "npm skipped it"}</td>
299
+ <td className="num strong">{fmt(d.downloads)}</td>
300
+ <td className="num">{fmt(d.packages)}</td>
301
+ </tr>
302
+ ))}
303
+ </tbody>
304
+ </table>
305
+ </div>
306
+ </section>
307
+
308
+ {derived.newPackages.length > 0 && (
309
+ <section className="admin-section">
310
+ <h2>New packages (last 7 days)</h2>
311
+ <p className="muted small">First download in the last 7 days, or discovered then and not downloaded yet.</p>
312
+ <p className="mono small">
313
+ {derived.newPackages
314
+ .map((p) => `${p.name} (${p.firstDownload ? `first download ${fmtDay(p.firstDownload)}` : `seen ${fmtDay(p.firstSeen)}`})`)
315
+ .join(", ")}
316
+ </p>
317
+ </section>
318
+ )}
319
+
320
+ <section className="admin-section">
321
+ <div className="admin-section-head">
322
+ <h2>Packages by week ({data.packages.length})</h2>
323
+ <input
324
+ className="admin-filter"
325
+ placeholder="Filter packages"
326
+ value={filter}
327
+ onChange={(e) => setFilter(e.target.value)}
328
+ />
329
+ </div>
330
+ <PackagesTable rows={data.packages} filter={filter} />
331
+ </section>
332
+
333
+ <section className="admin-section">
334
+ <h2>Published totals</h2>
335
+ <div className="admin-table-wrap">
336
+ <table className="admin-table">
337
+ <thead>
338
+ <tr>
339
+ <th>Through</th>
340
+ <th className="num">Total</th>
341
+ <th className="num">Packages</th>
342
+ <th>Computed</th>
343
+ </tr>
344
+ </thead>
345
+ <tbody>
346
+ {data.history.map((h) => (
347
+ <tr key={h.data_through}>
348
+ <td>{fmtDay(h.data_through)}</td>
349
+ <td className="num strong">{fmt(h.total)}</td>
350
+ <td className="num">{fmt(h.package_count)}</td>
351
+ <td>{fmtTime(h.computed_at, now)}</td>
352
+ </tr>
353
+ ))}
354
+ </tbody>
355
+ </table>
356
+ </div>
357
+ </section>
358
+ </>
359
+ )}
360
+ </main>
361
+ );
362
+ }