实现节点筛选函数,调整节点数据表结构
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
- [ ] Limiter
|
||||
- [ ] Compress
|
||||
|
||||
考虑将鉴权逻辑放到 handler 里,统一动静态鉴权以及解耦服务层
|
||||
|
||||
有些地方在用手动事务,有时间改成自动事务
|
||||
|
||||
remote 用环境变量保存账号密码!
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"platform/pkg/logs"
|
||||
"platform/pkg/orm"
|
||||
"platform/pkg/rds"
|
||||
"platform/pkg/remote"
|
||||
"platform/web"
|
||||
"syscall"
|
||||
)
|
||||
@@ -23,6 +24,7 @@ func main() {
|
||||
logs.Init()
|
||||
orm.Init()
|
||||
rds.Init()
|
||||
remote.Init()
|
||||
|
||||
// web 服务
|
||||
app, err := web.New(&web.Config{
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,8 @@ import (
|
||||
)
|
||||
import "gorm.io/driver/postgres"
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func Init() {
|
||||
|
||||
// 连接数据库
|
||||
@@ -39,4 +41,6 @@ func Init() {
|
||||
|
||||
// 初始化查询工具
|
||||
queries.SetDefault(db)
|
||||
|
||||
DB = db
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ type client struct {
|
||||
|
||||
var Client client
|
||||
|
||||
func Init() error {
|
||||
func Init() {
|
||||
// todo 从环境变量中获取参数
|
||||
Client = client{
|
||||
gatewayUrl: "http://110.40.82.248:9990",
|
||||
@@ -26,8 +26,6 @@ func Init() error {
|
||||
password: "123456",
|
||||
cloudUrl: "http://103.139.212.110",
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type PortConfig struct {
|
||||
|
||||
@@ -387,14 +387,17 @@ create table node (
|
||||
name varchar(255) not null unique,
|
||||
version int not null,
|
||||
fwd_port int not null,
|
||||
provider varchar(255) not null,
|
||||
location varchar(255) not null,
|
||||
isp varchar(255) not null,
|
||||
prov varchar(255) not null,
|
||||
city varchar(255) not null,
|
||||
|
||||
created_at timestamp default current_timestamp,
|
||||
updated_at timestamp default current_timestamp,
|
||||
deleted_at timestamp
|
||||
);
|
||||
create index node_provider_index on node (provider);
|
||||
create index node_location_index on node (location);
|
||||
create index node_isp_index on node (isp);
|
||||
create index node_prov_index on node (prov);
|
||||
create index node_city_index on node (city);
|
||||
|
||||
-- node表字段注释
|
||||
comment on table node is '节点表';
|
||||
@@ -402,8 +405,9 @@ comment on column node.id is '节点ID';
|
||||
comment on column node.name is '节点名称';
|
||||
comment on column node.version is '节点版本';
|
||||
comment on column node.fwd_port is '转发端口';
|
||||
comment on column node.provider is '运营商';
|
||||
comment on column node.location is '位置';
|
||||
comment on column node.isp is '运营商';
|
||||
comment on column node.prov is '省份';
|
||||
comment on column node.city is '城市';
|
||||
comment on column node.created_at is '创建时间';
|
||||
comment on column node.updated_at is '更新时间';
|
||||
comment on column node.deleted_at is '删除时间';
|
||||
|
||||
@@ -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"` // 删除时间
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user