diff --git a/internal/db/models/server_bandwidth_stat_dao.go b/internal/db/models/server_bandwidth_stat_dao.go index 6dc8fdb9..eeb2f699 100644 --- a/internal/db/models/server_bandwidth_stat_dao.go +++ b/internal/db/models/server_bandwidth_stat_dao.go @@ -76,6 +76,39 @@ func (this *ServerBandwidthStatDAO) UpdateServerBandwidth(tx *dbs.Tx, userId int }) } +// FindMinutelyPeekBandwidthBytes 获取某分钟的带宽峰值 +// day YYYYMMDD +// minute HHII +func (this *ServerBandwidthStatDAO) FindMinutelyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string, minute string) (int64, error) { + return this.Query(tx). + Table(this.partialTable(serverId)). + Result("bytes"). + Attr("serverId", serverId). + Attr("day", day). + Attr("timeAt", minute). + FindInt64Col(0) +} + +// FindDailyPeekBandwidthBytes 获取某天的带宽峰值 +// day YYYYMMDD +func (this *ServerBandwidthStatDAO) FindDailyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, day string) (int64, error) { + return this.Query(tx). + Table(this.partialTable(serverId)). + Attr("day", day). + Result("MAX(bytes)"). + FindInt64Col(0) +} + +// FindMonthlyPeekBandwidthBytes 获取某月的带宽峰值 +// month YYYYMM +func (this *ServerBandwidthStatDAO) FindMonthlyPeekBandwidthBytes(tx *dbs.Tx, serverId int64, month string) (int64, error) { + return this.Query(tx). + Table(this.partialTable(serverId)). + Between("day", month+"01", month+"31"). + Result("MAX(bytes)"). + FindInt64Col(0) +} + // FindServerStats 查找某个时间段的带宽统计 // 参数: // - day YYYYMMDD diff --git a/internal/rpc/services/service_server_bandwidth_stat.go b/internal/rpc/services/service_server_bandwidth_stat.go index 2aa3ac39..b07f1fba 100644 --- a/internal/rpc/services/service_server_bandwidth_stat.go +++ b/internal/rpc/services/service_server_bandwidth_stat.go @@ -54,6 +54,25 @@ func init() { }) } +// ServerBandwidthCacheKey 组合缓存Key +func ServerBandwidthCacheKey(serverId int64, day string, timeAt string) string { + return types.String(serverId) + "@" + day + "@" + timeAt +} + +func ServerBandwidthGetCacheBytes(serverId int64, day string, timeAt string) int64 { + var key = ServerBandwidthCacheKey(serverId, day, timeAt) + var bytes int64 = 0 + + serverBandwidthStatsLocker.Lock() + stat, ok := serverBandwidthStatsMap[key] + if ok { + bytes = stat.Bytes + } + serverBandwidthStatsLocker.Unlock() + + return bytes +} + type ServerBandwidthStatService struct { BaseService } @@ -66,7 +85,7 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C } for _, stat := range req.ServerBandwidthStats { - var key = types.String(stat.ServerId) + "@" + stat.Day + "@" + stat.TimeAt + var key = ServerBandwidthCacheKey(stat.ServerId, stat.Day, stat.TimeAt) serverBandwidthStatsLocker.Lock() oldStat, ok := serverBandwidthStatsMap[key] if ok { diff --git a/internal/rpc/services/service_server_stat_board.go b/internal/rpc/services/service_server_stat_board.go index 1c529fb9..5db836c9 100644 --- a/internal/rpc/services/service_server_stat_board.go +++ b/internal/rpc/services/service_server_stat_board.go @@ -409,6 +409,61 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context, var tx = this.NullTx() // 带宽统计 + { + var month = timeutil.Format("Ym") + var day = timeutil.Format("Ymd") + + // 当前N分钟区间 + { + // 查询最近的两个时段,以尽可能获取数据 + var minute1 = timeutil.FormatTime("Hi", time.Now().Unix()/300*300) + var minute2 = timeutil.FormatTime("Hi", time.Now().Unix()/300*300-300) + + for _, minute := range []string{minute1, minute2} { + bytes, err := models.SharedServerBandwidthStatDAO.FindMinutelyPeekBandwidthBytes(tx, req.ServerId, day, minute) + if err != nil { + return nil, err + } + if bytes == 0 { + // 尝试从缓存中读取 + bytes = ServerBandwidthGetCacheBytes(req.ServerId, day, minute) + } + + if bytes > 0 { + result.MinutelyPeekBandwidthBytes = bytes + break + } + } + } + + // 当天 + { + bytes, err := models.SharedServerBandwidthStatDAO.FindDailyPeekBandwidthBytes(tx, req.ServerId, day) + if err != nil { + return nil, err + } + result.DailyPeekBandwidthBytes = bytes + } + + // 当月 + { + bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, month) + if err != nil { + return nil, err + } + result.MonthlyPeekBandwidthBytes = bytes + } + + // 上月 + { + bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, timeutil.Format("Ym", time.Now().AddDate(0, -1, 0))) + if err != nil { + return nil, err + } + result.LastMonthlyPeekBandwidthBytes = bytes + } + } + { var bandwidthMinutes = utils.RangeMinutes(time.Now(), 12, 5) var bandwidthStatMap = map[string]*pb.ServerBandwidthStat{} @@ -438,11 +493,11 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context, ServerId: req.ServerId, Day: minute.Day, TimeAt: minute.Minute, - Bytes: 0, + Bytes: ServerBandwidthGetCacheBytes(req.ServerId, minute.Day, minute.Minute), // 从当前缓存中读取 }) } } - result.ServerBandwidthStats = pbBandwidthStats + result.MinutelyBandwidthStats = pbBandwidthStats } // 按日流量统计