fix(admin/usage): populate cache creation/read token breakdown in stats

This commit is contained in:
feitianbubu
2026-06-24 23:43:03 +08:00
parent a1560ae77c
commit 063454ae96
2 changed files with 8 additions and 0 deletions
@@ -3545,6 +3545,8 @@ func (r *usageLogRepository) GetStatsWithFilters(ctx context.Context, filters Us
COALESCE(SUM(input_tokens), 0) as total_input_tokens,
COALESCE(SUM(output_tokens), 0) as total_output_tokens,
COALESCE(SUM(cache_creation_tokens + cache_read_tokens), 0) as total_cache_tokens,
COALESCE(SUM(cache_creation_tokens), 0) as total_cache_creation_tokens,
COALESCE(SUM(cache_read_tokens), 0) as total_cache_read_tokens,
COALESCE(SUM(total_cost), 0) as total_cost,
COALESCE(SUM(actual_cost), 0) as total_actual_cost,
COALESCE(SUM(COALESCE(account_stats_cost, total_cost) * COALESCE(account_rate_multiplier, 1)), 0) as total_account_cost,
@@ -3575,6 +3577,8 @@ func (r *usageLogRepository) GetStatsWithFilters(ctx context.Context, filters Us
&stats.TotalInputTokens,
&stats.TotalOutputTokens,
&stats.TotalCacheTokens,
&stats.TotalCacheCreationTokens,
&stats.TotalCacheReadTokens,
&stats.TotalCost,
&stats.TotalActualCost,
&totalAccountCost,
@@ -29,6 +29,7 @@ func TestUsageLog_GetStatsWithFilters_AggregatesAndEndpoints(t *testing.T) {
_, err := repo.Create(ctx, &service.UsageLog{
UserID: user.ID, APIKeyID: apiKey.ID, AccountID: account.ID,
Model: "claude-3", InputTokens: 2, OutputTokens: 3,
CacheCreationTokens: 4, CacheReadTokens: 5,
TotalCost: 0.5, ActualCost: 0.4, CreatedAt: now,
InboundEndpoint: &inboundEndpoint, UpstreamEndpoint: &upstreamEndpoint,
})
@@ -44,6 +45,9 @@ func TestUsageLog_GetStatsWithFilters_AggregatesAndEndpoints(t *testing.T) {
require.Equal(t, int64(3), stats.TotalRequests)
require.Equal(t, int64(6), stats.TotalInputTokens)
require.Equal(t, int64(9), stats.TotalOutputTokens)
require.Equal(t, int64(27), stats.TotalCacheTokens)
require.Equal(t, int64(12), stats.TotalCacheCreationTokens)
require.Equal(t, int64(15), stats.TotalCacheReadTokens)
require.InDelta(t, 1.2, stats.TotalActualCost, 1e-9)
require.NotEmpty(t, stats.Endpoints)
require.NotEmpty(t, stats.UpstreamEndpoints)