阶段性提交

This commit is contained in:
GoEdgeLab
2021-06-27 21:59:37 +08:00
parent 572fc6a20b
commit 8ba4c9e774
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)
}