Files
EdgeAPI/internal/rpc/services/service_node_threshold.go

173 lines
5.5 KiB
Go
Raw Normal View History

2024-05-17 18:27:26 +08:00
// Copyright 2021 GoEdge CDN goedge.cdn@gmail.com. All rights reserved.
2021-05-04 21:02:31 +08:00
package services
import (
"context"
2024-07-27 14:15:25 +08:00
2021-05-04 21:02:31 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
2021-05-05 19:50:55 +08:00
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
2021-05-04 21:02:31 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/types"
)
// NodeThresholdService 节点阈值服务
type NodeThresholdService struct {
BaseService
}
// CreateNodeThreshold 创建阈值
func (this *NodeThresholdService) CreateNodeThreshold(ctx context.Context, req *pb.CreateNodeThresholdRequest) (*pb.CreateNodeThresholdResponse, error) {
2022-07-22 14:35:17 +08:00
_, err := this.ValidateAdmin(ctx)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
var tx = this.NullTx()
2021-05-05 19:50:55 +08:00
thresholdId, err := models.SharedNodeThresholdDAO.CreateThreshold(tx, rpcutils.UserTypeNode, req.NodeClusterId, req.NodeId, req.Item, req.Param, req.Operator, req.ValueJSON, req.Message, req.SumMethod, req.Duration, req.DurationUnit, req.NotifyDuration)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
return &pb.CreateNodeThresholdResponse{NodeThresholdId: thresholdId}, nil
}
// UpdateNodeThreshold 创建阈值
func (this *NodeThresholdService) UpdateNodeThreshold(ctx context.Context, req *pb.UpdateNodeThresholdRequest) (*pb.RPCSuccess, error) {
2022-07-22 14:35:17 +08:00
_, err := this.ValidateAdmin(ctx)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
var tx = this.NullTx()
2021-05-05 19:50:55 +08:00
err = models.SharedNodeThresholdDAO.UpdateThreshold(tx, req.NodeThresholdId, req.Item, req.Param, req.Operator, req.ValueJSON, req.Message, req.SumMethod, req.Duration, req.DurationUnit, req.NotifyDuration, req.IsOn)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
return this.Success()
}
// DeleteNodeThreshold 删除阈值
func (this *NodeThresholdService) DeleteNodeThreshold(ctx context.Context, req *pb.DeleteNodeThresholdRequest) (*pb.RPCSuccess, error) {
2022-07-22 14:35:17 +08:00
_, err := this.ValidateAdmin(ctx)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
var tx = this.NullTx()
err = models.SharedNodeThresholdDAO.DisableNodeThreshold(tx, req.NodeThresholdId)
if err != nil {
return nil, err
}
return this.Success()
}
// FindAllEnabledNodeThresholds 查询阈值
func (this *NodeThresholdService) FindAllEnabledNodeThresholds(ctx context.Context, req *pb.FindAllEnabledNodeThresholdsRequest) (*pb.FindAllEnabledNodeThresholdsResponse, error) {
2022-07-22 14:35:17 +08:00
_, err := this.ValidateAdmin(ctx)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
var tx = this.NullTx()
pbThresholds := []*pb.NodeThreshold{}
2021-05-05 19:50:55 +08:00
thresholds, err := models.SharedNodeThresholdDAO.FindAllEnabledThresholds(tx, req.Role, req.NodeClusterId, req.NodeId)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
for _, threshold := range thresholds {
2021-05-05 19:50:55 +08:00
// 节点信息
var pbNode *pb.Node = nil
if threshold.NodeId > 0 {
nodeName, err := models.SharedNodeDAO.FindNodeName(tx, int64(threshold.NodeId))
if err != nil {
return nil, err
}
if len(nodeName) == 0 {
continue
}
pbNode = &pb.Node{
Id: int64(threshold.NodeId),
Name: nodeName,
}
}
2021-05-04 21:02:31 +08:00
pbThresholds = append(pbThresholds, &pb.NodeThreshold{
2021-05-05 19:50:55 +08:00
Id: int64(threshold.Id),
ClusterId: int64(threshold.ClusterId),
Node: pbNode,
Item: threshold.Item,
Param: threshold.Param,
Operator: threshold.Operator,
2022-03-22 19:30:30 +08:00
ValueJSON: threshold.Value,
2021-05-05 19:50:55 +08:00
Message: threshold.Message,
Duration: types.Int32(threshold.Duration),
DurationUnit: threshold.DurationUnit,
SumMethod: threshold.SumMethod,
NotifyDuration: int32(threshold.NotifyDuration),
2022-03-22 21:45:07 +08:00
IsOn: threshold.IsOn,
2021-05-04 21:02:31 +08:00
})
}
return &pb.FindAllEnabledNodeThresholdsResponse{NodeThresholds: pbThresholds}, nil
}
// CountAllEnabledNodeThresholds 计算阈值数量
func (this *NodeThresholdService) CountAllEnabledNodeThresholds(ctx context.Context, req *pb.CountAllEnabledNodeThresholdsRequest) (*pb.RPCCountResponse, error) {
2022-07-22 14:35:17 +08:00
_, err := this.ValidateAdmin(ctx)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
var tx = this.NullTx()
2021-06-27 21:59:37 +08:00
count, err := models.SharedNodeThresholdDAO.CountAllEnabledThresholds(tx, req.Role, req.NodeClusterId, req.NodeId)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
return this.SuccessCount(count)
}
// FindEnabledNodeThreshold 查询单个阈值详情
func (this *NodeThresholdService) FindEnabledNodeThreshold(ctx context.Context, req *pb.FindEnabledNodeThresholdRequest) (*pb.FindEnabledNodeThresholdResponse, error) {
2022-07-22 14:35:17 +08:00
_, err := this.ValidateAdmin(ctx)
2021-05-04 21:02:31 +08:00
if err != nil {
return nil, err
}
var tx = this.NullTx()
threshold, err := models.SharedNodeThresholdDAO.FindEnabledNodeThreshold(tx, req.NodeThresholdId)
if err != nil {
return nil, err
}
if threshold == nil {
return &pb.FindEnabledNodeThresholdResponse{NodeThreshold: nil}, nil
}
2021-05-05 19:50:55 +08:00
// 节点信息
var pbNode *pb.Node = nil
if threshold.NodeId > 0 {
nodeName, err := models.SharedNodeDAO.FindNodeName(tx, int64(threshold.NodeId))
if err != nil {
return nil, err
}
if len(nodeName) == 0 {
return &pb.FindEnabledNodeThresholdResponse{NodeThreshold: nil}, nil
}
pbNode = &pb.Node{
Id: int64(threshold.NodeId),
Name: nodeName,
}
}
2021-05-04 21:02:31 +08:00
return &pb.FindEnabledNodeThresholdResponse{NodeThreshold: &pb.NodeThreshold{
2021-05-05 19:50:55 +08:00
Id: int64(threshold.Id),
ClusterId: int64(threshold.ClusterId),
Node: pbNode,
Item: threshold.Item,
Param: threshold.Param,
Operator: threshold.Operator,
2022-03-22 19:30:30 +08:00
ValueJSON: threshold.Value,
2021-05-05 19:50:55 +08:00
Message: threshold.Message,
Duration: types.Int32(threshold.Duration),
DurationUnit: threshold.DurationUnit,
SumMethod: threshold.SumMethod,
NotifyDuration: int32(threshold.NotifyDuration),
2022-03-22 21:45:07 +08:00
IsOn: threshold.IsOn,
2021-05-04 21:02:31 +08:00
}}, nil
}