Fix API key identity in usage analytics#2008
Fix API key identity in usage analytics#2008AveryanAlex wants to merge 3 commits intodiegosouzapw:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request enhances API key usage tracking by grouping statistics by a stable apiKeyId, enabling consistent reporting across name changes and providing visibility into historical aliases. Key changes include updated SQL aggregations, metadata fetching for current names, and UI adjustments to use stable identifiers. Review feedback identifies performance optimization opportunities, specifically recommending caching or filtering the database fetch for API keys and utilizing a Set for historical name tracking to avoid O(N*A) complexity during aggregation.
I am having trouble creating individual review comments. Click here to see my feedback.
src/app/api/usage/analytics/route.ts (258)
Fetching all API keys from the database on every analytics request might become a performance bottleneck if the number of keys grows significantly. Consider passing the apiKeyIds filter to getApiKeys to only fetch the necessary metadata, or implement a short-lived cache for the full list of keys.
src/lib/usage/usageStats.ts (326-328)
Using Array.prototype.includes inside a loop over all usage history entries results in O(N * A) complexity, where N is the number of history entries and A is the number of historical names. While A is typically small, using a Set for historicalApiKeyNames during the aggregation phase would be more efficient (O(N)). You can convert the Set back to an Array once the loop is complete.
Summary
api_key_idinstead of mutable API key names.Testing
node --import tsx/esm --test tests/unit/usage-analytics-route.test.ts tests/unit/usage-analytics.test.tsnpm run typecheck:corenpm run lint(passes with existing warnings only)Notes
npm run buildcould not complete locally before reinstalling dependencies because the checkout was missing docs packages:gray-matter,fuse.js,marked, andisomorphic-dompurify.