Skip to content

🏗️ refactor(status): move raw messages-table SQL into pkg/cache, drop the DB()/NewFromDB round-trip #423

@martinciu

Description

@martinciu

🔍 Problem

pkg/status runs raw SQL against the cache's messages schema, duplicating knowledge that pkg/cache owns:

  • 🪦 tsFormat re-declared at pkg/status/periods.go:14 (identical to pkg/cache/cache.go:24-28); literal layout strings again at pkg/status/compute.go:30 and compute.go:93
  • 🪦 Raw messages-table SQL outside pkg/cache: pkg/status/compute.go:31-40, pkg/status/periods.go:24-52, pkg/status/throughput.go:32-37
  • 🪦 Awkward round-trip: callers hold a *cache.Cache, unwrap with c.DB() to call status, which re-wraps via cache.NewFromDB(db) (pkg/status/compute.go:107) just to call SevenDaySamplesSince. Call sites: cmd/ccpulse/status.go:76,84,90, pkg/tui/model.go:978
  • 🪦 The "headline tokens = input + output" business rule is encoded independently in 5 SQL strings + Go across two packages (pkg/cache/cache.go:720, cache.go:838, pkg/cache/projects.go:33, pkg/status/throughput.go:34, pkg/status/periods.go, pkg/status/compute.go:54) — and 🧮 Align token counts with Claude Code /usage (input + output, no cache) #232 already changed that definition once

Column names, ts layout, and the headline-token definition must change in two packages in lockstep today. Cache.DB() exists almost solely to feed status, defeating the encapsulation pkg/cache otherwise maintains.

🛠️ Suggested shape

Move the three aggregate queries into pkg/cache as methods (e.g. FiveHourTotals, PeriodTotals, ThroughputTotals); have status.Compute* accept *cache.Cache (or a 3-method interface); delete the duplicated tsFormat and the DB()/NewFromDB round-trip.

⚠️ Notes

  • Mechanical move; both packages are well-tested, but status_test.go seeds via raw SQL and would move/change too (overlaps with the test-fixture consolidation candidate — coordinate if both land).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgoPull requests that update go codeimportance: lowPolish, refactor, or nice-to-havesize: mMedium — 1 to 3 hours

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions