Skip to content

Show graph statistics (total nodes, CVE count) on the homepage - #418

Closed
tomsmith8 wants to merge 2 commits into
mainfrom
strut/1794221193583
Closed

tomsmith8 wants to merge 2 commits into
mainfrom
strut/1794221193583

Conversation

@tomsmith8

Copy link
Copy Markdown
Contributor

I added a stats row to the homepage feed showing total nodes, plus a CVE count only when the graph has CVEs. Nothing was run or type-checked, so the code and the new tests are unverified.

  • Helper and tests: src/lib/graph-stats.ts has getDisplayStats and formatStatNumber. getDisplayStats always returns "Nodes" from node_count and returns nothing if node_count isn't a number. It adds a type stat only when its count is a number above 0. The per-type list is a TYPE_STATS array, currently just cve_count → "CVEs", so other graphs can add entries later. src/lib/__tests__/graph-stats.test.ts covers the cases you listed.
  • Component: src/components/feed/graph-stats.tsx calls getStats once on mount with an AbortController that aborts on unmount. It shows a pulse skeleton while loading, a muted "Stats unavailable" line on error (with console.error), and the grouped numbers on success. It renders nothing if node_count is missing. It has no admin or sign-in gate, so anonymous visitors of a public graph see it.
  • Mock mode: when mocks are enabled it skips the API call and shows a fixed "Nodes 1,234" tile.
  • Wiring: feed-view.tsx renders the component above HotTakes, only when there is no search term. That is the landing view, so it hides during search results; say if you want it always visible.
  • Interface change: in src/lib/graph-api.ts I exported StatsResponse and added optional node_count and cve_count.

I did not write a component test; only the helper is covered.

I added a stats row to the homepage feed showing total nodes, plus a CVE count only when the graph has CVEs. Nothing was run or type-checked, so the code and the new tests are unverified.

- **Helper and tests:** `src/lib/graph-stats.ts` has `getDisplayStats` and `formatStatNumber`. `getDisplayStats` always returns "Nodes" from `node_count` and returns nothing if `node_count` isn't a number. It adds a type stat only when its count is a number above 0. The per-type list is a `TYPE_STATS` array, currently just `cve_count` → "CVEs", so other graphs can add entries later. `src/lib/__tests__/graph-stats.test.ts` covers the cases you listed.
- **Component:** `src/components/feed/graph-stats.tsx` calls `getStats` once on mount with an `AbortController` that aborts on unmount. It shows a pulse skeleton while loading, a muted "Stats unavailable" line on error (with `console.error`), and the grouped numbers on success. It renders nothing if `node_count` is missing. It has no admin or sign-in gate, so anonymous visitors of a public graph see it.
- **Mock mode:** when mocks are enabled it skips the API call and shows a fixed "Nodes 1,234" tile.
- **Wiring:** `feed-view.tsx` renders the component above `HotTakes`, only when there is no search term. That is the landing view, so it hides during search results; say if you want it always visible.
- **Interface change:** in `src/lib/graph-api.ts` I exported `StatsResponse` and added optional `node_count` and `cve_count`.

I did not write a component test; only the helper is covered.
In `src/lib/graph-api.ts`, `StatsResponse` now has `node_count: number` (required) and no `cve_count` line. The index signature covers `cve_count`. I didn't run a type-check or build, so the TS2411 fix is unconfirmed.

I re-read `src/lib/graph-stats.ts` and `src/lib/__tests__/graph-stats.test.ts` and found nothing that needs changing:
- **`getDisplayStats`:** it takes `Partial<StatsResponse>`, so `node_count` is `number | undefined` and the `typeof` check still narrows it.
- **`cve_count`:** it is read through the `Record<string, unknown>` cast, so it doesn't depend on the removed property.
- **Test literals:** the ones I looked at, such as `{ node_count: 5, num_nodes: 0 }` and `{ num_nodes: 7 }`, are valid `Partial` values. I only grepped the test file for `num_nodes` and `StatsResponse`, so I didn't read every literal in it, including the `cve_count: 3` one.

Nothing else in `src` builds a full `StatsResponse` literal that would now be missing the required `node_count`.
@tomsmith8 tomsmith8 closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant