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

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

@@ -18,8 +18,9 @@ type Node struct {
Name string `gorm:"column:name;not null;comment:节点名称" json:"name"` // 节点名称
Version int32 `gorm:"column:version;not null;comment:节点版本" json:"version"` // 节点版本
FwdPort int32 `gorm:"column:fwd_port;not null;comment:转发端口" json:"fwd_port"` // 转发端口
Provider string `gorm:"column:provider;not null;comment:运营商" json:"provider"` // 运营商
Location string `gorm:"column:location;not null;comment:位置" json:"location"` // 位置
Isp string `gorm:"column:isp;not null;comment:运营商" json:"isp"` // 运营商
Prov string `gorm:"column:prov;not null;comment:省份" json:"prov"` // 省份
City string `gorm:"column:city;not null;comment:城市" json:"city"` // 城市
CreatedAt time.Time `gorm:"column:created_at;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt time.Time `gorm:"column:updated_at;default:CURRENT_TIMESTAMP;comment:更新时间" json:"updated_at"` // 更新时间
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;comment:删除时间" json:"deleted_at"` // 删除时间

View File

@@ -31,8 +31,9 @@ func newNode(db *gorm.DB, opts ...gen.DOOption) node {
_node.Name = field.NewString(tableName, "name")
_node.Version = field.NewInt32(tableName, "version")
_node.FwdPort = field.NewInt32(tableName, "fwd_port")
_node.Provider = field.NewString(tableName, "provider")
_node.Location = field.NewString(tableName, "location")
_node.Isp = field.NewString(tableName, "isp")
_node.Prov = field.NewString(tableName, "prov")
_node.City = field.NewString(tableName, "city")
_node.CreatedAt = field.NewTime(tableName, "created_at")
_node.UpdatedAt = field.NewTime(tableName, "updated_at")
_node.DeletedAt = field.NewField(tableName, "deleted_at")
@@ -50,8 +51,9 @@ type node struct {
Name field.String // 节点名称
Version field.Int32 // 节点版本
FwdPort field.Int32 // 转发端口
Provider field.String // 运营商
Location field.String // 位置
Isp field.String // 运营商
Prov field.String // 省份
City field.String // 城市
CreatedAt field.Time // 创建时间
UpdatedAt field.Time // 更新时间
DeletedAt field.Field // 删除时间
@@ -75,8 +77,9 @@ func (n *node) updateTableName(table string) *node {
n.Name = field.NewString(table, "name")
n.Version = field.NewInt32(table, "version")
n.FwdPort = field.NewInt32(table, "fwd_port")
n.Provider = field.NewString(table, "provider")
n.Location = field.NewString(table, "location")
n.Isp = field.NewString(table, "isp")
n.Prov = field.NewString(table, "prov")
n.City = field.NewString(table, "city")
n.CreatedAt = field.NewTime(table, "created_at")
n.UpdatedAt = field.NewTime(table, "updated_at")
n.DeletedAt = field.NewField(table, "deleted_at")
@@ -96,13 +99,14 @@ func (n *node) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (n *node) fillFieldMap() {
n.fieldMap = make(map[string]field.Expr, 9)
n.fieldMap = make(map[string]field.Expr, 10)
n.fieldMap["id"] = n.ID
n.fieldMap["name"] = n.Name
n.fieldMap["version"] = n.Version
n.fieldMap["fwd_port"] = n.FwdPort
n.fieldMap["provider"] = n.Provider
n.fieldMap["location"] = n.Location
n.fieldMap["isp"] = n.Isp
n.fieldMap["prov"] = n.Prov
n.fieldMap["city"] = n.City
n.fieldMap["created_at"] = n.CreatedAt
n.fieldMap["updated_at"] = n.UpdatedAt
n.fieldMap["deleted_at"] = n.DeletedAt

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"`
}