阶段性提交

This commit is contained in:
刘祥超
2021-06-27 21:59:37 +08:00
parent 4869c11d60
commit 5774beda6f
26 changed files with 794 additions and 14 deletions

View File

@@ -39,7 +39,7 @@ func init() {
})
}
// 启用条目
// EnableDBNode 启用条目
func (this *DBNodeDAO) EnableDBNode(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -48,7 +48,7 @@ func (this *DBNodeDAO) EnableDBNode(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableDBNode 禁用条目
func (this *DBNodeDAO) DisableDBNode(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -57,7 +57,7 @@ func (this *DBNodeDAO) DisableDBNode(tx *dbs.Tx, id int64) error {
return err
}
// 查找启用中的条目
// FindEnabledDBNode 查找启用中的条目
func (this *DBNodeDAO) FindEnabledDBNode(tx *dbs.Tx, id int64) (*DBNode, error) {
result, err := this.Query(tx).
Pk(id).
@@ -71,7 +71,7 @@ func (this *DBNodeDAO) FindEnabledDBNode(tx *dbs.Tx, id int64) (*DBNode, error)
return node, nil
}
// 根据主键查找名称
// FindDBNodeName 根据主键查找名称
func (this *DBNodeDAO) FindDBNodeName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -79,14 +79,14 @@ func (this *DBNodeDAO) FindDBNodeName(tx *dbs.Tx, id int64) (string, error) {
FindStringCol("")
}
// 计算可用的节点数量
// CountAllEnabledNodes 计算可用的节点数量
func (this *DBNodeDAO) CountAllEnabledNodes(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(DBNodeStateEnabled).
Count()
}
// 获取单页的节点
// ListEnabledNodes 获取单页的节点
func (this *DBNodeDAO) ListEnabledNodes(tx *dbs.Tx, offset int64, size int64) (result []*DBNode, err error) {
_, err = this.Query(tx).
State(DBNodeStateEnabled).
@@ -101,7 +101,7 @@ func (this *DBNodeDAO) ListEnabledNodes(tx *dbs.Tx, offset int64, size int64) (r
return
}
// 创建节点
// CreateDBNode 创建节点
func (this *DBNodeDAO) CreateDBNode(tx *dbs.Tx, isOn bool, name string, description string, host string, port int32, database string, username string, password string, charset string) (int64, error) {
op := NewDBNodeOperator()
op.State = NodeStateEnabled
@@ -121,7 +121,7 @@ func (this *DBNodeDAO) CreateDBNode(tx *dbs.Tx, isOn bool, name string, descript
return types.Int64(op.Id), nil
}
// 修改节点
// UpdateNode 修改节点
func (this *DBNodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, isOn bool, name string, description string, host string, port int32, database string, username string, password string, charset string) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
@@ -141,7 +141,7 @@ func (this *DBNodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, isOn bool, name stri
return err
}
// 查找所有可用的数据库节点
// FindAllEnabledAndOnDBNodes 查找所有可用的数据库节点
func (this *DBNodeDAO) FindAllEnabledAndOnDBNodes(tx *dbs.Tx) (result []*DBNode, err error) {
_, err = this.Query(tx).
State(DBNodeStateEnabled).
@@ -155,7 +155,7 @@ func (this *DBNodeDAO) FindAllEnabledAndOnDBNodes(tx *dbs.Tx) (result []*DBNode,
return
}
// 加密密码
// EncodePassword 加密密码
func (this *DBNodeDAO) EncodePassword(password string) string {
if strings.HasPrefix(password, DBNodePasswordEncodedPrefix) {
return password
@@ -164,7 +164,7 @@ func (this *DBNodeDAO) EncodePassword(password string) string {
return DBNodePasswordEncodedPrefix + encodedString
}
// 解密密码
// DecodePassword 解密密码
func (this *DBNodeDAO) DecodePassword(password string) string {
if !strings.HasPrefix(password, DBNodePasswordEncodedPrefix) {
return password

View File

@@ -0,0 +1,174 @@
package metrics
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
MetricItemStateEnabled = 1 // 已启用
MetricItemStateDisabled = 0 // 已禁用
)
type MetricItemDAO dbs.DAO
func NewMetricItemDAO() *MetricItemDAO {
return dbs.NewDAO(&MetricItemDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeMetricItems",
Model: new(MetricItem),
PkName: "id",
},
}).(*MetricItemDAO)
}
var SharedMetricItemDAO *MetricItemDAO
func init() {
dbs.OnReady(func() {
SharedMetricItemDAO = NewMetricItemDAO()
})
}
// EnableMetricItem 启用条目
func (this *MetricItemDAO) EnableMetricItem(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", MetricItemStateEnabled).
Update()
return err
}
// DisableMetricItem 禁用条目
func (this *MetricItemDAO) DisableMetricItem(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", MetricItemStateDisabled).
Update()
return err
}
// FindEnabledMetricItem 查找启用中的条目
func (this *MetricItemDAO) FindEnabledMetricItem(tx *dbs.Tx, id int64) (*MetricItem, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", MetricItemStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*MetricItem), err
}
// FindMetricItemName 根据主键查找名称
func (this *MetricItemDAO) FindMetricItemName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreateItem 创建指标
func (this *MetricItemDAO) CreateItem(tx *dbs.Tx, code string, category string, name string, keys []string, period int32, periodUnit string, value string) (int64, error) {
op := NewMetricItemOperator()
op.Code = code
op.Category = category
op.Name = name
if len(keys) > 0 {
keysJSON, err := json.Marshal(keys)
if err != nil {
return 0, err
}
op.Keys = keysJSON
} else {
op.Keys = "[]"
}
op.Period = period
op.PeriodUnit = periodUnit
op.Value = value
op.IsOn = true
op.State = MetricItemStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateItem 修改\指标
func (this *MetricItemDAO) UpdateItem(tx *dbs.Tx, itemId int64, name string, keys []string, period int32, periodUnit string, value string, isOn bool) error {
if itemId <= 0 {
return errors.New("invalid itemId")
}
op := NewMetricItemOperator()
op.Id = itemId
op.Name = name
if len(keys) > 0 {
keysJSON, err := json.Marshal(keys)
if err != nil {
return err
}
op.Keys = keysJSON
} else {
op.Keys = "[]"
}
op.Period = period
op.PeriodUnit = periodUnit
op.Value = value
op.IsOn = isOn
return this.Save(tx, op)
}
// CountEnabledItems 计算指标的数量
func (this *MetricItemDAO) CountEnabledItems(tx *dbs.Tx, category serverconfigs.MetricItemCategory) (int64, error) {
return this.Query(tx).
State(MetricItemStateEnabled).
Attr("userId", 0).
Attr("category", category).
Count()
}
// ListEnabledItems 列出单页指标
func (this *MetricItemDAO) ListEnabledItems(tx *dbs.Tx, category serverconfigs.MetricItemCategory, offset int64, size int64) (result []*MetricItem, err error) {
_, err = this.Query(tx).
State(MetricItemStateEnabled).
Attr("userId", 0).
Attr("category", category).
Offset(offset).
Limit(size).
DescPk().
Slice(&result).
FindAll()
return
}
// ComposeItemConfig 组合指标配置
func (this *MetricItemDAO) ComposeItemConfig(tx *dbs.Tx, itemId int64) (*serverconfigs.MetricItemConfig, error) {
if itemId <= 0 {
return nil, nil
}
one, err := this.Query(tx).
Pk(itemId).
State(MetricItemStateEnabled).
Find()
if err != nil {
return nil, err
}
if one == nil {
return nil, nil
}
var item = one.(*MetricItem)
var config = &serverconfigs.MetricItemConfig{
Id: int64(item.Id),
IsOn: item.IsOn == 1,
Period: types.Int(item.Period),
PeriodUnit: item.PeriodUnit,
Category: item.Category,
Value: item.Value,
Keys: item.DecodeKeys(),
}
return config, nil
}

View File

@@ -0,0 +1,6 @@
package metrics
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,36 @@
package metrics
// MetricItem 指标定义
type MetricItem struct {
Id uint64 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
Code string `field:"code"` // 代号(用来区分是否内置)
Category string `field:"category"` // 类型比如http, tcp等
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
Name string `field:"name"` // 指标名称
Keys string `field:"keys"` // 统计的Key
Period uint32 `field:"period"` // 周期
PeriodUnit string `field:"periodUnit"` // 周期单位
Value string `field:"value"` // 值运算
State uint8 `field:"state"` // 状态
}
type MetricItemOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
Code interface{} // 代号(用来区分是否内置)
Category interface{} // 类型比如http, tcp等
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
Name interface{} // 指标名称
Keys interface{} // 统计的Key
Period interface{} // 周期
PeriodUnit interface{} // 周期单位
Value interface{} // 值运算
State interface{} // 状态
}
func NewMetricItemOperator() *MetricItemOperator {
return &MetricItemOperator{}
}

View File

@@ -0,0 +1,11 @@
package metrics
import "encoding/json"
func (this *MetricItem) DecodeKeys() []string {
var result []string
if len(this.Keys) > 0 {
_ = json.Unmarshal([]byte(this.Keys), &result)
}
return result
}

View File

@@ -0,0 +1,28 @@
package metrics
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
type MetricKeyDAO dbs.DAO
func NewMetricKeyDAO() *MetricKeyDAO {
return dbs.NewDAO(&MetricKeyDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeMetricKeys",
Model: new(MetricKey),
PkName: "id",
},
}).(*MetricKeyDAO)
}
var SharedMetricKeyDAO *MetricKeyDAO
func init() {
dbs.OnReady(func() {
SharedMetricKeyDAO = NewMetricKeyDAO()
})
}

View File

@@ -0,0 +1,6 @@
package metrics
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,20 @@
package metrics
// MetricKey 指标键值
type MetricKey struct {
Id uint64 `field:"id"` // ID
ItemId uint64 `field:"itemId"` // 指标ID
Value string `field:"value"` // 值
Hash string `field:"hash"` // 对值进行Hash
}
type MetricKeyOperator struct {
Id interface{} // ID
ItemId interface{} // 指标ID
Value interface{} // 值
Hash interface{} // 对值进行Hash
}
func NewMetricKeyOperator() *MetricKeyOperator {
return &MetricKeyOperator{}
}

View File

@@ -0,0 +1 @@
package metrics

View File

@@ -0,0 +1,28 @@
package metrics
import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
type MetricStatDAO dbs.DAO
func NewMetricStatDAO() *MetricStatDAO {
return dbs.NewDAO(&MetricStatDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeMetricStats",
Model: new(MetricStat),
PkName: "id",
},
}).(*MetricStatDAO)
}
var SharedMetricStatDAO *MetricStatDAO
func init() {
dbs.OnReady(func() {
SharedMetricStatDAO = NewMetricStatDAO()
})
}

View File

@@ -0,0 +1,6 @@
package metrics
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,28 @@
package metrics
// MetricStat 指标统计数据
type MetricStat struct {
Id uint64 `field:"id"` // ID
ClusterId uint32 `field:"clusterId"` // 集群ID
NodeId uint32 `field:"nodeId"` // 节点ID
ServerId uint32 `field:"serverId"` // 服务ID
ItemId uint64 `field:"itemId"` // 指标
KeyId uint64 `field:"keyId"` // 指标键ID
Value float64 `field:"value"` // 数值
Minute string `field:"minute"` // 分钟值YYYYMMDDHHII
}
type MetricStatOperator struct {
Id interface{} // ID
ClusterId interface{} // 集群ID
NodeId interface{} // 节点ID
ServerId interface{} // 服务ID
ItemId interface{} // 指标
KeyId interface{} // 指标键ID
Value interface{} // 数值
Minute interface{} // 分钟值YYYYMMDDHHII
}
func NewMetricStatOperator() *MetricStatOperator {
return &MetricStatOperator{}
}

View File

@@ -0,0 +1 @@
package metrics

View File

@@ -0,0 +1,105 @@
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"
)
const (
NodeClusterMetricItemStateEnabled = 1 // 已启用
NodeClusterMetricItemStateDisabled = 0 // 已禁用
)
type NodeClusterMetricItemDAO dbs.DAO
func NewNodeClusterMetricItemDAO() *NodeClusterMetricItemDAO {
return dbs.NewDAO(&NodeClusterMetricItemDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeNodeClusterMetricItems",
Model: new(NodeClusterMetricItem),
PkName: "id",
},
}).(*NodeClusterMetricItemDAO)
}
var SharedNodeClusterMetricItemDAO *NodeClusterMetricItemDAO
func init() {
dbs.OnReady(func() {
SharedNodeClusterMetricItemDAO = NewNodeClusterMetricItemDAO()
})
}
// EnableNodeClusterMetricItem 启用条目
func (this *NodeClusterMetricItemDAO) EnableNodeClusterMetricItem(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NodeClusterMetricItemStateEnabled).
Update()
return err
}
// DisableNodeClusterMetricItem 禁用条目
func (this *NodeClusterMetricItemDAO) DisableNodeClusterMetricItem(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NodeClusterMetricItemStateDisabled).
Update()
return err
}
// FindEnabledNodeClusterMetricItem 查找启用中的条目
func (this *NodeClusterMetricItemDAO) FindEnabledNodeClusterMetricItem(tx *dbs.Tx, id uint32) (*NodeClusterMetricItem, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NodeClusterMetricItemStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*NodeClusterMetricItem), err
}
// DisableClusterItem 禁用某个集群的指标
func (this *NodeClusterMetricItemDAO) DisableClusterItem(tx *dbs.Tx, clusterId int64, itemId int64) error {
return this.Query(tx).
Attr("clusterId", clusterId).
Attr("itemId", itemId).
State(NodeClusterMetricItemStateEnabled).
Set("state", NodeClusterMetricItemStateDisabled).
UpdateQuickly()
}
// EnableClusterItem 启用某个集群的指标
func (this *NodeClusterMetricItemDAO) EnableClusterItem(tx *dbs.Tx, clusterId int64, itemId int64) error {
if clusterId <= 0 || itemId <= 0 {
return errors.New("clusterId or itemId should not be 0")
}
var op = NewNodeClusterMetricItemOperator()
op.ClusterId = clusterId
op.ItemId = itemId
op.IsOn = true
return this.Save(tx, op)
}
// FindAllClusterItems 查找某个集群的指标
func (this *NodeClusterMetricItemDAO) FindAllClusterItems(tx *dbs.Tx, clusterId int64) (result []*NodeClusterMetricItem, err error) {
_, err = this.Query(tx).
Attr("clusterId", clusterId).
State(NodeClusterMetricItemStateEnabled).
DescPk().
Slice(&result).
FindAll()
return
}
// CountAllClusterItems 计算集群中指标数量
func (this *NodeClusterMetricItemDAO) CountAllClusterItems(tx *dbs.Tx, clusterId int64) (int64, error) {
return this.Query(tx).
Attr("clusterId", clusterId).
State(NodeClusterMetricItemStateEnabled).
Count()
}

View File

@@ -0,0 +1,6 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,22 @@
package models
// NodeClusterMetricItem 集群使用的指标
type NodeClusterMetricItem struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
ClusterId uint32 `field:"clusterId"` // 集群ID
ItemId uint64 `field:"itemId"` // 指标ID
State uint8 `field:"state"` // 是否启用
}
type NodeClusterMetricItemOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
ClusterId interface{} // 集群ID
ItemId interface{} // 指标ID
State interface{} // 是否启用
}
func NewNodeClusterMetricItemOperator() *NodeClusterMetricItemOperator {
return &NodeClusterMetricItemOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -640,6 +640,9 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*nodeconfigs.N
}
}
// 指标
return config, nil
}

View File

@@ -214,6 +214,7 @@ func (this *NodeIPAddressDAO) FindNodeAccessIPAddresses(tx *dbs.Tx, nodeId int64
role = nodeconfigs.NodeRoleNode
}
_, err = this.Query(tx).
Attr("role", role).
Attr("nodeId", nodeId).
State(NodeIPAddressStateEnabled).
Attr("canAccess", true).

View File

@@ -171,11 +171,12 @@ func (this *NodeThresholdDAO) FindAllEnabledAndOnNodeThresholds(tx *dbs.Tx, role
}
// CountAllEnabledThresholds 计算阈值的数量
func (this *NodeThresholdDAO) CountAllEnabledThresholds(tx *dbs.Tx, clusterId int64, nodeId int64) (int64, error) {
func (this *NodeThresholdDAO) CountAllEnabledThresholds(tx *dbs.Tx, role string, clusterId int64, nodeId int64) (int64, error) {
if clusterId <= 0 && nodeId <= 0 {
return 0, nil
}
query := this.Query(tx)
query.Attr("role", role)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
}

View File

@@ -443,6 +443,16 @@ func (this *APINode) registerServices(server *grpc.Server) {
pb.RegisterHTTPAuthPolicyServiceServer(server, instance)
this.rest(instance)
}
{
instance := this.serviceInstance(&services.MetricItemService{}).(*services.MetricItemService)
pb.RegisterMetricItemServiceServer(server, instance)
this.rest(instance)
}
{
instance := this.serviceInstance(&services.NodeClusterMetricItemService{}).(*services.NodeClusterMetricItemService)
pb.RegisterNodeClusterMetricItemServiceServer(server, instance)
this.rest(instance)
}
// TODO check service names
for serviceName := range server.GetServiceInfo() {

View File

@@ -0,0 +1,132 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/metrics"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/types"
)
// MetricItemService 指标相关服务
type MetricItemService struct {
BaseService
}
// CreateMetricItem 创建指标
func (this *MetricItemService) CreateMetricItem(ctx context.Context, req *pb.CreateMetricItemRequest) (*pb.CreateMetricItemResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
itemId, err := metrics.SharedMetricItemDAO.CreateItem(tx, req.Code, req.Category, req.Name, req.Keys, req.Period, req.PeriodUnit, req.Value)
if err != nil {
return nil, err
}
return &pb.CreateMetricItemResponse{MetricItemId: itemId}, nil
}
// UpdateMetricItem 修改指标
func (this *MetricItemService) UpdateMetricItem(ctx context.Context, req *pb.UpdateMetricItemRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
err = metrics.SharedMetricItemDAO.UpdateItem(tx, req.MetricItemId, req.Name, req.Keys, req.Period, req.PeriodUnit, req.Value, req.IsOn)
if err != nil {
return nil, err
}
return this.Success()
}
// FindEnabledMetricItem 查找单个指标信息
func (this *MetricItemService) FindEnabledMetricItem(ctx context.Context, req *pb.FindEnabledMetricItemRequest) (*pb.FindEnabledMetricItemResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
item, err := metrics.SharedMetricItemDAO.FindEnabledMetricItem(tx, req.MetricItemId)
if err != nil {
return nil, err
}
if item == nil {
return &pb.FindEnabledMetricItemResponse{MetricItem: nil}, nil
}
return &pb.FindEnabledMetricItemResponse{MetricItem: &pb.MetricItem{
Id: int64(item.Id),
IsOn: item.IsOn == 1,
Code: item.Code,
Category: item.Category,
Name: item.Name,
Keys: item.DecodeKeys(),
Period: types.Int32(item.Period),
PeriodUnit: item.PeriodUnit,
Value: item.Value,
}}, nil
}
// CountAllEnabledMetricItems 计算指标数量
func (this *MetricItemService) CountAllEnabledMetricItems(ctx context.Context, req *pb.CountAllEnabledMetricItemsRequest) (*pb.RPCCountResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
count, err := metrics.SharedMetricItemDAO.CountEnabledItems(tx, req.Category)
if err != nil {
return nil, err
}
return this.SuccessCount(count)
}
// ListEnabledMetricItems 列出单页指标
func (this *MetricItemService) ListEnabledMetricItems(ctx context.Context, req *pb.ListEnabledMetricItemsRequest) (*pb.ListEnabledMetricItemsResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
items, err := metrics.SharedMetricItemDAO.ListEnabledItems(tx, req.Category, req.Offset, req.Size)
if err != nil {
return nil, err
}
var pbItems = []*pb.MetricItem{}
for _, item := range items {
pbItems = append(pbItems, &pb.MetricItem{
Id: int64(item.Id),
IsOn: item.IsOn == 1,
Code: item.Code,
Category: item.Category,
Name: item.Name,
Keys: item.DecodeKeys(),
Period: types.Int32(item.Period),
PeriodUnit: item.PeriodUnit,
Value: item.Value,
})
}
return &pb.ListEnabledMetricItemsResponse{MetricItems: pbItems}, nil
}
// DeleteMetricItem 删除指标
func (this *MetricItemService) DeleteMetricItem(ctx context.Context, req *pb.DeleteMetricItemRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
err = metrics.SharedMetricItemDAO.DisableMetricItem(tx, req.MetricItemId)
if err != nil {
return nil, err
}
return this.Success()
}

View File

@@ -9,7 +9,9 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeAPI/internal/tasks"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
@@ -887,3 +889,74 @@ func (this *NodeClusterService) FindLatestNodeClusters(ctx context.Context, req
}
return &pb.FindLatestNodeClustersResponse{NodeClusters: pbClusters}, nil
}
// FindEnabledNodeClusterConfigInfo 取得集群的配置概要信息
func (this *NodeClusterService) FindEnabledNodeClusterConfigInfo(ctx context.Context, req *pb.FindEnabledNodeClusterConfigInfoRequest) (*pb.FindEnabledNodeClusterConfigInfoResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(tx, req.NodeClusterId)
if err != nil {
return nil, err
}
if cluster == nil {
return &pb.FindEnabledNodeClusterConfigInfoResponse{}, nil
}
var result = &pb.FindEnabledNodeClusterConfigInfoResponse{}
// health check
if len(cluster.HealthCheck) > 0 {
healthCheckConfig := &serverconfigs.HealthCheckConfig{}
err = json.Unmarshal([]byte(cluster.HealthCheck), healthCheckConfig)
if err != nil {
return nil, err
}
result.HealthCheckIsOn = healthCheckConfig.IsOn
}
// firewall actions
countFirewallActions, err := models.SharedNodeClusterFirewallActionDAO.CountAllEnabledFirewallActions(tx, req.NodeClusterId)
if err != nil {
return nil, err
}
result.HasFirewallActions = countFirewallActions > 0
// thresholds
countThresholds, err := models.SharedNodeThresholdDAO.CountAllEnabledThresholds(tx, "node", req.NodeClusterId, 0)
if err != nil {
return nil, err
}
result.HasThresholds = countThresholds > 0
// message receivers
countReceivers, err := models.SharedMessageReceiverDAO.CountAllEnabledReceivers(tx, models.MessageTaskTarget{
ClusterId: req.NodeClusterId,
}, "")
if err != nil {
return nil, err
}
result.HasMessageReceivers = countReceivers > 0
// toa
if len(cluster.Toa) > 0 {
var toaConfig = &nodeconfigs.TOAConfig{}
err = json.Unmarshal([]byte(cluster.Toa), toaConfig)
if err != nil {
return nil, err
}
result.IsTOAEnabled = toaConfig.IsOn
}
// metric items
countMetricItems, err := models.SharedNodeClusterMetricItemDAO.CountAllClusterItems(tx, req.NodeClusterId)
if err != nil {
return nil, err
}
result.HasMetricItems = countMetricItems > 0
return result, nil
}

View File

@@ -0,0 +1,81 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/metrics"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/types"
)
// NodeClusterMetricItemService 集群指标
type NodeClusterMetricItemService struct {
BaseService
}
// EnableNodeClusterMetricItem 启用某个指标
func (this *NodeClusterMetricItemService) EnableNodeClusterMetricItem(ctx context.Context, req *pb.EnableNodeClusterMetricItemRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
err = models.SharedNodeClusterMetricItemDAO.EnableClusterItem(tx, req.NodeClusterId, req.MetricItemId)
if err != nil {
return nil, err
}
return this.Success()
}
// DisableNodeClusterMetricItem 禁用某个指标
func (this *NodeClusterMetricItemService) DisableNodeClusterMetricItem(ctx context.Context, req *pb.DisableNodeClusterMetricItemRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
err = models.SharedNodeClusterMetricItemDAO.DisableClusterItem(tx, req.NodeClusterId, req.MetricItemId)
if err != nil {
return nil, err
}
return this.Success()
}
// FindAllNodeClusterMetricItems 查找集群中所有指标
func (this *NodeClusterMetricItemService) FindAllNodeClusterMetricItems(ctx context.Context, req *pb.FindAllNodeClusterMetricItemsRequest) (*pb.FindAllNodeClusterMetricItemsResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
clusterItems, err := models.SharedNodeClusterMetricItemDAO.FindAllClusterItems(tx, req.NodeClusterId)
if err != nil {
return nil, err
}
var pbItems = []*pb.MetricItem{}
for _, clusterItem := range clusterItems {
item, err := metrics.SharedMetricItemDAO.FindEnabledMetricItem(tx, int64(clusterItem.ItemId))
if err != nil {
return nil, err
}
if item != nil {
pbItems = append(pbItems, &pb.MetricItem{
Id: int64(item.Id),
IsOn: item.IsOn == 1,
Code: item.Code,
Category: item.Category,
Name: item.Name,
Keys: item.DecodeKeys(),
Period: types.Int32(item.Period),
PeriodUnit: item.PeriodUnit,
Value: item.Value,
})
}
}
return &pb.FindAllNodeClusterMetricItemsResponse{MetricItems: pbItems}, nil
}

View File

@@ -114,7 +114,7 @@ func (this *NodeThresholdService) CountAllEnabledNodeThresholds(ctx context.Cont
}
var tx = this.NullTx()
count, err := models.SharedNodeThresholdDAO.CountAllEnabledThresholds(tx, req.NodeClusterId, req.NodeId)
count, err := models.SharedNodeThresholdDAO.CountAllEnabledThresholds(tx, req.Role, req.NodeClusterId, req.NodeId)
if err != nil {
return nil, err
}

File diff suppressed because one or more lines are too long