2023-05-17 18:42:21 +08:00
package models
2021-01-21 20:22:58 +08:00
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
2021-12-14 10:49:29 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/goman"
2021-07-05 11:37:22 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
2021-07-06 20:06:34 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2021-01-21 20:22:58 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
2021-07-19 21:01:26 +08:00
"github.com/iwind/TeaGo/rands"
2021-07-05 11:37:22 +08:00
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
2021-01-21 20:22:58 +08:00
)
type NodeTrafficDailyStatDAO dbs . DAO
2021-07-05 11:37:22 +08:00
func init ( ) {
dbs . OnReadyDone ( func ( ) {
// 清理数据任务
2021-07-19 21:01:26 +08:00
var ticker = time . NewTicker ( time . Duration ( rands . Int ( 24 , 48 ) ) * time . Hour )
2021-12-14 10:49:29 +08:00
goman . New ( func ( ) {
2021-07-05 11:37:22 +08:00
for range ticker . C {
2023-07-01 17:54:40 +08:00
err := SharedNodeTrafficDailyStatDAO . CleanDefaultDays ( nil , 32 ) // 只保留N天
2021-07-05 11:37:22 +08:00
if err != nil {
remotelogs . Error ( "NodeTrafficDailyStatDAO" , "clean expired data failed: " + err . Error ( ) )
}
}
2021-12-14 10:49:29 +08:00
} )
2021-07-05 11:37:22 +08:00
} )
}
2021-01-21 20:22:58 +08:00
func NewNodeTrafficDailyStatDAO ( ) * NodeTrafficDailyStatDAO {
return dbs . NewDAO ( & NodeTrafficDailyStatDAO {
DAOObject : dbs . DAOObject {
DB : Tea . Env ,
Table : "edgeNodeTrafficDailyStats" ,
Model : new ( NodeTrafficDailyStat ) ,
PkName : "id" ,
} ,
} ) . ( * NodeTrafficDailyStatDAO )
}
var SharedNodeTrafficDailyStatDAO * NodeTrafficDailyStatDAO
func init ( ) {
dbs . OnReady ( func ( ) {
SharedNodeTrafficDailyStatDAO = NewNodeTrafficDailyStatDAO ( )
} )
}
2021-07-05 11:37:22 +08:00
// IncreaseDailyStat 增加统计数据
2021-07-13 11:04:45 +08:00
func ( this * NodeTrafficDailyStatDAO ) IncreaseDailyStat ( tx * dbs . Tx , clusterId int64 , role string , nodeId int64 , day string , bytes int64 , cachedBytes int64 , countRequests int64 , countCachedRequests int64 , countAttackRequests int64 , attackBytes int64 ) error {
2021-01-21 20:22:58 +08:00
if len ( day ) != 8 {
return errors . New ( "invalid day '" + day + "'" )
}
err := this . Query ( tx ) .
Param ( "bytes" , bytes ) .
2021-07-05 11:37:22 +08:00
Param ( "cachedBytes" , cachedBytes ) .
Param ( "countRequests" , countRequests ) .
Param ( "countCachedRequests" , countCachedRequests ) .
2021-07-13 11:04:45 +08:00
Param ( "countAttackRequests" , countAttackRequests ) .
Param ( "attackBytes" , attackBytes ) .
2021-01-21 20:22:58 +08:00
InsertOrUpdateQuickly ( maps . Map {
2021-07-05 11:37:22 +08:00
"clusterId" : clusterId ,
"role" : role ,
"nodeId" : nodeId ,
"day" : day ,
"bytes" : bytes ,
"cachedBytes" : cachedBytes ,
"countRequests" : countRequests ,
"countCachedRequests" : countCachedRequests ,
2021-07-13 11:04:45 +08:00
"countAttackRequests" : countAttackRequests ,
"attackBytes" : attackBytes ,
2021-01-21 20:22:58 +08:00
} , maps . Map {
2021-07-05 11:37:22 +08:00
"bytes" : dbs . SQL ( "bytes+:bytes" ) ,
"cachedBytes" : dbs . SQL ( "cachedBytes+:cachedBytes" ) ,
"countRequests" : dbs . SQL ( "countRequests+:countRequests" ) ,
"countCachedRequests" : dbs . SQL ( "countCachedRequests+:countCachedRequests" ) ,
2021-07-13 11:04:45 +08:00
"countAttackRequests" : dbs . SQL ( "countAttackRequests+:countAttackRequests" ) ,
"attackBytes" : dbs . SQL ( "attackBytes+:attackBytes" ) ,
2021-01-21 20:22:58 +08:00
} )
if err != nil {
return err
}
2023-05-17 18:42:21 +08:00
// 触发钩子
return this . increaseDailyStatHook ( tx , role , nodeId )
2021-01-21 20:22:58 +08:00
}
2021-07-05 11:37:22 +08:00
2021-07-06 20:06:34 +08:00
// FindDailyStats 获取日期之间统计
func ( this * NodeTrafficDailyStatDAO ) FindDailyStats ( tx * dbs . Tx , role string , nodeId int64 , dayFrom string , dayTo string ) ( result [ ] * NodeTrafficDailyStat , err error ) {
ones , err := this . Query ( tx ) .
Attr ( "nodeId" , nodeId ) .
Attr ( "role" , role ) .
Between ( "day" , dayFrom , dayTo ) .
FindAll ( )
if err != nil {
return nil , err
}
dayMap := map [ string ] * NodeTrafficDailyStat { } // day => Stat
for _ , one := range ones {
stat := one . ( * NodeTrafficDailyStat )
dayMap [ stat . Day ] = stat
}
days , err := utils . RangeDays ( dayFrom , dayTo )
if err != nil {
return nil , err
}
for _ , day := range days {
stat , ok := dayMap [ day ]
if ok {
result = append ( result , stat )
} else {
result = append ( result , & NodeTrafficDailyStat { Day : day } )
}
}
return result , nil
}
2023-03-15 17:02:09 +08:00
// SumDailyStat 计算日期之间的总和
func ( this * NodeTrafficDailyStatDAO ) SumDailyStat ( tx * dbs . Tx , role string , nodeId int64 , dayFrom string , dayTo string ) ( * NodeTrafficDailyStat , error ) {
one , err := this . Query ( tx ) .
Result ( "SUM(bytes) AS bytes" , "SUM(cachedBytes) AS cachedBytes" , "SUM(countRequests) AS countRequests" , "SUM(countCachedRequests) AS countCachedRequests" , "SUM(countAttackRequests) AS countAttackRequests" , "SUM(attackBytes) AS attackBytes" ) .
Attr ( "nodeId" , nodeId ) .
Attr ( "role" , role ) .
Between ( "day" , dayFrom , dayTo ) .
Find ( )
if err != nil || one == nil {
return nil , err
}
return one . ( * NodeTrafficDailyStat ) , nil
}
2023-07-01 17:54:40 +08:00
// CleanDays 清理历史数据
func ( this * NodeTrafficDailyStatDAO ) CleanDays ( tx * dbs . Tx , days int ) error {
2021-07-05 11:37:22 +08:00
var day = timeutil . Format ( "Ymd" , time . Now ( ) . AddDate ( 0 , 0 , - days ) )
_ , err := this . Query ( tx ) .
Lt ( "day" , day ) .
Delete ( )
return err
}
2023-07-01 17:54:40 +08:00
func ( this * NodeTrafficDailyStatDAO ) CleanDefaultDays ( tx * dbs . Tx , defaultDays int ) error {
databaseConfig , err := SharedSysSettingDAO . ReadDatabaseConfig ( tx )
if err != nil {
return err
}
if databaseConfig != nil && databaseConfig . NodeTrafficDailyStat . Clean . Days > 0 {
defaultDays = databaseConfig . NodeTrafficDailyStat . Clean . Days
}
if defaultDays <= 0 {
defaultDays = 32
}
return this . CleanDays ( tx , defaultDays )
}