@@ -7,13 +7,12 @@ import (
77 "github.com/iwind/TeaGo/dbs"
88 timeutil "github.com/iwind/TeaGo/utils/time"
99 "strconv"
10+ "strings"
1011 "time"
1112)
1213
1314type NodeLogDAO dbs.DAO
1415
15- const ()
16-
1716func NewNodeLogDAO () * NodeLogDAO {
1817 return dbs .NewDAO (& NodeLogDAO {
1918 DAOObject : dbs.DAOObject {
@@ -33,7 +32,7 @@ func init() {
3332 })
3433}
3534
36- // 创建日志
35+ // CreateLog 创建日志
3736func (this * NodeLogDAO ) CreateLog (tx * dbs.Tx , nodeRole NodeRole , nodeId int64 , level string , tag string , description string , createdAt int64 ) error {
3837 op := NewNodeLogOperator ()
3938 op .Role = nodeRole
@@ -47,7 +46,7 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, l
4746 return err
4847}
4948
50- // 清除超出一定日期的日志
49+ // DeleteExpiredLogs 清除超出一定日期的日志
5150func (this * NodeLogDAO ) DeleteExpiredLogs (tx * dbs.Tx , days int ) error {
5251 if days <= 0 {
5352 return errors .New ("invalid days '" + strconv .Itoa (days ) + "'" )
@@ -61,19 +60,65 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
6160 return err
6261}
6362
64- // 计算节点数量
65- func (this * NodeLogDAO ) CountNodeLogs (tx * dbs.Tx , role string , nodeId int64 ) (int64 , error ) {
66- return this .Query (tx ).
67- Attr ("nodeId" , nodeId ).
68- Attr ("role" , role ).
69- Count ()
63+ // CountNodeLogs 计算节点日志数量
64+ func (this * NodeLogDAO ) CountNodeLogs (tx * dbs.Tx , role string , nodeId int64 , dayFrom string , dayTo string , keyword string , level string ) (int64 , error ) {
65+ query := this .Query (tx ).
66+ Attr ("role" , role )
67+ if nodeId > 0 {
68+ query .Attr ("nodeId" , nodeId )
69+ } else {
70+ switch role {
71+ case NodeRoleNode :
72+ query .Where ("nodeId IN (SELECT id FROM " + SharedNodeDAO .Table + " WHERE state=1)" )
73+ }
74+ }
75+ if len (dayFrom ) > 0 {
76+ dayFrom = strings .ReplaceAll (dayFrom , "-" , "" )
77+ query .Gte ("day" , dayFrom )
78+ }
79+ if len (dayTo ) > 0 {
80+ dayTo = strings .ReplaceAll (dayTo , "-" , "" )
81+ query .Lte ("day" , dayTo )
82+ }
83+ if len (keyword ) > 0 {
84+ query .Where ("(tag LIKE :keyword OR description LIKE :keyword)" ).
85+ Param ("keyword" , "%" + keyword + "%" )
86+ }
87+ if len (level ) > 0 {
88+ query .Attr ("level" , level )
89+ }
90+
91+ return query .Count ()
7092}
7193
72- // 列出单页日志
73- func (this * NodeLogDAO ) ListNodeLogs (tx * dbs.Tx , role string , nodeId int64 , offset int64 , size int64 ) (result []* NodeLog , err error ) {
74- _ , err = this .Query (tx ).
75- Attr ("nodeId" , nodeId ).
76- Attr ("role" , role ).
94+ // ListNodeLogs 列出单页日志
95+ 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 ) {
96+ query := this .Query (tx ).
97+ Attr ("role" , role )
98+ if nodeId > 0 {
99+ query .Attr ("nodeId" , nodeId )
100+ } else {
101+ switch role {
102+ case NodeRoleNode :
103+ query .Where ("nodeId IN (SELECT id FROM " + SharedNodeDAO .Table + " WHERE state=1)" )
104+ }
105+ }
106+ if len (dayFrom ) > 0 {
107+ dayFrom = strings .ReplaceAll (dayFrom , "-" , "" )
108+ query .Gte ("day" , dayFrom )
109+ }
110+ if len (dayTo ) > 0 {
111+ dayTo = strings .ReplaceAll (dayTo , "-" , "" )
112+ query .Lte ("day" , dayTo )
113+ }
114+ if len (keyword ) > 0 {
115+ query .Where ("(tag LIKE :keyword OR description LIKE :keyword)" ).
116+ Param ("keyword" , "%" + keyword + "%" )
117+ }
118+ if len (level ) > 0 {
119+ query .Attr ("level" , level )
120+ }
121+ _ , err = query .
77122 Offset (offset ).
78123 Limit (size ).
79124 Slice (& result ).
0 commit comments