节点日志增加服务ID、是否修复等字段

This commit is contained in:
GoEdgeLab
2021-05-23 20:46:51 +08:00
parent 36e7c1111a
commit aa67a1c241
5 changed files with 86 additions and 9 deletions

View File

@@ -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
}