为用户在 anthropic/openai/gemini/antigravity 四个平台上提供日/周/月 三个窗口的 USD 配额管控。配额语义:未设置=不限制,0=禁用,>0=美元上限。 两层模型: - 配置层:系统默认配额,以及 email/linuxdo/oidc/wechat/github/google/ dingtalk 七个鉴权来源的默认配额,存于 settings,以嵌套 JSON 整体读写 (系统 1 个 key + 每个来源 1 个 key),整体替换语义。 - 运行时层:user_platform_quota 表按用户记录实际配额,与配置层解耦。 后端:新增 ent schema 与 140_user_platform_quotas.sql 迁移、repository 与 service 端口、计费链路集成、管理端与用户端读写接口。 前端:管理端设置页配额编辑、用户配额管理 Modal、用户 Dashboard 展示、 中英文案。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
105 lines
3.8 KiB
Go
105 lines
3.8 KiB
Go
// Package quotaview provides shared quota response helpers for user and admin handlers.
|
||
// Extracted to avoid import cycles between handler and handler/admin packages.
|
||
package quotaview
|
||
|
||
import (
|
||
"time"
|
||
|
||
"github.com/Wei-Shaw/sub2api/internal/pkg/timezone"
|
||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||
)
|
||
|
||
// LazyZeroQuotaForResponse 按 D14 规则把过期档位归零(不写 DB)。
|
||
// includeWindowStart=true 时输出 *_window_start 字段(admin 视角调试用)
|
||
func LazyZeroQuotaForResponse(r service.UserPlatformQuotaRecord, now time.Time, includeWindowStart bool) map[string]any {
|
||
daily := buildWindowSlice(r.DailyUsageUSD, r.DailyLimitUSD, r.DailyWindowStart, NeedsDailyReset(r.DailyWindowStart, now), nextDailyResetTime(now), includeWindowStart)
|
||
weekly := buildWindowSlice(r.WeeklyUsageUSD, r.WeeklyLimitUSD, r.WeeklyWindowStart, NeedsWeeklyReset(r.WeeklyWindowStart, now), nextWeeklyResetTime(now), includeWindowStart)
|
||
monthly := buildWindowSlice(r.MonthlyUsageUSD, r.MonthlyLimitUSD, r.MonthlyWindowStart, NeedsMonthlyReset(r.MonthlyWindowStart, now), NextMonthlyResetTimeFrom(r.MonthlyWindowStart, now), includeWindowStart)
|
||
out := map[string]any{
|
||
"platform": r.Platform,
|
||
"daily_usage_usd": daily.usage,
|
||
"daily_limit_usd": daily.limit,
|
||
"daily_window_resets_at": daily.resetsAt,
|
||
"weekly_usage_usd": weekly.usage,
|
||
"weekly_limit_usd": weekly.limit,
|
||
"weekly_window_resets_at": weekly.resetsAt,
|
||
"monthly_usage_usd": monthly.usage,
|
||
"monthly_limit_usd": monthly.limit,
|
||
"monthly_window_resets_at": monthly.resetsAt,
|
||
}
|
||
if includeWindowStart {
|
||
out["daily_window_start"] = daily.windowStart
|
||
out["weekly_window_start"] = weekly.windowStart
|
||
out["monthly_window_start"] = monthly.windowStart
|
||
}
|
||
return out
|
||
}
|
||
|
||
type windowSlice struct {
|
||
usage float64
|
||
limit *float64
|
||
resetsAt *string
|
||
windowStart *string
|
||
}
|
||
|
||
func buildWindowSlice(usage float64, limit *float64, start *time.Time, expired bool, nextReset time.Time, includeStart bool) windowSlice {
|
||
out := windowSlice{usage: usage, limit: limit}
|
||
if expired {
|
||
out.usage = 0
|
||
out.resetsAt = nil
|
||
} else if start != nil {
|
||
s := nextReset.Format(time.RFC3339)
|
||
out.resetsAt = &s
|
||
}
|
||
if includeStart && start != nil {
|
||
s := start.Format(time.RFC3339)
|
||
out.windowStart = &s
|
||
}
|
||
return out
|
||
}
|
||
|
||
// NeedsDailyReset 判断日窗口是否已过期:start 早于「全局时区当天 0 点」即过期。
|
||
// 时区跟随 timezone.Location()(全局服务器时区),与 billing / repo 写入的 window_start 同口径。
|
||
func NeedsDailyReset(start *time.Time, now time.Time) bool {
|
||
if start == nil {
|
||
return false
|
||
}
|
||
return start.Before(timezone.StartOfDay(now))
|
||
}
|
||
|
||
func NeedsWeeklyReset(start *time.Time, now time.Time) bool {
|
||
if start == nil {
|
||
return false
|
||
}
|
||
return start.Before(timezone.StartOfWeek(now))
|
||
}
|
||
|
||
// NeedsMonthlyReset 30 天滚动窗口语义(与订阅模式 NeedsMonthlyReset 一致)。
|
||
func NeedsMonthlyReset(start *time.Time, now time.Time) bool {
|
||
if start == nil {
|
||
return false
|
||
}
|
||
return now.Sub(*start) >= 30*24*time.Hour
|
||
}
|
||
|
||
func nextDailyResetTime(now time.Time) time.Time {
|
||
return timezone.StartOfDay(now).AddDate(0, 0, 1)
|
||
}
|
||
|
||
func nextWeeklyResetTime(now time.Time) time.Time {
|
||
return timezone.StartOfWeek(now).AddDate(0, 0, 7)
|
||
}
|
||
|
||
// NextMonthlyResetTimeFrom 计算 30 天滚动月度窗口的下次重置时间。
|
||
// 语义:
|
||
// - start != nil → 返回 start + 30d(与 billing_cache_service.nextMonthlyResetFrom 一致)
|
||
// - start == nil → 退化为 now + 30d(保留旧行为,避免 nil 崩溃)
|
||
//
|
||
// 导出(首字母大写)以允许测试直接调用。
|
||
func NextMonthlyResetTimeFrom(start *time.Time, now time.Time) time.Time {
|
||
if start == nil {
|
||
return now.Add(30 * 24 * time.Hour)
|
||
}
|
||
return start.Add(30 * 24 * time.Hour)
|
||
}
|