Phase 3-B: Add Daily Snapshot Job + Usage History Endpoint #47

Open
opened 2026-05-24 16:55:18 -05:00 by null · 0 comments
Owner

Source plan: /home/kaspa/.claude/plans/with-our-backend-created-precious-owl.md

Feature context: Feature 3: Daily Cost Snapshot (Historical Trending)

Scope

Phase 3-B — Backend, Batch 2: Snapshot Job + History Endpoint

Summary

Add an RQ background job that runs daily to write a snapshot, and a GET /claude-code/usage/history endpoint to read the history.

Affected area

  • Backend service: RQ worker, Claude Code reader
  • API: /claude-code

Affected files

  • backend/app/services/claude_code_reader.py — add snapshot_today() function
  • backend/app/tasks/claude_usage.py — new file, RQ task take_daily_snapshot
  • scripts/rq-docker — register the daily snapshot cron (or use existing cron mechanism)
  • backend/app/api/claude_code.py — add GET /claude-code/usage/history route
  • backend/app/schemas/claude_code.py — add UsageHistoryResponse schema

Affected routes or endpoints

  • GET /api/v1/claude-code/usage/history?range=7d|30d|90d

Expected behavior

  • take_daily_snapshot(): reads current list_sessions(), aggregates totals by model, upserts a ClaudeUsageSnapshot row for today's date
  • Cron: runs once daily at midnight (or on worker startup if no row exists for today)
  • GET /usage/history: returns an array of {date, total_cost_usd, total_tokens, session_count, per_model} sorted ascending by date, scoped to the requested range

Steps to reproduce (acceptance criteria)

  1. Trigger the job manually via RQ CLI or a test call
  2. A row appears in claude_usage_snapshots for today
  3. GET /api/v1/claude-code/usage/history?range=7d returns that row
  4. Running the job again today does not create a duplicate (upsert)

Source plan: `/home/kaspa/.claude/plans/with-our-backend-created-precious-owl.md` Feature context: **Feature 3: Daily Cost Snapshot (Historical Trending)** ## Scope ### Phase 3-B — Backend, Batch 2: Snapshot Job + History Endpoint #### Summary Add an RQ background job that runs daily to write a snapshot, and a `GET /claude-code/usage/history` endpoint to read the history. #### Affected area - Backend service: RQ worker, Claude Code reader - API: `/claude-code` #### Affected files - `backend/app/services/claude_code_reader.py` — add `snapshot_today()` function - `backend/app/tasks/claude_usage.py` — new file, RQ task `take_daily_snapshot` - `scripts/rq-docker` — register the daily snapshot cron (or use existing cron mechanism) - `backend/app/api/claude_code.py` — add `GET /claude-code/usage/history` route - `backend/app/schemas/claude_code.py` — add `UsageHistoryResponse` schema #### Affected routes or endpoints - `GET /api/v1/claude-code/usage/history?range=7d|30d|90d` #### Expected behavior - `take_daily_snapshot()`: reads current `list_sessions()`, aggregates totals by model, upserts a `ClaudeUsageSnapshot` row for today's date - Cron: runs once daily at midnight (or on worker startup if no row exists for today) - `GET /usage/history`: returns an array of `{date, total_cost_usd, total_tokens, session_count, per_model}` sorted ascending by date, scoped to the requested range #### Steps to reproduce (acceptance criteria) 1. Trigger the job manually via RQ CLI or a test call 2. A row appears in `claude_usage_snapshots` for today 3. `GET /api/v1/claude-code/usage/history?range=7d` returns that row 4. Running the job again today does not create a duplicate (upsert) ---
null changed title from 3-B: Backend, Batch 2: Snapshot Job + History Endpoint to Phase 3-B: Add Daily Snapshot Job + Usage History Endpoint 2026-05-24 16:56:10 -05:00
Sign in to join this conversation.
No description provided.