AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

feat(usage): add per-PROXY_API_KEY breakdown to Usage dashboard and requests.db

Open lawrancehoy-cyber opened this issue 3 days ago • 0 comments

想要什么,用来做什么 / What you want and what for

What I want and what for

AIStudio2API already supports multiple comma-separated PROXY_API_KEY values and per-key account scoping via API_KEY_ACCOUNTS (web/src/components/SettingsPanel.vue, internal/aistudio/channel.go:108-133), which makes it easy to share one server instance across multiple clients, projects, or users (for example, separating Claude Code, OpenCode, n8n, or different teammates on one VPS).

Today, however, the Usage dashboard and requests.db cannot break down requests, tokens, latency, or estimated cost by PROXY_API_KEY:

  • apiKeyContextMiddleware (internal/api/middleware.go:634-647) already identifies the exact matched key from PROXY_API_KEY, but accessLogMetadata (middleware.go:26-65) only records authorized: bool.
  • api.UsageDimensions (internal/api/ledger.go:42) and requestdb (internal/requestdb/requestdb.go:42-66) only track model, account, channel, protocol, and state.

Adding a non-secret api_key dimension to the existing Usage ledger and dashboard (with zero new config flags and no extra UI clutter on single-key / keyless setups) would let multi-key users see per-client token consumption, error rates, latency, and estimated API cost directly in the Usage tab and CSV export.

Concrete Code Pointers & Verified Details (v0.3.15 / ddb1af5)

  1. Record a non-secret key label in middleware (internal/api/middleware.go, internal/app/request_logging.go):

    • In apiKeyContextMiddleware (middleware.go:634-647), when a request matches a configured PROXY_API_KEY, store a masked non-secret identifier on accessLogMetadata (never write plaintext keys to requests.db, /api/usage, or CSV exports).
    • Matching the existing UI convention (maskKey in web/src/apikey.ts:10-12, ApiKeyInput.vue, SettingsPanel.vue:749-752, AccessPanel.vue:466): use ••••<last4> (disambiguating only if two configured keys share the same last 4 characters, e.g., appending a short hash suffix), and "" when PROXY_API_KEY is empty.
    • Forward APIKey through api.AccessLog (internal/api/admin.go:153-188) -> requestRow (internal/app/request_logging.go:102-117) -> requestdb.Row.
  2. SQLite schema & WITHOUT ROWID rollup migration (internal/requestdb/requestdb.go, usage.go, records.go):

    • Add api_key TEXT NOT NULL DEFAULT '' to requests (requestdb.go:51-59), rollupColumns (requestdb.go:42-48), rollupKey, and mergeRollup (requestdb.go:237-253, 346-375).
    • Important migration detail verified on SQLite: sqlitedb.Open (internal/sqlitedb/sqlitedb.go:92-99) runs ALTER TABLE ... ADD COLUMN before CREATE TABLE IF NOT EXISTS. On existing WITHOUT ROWID rollup tables (hours, days), ALTER TABLE appends api_key with pk = 0 (outside the primary key), which causes INSERT OR REPLACE for two different keys in the same bucket to overwrite each other. On requestdb.Open, checking PRAGMA table_info(hours) for api_key having pk > 0 and rebuilding hours/days from requests once when missing avoids silent data loss (scanning 25k rows takes ~50 ms, following the existing alignDays() pattern at requestdb.go:391-445).
    • Add "api_key" to api.UsageDimensions (internal/api/ledger.go:42), UsageRecord.APIKey (ledger.go:202-229), recordColumns, and csvHeader (internal/requestdb/records.go:18-21, 95-99). Existing scanCost (usage.go:415-418) remains a covering index scan when unfiltered and naturally supports api_key filtering via filterSQL.
  3. Uncluttered Usage UI (web/src/components/UsagePanel.vue, web/src/components/usage/*):

    • Add 'api_key' to UsageDimension (web/src/types.ts:515), dimensions (web/src/components/usage/model.ts:19), RankingTable.vue, UsageCharts.vue, RecordsTable.vue (optional column), RecordDrawer.vue, and usage.dimension.api_key / usage.none.api_key locale keys.
    • Hide the API Key filter dropdown / ranking tab / stack option automatically when report.options.api_key has no non-empty values (or only 1 key and no active api_key filter), so single-key and keyless local users see zero extra UI chrome.

版本 / Version

v0.3.15 (ddb1af5)

lawrancehoy-cyber avatar Nov 29 '26 17:11 lawrancehoy-cyber