mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
模型统计和端点统计在仅按 account_id 过滤(无 user_id/api_key_id)时, 把 actual_cost 换成了账号成本 SUM(COALESCE(account_stats_cost, total_cost) * COALESCE(account_rate_multiplier, 1));展开后的用户明细与分组分布用的是 真实 SUM(actual_cost),于是模型行「实际」会小于其下单个用户行的「实际」, 且与同页「成本」列数值相同。 - actual_cost 统一为用户实际扣费,账号成本由 account_cost 承载; EndpointStat 新增 account_cost,端点分布表增加「成本」列,用户侧 /api/v1/usage/stats 清零该字段、用户页隐藏该列 - Gemini 配额用量(geminiAggregateUsage、GetGeminiUsageTotalsBatch) 显式按账号成本累计,账号用量窗口展示不变 接口口径变化:仅按账户过滤时,/admin/dashboard/models、/admin/usage/stats 的 endpoints/upstream_endpoints/endpoint_paths、/admin/accounts/:id/stats 的 models/endpoints 中 actual_cost 由账号成本改为用户实际扣费,账号成本改看 account_cost;/admin/accounts/:id/stats 的 history/summary 口径不变。
93 lines
3.6 KiB
Go
93 lines
3.6 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/usagestats"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAccountIsGeminiThirdPartyAPIKey(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
account *Account
|
|
expected bool
|
|
}{
|
|
{"nil_account", nil, false},
|
|
{"empty_base_url_is_official", geminiQuotaAPIKeyAccount(""), false},
|
|
{"official_base_url", geminiQuotaAPIKeyAccount("https://generativelanguage.googleapis.com"), false},
|
|
{"official_base_url_trailing_slash", geminiQuotaAPIKeyAccount("https://generativelanguage.googleapis.com/"), false},
|
|
{"official_host_over_http", geminiQuotaAPIKeyAccount("http://generativelanguage.googleapis.com"), true},
|
|
{"official_host_with_path_prefix", geminiQuotaAPIKeyAccount("https://generativelanguage.googleapis.com/proxy"), true},
|
|
{"relay_base_url", geminiQuotaAPIKeyAccount("https://relay.example.com"), true},
|
|
{"oauth_account", &Account{Platform: PlatformGemini, Type: AccountTypeOAuth, Credentials: map[string]any{"base_url": "https://relay.example.com"}}, false},
|
|
{"vertex_service_account", &Account{Platform: PlatformGemini, Type: AccountTypeServiceAccount}, false},
|
|
{"non_gemini_platform", &Account{Platform: PlatformOpenAI, Type: AccountTypeAPIKey, Credentials: map[string]any{"base_url": "https://relay.example.com"}}, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require.Equal(t, tt.expected, tt.account.IsGeminiThirdPartyAPIKey())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGeminiQuotaService_QuotaForAccountSkipsThirdPartyAndVertex(t *testing.T) {
|
|
svc := NewGeminiQuotaService(&config.Config{}, nil)
|
|
ctx := context.Background()
|
|
|
|
quota, ok := svc.QuotaForAccount(ctx, geminiQuotaAPIKeyAccount(""))
|
|
require.True(t, ok)
|
|
require.Equal(t, int64(50), quota.ProRPD)
|
|
|
|
paid := geminiQuotaAPIKeyAccount("https://generativelanguage.googleapis.com")
|
|
paid.Credentials["tier_id"] = GeminiTierAIStudioPaid
|
|
quota, ok = svc.QuotaForAccount(ctx, paid)
|
|
require.True(t, ok)
|
|
require.Equal(t, int64(-1), quota.ProRPD)
|
|
|
|
relay := geminiQuotaAPIKeyAccount("https://relay.example.com")
|
|
relay.Credentials["tier_id"] = GeminiTierAIStudioFree
|
|
_, ok = svc.QuotaForAccount(ctx, relay)
|
|
require.False(t, ok, "第三方上游不套用 AI Studio 模拟配额")
|
|
|
|
vertex := &Account{
|
|
Platform: PlatformGemini,
|
|
Type: AccountTypeServiceAccount,
|
|
Credentials: map[string]any{"project_id": "my-project", "tier_id": "vertex"},
|
|
}
|
|
_, ok = svc.QuotaForAccount(ctx, vertex)
|
|
require.False(t, ok, "Vertex 按量付费不套用模拟配额")
|
|
|
|
googleOne := &Account{
|
|
Platform: PlatformGemini,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{"oauth_type": "google_one", "tier_id": GeminiTierGoogleOneFree},
|
|
}
|
|
quota, ok = svc.QuotaForAccount(ctx, googleOne)
|
|
require.True(t, ok)
|
|
require.Positive(t, quota.SharedRPD)
|
|
}
|
|
|
|
func geminiQuotaAPIKeyAccount(baseURL string) *Account {
|
|
credentials := map[string]any{}
|
|
if baseURL != "" {
|
|
credentials["base_url"] = baseURL
|
|
}
|
|
return &Account{Platform: PlatformGemini, Type: AccountTypeAPIKey, Credentials: credentials}
|
|
}
|
|
|
|
func TestGeminiAggregateUsageUsesAccountCost(t *testing.T) {
|
|
totals := geminiAggregateUsage([]usagestats.ModelStat{
|
|
{Model: "gemini-3.7-flash", Requests: 2, TotalTokens: 20, ActualCost: 5, AccountCost: 1.5},
|
|
{Model: "gemini-4-argon", Requests: 1, TotalTokens: 10, ActualCost: 8, AccountCost: 3},
|
|
})
|
|
require.Equal(t, int64(2), totals.FlashRequests)
|
|
require.Equal(t, 1.5, totals.FlashCost)
|
|
require.Equal(t, int64(1), totals.ProRequests)
|
|
require.Equal(t, 3.0, totals.ProCost)
|
|
}
|