Skip to content

fix(usage): restore /usage stats with SQLite source and JSON fallback#36

Merged
Microck merged 1 commit into
Microck:masterfrom
paulocarinhena:fix/usage-sqlite-fallback
May 11, 2026
Merged

fix(usage): restore /usage stats with SQLite source and JSON fallback#36
Microck merged 1 commit into
Microck:masterfrom
paulocarinhena:fix/usage-sqlite-fallback

Conversation

@paulocarinhena
Copy link
Copy Markdown
Contributor

Problem

The /usage page was returning empty stats because the backend endpoint (GET /api/usage) only read legacy JSON files under storage/message and storage/session.

Recent OpenCode versions persist conversation data in opencode.db (SQLite), so environments without legacy JSON logs had no usage data to aggregate.

Solution

  • Added native SQLite support using Node's built-in node:sqlite (DatabaseSync) in the usage endpoint.
  • Implemented a SQLite-first read path that aggregates usage from message, session, and project tables.
  • Preserved the existing response contract (totalCost, totalTokens, byModel, byDay, byProject) so no frontend changes are required.
  • Kept backwards compatibility via fallback to the previous JSON-based parser when SQLite is unavailable or fails.
  • Kept all existing filters and grouping behavior (projectId, range, from, to, granularity), including per-model timeline token keys used by charts.

Why this works

The endpoint now supports both storage formats used across OpenCode versions. New installs get data from SQLite immediately, while older installs continue to work with JSON logs.

Validation

  • node --check server/index.js
  • Manual API path verification for /api/usage with standard filters/ranges.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 10, 2026

@paulocarinhena is attempting to deploy a commit to the Projects Team on Vercel.

A member of the Team first needs to authorize it.

@Microck Microck merged commit 5c7ca09 into Microck:master May 11, 2026
1 check failed
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.

2 participants