@@ -2,6 +2,7 @@ package models
22
33import (
44 "github.com/TeaOSLab/EdgeAPI/internal/errors"
5+ "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
56 _ "github.com/go-sql-driver/mysql"
67 "github.com/iwind/TeaGo/Tea"
78 "github.com/iwind/TeaGo/dbs"
@@ -34,8 +35,8 @@ func init() {
3435}
3536
3637// CreateLog 创建日志
37- 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 )
38+ func (this * NodeLogDAO ) CreateLog (tx * dbs.Tx , nodeRole NodeRole , nodeId int64 , serverId int64 , level string , tag string , description string , createdAt int64 ) error {
39+ hash := stringutil .Md5 (nodeRole + "@" + strconv .FormatInt (nodeId , 10 ) + "@" + strconv . FormatInt ( serverId , 10 ) + "@" + level + "@" + tag + "@" + description )
3940
4041 // 检查是否在重复最后一条,避免重复创建
4142 lastLog , err := this .Query (tx ).
@@ -59,6 +60,7 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, l
5960 op := NewNodeLogOperator ()
6061 op .Role = nodeRole
6162 op .NodeId = nodeId
63+ op .ServerId = serverId
6264 op .Level = level
6365 op .Tag = tag
6466 op .Description = description
@@ -84,7 +86,7 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
8486}
8587
8688// CountNodeLogs 计算节点日志数量
87- func (this * NodeLogDAO ) CountNodeLogs (tx * dbs.Tx , role string , nodeId int64 , dayFrom string , dayTo string , keyword string , level string ) (int64 , error ) {
89+ func (this * NodeLogDAO ) CountNodeLogs (tx * dbs.Tx , role string , nodeId int64 , serverId int64 , dayFrom string , dayTo string , keyword string , level string ) (int64 , error ) {
8890 query := this .Query (tx ).
8991 Attr ("role" , role )
9092 if nodeId > 0 {
@@ -95,6 +97,9 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, day
9597 query .Where ("nodeId IN (SELECT id FROM " + SharedNodeDAO .Table + " WHERE state=1)" )
9698 }
9799 }
100+ if serverId > 0 {
101+ query .Attr ("serverId" , serverId )
102+ }
98103 if len (dayFrom ) > 0 {
99104 dayFrom = strings .ReplaceAll (dayFrom , "-" , "" )
100105 query .Gte ("day" , dayFrom )
@@ -115,7 +120,18 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, day
115120}
116121
117122// ListNodeLogs 列出单页日志
118- 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 ) {
123+ func (this * NodeLogDAO ) ListNodeLogs (tx * dbs.Tx ,
124+ role string ,
125+ nodeId int64 ,
126+ serverId int64 ,
127+ allServers bool ,
128+ dayFrom string ,
129+ dayTo string ,
130+ keyword string ,
131+ level string ,
132+ fixedState configutils.BoolState ,
133+ offset int64 ,
134+ size int64 ) (result []* NodeLog , err error ) {
119135 query := this .Query (tx ).
120136 Attr ("role" , role )
121137 if nodeId > 0 {
@@ -126,6 +142,16 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayF
126142 query .Where ("nodeId IN (SELECT id FROM " + SharedNodeDAO .Table + " WHERE state=1)" )
127143 }
128144 }
145+ if serverId > 0 {
146+ query .Attr ("serverId" , serverId )
147+ } else if allServers {
148+ query .Where ("serverId>0" )
149+ }
150+ if fixedState == configutils .BoolStateYes {
151+ query .Attr ("isFixed" , 1 )
152+ } else if fixedState == configutils .BoolStateNo {
153+ query .Attr ("isFixed" , 0 )
154+ }
129155 if len (dayFrom ) > 0 {
130156 dayFrom = strings .ReplaceAll (dayFrom , "-" , "" )
131157 query .Gte ("day" , dayFrom )
@@ -149,3 +175,33 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, dayF
149175 FindAll ()
150176 return
151177}
178+
179+ // UpdateNodeLogFixed 设置节点日志为已修复
180+ func (this * NodeLogDAO ) UpdateNodeLogFixed (tx * dbs.Tx , logId int64 ) error {
181+ if logId <= 0 {
182+ return errors .New ("invalid logId" )
183+ }
184+
185+ // 我们把相同内容的日志都置为已修复
186+ hash , err := this .Query (tx ).
187+ Pk (logId ).
188+ Result ("hash" ).
189+ FindStringCol ("" )
190+ if err != nil {
191+ return err
192+ }
193+ if len (hash ) == 0 {
194+ return nil
195+ }
196+
197+ err = this .Query (tx ).
198+ Attr ("hash" , hash ).
199+ Attr ("isFixed" , false ).
200+ Set ("isFixed" , true ).
201+ UpdateQuickly ()
202+ if err != nil {
203+ return err
204+ }
205+
206+ return nil
207+ }
0 commit comments