Skip to content

Commit aece128

Browse files
committed
节点日志可以通过日期、关键词、级别等筛选
1 parent 0269202 commit aece128

3 files changed

Lines changed: 75 additions & 22 deletions

File tree

internal/db/models/node_log_dao.go

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import (
77
"github.com/iwind/TeaGo/dbs"
88
timeutil "github.com/iwind/TeaGo/utils/time"
99
"strconv"
10+
"strings"
1011
"time"
1112
)
1213

1314
type NodeLogDAO dbs.DAO
1415

15-
const ()
16-
1716
func NewNodeLogDAO() *NodeLogDAO {
1817
return dbs.NewDAO(&NodeLogDAO{
1918
DAOObject: dbs.DAOObject{
@@ -33,7 +32,7 @@ func init() {
3332
})
3433
}
3534

36-
// 创建日志
35+
// CreateLog 创建日志
3736
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, level string, tag string, description string, createdAt int64) error {
3837
op := NewNodeLogOperator()
3938
op.Role = nodeRole
@@ -47,7 +46,7 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, l
4746
return err
4847
}
4948

50-
// 清除超出一定日期的日志
49+
// DeleteExpiredLogs 清除超出一定日期的日志
5150
func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
5251
if days <= 0 {
5352
return errors.New("invalid days '" + strconv.Itoa(days) + "'")
@@ -61,19 +60,65 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
6160
return err
6261
}
6362

64-
// 计算节点数量
65-
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64) (int64, error) {
66-
return this.Query(tx).
67-
Attr("nodeId", nodeId).
68-
Attr("role", role).
69-
Count()
63+
// CountNodeLogs 计算节点日志数量
64+
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
65+
query := this.Query(tx).
66+
Attr("role", role)
67+
if nodeId > 0 {
68+
query.Attr("nodeId", nodeId)
69+
} else {
70+
switch role {
71+
case NodeRoleNode:
72+
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
73+
}
74+
}
75+
if len(dayFrom) > 0 {
76+
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
77+
query.Gte("day", dayFrom)
78+
}
79+
if len(dayTo) > 0 {
80+
dayTo = strings.ReplaceAll(dayTo, "-", "")
81+
query.Lte("day", dayTo)
82+
}
83+
if len(keyword) > 0 {
84+
query.Where("(tag LIKE :keyword OR description LIKE :keyword)").
85+
Param("keyword", "%"+keyword+"%")
86+
}
87+
if len(level) > 0 {
88+
query.Attr("level", level)
89+
}
90+
91+
return query.Count()
7092
}
7193

72-
// 列出单页日志
73-
func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, offset int64, size int64) (result []*NodeLog, err error) {
74-
_, err = this.Query(tx).
75-
Attr("nodeId", nodeId).
76-
Attr("role", role).
94+
// ListNodeLogs 列出单页日志
95+
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) {
96+
query := this.Query(tx).
97+
Attr("role", role)
98+
if nodeId > 0 {
99+
query.Attr("nodeId", nodeId)
100+
} else {
101+
switch role {
102+
case NodeRoleNode:
103+
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
104+
}
105+
}
106+
if len(dayFrom) > 0 {
107+
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
108+
query.Gte("day", dayFrom)
109+
}
110+
if len(dayTo) > 0 {
111+
dayTo = strings.ReplaceAll(dayTo, "-", "")
112+
query.Lte("day", dayTo)
113+
}
114+
if len(keyword) > 0 {
115+
query.Where("(tag LIKE :keyword OR description LIKE :keyword)").
116+
Param("keyword", "%"+keyword+"%")
117+
}
118+
if len(level) > 0 {
119+
query.Attr("level", level)
120+
}
121+
_, err = query.
77122
Offset(offset).
78123
Limit(size).
79124
Slice(&result).

internal/db/models/node_log_model.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package models
22

3-
// 节点日志
3+
// NodeLog 节点日志
44
type NodeLog struct {
55
Id uint64 `field:"id"` // ID
66
Role string `field:"role"` // 节点角色
@@ -10,6 +10,9 @@ type NodeLog struct {
1010
Level string `field:"level"` // 级别
1111
NodeId uint32 `field:"nodeId"` // 节点ID
1212
Day string `field:"day"` // 日期
13+
ServerId uint32 `field:"serverId"` // 服务ID
14+
Hash string `field:"hash"` // 信息内容Hash
15+
Count uint32 `field:"count"` // 重复次数
1316
}
1417

1518
type NodeLogOperator struct {
@@ -21,6 +24,9 @@ type NodeLogOperator struct {
2124
Level interface{} // 级别
2225
NodeId interface{} // 节点ID
2326
Day interface{} // 日期
27+
ServerId interface{} // 服务ID
28+
Hash interface{} // 信息内容Hash
29+
Count interface{} // 重复次数
2430
}
2531

2632
func NewNodeLogOperator() *NodeLogOperator {

internal/rpc/services/service_node_log.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
66
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
77
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
8+
"github.com/iwind/TeaGo/types"
89
)
910

10-
// 节点日志相关服务
11+
// NodeLogService 节点日志相关服务
1112
type NodeLogService struct {
1213
BaseService
1314
}
1415

15-
// 创建日志
16+
// CreateNodeLogs 创建日志
1617
func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNodeLogsRequest) (*pb.CreateNodeLogsResponse, error) {
1718
_, _, err := rpcutils.ValidateRequest(ctx)
1819
if err != nil {
@@ -30,7 +31,7 @@ func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNo
3031
return &pb.CreateNodeLogsResponse{}, nil
3132
}
3233

33-
// 查询日志数量
34+
// CountNodeLogs 查询日志数量
3435
func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNodeLogsRequest) (*pb.RPCCountResponse, error) {
3536
_, _, err := rpcutils.ValidateRequest(ctx)
3637
if err != nil {
@@ -39,14 +40,14 @@ func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNode
3940

4041
tx := this.NullTx()
4142

42-
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId)
43+
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId, req.DayFrom, req.DayTo, req.Keyword, req.Level)
4344
if err != nil {
4445
return nil, err
4546
}
4647
return this.SuccessCount(count)
4748
}
4849

49-
// 列出单页日志
50+
// ListNodeLogs 列出单页日志
5051
func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLogsRequest) (*pb.ListNodeLogsResponse, error) {
5152
_, _, err := rpcutils.ValidateRequest(ctx)
5253
if err != nil {
@@ -55,7 +56,7 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
5556

5657
tx := this.NullTx()
5758

58-
logs, err := models.SharedNodeLogDAO.ListNodeLogs(tx, req.Role, req.NodeId, req.Offset, req.Size)
59+
logs, err := models.SharedNodeLogDAO.ListNodeLogs(tx, req.Role, req.NodeId, req.DayFrom, req.DayTo, req.Keyword, req.Level, req.Offset, req.Size)
5960
if err != nil {
6061
return nil, err
6162
}
@@ -69,6 +70,7 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
6970
Level: log.Level,
7071
NodeId: int64(log.NodeId),
7172
CreatedAt: int64(log.CreatedAt),
73+
Count: types.Int32(log.Count),
7274
})
7375
}
7476
return &pb.ListNodeLogsResponse{NodeLogs: result}, nil

0 commit comments

Comments
 (0)