mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-06 01:00:24 +08:00
[统计]记录流量(以5分钟作为间隔)
This commit is contained in:
57
internal/db/models/server_daily_stat_dao.go
Normal file
57
internal/db/models/server_daily_stat_dao.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
_ "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"
|
||||
)
|
||||
|
||||
type ServerDailyStatDAO dbs.DAO
|
||||
|
||||
func NewServerDailyStatDAO() *ServerDailyStatDAO {
|
||||
return dbs.NewDAO(&ServerDailyStatDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeServerDailyStats",
|
||||
Model: new(ServerDailyStat),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*ServerDailyStatDAO)
|
||||
}
|
||||
|
||||
var SharedServerDailyStatDAO *ServerDailyStatDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedServerDailyStatDAO = NewServerDailyStatDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// 提交数据
|
||||
func (this *ServerDailyStatDAO) SaveStats(stats []*pb.ServerDailyStat) error {
|
||||
for _, stat := range stats {
|
||||
day := timeutil.FormatTime("Ymd", stat.CreatedAt)
|
||||
timeFrom := timeutil.FormatTime("His", stat.CreatedAt)
|
||||
timeTo := timeutil.FormatTime("His", stat.CreatedAt+5*60) // 5分钟
|
||||
|
||||
_, _, err := this.Query().
|
||||
Param("bytes", stat.Bytes).
|
||||
InsertOrUpdate(maps.Map{
|
||||
"serverId": stat.ServerId,
|
||||
"regionId": stat.RegionId,
|
||||
"bytes": dbs.SQL("bytes+:bytes"),
|
||||
"day": day,
|
||||
"timeFrom": timeFrom,
|
||||
"timeTo": timeTo,
|
||||
}, maps.Map{
|
||||
"bytes": dbs.SQL("bytes+:bytes"),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user