Features:
- Node group CRUD operations with traffic-based filtering - Three grouping modes: average distribution, subscription-based, and traffic-based - Automatic and manual group recalculation with history tracking - Group assignment preview before applying changes - User subscription group locking to prevent automatic reassignment - Subscribe-to-group mapping configuration - Group calculation history and detailed reports - System configuration for group management (enabled/mode/auto_create) Database: - Add node_group table for group definitions - Add group_history and group_history_detail tables for tracking - Add node_group_ids (JSON) to nodes and subscribe tables - Add node_group_id and group_locked fields to user_subscribe table - Add migration files for schema changes
This commit is contained in:
@@ -33,15 +33,16 @@ type FilterParams struct {
|
||||
}
|
||||
|
||||
type FilterNodeParams struct {
|
||||
Page int // Page Number
|
||||
Size int // Page Size
|
||||
NodeId []int64 // Node IDs
|
||||
ServerId []int64 // Server IDs
|
||||
Tag []string // Tags
|
||||
Search string // Search Address or Name
|
||||
Protocol string // Protocol
|
||||
Preload bool // Preload Server
|
||||
Enabled *bool // Enabled
|
||||
Page int // Page Number
|
||||
Size int // Page Size
|
||||
NodeId []int64 // Node IDs
|
||||
ServerId []int64 // Server IDs
|
||||
Tag []string // Tags
|
||||
NodeGroupIds []int64 // Node Group IDs
|
||||
Search string // Search Address or Name
|
||||
Protocol string // Protocol
|
||||
Preload bool // Preload Server
|
||||
Enabled *bool // Enabled
|
||||
}
|
||||
|
||||
// FilterServerList Filter Server List
|
||||
@@ -96,6 +97,18 @@ func (m *customServerModel) FilterNodeList(ctx context.Context, params *FilterNo
|
||||
if len(params.Tag) > 0 {
|
||||
query = query.Scopes(InSet("tags", params.Tag))
|
||||
}
|
||||
if len(params.NodeGroupIds) > 0 {
|
||||
// Filter by node_group_ids using JSON_CONTAINS for each group ID
|
||||
// Multiple group IDs: node must belong to at least one of the groups
|
||||
var conditions []string
|
||||
for _, gid := range params.NodeGroupIds {
|
||||
conditions = append(conditions, fmt.Sprintf("JSON_CONTAINS(node_group_ids, %d)", gid))
|
||||
}
|
||||
if len(conditions) > 0 {
|
||||
query = query.Where("(" + strings.Join(conditions, " OR ") + ")")
|
||||
}
|
||||
}
|
||||
// If no NodeGroupIds specified, return all nodes (including public nodes)
|
||||
if params.Protocol != "" {
|
||||
query = query.Where("protocol = ?", params.Protocol)
|
||||
}
|
||||
|
||||
+60
-12
@@ -1,25 +1,73 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// JSONInt64Slice is a custom type for handling []int64 as JSON in database
|
||||
type JSONInt64Slice []int64
|
||||
|
||||
// Scan implements sql.Scanner interface
|
||||
func (j *JSONInt64Slice) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
*j = []int64{}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle []byte
|
||||
bytes, ok := value.([]byte)
|
||||
if !ok {
|
||||
// Try to handle string
|
||||
str, ok := value.(string)
|
||||
if !ok {
|
||||
*j = []int64{}
|
||||
return nil
|
||||
}
|
||||
bytes = []byte(str)
|
||||
}
|
||||
|
||||
if len(bytes) == 0 {
|
||||
*j = []int64{}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if it's a JSON array
|
||||
if bytes[0] != '[' {
|
||||
// Not a JSON array, return empty slice
|
||||
*j = []int64{}
|
||||
return nil
|
||||
}
|
||||
|
||||
return json.Unmarshal(bytes, j)
|
||||
}
|
||||
|
||||
// Value implements driver.Valuer interface
|
||||
func (j JSONInt64Slice) Value() (driver.Value, error) {
|
||||
if len(j) == 0 {
|
||||
return "[]", nil
|
||||
}
|
||||
return json.Marshal(j)
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Id int64 `gorm:"primary_key"`
|
||||
Name string `gorm:"type:varchar(100);not null;default:'';comment:Node Name"`
|
||||
Tags string `gorm:"type:varchar(255);not null;default:'';comment:Tags"`
|
||||
Port uint16 `gorm:"not null;default:0;comment:Connect Port"`
|
||||
Address string `gorm:"type:varchar(255);not null;default:'';comment:Connect Address"`
|
||||
ServerId int64 `gorm:"not null;default:0;comment:Server ID"`
|
||||
Server *Server `gorm:"foreignKey:ServerId;references:Id"`
|
||||
Protocol string `gorm:"type:varchar(100);not null;default:'';comment:Protocol"`
|
||||
Enabled *bool `gorm:"type:boolean;not null;default:true;comment:Enabled"`
|
||||
Sort int `gorm:"uniqueIndex;not null;default:0;comment:Sort"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
Id int64 `gorm:"primary_key"`
|
||||
Name string `gorm:"type:varchar(100);not null;default:'';comment:Node Name"`
|
||||
Tags string `gorm:"type:varchar(255);not null;default:'';comment:Tags"`
|
||||
Port uint16 `gorm:"not null;default:0;comment:Connect Port"`
|
||||
Address string `gorm:"type:varchar(255);not null;default:'';comment:Connect Address"`
|
||||
ServerId int64 `gorm:"not null;default:0;comment:Server ID"`
|
||||
Server *Server `gorm:"foreignKey:ServerId;references:Id"`
|
||||
Protocol string `gorm:"type:varchar(100);not null;default:'';comment:Protocol"`
|
||||
Enabled *bool `gorm:"type:boolean;not null;default:true;comment:Enabled"`
|
||||
Sort int `gorm:"uniqueIndex;not null;default:0;comment:Sort"`
|
||||
NodeGroupIds JSONInt64Slice `gorm:"type:json;comment:Node Group IDs (JSON array, multiple groups)"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
func (n *Node) TableName() string {
|
||||
|
||||
Reference in New Issue
Block a user