2020-10-09 11:06:37 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
2021-05-23 20:46:51 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
2021-05-26 14:40:05 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
2020-10-09 11:06:37 +08:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
|
"github.com/iwind/TeaGo/Tea"
|
|
|
|
|
"github.com/iwind/TeaGo/dbs"
|
2021-05-19 20:47:23 +08:00
|
|
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
2020-10-09 11:06:37 +08:00
|
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
|
|
|
"strconv"
|
2021-05-19 19:03:46 +08:00
|
|
|
"strings"
|
2020-10-09 11:06:37 +08:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type NodeLogDAO dbs.DAO
|
|
|
|
|
|
|
|
|
|
func NewNodeLogDAO() *NodeLogDAO {
|
|
|
|
|
return dbs.NewDAO(&NodeLogDAO{
|
|
|
|
|
DAOObject: dbs.DAOObject{
|
|
|
|
|
DB: Tea.Env,
|
|
|
|
|
Table: "edgeNodeLogs",
|
|
|
|
|
Model: new(NodeLog),
|
|
|
|
|
PkName: "id",
|
|
|
|
|
},
|
|
|
|
|
}).(*NodeLogDAO)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 20:05:13 +08:00
|
|
|
var SharedNodeLogDAO *NodeLogDAO
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
dbs.OnReady(func() {
|
|
|
|
|
SharedNodeLogDAO = NewNodeLogDAO()
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-10-09 11:06:37 +08:00
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
// CreateLog 创建日志
|
2021-05-26 14:40:05 +08:00
|
|
|
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, level string, tag string, description string, createdAt int64) error {
|
2021-05-23 20:46:51 +08:00
|
|
|
hash := stringutil.Md5(nodeRole + "@" + strconv.FormatInt(nodeId, 10) + "@" + strconv.FormatInt(serverId, 10) + "@" + level + "@" + tag + "@" + description)
|
2021-05-19 20:47:23 +08:00
|
|
|
|
|
|
|
|
// 检查是否在重复最后一条,避免重复创建
|
|
|
|
|
lastLog, err := this.Query(tx).
|
|
|
|
|
Result("id", "hash", "createdAt").
|
|
|
|
|
DescPk().
|
|
|
|
|
Find()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if lastLog != nil {
|
|
|
|
|
nodeLog := lastLog.(*NodeLog)
|
|
|
|
|
if nodeLog.Hash == hash && time.Now().Unix()-int64(nodeLog.CreatedAt) < 1800 {
|
|
|
|
|
err = this.Query(tx).
|
|
|
|
|
Pk(nodeLog.Id).
|
|
|
|
|
Set("count", dbs.SQL("count+1")).
|
|
|
|
|
UpdateQuickly()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-09 11:06:37 +08:00
|
|
|
op := NewNodeLogOperator()
|
|
|
|
|
op.Role = nodeRole
|
|
|
|
|
op.NodeId = nodeId
|
2021-05-23 20:46:51 +08:00
|
|
|
op.ServerId = serverId
|
2020-10-09 11:06:37 +08:00
|
|
|
op.Level = level
|
|
|
|
|
op.Tag = tag
|
|
|
|
|
op.Description = description
|
|
|
|
|
op.CreatedAt = createdAt
|
|
|
|
|
op.Day = timeutil.FormatTime("Ymd", createdAt)
|
2021-05-19 20:47:23 +08:00
|
|
|
op.Hash = hash
|
|
|
|
|
err = this.Save(tx, op)
|
2020-10-09 11:06:37 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
// DeleteExpiredLogs 清除超出一定日期的日志
|
2021-01-01 23:31:30 +08:00
|
|
|
func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
2020-10-09 11:06:37 +08:00
|
|
|
if days <= 0 {
|
|
|
|
|
return errors.New("invalid days '" + strconv.Itoa(days) + "'")
|
|
|
|
|
}
|
|
|
|
|
date := time.Now().AddDate(0, 0, -days)
|
|
|
|
|
expireDay := timeutil.Format("Ymd", date)
|
2021-01-01 23:31:30 +08:00
|
|
|
_, err := this.Query(tx).
|
2020-10-09 11:06:37 +08:00
|
|
|
Where("day<=:day").
|
|
|
|
|
Param("day", expireDay).
|
|
|
|
|
Delete()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
// CountNodeLogs 计算节点日志数量
|
2021-05-23 20:46:51 +08:00
|
|
|
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
|
2021-05-19 19:03:46 +08:00
|
|
|
query := this.Query(tx).
|
|
|
|
|
Attr("role", role)
|
|
|
|
|
if nodeId > 0 {
|
|
|
|
|
query.Attr("nodeId", nodeId)
|
|
|
|
|
} else {
|
|
|
|
|
switch role {
|
2021-05-26 14:40:05 +08:00
|
|
|
case nodeconfigs.NodeRoleNode:
|
2021-05-19 19:03:46 +08:00
|
|
|
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-23 20:46:51 +08:00
|
|
|
if serverId > 0 {
|
|
|
|
|
query.Attr("serverId", serverId)
|
|
|
|
|
}
|
2021-05-19 19:03:46 +08:00
|
|
|
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()
|
2020-10-09 11:06:37 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-19 19:03:46 +08:00
|
|
|
// ListNodeLogs 列出单页日志
|
2021-05-23 20:46:51 +08:00
|
|
|
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) {
|
2021-05-19 19:03:46 +08:00
|
|
|
query := this.Query(tx).
|
|
|
|
|
Attr("role", role)
|
|
|
|
|
if nodeId > 0 {
|
|
|
|
|
query.Attr("nodeId", nodeId)
|
|
|
|
|
} else {
|
|
|
|
|
switch role {
|
2021-05-26 14:40:05 +08:00
|
|
|
case nodeconfigs.NodeRoleNode:
|
2021-05-19 19:03:46 +08:00
|
|
|
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-23 20:46:51 +08:00
|
|
|
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)
|
|
|
|
|
}
|
2021-05-19 19:03:46 +08:00
|
|
|
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.
|
2020-10-09 11:06:37 +08:00
|
|
|
Offset(offset).
|
|
|
|
|
Limit(size).
|
|
|
|
|
Slice(&result).
|
|
|
|
|
DescPk().
|
|
|
|
|
FindAll()
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-05-23 20:46:51 +08:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|