mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-09 03:50:26 +08:00
节点日志增加服务ID、是否修复等字段
This commit is contained in:
@@ -218,7 +218,7 @@ func (this *DBNodeInitializer) loop() error {
|
||||
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
|
||||
|
||||
// 创建节点日志
|
||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, NodeRoleDatabase, nodeId, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
||||
if createLogErr != nil {
|
||||
logs.Println("[NODE_LOG]" + createLogErr.Error())
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
@@ -34,8 +35,8 @@ func init() {
|
||||
}
|
||||
|
||||
// CreateLog 创建日志
|
||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, level string, tag string, description string, createdAt int64) error {
|
||||
hash := stringutil.Md5(nodeRole + "@" + strconv.FormatInt(nodeId, 10) + "@" + level + "@" + tag + "@" + description)
|
||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, serverId int64, level string, tag string, description string, createdAt int64) error {
|
||||
hash := stringutil.Md5(nodeRole + "@" + strconv.FormatInt(nodeId, 10) + "@" + strconv.FormatInt(serverId, 10) + "@" + level + "@" + tag + "@" + description)
|
||||
|
||||
// 检查是否在重复最后一条,避免重复创建
|
||||
lastLog, err := this.Query(tx).
|
||||
@@ -59,6 +60,7 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, l
|
||||
op := NewNodeLogOperator()
|
||||
op.Role = nodeRole
|
||||
op.NodeId = nodeId
|
||||
op.ServerId = serverId
|
||||
op.Level = level
|
||||
op.Tag = tag
|
||||
op.Description = description
|
||||
@@ -84,7 +86,7 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
||||
}
|
||||
|
||||
// CountNodeLogs 计算节点日志数量
|
||||
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
|
||||
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
|
||||
query := this.Query(tx).
|
||||
Attr("role", role)
|
||||
if nodeId > 0 {
|
||||
@@ -95,6 +97,9 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, day
|
||||
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
||||
}
|
||||
}
|
||||
if serverId > 0 {
|
||||
query.Attr("serverId", serverId)
|
||||
}
|
||||
if len(dayFrom) > 0 {
|
||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||
query.Gte("day", dayFrom)
|
||||
@@ -115,7 +120,18 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, day
|
||||
}
|
||||
|
||||
// ListNodeLogs 列出单页日志
|
||||
func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayFrom string, dayTo string, keyword string, level string, offset int64, size int64) (result []*NodeLog, err error) {
|
||||
func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
|
||||
role string,
|
||||
nodeId int64,
|
||||
serverId int64,
|
||||
allServers bool,
|
||||
dayFrom string,
|
||||
dayTo string,
|
||||
keyword string,
|
||||
level string,
|
||||
fixedState configutils.BoolState,
|
||||
offset int64,
|
||||
size int64) (result []*NodeLog, err error) {
|
||||
query := this.Query(tx).
|
||||
Attr("role", role)
|
||||
if nodeId > 0 {
|
||||
@@ -126,6 +142,16 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayF
|
||||
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
||||
}
|
||||
}
|
||||
if serverId > 0 {
|
||||
query.Attr("serverId", serverId)
|
||||
} else if allServers {
|
||||
query.Where("serverId>0")
|
||||
}
|
||||
if fixedState == configutils.BoolStateYes {
|
||||
query.Attr("isFixed", 1)
|
||||
} else if fixedState == configutils.BoolStateNo {
|
||||
query.Attr("isFixed", 0)
|
||||
}
|
||||
if len(dayFrom) > 0 {
|
||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||
query.Gte("day", dayFrom)
|
||||
@@ -149,3 +175,33 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayF
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateNodeLogFixed 设置节点日志为已修复
|
||||
func (this *NodeLogDAO) UpdateNodeLogFixed(tx *dbs.Tx, logId int64) error {
|
||||
if logId <= 0 {
|
||||
return errors.New("invalid logId")
|
||||
}
|
||||
|
||||
// 我们把相同内容的日志都置为已修复
|
||||
hash, err := this.Query(tx).
|
||||
Pk(logId).
|
||||
Result("hash").
|
||||
FindStringCol("")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(hash) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = this.Query(tx).
|
||||
Attr("hash", hash).
|
||||
Attr("isFixed", false).
|
||||
Set("isFixed", true).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ type NodeLog struct {
|
||||
ServerId uint32 `field:"serverId"` // 服务ID
|
||||
Hash string `field:"hash"` // 信息内容Hash
|
||||
Count uint32 `field:"count"` // 重复次数
|
||||
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
||||
}
|
||||
|
||||
type NodeLogOperator struct {
|
||||
@@ -27,6 +28,7 @@ type NodeLogOperator struct {
|
||||
ServerId interface{} // 服务ID
|
||||
Hash interface{} // 信息内容Hash
|
||||
Count interface{} // 重复次数
|
||||
IsFixed interface{} // 是否已处理
|
||||
}
|
||||
|
||||
func NewNodeLogOperator() *NodeLogOperator {
|
||||
|
||||
@@ -99,7 +99,7 @@ Loop:
|
||||
for {
|
||||
select {
|
||||
case log := <-logChan:
|
||||
err := models.SharedNodeLogDAO.CreateLog(nil, models.NodeRoleAPI, log.NodeId, log.Level, log.Tag, log.Description, log.CreatedAt)
|
||||
err := models.SharedNodeLogDAO.CreateLog(nil, models.NodeRoleAPI, log.NodeId, 0, log.Level, log.Tag, log.Description, log.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNo
|
||||
tx := this.NullTx()
|
||||
|
||||
for _, nodeLog := range req.NodeLogs {
|
||||
err := models.SharedNodeLogDAO.CreateLog(tx, nodeLog.Role, nodeLog.NodeId, nodeLog.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt)
|
||||
err := models.SharedNodeLogDAO.CreateLog(tx, nodeLog.Role, nodeLog.NodeId, nodeLog.ServerId, nodeLog.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNode
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId, req.DayFrom, req.DayTo, req.Keyword, req.Level)
|
||||
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId, req.ServerId, req.DayFrom, req.DayTo, req.Keyword, req.Level)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -56,7 +56,7 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
logs, err := models.SharedNodeLogDAO.ListNodeLogs(tx, req.Role, req.NodeId, req.DayFrom, req.DayTo, req.Keyword, req.Level, req.Offset, req.Size)
|
||||
logs, err := models.SharedNodeLogDAO.ListNodeLogs(tx, req.Role, req.NodeId, req.ServerId, req.AllServers, req.DayFrom, req.DayTo, req.Keyword, req.Level, types.Int8(req.FixedState), req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -64,14 +64,33 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
|
||||
result := []*pb.NodeLog{}
|
||||
for _, log := range logs {
|
||||
result = append(result, &pb.NodeLog{
|
||||
Id: int64(log.Id),
|
||||
Role: log.Role,
|
||||
Tag: log.Tag,
|
||||
Description: log.Description,
|
||||
Level: log.Level,
|
||||
NodeId: int64(log.NodeId),
|
||||
ServerId: int64(log.ServerId),
|
||||
CreatedAt: int64(log.CreatedAt),
|
||||
Count: types.Int32(log.Count),
|
||||
IsFixed: log.IsFixed == 1,
|
||||
})
|
||||
}
|
||||
return &pb.ListNodeLogsResponse{NodeLogs: result}, nil
|
||||
}
|
||||
|
||||
// FixNodeLog 设置日志为已修复
|
||||
func (this *NodeLogService) FixNodeLog(ctx context.Context, req *pb.FixNodeLogRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedNodeLogDAO.UpdateNodeLogFixed(tx, req.NodeLogId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user