节点日志可以通过日期、关键词、级别等筛选

This commit is contained in:
GoEdgeLab
2021-05-19 19:03:46 +08:00
parent 30529d161c
commit 84845aee48
3 changed files with 75 additions and 22 deletions

View File

@@ -7,13 +7,12 @@ import (
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
timeutil "github.com/iwind/TeaGo/utils/time" timeutil "github.com/iwind/TeaGo/utils/time"
"strconv" "strconv"
"strings"
"time" "time"
) )
type NodeLogDAO dbs.DAO type NodeLogDAO dbs.DAO
const ()
func NewNodeLogDAO() *NodeLogDAO { func NewNodeLogDAO() *NodeLogDAO {
return dbs.NewDAO(&NodeLogDAO{ return dbs.NewDAO(&NodeLogDAO{
DAOObject: dbs.DAOObject{ DAOObject: dbs.DAOObject{
@@ -33,7 +32,7 @@ func init() {
}) })
} }
// 创建日志 // 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, level string, tag string, description string, createdAt int64) error {
op := NewNodeLogOperator() op := NewNodeLogOperator()
op.Role = nodeRole op.Role = nodeRole
@@ -47,7 +46,7 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, l
return err return err
} }
// 清除超出一定日期的日志 // DeleteExpiredLogs 清除超出一定日期的日志
func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error { func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
if days <= 0 { if days <= 0 {
return errors.New("invalid days '" + strconv.Itoa(days) + "'") return errors.New("invalid days '" + strconv.Itoa(days) + "'")
@@ -61,19 +60,65 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
return err return err
} }
// 计算节点数量 // CountNodeLogs 计算节点日志数量
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64) (int64, error) { func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
return this.Query(tx). query := this.Query(tx).
Attr("nodeId", nodeId). Attr("role", role)
Attr("role", role). if nodeId > 0 {
Count() query.Attr("nodeId", nodeId)
} else {
switch role {
case NodeRoleNode:
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
}
}
if len(dayFrom) > 0 {
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
query.Gte("day", dayFrom)
}
if len(dayTo) > 0 {
dayTo = strings.ReplaceAll(dayTo, "-", "")
query.Lte("day", dayTo)
}
if len(keyword) > 0 {
query.Where("(tag LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if len(level) > 0 {
query.Attr("level", level)
}
return query.Count()
} }
// 列出单页日志 // ListNodeLogs 列出单页日志
func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, offset int64, size int64) (result []*NodeLog, err error) { 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) {
_, err = this.Query(tx). query := this.Query(tx).
Attr("nodeId", nodeId). Attr("role", role)
Attr("role", role). if nodeId > 0 {
query.Attr("nodeId", nodeId)
} else {
switch role {
case NodeRoleNode:
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
}
}
if len(dayFrom) > 0 {
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
query.Gte("day", dayFrom)
}
if len(dayTo) > 0 {
dayTo = strings.ReplaceAll(dayTo, "-", "")
query.Lte("day", dayTo)
}
if len(keyword) > 0 {
query.Where("(tag LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if len(level) > 0 {
query.Attr("level", level)
}
_, err = query.
Offset(offset). Offset(offset).
Limit(size). Limit(size).
Slice(&result). Slice(&result).

View File

@@ -1,6 +1,6 @@
package models package models
// 节点日志 // NodeLog 节点日志
type NodeLog struct { type NodeLog struct {
Id uint64 `field:"id"` // ID Id uint64 `field:"id"` // ID
Role string `field:"role"` // 节点角色 Role string `field:"role"` // 节点角色
@@ -10,6 +10,9 @@ type NodeLog struct {
Level string `field:"level"` // 级别 Level string `field:"level"` // 级别
NodeId uint32 `field:"nodeId"` // 节点ID NodeId uint32 `field:"nodeId"` // 节点ID
Day string `field:"day"` // 日期 Day string `field:"day"` // 日期
ServerId uint32 `field:"serverId"` // 服务ID
Hash string `field:"hash"` // 信息内容Hash
Count uint32 `field:"count"` // 重复次数
} }
type NodeLogOperator struct { type NodeLogOperator struct {
@@ -21,6 +24,9 @@ type NodeLogOperator struct {
Level interface{} // 级别 Level interface{} // 级别
NodeId interface{} // 节点ID NodeId interface{} // 节点ID
Day interface{} // 日期 Day interface{} // 日期
ServerId interface{} // 服务ID
Hash interface{} // 信息内容Hash
Count interface{} // 重复次数
} }
func NewNodeLogOperator() *NodeLogOperator { func NewNodeLogOperator() *NodeLogOperator {

View File

@@ -5,14 +5,15 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/types"
) )
// 节点日志相关服务 // NodeLogService 节点日志相关服务
type NodeLogService struct { type NodeLogService struct {
BaseService BaseService
} }
// 创建日志 // CreateNodeLogs 创建日志
func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNodeLogsRequest) (*pb.CreateNodeLogsResponse, error) { func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNodeLogsRequest) (*pb.CreateNodeLogsResponse, error) {
_, _, err := rpcutils.ValidateRequest(ctx) _, _, err := rpcutils.ValidateRequest(ctx)
if err != nil { if err != nil {
@@ -30,7 +31,7 @@ func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNo
return &pb.CreateNodeLogsResponse{}, nil return &pb.CreateNodeLogsResponse{}, nil
} }
// 查询日志数量 // CountNodeLogs 查询日志数量
func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNodeLogsRequest) (*pb.RPCCountResponse, error) { func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNodeLogsRequest) (*pb.RPCCountResponse, error) {
_, _, err := rpcutils.ValidateRequest(ctx) _, _, err := rpcutils.ValidateRequest(ctx)
if err != nil { if err != nil {
@@ -39,14 +40,14 @@ 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) count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId, req.DayFrom, req.DayTo, req.Keyword, req.Level)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return this.SuccessCount(count) return this.SuccessCount(count)
} }
// 列出单页日志 // ListNodeLogs 列出单页日志
func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLogsRequest) (*pb.ListNodeLogsResponse, error) { func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLogsRequest) (*pb.ListNodeLogsResponse, error) {
_, _, err := rpcutils.ValidateRequest(ctx) _, _, err := rpcutils.ValidateRequest(ctx)
if err != nil { if err != nil {
@@ -55,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.Offset, req.Size) logs, err := models.SharedNodeLogDAO.ListNodeLogs(tx, req.Role, req.NodeId, req.DayFrom, req.DayTo, req.Keyword, req.Level, req.Offset, req.Size)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -69,6 +70,7 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
Level: log.Level, Level: log.Level,
NodeId: int64(log.NodeId), NodeId: int64(log.NodeId),
CreatedAt: int64(log.CreatedAt), CreatedAt: int64(log.CreatedAt),
Count: types.Int32(log.Count),
}) })
} }
return &pb.ListNodeLogsResponse{NodeLogs: result}, nil return &pb.ListNodeLogsResponse{NodeLogs: result}, nil