feat(订阅): 添加节点数量统计功能
Build docker and publish / build (20.15.1) (push) Failing after 33s

在订阅数据结构中新增node_count字段,用于统计符合条件的节点数量
实现根据节点ID和标签计算启用节点数量的逻辑
This commit is contained in:
2025-10-22 04:08:28 -07:00
parent b0a03401b8
commit 267582c6a4
3 changed files with 51 additions and 1 deletions
+28
View File
@@ -13,6 +13,7 @@ type customServerLogicModel interface {
FilterServerList(ctx context.Context, params *FilterParams) (int64, []*Server, error)
FilterNodeList(ctx context.Context, params *FilterNodeParams) (int64, []*Node, error)
ClearNodeCache(ctx context.Context, params *FilterNodeParams) error
CountNodesByIdsAndTags(ctx context.Context, nodeIds []int64, tags []string) (int64, error)
}
const (
@@ -189,3 +190,30 @@ func InSet(field string, values []string) func(db *gorm.DB) *gorm.DB {
return db.Where("("+strings.Join(conds, " OR ")+")", args...)
}
}
// CountNodesByIdsAndTags 根据节点ID和标签计算启用的节点数量
func (m *customServerModel) CountNodesByIdsAndTags(ctx context.Context, nodeIds []int64, tags []string) (int64, error) {
var count int64
query := m.WithContext(ctx).Model(&Node{}).Where("enabled = ?", true)
// 如果有节点ID或标签,添加相应的查询条件
if len(nodeIds) > 0 || len(tags) > 0 {
subQuery := m.WithContext(ctx).Model(&Node{}).Where("enabled = ?", true)
if len(nodeIds) > 0 && len(tags) > 0 {
// 节点ID和标签都存在时,使用OR条件
subQuery = subQuery.Where("id IN ? OR ?", nodeIds, InSet("tag", tags))
} else if len(nodeIds) > 0 {
// 只有节点ID
subQuery = subQuery.Where("id IN ?", nodeIds)
} else {
// 只有标签
subQuery = subQuery.Scopes(InSet("tag", tags))
}
query = subQuery
}
err := query.Count(&count).Error
return count, err
}