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())
|
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 {
|
if createLogErr != nil {
|
||||||
logs.Println("[NODE_LOG]" + createLogErr.Error())
|
logs.Println("[NODE_LOG]" + createLogErr.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -34,8 +35,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateLog 创建日志
|
// CreateLog 创建日志
|
||||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, level string, tag string, description string, createdAt int64) error {
|
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) + "@" + level + "@" + tag + "@" + description)
|
hash := stringutil.Md5(nodeRole + "@" + strconv.FormatInt(nodeId, 10) + "@" + strconv.FormatInt(serverId, 10) + "@" + level + "@" + tag + "@" + description)
|
||||||
|
|
||||||
// 检查是否在重复最后一条,避免重复创建
|
// 检查是否在重复最后一条,避免重复创建
|
||||||
lastLog, err := this.Query(tx).
|
lastLog, err := this.Query(tx).
|
||||||
@@ -59,6 +60,7 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, l
|
|||||||
op := NewNodeLogOperator()
|
op := NewNodeLogOperator()
|
||||||
op.Role = nodeRole
|
op.Role = nodeRole
|
||||||
op.NodeId = nodeId
|
op.NodeId = nodeId
|
||||||
|
op.ServerId = serverId
|
||||||
op.Level = level
|
op.Level = level
|
||||||
op.Tag = tag
|
op.Tag = tag
|
||||||
op.Description = description
|
op.Description = description
|
||||||
@@ -84,7 +86,7 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CountNodeLogs 计算节点日志数量
|
// 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).
|
query := this.Query(tx).
|
||||||
Attr("role", role)
|
Attr("role", role)
|
||||||
if nodeId > 0 {
|
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)")
|
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if serverId > 0 {
|
||||||
|
query.Attr("serverId", serverId)
|
||||||
|
}
|
||||||
if len(dayFrom) > 0 {
|
if len(dayFrom) > 0 {
|
||||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||||
query.Gte("day", dayFrom)
|
query.Gte("day", dayFrom)
|
||||||
@@ -115,7 +120,18 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, day
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListNodeLogs 列出单页日志
|
// 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).
|
query := this.Query(tx).
|
||||||
Attr("role", role)
|
Attr("role", role)
|
||||||
if nodeId > 0 {
|
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)")
|
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 {
|
if len(dayFrom) > 0 {
|
||||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||||
query.Gte("day", dayFrom)
|
query.Gte("day", dayFrom)
|
||||||
@@ -149,3 +175,33 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayF
|
|||||||
FindAll()
|
FindAll()
|
||||||
return
|
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
|
ServerId uint32 `field:"serverId"` // 服务ID
|
||||||
Hash string `field:"hash"` // 信息内容Hash
|
Hash string `field:"hash"` // 信息内容Hash
|
||||||
Count uint32 `field:"count"` // 重复次数
|
Count uint32 `field:"count"` // 重复次数
|
||||||
|
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeLogOperator struct {
|
type NodeLogOperator struct {
|
||||||
@@ -27,6 +28,7 @@ type NodeLogOperator struct {
|
|||||||
ServerId interface{} // 服务ID
|
ServerId interface{} // 服务ID
|
||||||
Hash interface{} // 信息内容Hash
|
Hash interface{} // 信息内容Hash
|
||||||
Count interface{} // 重复次数
|
Count interface{} // 重复次数
|
||||||
|
IsFixed interface{} // 是否已处理
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNodeLogOperator() *NodeLogOperator {
|
func NewNodeLogOperator() *NodeLogOperator {
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ Loop:
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case log := <-logChan:
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNo
|
|||||||
tx := this.NullTx()
|
tx := this.NullTx()
|
||||||
|
|
||||||
for _, nodeLog := range req.NodeLogs {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,7 @@ func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNode
|
|||||||
|
|
||||||
tx := this.NullTx()
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
|
|||||||
|
|
||||||
tx := this.NullTx()
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -64,14 +64,33 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
|
|||||||
result := []*pb.NodeLog{}
|
result := []*pb.NodeLog{}
|
||||||
for _, log := range logs {
|
for _, log := range logs {
|
||||||
result = append(result, &pb.NodeLog{
|
result = append(result, &pb.NodeLog{
|
||||||
|
Id: int64(log.Id),
|
||||||
Role: log.Role,
|
Role: log.Role,
|
||||||
Tag: log.Tag,
|
Tag: log.Tag,
|
||||||
Description: log.Description,
|
Description: log.Description,
|
||||||
Level: log.Level,
|
Level: log.Level,
|
||||||
NodeId: int64(log.NodeId),
|
NodeId: int64(log.NodeId),
|
||||||
|
ServerId: int64(log.ServerId),
|
||||||
CreatedAt: int64(log.CreatedAt),
|
CreatedAt: int64(log.CreatedAt),
|
||||||
Count: types.Int32(log.Count),
|
Count: types.Int32(log.Count),
|
||||||
|
IsFixed: log.IsFixed == 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return &pb.ListNodeLogsResponse{NodeLogs: result}, nil
|
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