mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-04 22:35:48 +08:00
实现峰值带宽和平均带宽两种带宽算法
This commit is contained in:
@@ -65,7 +65,7 @@ func init() {
|
||||
for _, stat := range m {
|
||||
// 更新服务的带宽峰值
|
||||
if stat.ServerId > 0 {
|
||||
err := models.SharedServerBandwidthStatDAO.UpdateServerBandwidth(tx, stat.UserId, stat.ServerId, stat.Day, stat.TimeAt, stat.Bytes)
|
||||
err := models.SharedServerBandwidthStatDAO.UpdateServerBandwidth(tx, stat.UserId, stat.ServerId, stat.Day, stat.TimeAt, stat.Bytes, stat.TotalBytes)
|
||||
if err != nil {
|
||||
remotelogs.Error("ServerBandwidthStatService", "dump bandwidth stats failed: "+err.Error())
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func init() {
|
||||
|
||||
// 更新用户的带宽峰值
|
||||
if stat.UserId > 0 {
|
||||
err = models.SharedUserBandwidthStatDAO.UpdateUserBandwidth(tx, stat.UserId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes)
|
||||
err = models.SharedUserBandwidthStatDAO.UpdateUserBandwidth(tx, stat.UserId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes, stat.TotalBytes)
|
||||
if err != nil {
|
||||
remotelogs.Error("SharedUserBandwidthStatDAO", "dump bandwidth stats failed: "+err.Error())
|
||||
}
|
||||
@@ -126,6 +126,7 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C
|
||||
oldStat, ok := serverBandwidthStatsMap[key]
|
||||
if ok {
|
||||
oldStat.Bytes += stat.Bytes
|
||||
oldStat.TotalBytes += stat.TotalBytes
|
||||
} else {
|
||||
serverBandwidthStatsMap[key] = &pb.ServerBandwidthStat{
|
||||
Id: 0,
|
||||
@@ -135,6 +136,7 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C
|
||||
Day: stat.Day,
|
||||
TimeAt: stat.TimeAt,
|
||||
Bytes: stat.Bytes,
|
||||
TotalBytes: stat.TotalBytes,
|
||||
}
|
||||
}
|
||||
serverBandwidthStatsLocker.Unlock()
|
||||
@@ -150,12 +152,26 @@ func (this *ServerBandwidthStatService) FindServerBandwidthStats(ctx context.Con
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var stats = []*models.ServerBandwidthStat{}
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 带宽算法
|
||||
if len(req.Algo) == 0 {
|
||||
userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bandwidthAlgo, err := models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, userId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Algo = bandwidthAlgo
|
||||
}
|
||||
|
||||
var stats = []*models.ServerBandwidthStat{}
|
||||
if len(req.Day) > 0 {
|
||||
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithDay(tx, req.ServerId, req.Day)
|
||||
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithDay(tx, req.ServerId, req.Day, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
} else if len(req.Month) > 0 {
|
||||
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithMonth(tx, req.ServerId, req.Month)
|
||||
stats, err = models.SharedServerBandwidthStatDAO.FindAllServerStatsWithMonth(tx, req.ServerId, req.Month, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
} else {
|
||||
// 默认返回空
|
||||
return nil, errors.New("'month' or 'day' parameter is needed")
|
||||
@@ -188,12 +204,26 @@ func (this *ServerBandwidthStatService) FindHourlyServerBandwidthStats(ctx conte
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 带宽算法
|
||||
if len(req.Algo) == 0 {
|
||||
userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bandwidthAlgo, err := models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, userId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Algo = bandwidthAlgo
|
||||
}
|
||||
|
||||
if req.Hours <= 0 {
|
||||
req.Hours = 12
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
stats, err := models.SharedServerBandwidthStatDAO.FindHourlyBandwidthStats(tx, req.ServerId, req.Hours)
|
||||
stats, err := models.SharedServerBandwidthStatDAO.FindHourlyBandwidthStats(tx, req.ServerId, req.Hours, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -210,7 +240,7 @@ func (this *ServerBandwidthStatService) FindHourlyServerBandwidthStats(ctx conte
|
||||
var timeTo = timeutil.Format("YmdHi")
|
||||
|
||||
var pbNthStat *pb.FindHourlyServerBandwidthStatsResponse_Stat
|
||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenTimes(tx, req.ServerId, timeFrom, timeTo, percentile)
|
||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenTimes(tx, req.ServerId, timeFrom, timeTo, percentile, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -239,6 +269,19 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStats(ctx contex
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 带宽算法
|
||||
if len(req.Algo) == 0 {
|
||||
userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bandwidthAlgo, err := models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, userId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Algo = bandwidthAlgo
|
||||
}
|
||||
|
||||
if req.Days <= 0 {
|
||||
req.Days = 30
|
||||
}
|
||||
@@ -247,7 +290,7 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStats(ctx contex
|
||||
var dayFrom = timeutil.FormatTime("Ymd", timestamp)
|
||||
var dayTo = timeutil.Format("Ymd")
|
||||
|
||||
stats, err := models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, dayFrom, dayTo)
|
||||
stats, err := models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, dayFrom, dayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -268,7 +311,7 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStats(ctx contex
|
||||
}
|
||||
|
||||
var pbNthStat = &pb.FindDailyServerBandwidthStatsResponse_Stat{}
|
||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenDays(tx, req.ServerId, dayFrom, dayTo, percentile)
|
||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenDays(tx, req.ServerId, dayFrom, dayTo, percentile, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -308,6 +351,27 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
|
||||
}
|
||||
}
|
||||
|
||||
// 带宽算法
|
||||
if len(req.Algo) == 0 {
|
||||
var bandwidthUserId = userId
|
||||
if bandwidthUserId <= 0 {
|
||||
if req.UserId > 0 {
|
||||
bandwidthUserId = req.UserId
|
||||
} else if req.ServerId > 0 {
|
||||
bandwidthUserId, err = models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
if bandwidthUserId > 0 {
|
||||
req.Algo, err = models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, bandwidthUserId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if req.UserId <= 0 && req.ServerId <= 0 {
|
||||
return &pb.FindDailyServerBandwidthStatsBetweenDaysResponse{
|
||||
Stats: nil,
|
||||
@@ -327,10 +391,10 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
|
||||
var pbStats = []*pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat{}
|
||||
var pbNthStat *pb.FindDailyServerBandwidthStatsBetweenDaysResponse_Stat
|
||||
if req.ServerId > 0 { // 服务统计
|
||||
pbStats, err = models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo)
|
||||
pbStats, err = models.SharedServerBandwidthStatDAO.FindBandwidthStatsBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
|
||||
// nth
|
||||
stat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Percentile)
|
||||
stat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenDays(tx, req.ServerId, req.DayFrom, req.DayTo, req.Percentile, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -343,10 +407,10 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
|
||||
}
|
||||
}
|
||||
} else { // 用户统计
|
||||
pbStats, err = models.SharedUserBandwidthStatDAO.FindUserBandwidthStatsBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo)
|
||||
pbStats, err = models.SharedUserBandwidthStatDAO.FindUserBandwidthStatsBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
|
||||
// nth
|
||||
stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo, req.Percentile)
|
||||
stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo, req.Percentile, req.Algo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -409,6 +409,19 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
||||
var result = &pb.ComposeServerStatBoardResponse{}
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 用户ID
|
||||
userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var bandwidthAglo = ""
|
||||
if userId > 0 {
|
||||
bandwidthAglo, err = models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, userId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// 带宽统计
|
||||
{
|
||||
var month = timeutil.Format("Ym")
|
||||
@@ -423,7 +436,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
||||
var minute3 = timeutil.FormatTime("Hi", timestamp-300*2)
|
||||
|
||||
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, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -437,7 +450,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
||||
|
||||
// 当天
|
||||
{
|
||||
bytes, err := models.SharedServerBandwidthStatDAO.FindDailyPeekBandwidthBytes(tx, req.ServerId, day)
|
||||
bytes, err := models.SharedServerBandwidthStatDAO.FindDailyPeekBandwidthBytes(tx, req.ServerId, day, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -446,7 +459,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
||||
|
||||
// 当月
|
||||
{
|
||||
bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, month)
|
||||
bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, month, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -455,7 +468,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
||||
|
||||
// 上月
|
||||
{
|
||||
bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, timeutil.Format("Ym", time.Now().AddDate(0, -1, 0)))
|
||||
bytes, err := models.SharedServerBandwidthStatDAO.FindMonthlyPeekBandwidthBytes(tx, req.ServerId, timeutil.Format("Ym", time.Now().AddDate(0, -1, 0)), bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -476,7 +489,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
||||
timeTo = r.Day + r.MinuteTo
|
||||
}
|
||||
|
||||
bandwidthStats, err := models.SharedServerBandwidthStatDAO.FindServerStats(tx, req.ServerId, r.Day, r.MinuteFrom, r.MinuteTo)
|
||||
bandwidthStats, err := models.SharedServerBandwidthStatDAO.FindServerStats(tx, req.ServerId, r.Day, r.MinuteFrom, r.MinuteTo, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -519,7 +532,7 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
|
||||
}
|
||||
result.BandwidthPercentile = percentile
|
||||
|
||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenTimes(tx, req.ServerId, timeFrom, timeTo, percentile)
|
||||
percentileStat, err := models.SharedServerBandwidthStatDAO.FindPercentileBetweenTimes(tx, req.ServerId, timeFrom, timeTo, percentile, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ func (this *UserService) UpdateUser(ctx context.Context, req *pb.UpdateUserReque
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedUserDAO.UpdateUser(tx, req.UserId, req.Username, req.Password, req.Fullname, req.Mobile, req.Tel, req.Email, req.Remark, req.IsOn, req.NodeClusterId)
|
||||
err = models.SharedUserDAO.UpdateUser(tx, req.UserId, req.Username, req.Password, req.Fullname, req.Mobile, req.Tel, req.Email, req.Remark, req.IsOn, req.NodeClusterId, req.BandwidthAlgo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -259,6 +259,7 @@ func (this *UserService) FindEnabledUser(ctx context.Context, req *pb.FindEnable
|
||||
NodeCluster: pbCluster,
|
||||
IsIndividualIdentified: isIndividualIdentified,
|
||||
IsEnterpriseIdentified: isEnterpriseIdentified,
|
||||
BandwidthAlgo: user.BandwidthAlgo,
|
||||
OtpLogin: pbOtpAuth,
|
||||
}}, nil
|
||||
}
|
||||
@@ -404,6 +405,11 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
bandwidthAglo, err := models.SharedUserDAO.FindUserBandwidthAlgoForView(tx, req.UserId, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 网站数量
|
||||
countServers, err := models.SharedServerDAO.CountAllEnabledServersMatch(tx, 0, "", req.UserId, 0, configutils.BoolStateAll, []string{})
|
||||
if err != nil {
|
||||
@@ -423,7 +429,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
||||
// 本月带宽峰值
|
||||
var monthlyPeekBandwidthBytes int64 = 0
|
||||
{
|
||||
stat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInMonth(tx, req.UserId, currentMonth)
|
||||
stat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInMonth(tx, req.UserId, currentMonth, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -435,7 +441,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
||||
// 本日带宽峰值
|
||||
var dailyPeekBandwidthBytes int64 = 0
|
||||
{
|
||||
stat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInDay(tx, req.UserId, currentDay)
|
||||
stat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInDay(tx, req.UserId, currentDay, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -478,7 +484,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
||||
}
|
||||
|
||||
// 峰值带宽
|
||||
peekBandwidthBytesStat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInDay(tx, req.UserId, day)
|
||||
peekBandwidthBytesStat, err := models.SharedUserBandwidthStatDAO.FindUserPeekBandwidthInDay(tx, req.UserId, day, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -515,7 +521,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo
|
||||
bandwidthPercentile = userConfig.TrafficStats.BandwidthPercentile
|
||||
}
|
||||
result.BandwidthPercentile = bandwidthPercentile
|
||||
stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, 0, dayFrom, dayTo, bandwidthPercentile)
|
||||
stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, 0, dayFrom, dayTo, bandwidthPercentile, bandwidthAglo == systemconfigs.BandwidthAlgoAvg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user