mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 15:06:36 +00:00
feat: display client affinity count badge in account status column
Add colored circle badge showing affinity client count before the account status indicator. Badge color reflects count severity (gray=0, green=1-5, yellow=6-15, red=16+). Tooltip shows full client ID list on hover. Backend: AccountHandler batch queries reverse affinity index via GetAccountAffinityClientsBatch, returns affinity_client_count and affinity_clients in DTO. GatewayCache interface extended with the new batch method backed by a Lua script (get_affinity_clients.lua). All test mocks synchronized with updated GatewayCache interface. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
b9dc2e8eb5
commit
ea5e5dd9c7
23 files changed
+176
-15
No files matched your search
@@ -24,10 +24,13 @@ var (
|
||||
updateAffinityLua string
|
||||
//go:embed lua/get_affinity_count.lua
|
||||
getAffinityCountLua string
|
||||
//go:embed lua/get_affinity_clients.lua
|
||||
getAffinityClientsLua string
|
||||
|
||||
getAffinityScript = redis.NewScript(getAffinityLua)
|
||||
updateAffinityScript = redis.NewScript(updateAffinityLua)
|
||||
getAffinityCountScript = redis.NewScript(getAffinityCountLua)
|
||||
getAffinityScript = redis.NewScript(getAffinityLua)
|
||||
updateAffinityScript = redis.NewScript(updateAffinityLua)
|
||||
getAffinityCountScript = redis.NewScript(getAffinityCountLua)
|
||||
getAffinityClientsScript = redis.NewScript(getAffinityClientsLua)
|
||||
)
|
||||
|
||||
type gatewayCache struct {
|
||||
@@ -140,3 +143,57 @@ func (c *gatewayCache) GetAccountAffinityCountBatch(ctx context.Context, groupID
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetAccountAffinityClientsBatch 批量获取每个账号跨所有分组的亲和客户端列表(去重)。
|
||||
// accountGroups: map[accountID][]groupID,对每个 (groupID, accountID) 组合查询反向索引。
|
||||
func (c *gatewayCache) GetAccountAffinityClientsBatch(ctx context.Context, accountGroups map[int64][]int64, ttl time.Duration) (map[int64][]string, error) {
|
||||
if len(accountGroups) == 0 {
|
||||
return map[int64][]string{}, nil
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
expireThreshold := now - int64(ttl.Seconds())
|
||||
|
||||
// 构建所有 (accountID, groupID) 组合的查询
|
||||
type queryItem struct {
|
||||
accountID int64
|
||||
groupID int64
|
||||
}
|
||||
var queries []queryItem
|
||||
for accID, groupIDs := range accountGroups {
|
||||
for _, gID := range groupIDs {
|
||||
queries = append(queries, queryItem{accountID: accID, groupID: gID})
|
||||
}
|
||||
}
|
||||
|
||||
pipe := c.rdb.Pipeline()
|
||||
cmds := make([]*redis.Cmd, len(queries))
|
||||
for i, q := range queries {
|
||||
key := buildAffinityReverseKey(q.groupID, q.accountID)
|
||||
cmds[i] = getAffinityClientsScript.Run(ctx, pipe, []string{key}, expireThreshold)
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
if err != nil && err != redis.Nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 合并结果:同一个 accountID 跨多个 group 的 clientID 去重
|
||||
result := make(map[int64][]string, len(accountGroups))
|
||||
seen := make(map[int64]map[string]struct{}, len(accountGroups))
|
||||
for i, q := range queries {
|
||||
clients, _ := cmds[i].StringSlice()
|
||||
if len(clients) == 0 {
|
||||
continue
|
||||
}
|
||||
if seen[q.accountID] == nil {
|
||||
seen[q.accountID] = make(map[string]struct{})
|
||||
}
|
||||
for _, clientID := range clients {
|
||||
if _, exists := seen[q.accountID][clientID]; !exists {
|
||||
seen[q.accountID][clientID] = struct{}{}
|
||||
result[q.accountID] = append(result[q.accountID], clientID)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in new issue
Block a user