mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-21 05:40:25 +08:00
实现集群看板
This commit is contained in:
@@ -2,15 +2,33 @@ package stats
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TrafficDailyStatDAO dbs.DAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReadyDone(func() {
|
||||
// 清理数据任务
|
||||
var ticker = time.NewTicker(24 * time.Hour)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
err := SharedTrafficDailyStatDAO.Clean(nil, 60) // 只保留60天
|
||||
if err != nil {
|
||||
remotelogs.Error("TrafficDailyStatDAO", "clean expired data failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func NewTrafficDailyStatDAO() *TrafficDailyStatDAO {
|
||||
return dbs.NewDAO(&TrafficDailyStatDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
@@ -30,18 +48,27 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 增加流量
|
||||
func (this *TrafficDailyStatDAO) IncreaseDailyBytes(tx *dbs.Tx, day string, bytes int64) error {
|
||||
// IncreaseDailyStat 增加统计数据
|
||||
func (this *TrafficDailyStatDAO) IncreaseDailyStat(tx *dbs.Tx, day string, bytes int64, cachedBytes int64, countRequests int64, countCachedRequests int64) error {
|
||||
if len(day) != 8 {
|
||||
return errors.New("invalid day '" + day + "'")
|
||||
}
|
||||
err := this.Query(tx).
|
||||
Param("bytes", bytes).
|
||||
Param("cachedBytes", cachedBytes).
|
||||
Param("countRequests", countRequests).
|
||||
Param("countCachedRequests", countCachedRequests).
|
||||
InsertOrUpdateQuickly(maps.Map{
|
||||
"day": day,
|
||||
"bytes": bytes,
|
||||
"day": day,
|
||||
"bytes": bytes,
|
||||
"cachedBytes": cachedBytes,
|
||||
"countRequests": countRequests,
|
||||
"countCachedRequests": countCachedRequests,
|
||||
}, maps.Map{
|
||||
"bytes": dbs.SQL("bytes+:bytes"),
|
||||
"bytes": dbs.SQL("bytes+:bytes"),
|
||||
"cachedBytes": dbs.SQL("cachedBytes+:cachedBytes"),
|
||||
"countRequests": dbs.SQL("countRequests+:countRequests"),
|
||||
"countCachedRequests": dbs.SQL("countCachedRequests+:countCachedRequests"),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -49,11 +76,14 @@ func (this *TrafficDailyStatDAO) IncreaseDailyBytes(tx *dbs.Tx, day string, byte
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取日期之间统计
|
||||
// FindDailyStats 获取日期之间统计
|
||||
func (this *TrafficDailyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayTo string) (result []*TrafficDailyStat, err error) {
|
||||
ones, err := this.Query(tx).
|
||||
Between("day", dayFrom, dayTo).
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dayMap := map[string]*TrafficDailyStat{} // day => Stat
|
||||
for _, one := range ones {
|
||||
stat := one.(*TrafficDailyStat)
|
||||
@@ -73,3 +103,12 @@ func (this *TrafficDailyStatDAO) FindDailyStats(tx *dbs.Tx, dayFrom string, dayT
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Clean 清理历史数据
|
||||
func (this *TrafficDailyStatDAO) Clean(tx *dbs.Tx, days int) error {
|
||||
var day = timeutil.Format("Ymd", time.Now().AddDate(0, 0, -days))
|
||||
_, err := this.Query(tx).
|
||||
Lt("day", day).
|
||||
Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user