mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-07 18:50:26 +08:00
服务看板增加峰值带宽数据
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
// 按日流量统计
|
||||
|
||||
Reference in New Issue
Block a user