Commit 60fce4f1 authored by wioos's avatar wioos
Browse files

fix: 修复 lite 模式跳过窗口费用查询导致 $0.00 显示的问题

commit 80ae592c 引入 lite 模式优化首次加载性能,但将窗口费用查询也一起跳过了。
commit 491a7444

 尝试用 30 秒快照缓存修复,但缓存过期后问题复现。

移除窗口费用查询的 lite/非 lite 区分,始终执行 PostgreSQL 聚合查询。
同时删除不再需要的 account_window_cost_cache.go 文件。
Co-Authored-By: default avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 9af65efc
...@@ -288,48 +288,32 @@ func (h *AccountHandler) List(c *gin.Context) { ...@@ -288,48 +288,32 @@ func (h *AccountHandler) List(c *gin.Context) {
} }
} }
// 窗口费用获取:lite 模式从快照缓存读取,非 lite 模式执行 PostgreSQL 查询后写入缓存 // 始终获取窗口费用(PostgreSQL 聚合查询)
if len(windowCostAccountIDs) > 0 { if len(windowCostAccountIDs) > 0 {
if lite { windowCosts = make(map[int64]float64)
// lite 模式:尝试从快照缓存读取 var mu sync.Mutex
cacheKey := buildWindowCostCacheKey(windowCostAccountIDs) g, gctx := errgroup.WithContext(c.Request.Context())
if cached, ok := accountWindowCostCache.Get(cacheKey); ok { g.SetLimit(10) // 限制并发数
if costs, ok := cached.Payload.(map[int64]float64); ok {
windowCosts = costs for i := range accounts {
} acc := &accounts[i]
if !acc.IsAnthropicOAuthOrSetupToken() || acc.GetWindowCostLimit() <= 0 {
continue
} }
// 缓存未命中则 windowCosts 保持 nil(仅发生在服务刚启动时) accCopy := acc // 闭包捕获
} else { g.Go(func() error {
// 非 lite 模式:执行 PostgreSQL 聚合查询(高开销) // 使用统一的窗口开始时间计算逻辑(考虑窗口过期情况)
windowCosts = make(map[int64]float64) startTime := accCopy.GetCurrentWindowStartTime()
var mu sync.Mutex stats, err := h.accountUsageService.GetAccountWindowStats(gctx, accCopy.ID, startTime)
g, gctx := errgroup.WithContext(c.Request.Context()) if err == nil && stats != nil {
g.SetLimit(10) // 限制并发数 mu.Lock()
windowCosts[accCopy.ID] = stats.StandardCost // 使用标准费用
for i := range accounts { mu.Unlock()
acc := &accounts[i]
if !acc.IsAnthropicOAuthOrSetupToken() || acc.GetWindowCostLimit() <= 0 {
continue
} }
accCopy := acc // 闭包捕获 return nil // 不返回错误,允许部分失败
g.Go(func() error { })
// 使用统一的窗口开始时间计算逻辑(考虑窗口过期情况)
startTime := accCopy.GetCurrentWindowStartTime()
stats, err := h.accountUsageService.GetAccountWindowStats(gctx, accCopy.ID, startTime)
if err == nil && stats != nil {
mu.Lock()
windowCosts[accCopy.ID] = stats.StandardCost // 使用标准费用
mu.Unlock()
}
return nil // 不返回错误,允许部分失败
})
}
_ = g.Wait()
// 查询完毕后写入快照缓存,供 lite 模式使用
cacheKey := buildWindowCostCacheKey(windowCostAccountIDs)
accountWindowCostCache.Set(cacheKey, windowCosts)
} }
_ = g.Wait()
} }
// Build response with concurrency info // Build response with concurrency info
......
package admin
import (
"strconv"
"strings"
"time"
)
var accountWindowCostCache = newSnapshotCache(30 * time.Second)
func buildWindowCostCacheKey(accountIDs []int64) string {
if len(accountIDs) == 0 {
return "accounts_window_cost_empty"
}
var b strings.Builder
b.Grow(len(accountIDs) * 6)
_, _ = b.WriteString("accounts_window_cost:")
for i, id := range accountIDs {
if i > 0 {
_ = b.WriteByte(',')
}
_, _ = b.WriteString(strconv.FormatInt(id, 10))
}
return b.String()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment