mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
Merge branch 'release/custom-0.1.103' into release/custom-0.1.104
# Conflicts: # backend/internal/service/user_service_test.go # frontend/src/components/account/AccountUsageCell.vue # frontend/src/components/common/DataTable.vue
This commit is contained in:
commit
bda47a8166
159 files changed
+15642
-780
No files matched your search
@@ -57,6 +57,7 @@ type AccountHandler struct {
|
||||
sessionLimitCache service.SessionLimitCache
|
||||
rpmCache service.RPMCache
|
||||
tokenCacheInvalidator service.TokenCacheInvalidator
|
||||
gatewayCache service.GatewayCache
|
||||
}
|
||||
|
||||
// NewAccountHandler creates a new admin account handler
|
||||
@@ -74,6 +75,7 @@ func NewAccountHandler(
|
||||
sessionLimitCache service.SessionLimitCache,
|
||||
rpmCache service.RPMCache,
|
||||
tokenCacheInvalidator service.TokenCacheInvalidator,
|
||||
gatewayCache service.GatewayCache,
|
||||
) *AccountHandler {
|
||||
return &AccountHandler{
|
||||
adminService: adminService,
|
||||
@@ -89,6 +91,7 @@ func NewAccountHandler(
|
||||
sessionLimitCache: sessionLimitCache,
|
||||
rpmCache: rpmCache,
|
||||
tokenCacheInvalidator: tokenCacheInvalidator,
|
||||
gatewayCache: gatewayCache,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,6 +211,34 @@ func (h *AccountHandler) buildAccountResponseWithRuntime(ctx context.Context, ac
|
||||
}
|
||||
}
|
||||
|
||||
// 亲和客户端数据(启用亲和的账号始终返回 count,即使为 0)
|
||||
if account.IsAffinityEnabled() {
|
||||
if h.gatewayCache != nil && len(account.GroupIDs) > 0 {
|
||||
accountGroups := map[int64][]int64{account.ID: account.GroupIDs}
|
||||
if clients, err := h.gatewayCache.GetAccountAffinityClientsBatch(ctx, accountGroups, service.ClientAffinityTTL); err == nil {
|
||||
if cl, ok := clients[account.ID]; ok && len(cl) > 0 {
|
||||
count := int64(len(cl))
|
||||
item.AffinityClientCount = &count
|
||||
item.AffinityClients = cl
|
||||
userCount := countUniqueUsersFromAffinityMembers(cl)
|
||||
item.AffinityUserCount = &userCount
|
||||
} else {
|
||||
zero := int64(0)
|
||||
item.AffinityClientCount = &zero
|
||||
item.AffinityUserCount = &zero
|
||||
}
|
||||
} else {
|
||||
zero := int64(0)
|
||||
item.AffinityClientCount = &zero
|
||||
item.AffinityUserCount = &zero
|
||||
}
|
||||
} else {
|
||||
zero := int64(0)
|
||||
item.AffinityClientCount = &zero
|
||||
item.AffinityUserCount = &zero
|
||||
}
|
||||
}
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
@@ -333,6 +364,21 @@ func (h *AccountHandler) List(c *gin.Context) {
|
||||
_ = g.Wait()
|
||||
}
|
||||
|
||||
// 获取亲和客户端数据(Redis Pipeline,低开销)
|
||||
var affinityClients map[int64][]string
|
||||
if h.gatewayCache != nil {
|
||||
accountGroups := make(map[int64][]int64)
|
||||
for i := range accounts {
|
||||
acc := &accounts[i]
|
||||
if acc.IsAffinityEnabled() && len(acc.GroupIDs) > 0 {
|
||||
accountGroups[acc.ID] = acc.GroupIDs
|
||||
}
|
||||
}
|
||||
if len(accountGroups) > 0 {
|
||||
affinityClients, _ = h.gatewayCache.GetAccountAffinityClientsBatch(c.Request.Context(), accountGroups, service.ClientAffinityTTL)
|
||||
}
|
||||
}
|
||||
|
||||
// Build response with concurrency info
|
||||
result := make([]AccountWithConcurrency, len(accounts))
|
||||
for i := range accounts {
|
||||
@@ -363,6 +409,22 @@ func (h *AccountHandler) List(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 注入亲和客户端数据到 DTO(启用亲和的账号始终返回 count,即使为 0)
|
||||
if acc.IsAffinityEnabled() {
|
||||
if clients, ok := affinityClients[acc.ID]; ok && len(clients) > 0 {
|
||||
count := int64(len(clients))
|
||||
item.AffinityClientCount = &count
|
||||
item.AffinityClients = clients
|
||||
// 从成员列表中解析唯一用户数
|
||||
userCount := countUniqueUsersFromAffinityMembers(clients)
|
||||
item.AffinityUserCount = &userCount
|
||||
} else {
|
||||
zero := int64(0)
|
||||
item.AffinityClientCount = &zero
|
||||
item.AffinityUserCount = &zero
|
||||
}
|
||||
}
|
||||
|
||||
result[i] = item
|
||||
}
|
||||
|
||||
@@ -583,6 +645,8 @@ func (h *AccountHandler) Update(c *gin.Context) {
|
||||
// base_rpm 输入校验:负值归零,超过 10000 截断
|
||||
sanitizeExtraBaseRPM(req.Extra)
|
||||
|
||||
oldStates := h.captureAffinityStates(c.Request.Context(), []int64{accountID})
|
||||
|
||||
// 确定是否跳过混合渠道检查
|
||||
skipCheck := req.ConfirmMixedChannelRisk != nil && *req.ConfirmMixedChannelRisk
|
||||
|
||||
@@ -619,6 +683,8 @@ func (h *AccountHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
h.clearAffinityCacheForBulkIfDisabled(c.Request.Context(), []int64{accountID}, oldStates)
|
||||
|
||||
response.Success(c, h.buildAccountResponseWithRuntime(c.Request.Context(), account))
|
||||
}
|
||||
|
||||
@@ -1335,6 +1401,8 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
oldStates := h.captureAffinityStates(c.Request.Context(), req.AccountIDs)
|
||||
|
||||
result, err := h.adminService.BulkUpdateAccounts(c.Request.Context(), &service.BulkUpdateAccountsInput{
|
||||
AccountIDs: req.AccountIDs,
|
||||
Name: req.Name,
|
||||
@@ -1356,6 +1424,12 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
|
||||
c.JSON(409, gin.H{
|
||||
"error": "mixed_channel_warning",
|
||||
"message": mixedErr.Error(),
|
||||
"details": gin.H{
|
||||
"group_id": mixedErr.GroupID,
|
||||
"group_name": mixedErr.GroupName,
|
||||
"current_platform": mixedErr.CurrentPlatform,
|
||||
"other_platform": mixedErr.OtherPlatform,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -1363,6 +1437,8 @@ func (h *AccountHandler) BulkUpdate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
h.clearAffinityCacheForBulkIfDisabled(c.Request.Context(), req.AccountIDs, oldStates)
|
||||
|
||||
response.Success(c, result)
|
||||
}
|
||||
|
||||
@@ -1582,6 +1658,25 @@ func (h *AccountHandler) ResetQuota(c *gin.Context) {
|
||||
response.Success(c, h.buildAccountResponseWithRuntime(c.Request.Context(), account))
|
||||
}
|
||||
|
||||
// mergeGroupIDs 合并两个 groupID 切片并去重。
|
||||
func mergeGroupIDs(a, b []int64) []int64 {
|
||||
seen := make(map[int64]struct{}, len(a)+len(b))
|
||||
result := make([]int64, 0, len(a)+len(b))
|
||||
for _, id := range a {
|
||||
if _, ok := seen[id]; !ok {
|
||||
seen[id] = struct{}{}
|
||||
result = append(result, id)
|
||||
}
|
||||
}
|
||||
for _, id := range b {
|
||||
if _, ok := seen[id]; !ok {
|
||||
seen[id] = struct{}{}
|
||||
result = append(result, id)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetTempUnschedulable handles getting temporary unschedulable status
|
||||
// GET /api/v1/admin/accounts/:id/temp-unschedulable
|
||||
func (h *AccountHandler) GetTempUnschedulable(c *gin.Context) {
|
||||
|
||||
Reference in new issue
Block a user