mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-11 22:00:27 +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 TrafficHourlyStatDAO dbs.DAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReadyDone(func() {
|
||||
// 清理数据任务
|
||||
var ticker = time.NewTicker(24 * time.Hour)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
err := SharedTrafficHourlyStatDAO.Clean(nil, 60) // 只保留60天
|
||||
if err != nil {
|
||||
remotelogs.Error("TrafficHourlyStatDAO", "clean expired data failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func NewTrafficHourlyStatDAO() *TrafficHourlyStatDAO {
|
||||
return dbs.NewDAO(&TrafficHourlyStatDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
@@ -30,18 +48,27 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 增加流量
|
||||
func (this *TrafficHourlyStatDAO) IncreaseHourlyBytes(tx *dbs.Tx, hour string, bytes int64) error {
|
||||
// IncreaseHourlyStat 增加流量
|
||||
func (this *TrafficHourlyStatDAO) IncreaseHourlyStat(tx *dbs.Tx, hour string, bytes int64, cachedBytes int64, countRequests int64, countCachedRequests int64) error {
|
||||
if len(hour) != 10 {
|
||||
return errors.New("invalid hour '" + hour + "'")
|
||||
}
|
||||
err := this.Query(tx).
|
||||
Param("bytes", bytes).
|
||||
Param("cachedBytes", cachedBytes).
|
||||
Param("countRequests", countRequests).
|
||||
Param("countCachedRequests", countCachedRequests).
|
||||
InsertOrUpdateQuickly(maps.Map{
|
||||
"hour": hour,
|
||||
"bytes": bytes,
|
||||
"hour": hour,
|
||||
"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 *TrafficHourlyStatDAO) IncreaseHourlyBytes(tx *dbs.Tx, hour string, b
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取日期之间统计
|
||||
// FindHourlyStats 获取小时之间统计
|
||||
func (this *TrafficHourlyStatDAO) FindHourlyStats(tx *dbs.Tx, hourFrom string, hourTo string) (result []*TrafficHourlyStat, err error) {
|
||||
ones, err := this.Query(tx).
|
||||
Between("hour", hourFrom, hourTo).
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hourMap := map[string]*TrafficHourlyStat{} // hour => Stat
|
||||
for _, one := range ones {
|
||||
stat := one.(*TrafficHourlyStat)
|
||||
@@ -73,3 +103,12 @@ func (this *TrafficHourlyStatDAO) FindHourlyStats(tx *dbs.Tx, hourFrom string, h
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Clean 清理历史数据
|
||||
func (this *TrafficHourlyStatDAO) Clean(tx *dbs.Tx, days int) error {
|
||||
var hour = timeutil.Format("Ymd00", time.Now().AddDate(0, 0, -days))
|
||||
_, err := this.Query(tx).
|
||||
Lt("hour", hour).
|
||||
Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user