Skip to content

Commit cfa2717

Browse files
committed
实现日志消息聚合
1 parent aece128 commit cfa2717

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

internal/db/models/node_log_dao.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
_ "github.com/go-sql-driver/mysql"
66
"github.com/iwind/TeaGo/Tea"
77
"github.com/iwind/TeaGo/dbs"
8+
stringutil "github.com/iwind/TeaGo/utils/string"
89
timeutil "github.com/iwind/TeaGo/utils/time"
910
"strconv"
1011
"strings"
@@ -34,6 +35,27 @@ func init() {
3435

3536
// CreateLog 创建日志
3637
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, level string, tag string, description string, createdAt int64) error {
38+
hash := stringutil.Md5(nodeRole + "@" + strconv.FormatInt(nodeId, 10) + "@" + level + "@" + tag + "@" + description)
39+
40+
// 检查是否在重复最后一条,避免重复创建
41+
lastLog, err := this.Query(tx).
42+
Result("id", "hash", "createdAt").
43+
DescPk().
44+
Find()
45+
if err != nil {
46+
return err
47+
}
48+
if lastLog != nil {
49+
nodeLog := lastLog.(*NodeLog)
50+
if nodeLog.Hash == hash && time.Now().Unix()-int64(nodeLog.CreatedAt) < 1800 {
51+
err = this.Query(tx).
52+
Pk(nodeLog.Id).
53+
Set("count", dbs.SQL("count+1")).
54+
UpdateQuickly()
55+
return err
56+
}
57+
}
58+
3759
op := NewNodeLogOperator()
3860
op.Role = nodeRole
3961
op.NodeId = nodeId
@@ -42,7 +64,8 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, l
4264
op.Description = description
4365
op.CreatedAt = createdAt
4466
op.Day = timeutil.FormatTime("Ymd", createdAt)
45-
err := this.Save(tx, op)
67+
op.Hash = hash
68+
err = this.Save(tx, op)
4669
return err
4770
}
4871

0 commit comments

Comments
 (0)