diff --git a/internal/db/models/node_dao.go b/internal/db/models/node_dao.go index b2762420..03150ad3 100644 --- a/internal/db/models/node_dao.go +++ b/internal/db/models/node_dao.go @@ -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 { diff --git a/internal/db/models/node_group_dao.go b/internal/db/models/node_group_dao.go index 80cd9efe..fedafa04 100644 --- a/internal/db/models/node_group_dao.go +++ b/internal/db/models/node_group_dao.go @@ -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 +} diff --git a/internal/db/models/node_group_model.go b/internal/db/models/node_group_model.go index 0965ae2b..80a3592f 100644 --- a/internal/db/models/node_group_model.go +++ b/internal/db/models/node_group_model.go @@ -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{} // 状态 diff --git a/internal/nodes/api_node.go b/internal/nodes/api_node.go index d05af269..6612154c 100644 --- a/internal/nodes/api_node.go +++ b/internal/nodes/api_node.go @@ -171,6 +171,7 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err pb.RegisterNodeLogServiceServer(rpcServer, &services.NodeLogService{}) pb.RegisterHTTPAccessLogServiceServer(rpcServer, &services.HTTPAccessLogService{}) pb.RegisterMessageServiceServer(rpcServer, &services.MessageService{}) + pb.RegisterNodeGroupServiceServer(rpcServer, &services.NodeGroupService{}) err := rpcServer.Serve(listener) if err != nil { return errors.New("[API]start rpc failed: " + err.Error()) diff --git a/internal/rpc/services/service_node.go b/internal/rpc/services/service_node.go index cc22d17a..22abb951 100644 --- a/internal/rpc/services/service_node.go +++ b/internal/rpc/services/service_node.go @@ -102,7 +102,7 @@ func (this *NodeService) CountAllEnabledNodesMatch(ctx context.Context, req *pb. if err != nil { return nil, err } - count, err := models.SharedNodeDAO.CountAllEnabledNodesMatch(req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState)) + count, err := models.SharedNodeDAO.CountAllEnabledNodesMatch(req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState), req.Keyword, req.GroupId) if err != nil { return nil, err } @@ -115,7 +115,7 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List if err != nil { return nil, err } - nodes, err := models.SharedNodeDAO.ListEnabledNodesMatch(req.Offset, req.Size, req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState)) + nodes, err := models.SharedNodeDAO.ListEnabledNodesMatch(req.Offset, req.Size, req.ClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState), req.Keyword, req.GroupId) if err != nil { return nil, err } @@ -790,3 +790,18 @@ func (this *NodeService) UpdateNodeLogin(ctx context.Context, req *pb.UpdateNode return rpcutils.RPCUpdateSuccess() } + +// 计算某个节点分组内的节点数量 +func (this *NodeService) CountAllEnabledNodesWithGroupId(ctx context.Context, req *pb.CountAllEnabledNodesWithGroupIdRequest) (*pb.CountAllEnabledNodesWithGroupIdResponse, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + count, err := models.SharedNodeDAO.CountAllEnabledNodesWithGroupId(req.GroupId) + if err != nil { + return nil, err + } + return &pb.CountAllEnabledNodesWithGroupIdResponse{Count: count}, nil +} diff --git a/internal/rpc/services/service_node_group.go b/internal/rpc/services/service_node_group.go new file mode 100644 index 00000000..031e8691 --- /dev/null +++ b/internal/rpc/services/service_node_group.go @@ -0,0 +1,122 @@ +package services + +import ( + "context" + "github.com/TeaOSLab/EdgeAPI/internal/db/models" + rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" +) + +// 节点分组相关服务 +type NodeGroupService struct { +} + +// 创建分组 +func (this *NodeGroupService) CreateNodeGroup(ctx context.Context, req *pb.CreateNodeGroupRequest) (*pb.CreateNodeGroupResponse, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + groupId, err := models.SharedNodeGroupDAO.CreateNodeGroup(req.ClusterId, req.Name) + if err != nil { + return nil, err + } + return &pb.CreateNodeGroupResponse{GroupId: groupId}, nil +} + +// 修改分组 +func (this *NodeGroupService) UpdateNodeGroup(ctx context.Context, req *pb.UpdateNodeGroupRequest) (*pb.RPCUpdateSuccess, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + err = models.SharedNodeGroupDAO.UpdateNodeGroup(req.GroupId, req.Name) + if err != nil { + return nil, err + } + + return rpcutils.RPCUpdateSuccess() +} + +// 删除分组 +func (this *NodeGroupService) DeleteNodeGroup(ctx context.Context, req *pb.DeleteNodeGroupRequest) (*pb.RPCDeleteSuccess, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + _, err = models.SharedNodeGroupDAO.DisableNodeGroup(req.GroupId) + if err != nil { + return nil, err + } + + return rpcutils.RPCDeleteSuccess() +} + +// 查询所有分组 +func (this *NodeGroupService) FindAllEnabledNodeGroupsWithClusterId(ctx context.Context, req *pb.FindAllEnabledNodeGroupsWithClusterIdRequest) (*pb.FindAllEnabledNodeGroupsWithClusterIdResponse, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + groups, err := models.SharedNodeGroupDAO.FindAllEnabledGroupsWithClusterId(req.ClusterId) + if err != nil { + return nil, err + } + result := []*pb.NodeGroup{} + for _, group := range groups { + result = append(result, &pb.NodeGroup{ + Id: int64(group.Id), + Name: group.Name, + }) + } + return &pb.FindAllEnabledNodeGroupsWithClusterIdResponse{Groups: result}, nil +} + +// 修改分组排序 +func (this *NodeGroupService) UpdateNodeGroupOrders(ctx context.Context, req *pb.UpdateNodeGroupOrdersRequest) (*pb.RPCUpdateSuccess, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + err = models.SharedNodeGroupDAO.UpdateGroupOrders(req.GroupIds) + if err != nil { + return nil, err + } + return rpcutils.RPCUpdateSuccess() +} + +// 查找单个分组信息 +func (this *NodeGroupService) FindEnabledNodeGroup(ctx context.Context, req *pb.FindEnabledNodeGroupRequest) (*pb.FindEnabledNodeGroupResponse, error) { + // 校验请求 + _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) + if err != nil { + return nil, err + } + + group, err := models.SharedNodeGroupDAO.FindEnabledNodeGroup(req.GroupId) + if err != nil { + return nil, err + } + if group == nil { + return &pb.FindEnabledNodeGroupResponse{ + Group: nil, + }, nil + } + + return &pb.FindEnabledNodeGroupResponse{ + Group: &pb.NodeGroup{ + Id: int64(group.Id), + Name: group.Name, + }, + }, nil +}