2020-10-09 11:06:37 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
|
|
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2021-05-19 19:03:46 +08:00
|
|
|
"github.com/iwind/TeaGo/types"
|
2020-10-09 11:06:37 +08:00
|
|
|
)
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
// NodeLogService 节点日志相关服务
|
2020-10-09 11:06:37 +08:00
|
|
|
type NodeLogService struct {
|
2020-11-24 17:36:47 +08:00
|
|
|
BaseService
|
2020-10-09 11:06:37 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
// CreateNodeLogs 创建日志
|
2020-10-09 11:06:37 +08:00
|
|
|
func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNodeLogsRequest) (*pb.CreateNodeLogsResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2020-10-09 11:06:37 +08:00
|
|
|
for _, nodeLog := range req.NodeLogs {
|
2021-01-01 23:31:30 +08:00
|
|
|
err := models.SharedNodeLogDAO.CreateLog(tx, nodeLog.Role, nodeLog.NodeId, nodeLog.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt)
|
2020-10-09 11:06:37 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &pb.CreateNodeLogsResponse{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
// CountNodeLogs 查询日志数量
|
2020-11-12 14:41:28 +08:00
|
|
|
func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNodeLogsRequest) (*pb.RPCCountResponse, error) {
|
2020-10-09 11:06:37 +08:00
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId, req.DayFrom, req.DayTo, req.Keyword, req.Level)
|
2020-10-09 11:06:37 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 17:36:47 +08:00
|
|
|
return this.SuccessCount(count)
|
2020-10-09 11:06:37 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
// ListNodeLogs 列出单页日志
|
2020-10-09 11:06:37 +08:00
|
|
|
func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLogsRequest) (*pb.ListNodeLogsResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
logs, err := models.SharedNodeLogDAO.ListNodeLogs(tx, req.Role, req.NodeId, req.DayFrom, req.DayTo, req.Keyword, req.Level, req.Offset, req.Size)
|
2020-10-09 11:06:37 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := []*pb.NodeLog{}
|
|
|
|
|
for _, log := range logs {
|
|
|
|
|
result = append(result, &pb.NodeLog{
|
|
|
|
|
Role: log.Role,
|
|
|
|
|
Tag: log.Tag,
|
|
|
|
|
Description: log.Description,
|
|
|
|
|
Level: log.Level,
|
|
|
|
|
NodeId: int64(log.NodeId),
|
|
|
|
|
CreatedAt: int64(log.CreatedAt),
|
2021-05-19 19:03:46 +08:00
|
|
|
Count: types.Int32(log.Count),
|
2020-10-09 11:06:37 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return &pb.ListNodeLogsResponse{NodeLogs: result}, nil
|
|
|
|
|
}
|