mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-06 07:25:48 +08:00
实现节点分组管理
This commit is contained in:
@@ -191,7 +191,7 @@ func (this *NodeDAO) CountAllEnabledNodes() (int64, error) {
|
||||
}
|
||||
|
||||
// 列出单页节点
|
||||
func (this *NodeDAO) ListEnabledNodesMatch(offset int64, size int64, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState) (result []*Node, err error) {
|
||||
func (this *NodeDAO) ListEnabledNodesMatch(offset int64, size int64, clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string, groupId int64) (result []*Node, err error) {
|
||||
query := this.Query().
|
||||
State(NodeStateEnabled).
|
||||
Offset(offset).
|
||||
@@ -224,6 +224,17 @@ func (this *NodeDAO) ListEnabledNodesMatch(offset int64, size int64, clusterId i
|
||||
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
||||
}
|
||||
|
||||
// 关键词
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword OR JSON_EXTRACT(status,'$.hostname') LIKE :keyword OR id IN (SELECT nodeId FROM "+SharedNodeIPAddressDAO.Table+" WHERE ip LIKE :keyword))").
|
||||
Param("keyword", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
// 分组
|
||||
if groupId > 0 {
|
||||
query.Attr("groupId", groupId)
|
||||
}
|
||||
|
||||
_, err = query.FindAll()
|
||||
return
|
||||
}
|
||||
@@ -309,7 +320,7 @@ func (this *NodeDAO) FindAllInactiveNodesWithClusterId(clusterId int64) (result
|
||||
}
|
||||
|
||||
// 计算节点数量
|
||||
func (this *NodeDAO) CountAllEnabledNodesMatch(clusterId int64, installState configutils.BoolState, activeState configutils.BoolState) (int64, error) {
|
||||
func (this *NodeDAO) CountAllEnabledNodesMatch(clusterId int64, installState configutils.BoolState, activeState configutils.BoolState, keyword string, groupId int64) (int64, error) {
|
||||
query := this.Query()
|
||||
query.State(NodeStateEnabled)
|
||||
|
||||
@@ -338,6 +349,17 @@ func (this *NodeDAO) CountAllEnabledNodesMatch(clusterId int64, installState con
|
||||
query.Where("(status IS NULL OR NOT JSON_EXTRACT(status, '$.isActive') OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>60)")
|
||||
}
|
||||
|
||||
// 关键词
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword OR JSON_EXTRACT(status,'$.hostname') LIKE :keyword OR id IN (SELECT nodeId FROM "+SharedNodeIPAddressDAO.Table+" WHERE ip LIKE :keyword))").
|
||||
Param("keyword", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
// 分组
|
||||
if groupId > 0 {
|
||||
query.Attr("groupId", groupId)
|
||||
}
|
||||
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
@@ -566,6 +588,14 @@ func (this *NodeDAO) FindAllLowerVersionNodesWithClusterId(clusterId int64, os s
|
||||
return
|
||||
}
|
||||
|
||||
// 查找某个节点分组下的所有节点数量
|
||||
func (this *NodeDAO) CountAllEnabledNodesWithGroupId(groupId int64) (int64, error) {
|
||||
return this.Query().
|
||||
State(NodeStateEnabled).
|
||||
Attr("groupId", groupId).
|
||||
Count()
|
||||
}
|
||||
|
||||
// 生成唯一ID
|
||||
func (this *NodeDAO) genUniqueId() (string, error) {
|
||||
for {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -33,7 +35,7 @@ func init() {
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *NodeGroupDAO) EnableNodeGroup(id uint32) (rowsAffected int64, err error) {
|
||||
func (this *NodeGroupDAO) EnableNodeGroup(id int64) (rowsAffected int64, err error) {
|
||||
return this.Query().
|
||||
Pk(id).
|
||||
Set("state", NodeGroupStateEnabled).
|
||||
@@ -41,7 +43,7 @@ func (this *NodeGroupDAO) EnableNodeGroup(id uint32) (rowsAffected int64, err er
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *NodeGroupDAO) DisableNodeGroup(id uint32) (rowsAffected int64, err error) {
|
||||
func (this *NodeGroupDAO) DisableNodeGroup(id int64) (rowsAffected int64, err error) {
|
||||
return this.Query().
|
||||
Pk(id).
|
||||
Set("state", NodeGroupStateDisabled).
|
||||
@@ -49,7 +51,7 @@ func (this *NodeGroupDAO) DisableNodeGroup(id uint32) (rowsAffected int64, err e
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *NodeGroupDAO) FindEnabledNodeGroup(id uint32) (*NodeGroup, error) {
|
||||
func (this *NodeGroupDAO) FindEnabledNodeGroup(id int64) (*NodeGroup, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", NodeGroupStateEnabled).
|
||||
@@ -61,10 +63,61 @@ func (this *NodeGroupDAO) FindEnabledNodeGroup(id uint32) (*NodeGroup, error) {
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *NodeGroupDAO) FindNodeGroupName(id uint32) (string, error) {
|
||||
func (this *NodeGroupDAO) FindNodeGroupName(id int64) (string, error) {
|
||||
name, err := this.Query().
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindCol("")
|
||||
return name.(string), err
|
||||
}
|
||||
|
||||
// 创建分组
|
||||
func (this *NodeGroupDAO) CreateNodeGroup(clusterId int64, name string) (int64, error) {
|
||||
op := NewNodeGroupOperator()
|
||||
op.ClusterId = clusterId
|
||||
op.Name = name
|
||||
op.State = NodeGroupStateEnabled
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 修改分组
|
||||
func (this *NodeGroupDAO) UpdateNodeGroup(groupId int64, name string) error {
|
||||
if groupId <= 0 {
|
||||
return errors.New("invalid groupId")
|
||||
}
|
||||
op := NewNodeGroupOperator()
|
||||
op.Id = groupId
|
||||
op.Name = name
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 查询所有分组
|
||||
func (this *NodeGroupDAO) FindAllEnabledGroupsWithClusterId(clusterId int64) (result []*NodeGroup, err error) {
|
||||
_, err = this.Query().
|
||||
State(NodeGroupStateEnabled).
|
||||
Attr("clusterId", clusterId).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 保存排序
|
||||
func (this *NodeGroupDAO) UpdateGroupOrders(groupIds []int64) error {
|
||||
for index, groupId := range groupIds {
|
||||
_, err := this.Query().
|
||||
Pk(groupId).
|
||||
Set("order", len(groupIds)-index).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package models
|
||||
type NodeGroup struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 名称
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
Order uint32 `field:"order"` // 排序
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
State uint8 `field:"state"` // 状态
|
||||
@@ -12,6 +13,7 @@ type NodeGroup struct {
|
||||
type NodeGroupOperator struct {
|
||||
Id interface{} // ID
|
||||
Name interface{} // 名称
|
||||
ClusterId interface{} // 集群ID
|
||||
Order interface{} // 排序
|
||||
CreatedAt interface{} // 创建时间
|
||||
State interface{} // 状态
|
||||
|
||||
Reference in New Issue
Block a user