Cache sector and industry by CIK (#14) #52

Merged
chris-kreidl merged 1 commit from 14-sector-cache into main 2026-11-12 02:31:43 +00:00
chris-kreidl commented 2026-11-12 02:27:45 +00:00 (Migrated from github.com)

🤖 Claude: Closes #14

Enrich now caches each company's sector profile in a sector_cache table, keyed by CIK, and asks Yahoo for a profile only when the cache has no entry for a company or the entry is more than 90 days old.

Changes

  • src/db.ts: adds the sector_cache table (cik PK, ticker, sector, industry, source yahoo/sic, fetchedAt).
  • src/sectors.ts (new): cachedSector (missing or >90 days → null), cacheSector, lookUpSector (Yahoo sector, else the SIC fallback from #45, else null, which is not cached and is retried next run).
  • src/enrich.ts: checks the cache first and requests Yahoo's summaryProfile module only on a miss. Its result gains profileLookups, which the CLI summary shows.
  • CLAUDE.md: structure section updated.

Real-data demo (bun cli.ts enrich rank, run twice)

Run 1 (empty cache) Run 2
Summary 1591 priced (8 sectors from SEC SIC codes) · 1591 profile lookups · 86 no ticker · 2 no quote 1591 priced (8 sectors from SEC SIC codes) · 0 profile lookups · 86 no ticker · 2 no quote
Time 35 min 32 min
  • The cache matches today's sectors: 1,591 rows (1,583 from Yahoo, 8 from SIC). Sectors in company_analysis match the pre-run database for 6,020 of 6,021 companies. The exception is MGSD, which had no sector before and now gets Transportation Services from its SIC code.
  • Run 2: no profile lookups, no cache entries rewritten, and no sector changed.
  • CSV: ticker, sector, industry and price are identical in all 1,391 rows both runs share. Only market cap, EY and the ranks changed. Run 2 has 7 fewer rows: MSM, WMK, BDL, UAN, NGL, NL and SIF kept the same price but Yahoo stopped returning their market cap, so #40 leaves them unranked. A direct probe shows Yahoo omits marketCap for these tickers whether or not the profile module is requested, so the cache didn't cause this. It's Yahoo's data.
  • Run 2 is barely faster because prices still need one quoteSummary call per company. The big speedup comes with #13 (batched quotes), which builds on this PR.

Definition of Done

  • Acceptance criteria met and demonstrated (above)
  • bun test passes (213 tests, 0 failures); new logic has tests (test/sectors.test.ts: cache key and columns, fresh/stale at 90 days, replacement, Yahoo → SIC → not cached)
  • bun check is clean and bun fmt --check passes
  • Runs end to end on real data (not just fixtures)
  • Stand-up posted on the milestone tracking issue when the story started and when this PR opened (#50)

🤖 Generated with Claude Code

🤖 **Claude:** Closes #14 Enrich now caches each company's sector profile in a `sector_cache` table, keyed by CIK, and asks Yahoo for a profile only when the cache has no entry for a company or the entry is more than 90 days old. ### Changes - `src/db.ts`: adds the `sector_cache` table (cik PK, ticker, sector, industry, source `yahoo`/`sic`, fetchedAt). - `src/sectors.ts` (new): `cachedSector` (missing or >90 days → null), `cacheSector`, `lookUpSector` (Yahoo sector, else the SIC fallback from #45, else null, which is not cached and is retried next run). - `src/enrich.ts`: checks the cache first and requests Yahoo's `summaryProfile` module only on a miss. Its result gains `profileLookups`, which the CLI summary shows. - `CLAUDE.md`: structure section updated. ### Real-data demo (`bun cli.ts enrich rank`, run twice) | | Run 1 (empty cache) | Run 2 | |---|---|---| | Summary | 1591 priced (8 sectors from SEC SIC codes) · **1591 profile lookups** · 86 no ticker · 2 no quote | 1591 priced (8 sectors from SEC SIC codes) · **0 profile lookups** · 86 no ticker · 2 no quote | | Time | 35 min | 32 min | - **The cache matches today's sectors:** 1,591 rows (1,583 from Yahoo, 8 from SIC). Sectors in `company_analysis` match the pre-run database for 6,020 of 6,021 companies. The exception is MGSD, which had no sector before and now gets `Transportation Services` from its SIC code. - **Run 2:** no profile lookups, no cache entries rewritten, and no sector changed. - **CSV:** ticker, sector, industry and price are identical in all 1,391 rows both runs share. Only market cap, EY and the ranks changed. Run 2 has 7 fewer rows: MSM, WMK, BDL, UAN, NGL, NL and SIF kept the same price but Yahoo stopped returning their market cap, so #40 leaves them unranked. A direct probe shows Yahoo omits `marketCap` for these tickers whether or not the profile module is requested, so the cache didn't cause this. It's Yahoo's data. - Run 2 is barely faster because prices still need one `quoteSummary` call per company. The big speedup comes with #13 (batched quotes), which builds on this PR. ### Definition of Done - [x] Acceptance criteria met and demonstrated (above) - [x] `bun test` passes (213 tests, 0 failures); new logic has tests (`test/sectors.test.ts`: cache key and columns, fresh/stale at 90 days, replacement, Yahoo → SIC → not cached) - [x] `bun check` is clean and `bun fmt --check` passes - [x] Runs end to end on real data (not just fixtures) - [x] Stand-up posted on the milestone tracking issue when the story started and when this PR opened (#50) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ckreidl/screener!52
No description provided.