实现节点筛选函数,调整节点数据表结构

This commit is contained in:
2025-03-26 16:34:54 +08:00
parent 1ac87f79c6
commit e337a9c08e
9 changed files with 572 additions and 316 deletions

View File

@@ -1,18 +1,67 @@
package services
import "platform/web/models"
import (
"encoding/json"
"fmt"
"platform/pkg/orm"
)
var Node = &nodeService{}
type nodeService struct{}
func (s *nodeService) Filter(userId int32, proto ChannelProtocol, count int, config ...NodeFilterConfig) ([]*models.Node, error) {
func (s *nodeService) Filter(userId int32, count int, config ...NodeFilterConfig) ([]*FilteredNode, error) {
_config := NodeFilterConfig{}
if len(config) > 0 {
_config = config[0]
}
return make([]*models.Node, 0), nil
// 筛选符合条件且未分配给用户过的节点
// 静态条件:省,市,运营商
// 排序方式1.分配给该用户的次数 2.分配给全部用户的次数 3.todo 节点的健康状态
var nodes []*FilteredNode
orm.DB.Raw(filterSqlRaw, userId, _config.Isp, _config.Prov, _config.City).
Limit(count).
Find(&nodes)
rs, _ := json.Marshal(nodes)
fmt.Printf(string(rs))
// 返回节点列表
return nodes, nil
}
type NodeFilterConfig struct {
province string
city string
provider string
Isp string
Prov string
City string
}
const filterSqlRaw = `
select
n.id as id,
n.name as name,
n.fwd_port as port,
count(c.*) as total,
count(c.*) filter ( where c.user_id = ? ) as assigned
from
node n
left join public.channel c
on n.id = c.node_id and c.expiration > now() and c.deleted_at is null
where
n.isp = ? and
n.prov = ? and
n.city = ?
group by
n.id
order by
assigned, total
`
type FilteredNode struct {
Id int32 `json:"id"`
Name string `json:"name"`
Port int32 `json:"port"`
Total int32 `json:"total"`
Assigned int32 `json:"assigned"`
}