减少带宽统计显示的空档期

This commit is contained in:
刘祥超
2022-09-05 16:04:34 +08:00
parent e5109b24d4
commit c66e28cb9d
3 changed files with 18 additions and 19 deletions

View File

@@ -862,20 +862,21 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
} }
// 排序 // 排序
var timestamp = time.Now().Unix() / 300 * 300 var timestamp = (time.Now().Unix()) / 300 * 300
var currentTime = timeutil.FormatTime("YmdHi", timestamp) var times = []string{
var prevTime = timeutil.FormatTime("YmdHi", timestamp-300) timeutil.FormatTime("YmdHi", timestamp),
timeutil.FormatTime("YmdHi", timestamp-300),
timeutil.FormatTime("YmdHi", timestamp-300*2),
}
switch order { switch order {
case "trafficOutAsc": case "trafficOutAsc":
query.Asc("IF(IF(bandwidthTime=:currentTime, bandwidthBytes, 0) > 0, IF(bandwidthTime=:currentTime, bandwidthBytes, 0), IF(bandwidthTime=:prevTime, bandwidthBytes, 0))") query.Asc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
query.Param("currentTime", currentTime) query.Param("times", strings.Join(times, ","))
query.Param("prevTime", prevTime)
query.DescPk() query.DescPk()
case "trafficOutDesc": case "trafficOutDesc":
query.Desc("IF(IF(bandwidthTime=:currentTime, bandwidthBytes, 0) > 0, IF(bandwidthTime=:currentTime, bandwidthBytes, 0), IF(bandwidthTime=:prevTime, bandwidthBytes, 0))") query.Desc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
query.Param("currentTime", currentTime) query.Param("times", strings.Join(times, ","))
query.Param("prevTime", prevTime)
query.DescPk() query.DescPk()
default: default:
query.DescPk() query.DescPk()
@@ -885,7 +886,7 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
// 修正带宽统计数据 // 修正带宽统计数据
for _, server := range result { for _, server := range result {
if len(server.BandwidthTime) > 0 && server.BandwidthBytes > 0 && server.BandwidthTime < prevTime { if len(server.BandwidthTime) > 0 && !lists.ContainsString(times, server.BandwidthTime) {
server.BandwidthBytes = 0 server.BandwidthBytes = 0
} }
} }

View File

@@ -9,7 +9,6 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/goman" "github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs" "github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"sync" "sync"
@@ -20,10 +19,7 @@ var serverBandwidthStatsMap = map[string]*pb.ServerBandwidthStat{} // key => ban
var serverBandwidthStatsLocker = &sync.Mutex{} var serverBandwidthStatsLocker = &sync.Mutex{}
func init() { func init() {
var ticker = time.NewTicker(5 * time.Minute) var ticker = time.NewTicker(1 * time.Minute)
if Tea.IsTesting() {
ticker = time.NewTicker(1 * time.Minute)
}
dbs.OnReadyDone(func() { dbs.OnReadyDone(func() {
goman.New(func() { goman.New(func() {

View File

@@ -415,11 +415,13 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
// 当前N分钟区间 // 当前N分钟区间
{ {
// 查询最近的个时段,以尽可能获取数据 // 查询最近的个时段,以尽可能获取数据
var minute1 = timeutil.FormatTime("Hi", time.Now().Unix()/300*300) var timestamp = time.Now().Unix() / 300 * 300
var minute2 = timeutil.FormatTime("Hi", time.Now().Unix()/300*300-300) var minute1 = timeutil.FormatTime("Hi", timestamp)
var minute2 = timeutil.FormatTime("Hi", timestamp-300)
var minute3 = timeutil.FormatTime("Hi", timestamp-300*2)
for _, minute := range []string{minute1, minute2} { for _, minute := range []string{minute1, minute2, minute3} {
bytes, err := models.SharedServerBandwidthStatDAO.FindMinutelyPeekBandwidthBytes(tx, req.ServerId, day, minute) bytes, err := models.SharedServerBandwidthStatDAO.FindMinutelyPeekBandwidthBytes(tx, req.ServerId, day, minute)
if err != nil { if err != nil {
return nil, err return nil, err