mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-03 22:06:36 +08:00
实现监控节点在线状态
This commit is contained in:
@@ -25,6 +25,7 @@ type MessageType = string
|
||||
|
||||
const (
|
||||
MessageTypeHealthCheckFail MessageType = "HealthCheckFail"
|
||||
MessageTypeNodeInactive MessageType = "NodeInactive"
|
||||
)
|
||||
|
||||
type MessageDAO dbs.DAO
|
||||
@@ -84,6 +85,12 @@ func (this *MessageDAO) CreateClusterMessage(clusterId int64, messageType Messag
|
||||
return err
|
||||
}
|
||||
|
||||
// 创建节点消息
|
||||
func (this *MessageDAO) CreateNodeMessage(clusterId int64, nodeId int64, messageType MessageType, level string, body string, paramsJSON []byte) error {
|
||||
_, err := this.createMessage(clusterId, nodeId, messageType, level, body, paramsJSON)
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除某天之前的消息
|
||||
func (this *MessageDAO) DeleteMessagesBeforeDay(dayTime time.Time) error {
|
||||
day := timeutil.Format("Ymd", dayTime)
|
||||
|
||||
@@ -294,6 +294,18 @@ func (this *NodeDAO) FindAllEnabledNodesWithClusterId(clusterId int64) (result [
|
||||
return
|
||||
}
|
||||
|
||||
// 取得一个集群离线的节点
|
||||
func (this *NodeDAO) FindAllInactiveNodesWithClusterId(clusterId int64) (result []*Node, err error) {
|
||||
_, err = this.Query().
|
||||
State(NodeStateEnabled).
|
||||
Attr("clusterId", clusterId).
|
||||
Attr("isOn", true). // 只监控启用的节点
|
||||
Where("(status IS NULL OR (JSON_EXTRACT(status, '$.isActive')=false AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>10) OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>120)").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 计算节点数量
|
||||
func (this *NodeDAO) CountAllEnabledNodesMatch(clusterId int64, installState configutils.BoolState, activeState configutils.BoolState) (int64, error) {
|
||||
query := this.Query()
|
||||
@@ -336,6 +348,20 @@ func (this *NodeDAO) UpdateNodeStatus(nodeId int64, statusJSON []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 更改节点在线状态
|
||||
func (this *NodeDAO) UpdateNodeIsActive(nodeId int64, isActive bool) error {
|
||||
b := "true"
|
||||
if !isActive {
|
||||
b = "false"
|
||||
}
|
||||
_, err := this.Query().
|
||||
Pk(nodeId).
|
||||
Where("status IS NOT NULL").
|
||||
Set("status", dbs.SQL("JSON_SET(status, '$.isActive', "+b+")")).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 设置节点安装状态
|
||||
func (this *NodeDAO) UpdateNodeIsInstalled(nodeId int64, isInstalled bool) error {
|
||||
_, err := this.Query().
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -26,3 +27,15 @@ func (this *Node) DecodeInstallStatus() (*NodeInstallStatus, error) {
|
||||
return status, nil
|
||||
}
|
||||
|
||||
// 节点状态
|
||||
func (this *Node) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
|
||||
if len(this.Status) == 0 || this.Status == "null" {
|
||||
return nil, nil
|
||||
}
|
||||
status := &nodeconfigs.NodeStatus{}
|
||||
err := json.Unmarshal([]byte(this.Status), status)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user