feat(usage): add per-PROXY_API_KEY breakdown to Usage dashboard and requests.db
想要什么,用来做什么 / 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 fromPROXY_API_KEY, butaccessLogMetadata(middleware.go:26-65) only recordsauthorized: bool.api.UsageDimensions(internal/api/ledger.go:42) andrequestdb(internal/requestdb/requestdb.go:42-66) only trackmodel,account,channel,protocol, andstate.
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)
-
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 configuredPROXY_API_KEY, store a masked non-secret identifier onaccessLogMetadata(never write plaintext keys torequests.db,/api/usage, or CSV exports). - Matching the existing UI convention (
maskKeyinweb/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""whenPROXY_API_KEYis empty. - Forward
APIKeythroughapi.AccessLog(internal/api/admin.go:153-188) ->requestRow(internal/app/request_logging.go:102-117) ->requestdb.Row.
- In
-
SQLite schema &
WITHOUT ROWIDrollup migration (internal/requestdb/requestdb.go,usage.go,records.go):- Add
api_key TEXT NOT NULL DEFAULT ''torequests(requestdb.go:51-59),rollupColumns(requestdb.go:42-48),rollupKey, andmergeRollup(requestdb.go:237-253, 346-375). - Important migration detail verified on SQLite:
sqlitedb.Open(internal/sqlitedb/sqlitedb.go:92-99) runsALTER TABLE ... ADD COLUMNbeforeCREATE TABLE IF NOT EXISTS. On existingWITHOUT ROWIDrollup tables (hours,days),ALTER TABLEappendsapi_keywithpk = 0(outside the primary key), which causesINSERT OR REPLACEfor two different keys in the same bucket to overwrite each other. Onrequestdb.Open, checkingPRAGMA table_info(hours)forapi_keyhavingpk > 0and rebuildinghours/daysfromrequestsonce when missing avoids silent data loss (scanning 25k rows takes ~50 ms, following the existingalignDays()pattern atrequestdb.go:391-445). - Add
"api_key"toapi.UsageDimensions(internal/api/ledger.go:42),UsageRecord.APIKey(ledger.go:202-229),recordColumns, andcsvHeader(internal/requestdb/records.go:18-21, 95-99). ExistingscanCost(usage.go:415-418) remains a covering index scan when unfiltered and naturally supportsapi_keyfiltering viafilterSQL.
- Add
-
Uncluttered Usage UI (
web/src/components/UsagePanel.vue,web/src/components/usage/*):- Add
'api_key'toUsageDimension(web/src/types.ts:515),dimensions(web/src/components/usage/model.ts:19),RankingTable.vue,UsageCharts.vue,RecordsTable.vue(optional column),RecordDrawer.vue, andusage.dimension.api_key/usage.none.api_keylocale keys. - Hide the
API Keyfilter dropdown / ranking tab / stack option automatically whenreport.options.api_keyhas no non-empty values (or only 1 key and no activeapi_keyfilter), so single-key and keyless local users see zero extra UI chrome.
- Add
版本 / Version
v0.3.15 (ddb1af5)