|
| 1 | +# Stats |
| 2 | + |
| 3 | +`StatsClient` (`src/app/stats/Client.tsx`) is the client-side view for the |
| 4 | +router metrics dashboard. It polls `/api/v1/stats` on a back-off schedule, |
| 5 | +displays live pair-count and pause/live status tiles, and lets users export a |
| 6 | +point-in-time snapshot as JSON or CSV. |
| 7 | + |
| 8 | +## Props |
| 9 | + |
| 10 | +`StatsClient` takes no props. It is the default export of `Client.tsx` and is |
| 11 | +rendered by the RSC wrapper `src/app/stats/page.tsx`. |
| 12 | + |
| 13 | +## States |
| 14 | + |
| 15 | +| State | Description | |
| 16 | +|-----------|---------------------------------------------------------------------------------| |
| 17 | +| **Loading** | `<Spinner label="Loading stats" />` and "Loading…" text inside the live region. | |
| 18 | +| **Error** | A styled card with `role="alert"` shows the error message, a note that retry is automatic, and a manual **Retry** button. | |
| 19 | +| **Empty** | `<EmptyState title="No stats available yet" …>` — API returned `totalPairs: 0`. | |
| 20 | +| **Success** | A `<dl>` grid of `<StatTile>` tiles (Pairs count, Live/Paused status), a freshness timestamp, and Download JSON / Download CSV buttons. | |
| 21 | + |
| 22 | +States are mutually exclusive within a single `aria-live="polite" aria-atomic="true"` section. |
| 23 | + |
| 24 | +## Polling |
| 25 | + |
| 26 | +Stats are refreshed automatically using `useBackoffInterval`: |
| 27 | + |
| 28 | +- On success: next poll fires after `POLL_MS` (5 s default). |
| 29 | +- On error: the delay doubles on each consecutive failure (10 s → 20 s → 40 s), capped at `MAX_POLL_MS` (60 s). |
| 30 | +- A successful response resets the failure counter and restores the 5 s cadence. |
| 31 | +- Unmounting the component cancels any pending poll timer. |
| 32 | + |
| 33 | +### Injecting timers for tests |
| 34 | + |
| 35 | +`useBackoffInterval` accepts optional `schedule` / `cancel` functions so tests |
| 36 | +can drive timing without real timers: |
| 37 | + |
| 38 | +```tsx |
| 39 | +useBackoffInterval(status, refetch, { |
| 40 | + baseMs: 5_000, |
| 41 | + maxMs: 60_000, |
| 42 | + schedule: (cb, ms) => setTimeout(cb, ms), |
| 43 | + cancel: clearTimeout, |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +## Freshness label |
| 48 | + |
| 49 | +A `<LastUpdated>` sub-component re-renders every second to display how long ago |
| 50 | +the last successful response was received (e.g. "just now", "12s ago"). The |
| 51 | +`formatStatsAge(deltaMs)` helper is exported for unit testing. |
| 52 | + |
| 53 | +## Export |
| 54 | + |
| 55 | +See [`docs/stats-export.md`](./stats-export.md) for the full snapshot shape, |
| 56 | +serialisation, and download implementation details. |
| 57 | + |
| 58 | +## Accessibility |
| 59 | + |
| 60 | +- Loading / empty / success live in a single `aria-live="polite" aria-atomic="true"` region. |
| 61 | +- The error card wraps its content in `role="alert"` and is rendered **outside** the live region so it announces immediately. |
| 62 | +- The **Retry** button in the error card is keyboard-operable (`<Button type="button">Retry</Button>`). |
| 63 | +- The metrics panel is a `<section aria-labelledby="stats-metrics-heading">` with a visually-hidden `<h2>`. |
| 64 | + |
| 65 | +## Exported symbols |
| 66 | + |
| 67 | +All of these are exported from `src/app/stats/Client.tsx` and are unit-tested |
| 68 | +in `page.test.tsx`. |
| 69 | + |
| 70 | +| Symbol | Kind | Purpose | |
| 71 | +|--------|------|---------| |
| 72 | +| `useBackoffInterval` | hook | Schedules the next poll after each settled request | |
| 73 | +| `formatStatsAge` | function | Formats a millisecond delta as a human-readable age string | |
| 74 | +| `buildStatsSnapshot` | function | Converts live `Stats` data to a typed snapshot object | |
| 75 | +| `statsSnapshotToJson` | function | Serialises a snapshot to pretty-printed JSON | |
| 76 | +| `statsSnapshotToCsv` | function | Serialises a snapshot to CSV | |
| 77 | +| `downloadStatsSnapshot` | function | Triggers a browser file download (JSON or CSV) | |
| 78 | +| `BackoffIntervalOptions` | type | Options accepted by `useBackoffInterval` | |
| 79 | +| `StatsSnapshot` | type | Snapshot shape | |
| 80 | +| `StatsSnapshotMetric` | type | Per-metric shape within a snapshot | |
| 81 | + |
| 82 | +## Usage example |
| 83 | + |
| 84 | +```tsx |
| 85 | +// src/app/stats/page.tsx (RSC wrapper) |
| 86 | +import StatsClient from './Client'; |
| 87 | +export default function StatsPage() { |
| 88 | + return <StatsClient />; |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Related files |
| 93 | + |
| 94 | +| File | Role | |
| 95 | +|------|------| |
| 96 | +| `src/lib/useApi.ts` | `useApi` hook used for the initial and manual refetch | |
| 97 | +| `src/lib/format.ts` | `formatNumber`, `formatTimestamp` | |
| 98 | +| `src/lib/validate.ts` | `isStats` — runtime response validator | |
| 99 | +| `src/components/StatTile.tsx` | Metric tile rendered in the success state | |
0 commit comments