服务列表带宽使用新的算法

This commit is contained in:
GoEdgeLab
2022-08-27 18:39:00 +08:00
parent d72f98c8b1
commit 43158faabb
6 changed files with 111 additions and 115 deletions

View File

@@ -862,49 +862,32 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
} }
// 排序 // 排序
var day = timeutil.Format("Ymd") var timestamp = time.Now().Unix() / 300 * 300
var minute = timeutil.FormatTime("His", time.Now().Unix()/300*300-300) var currentTime = timeutil.FormatTime("YmdHi", timestamp)
var selfTable = this.Table var prevTime = timeutil.FormatTime("YmdHi", timestamp-300)
var statTable = SharedServerDailyStatDAO.Table
var hasOnlyIds = false
switch order { switch order {
case "trafficOutAsc": case "trafficOutAsc":
query.Result("id") query.Asc("IF(IF(bandwidthTime=:currentTime, bandwidthBytes, 0) > 0, IF(bandwidthTime=:currentTime, bandwidthBytes, 0), IF(bandwidthTime=:prevTime, bandwidthBytes, 0))")
query.Join(SharedServerDailyStatDAO, dbs.QueryJoinLeft, selfTable+".id="+statTable+".serverId AND "+statTable+".day=:day AND "+statTable+".timeFrom=:minute") query.Param("currentTime", currentTime)
query.Param("day", day) query.Param("prevTime", prevTime)
query.Param("minute", minute) query.DescPk()
query.Group(selfTable + ".id")
query.Asc("SUM(" + statTable + ".bytes)").
DescPk()
hasOnlyIds = true
case "trafficOutDesc": case "trafficOutDesc":
query.Result("id") query.Desc("IF(IF(bandwidthTime=:currentTime, bandwidthBytes, 0) > 0, IF(bandwidthTime=:currentTime, bandwidthBytes, 0), IF(bandwidthTime=:prevTime, bandwidthBytes, 0))")
query.Join(SharedServerDailyStatDAO, dbs.QueryJoinLeft, selfTable+".id="+statTable+".serverId AND "+statTable+".day=:day AND "+statTable+".timeFrom=:minute") query.Param("currentTime", currentTime)
query.Param("day", day) query.Param("prevTime", prevTime)
query.Param("minute", minute) query.DescPk()
query.Group(selfTable + ".id")
query.Desc("SUM(" + statTable + ".bytes)").
DescPk()
hasOnlyIds = true
default: default:
query.DescPk() query.DescPk()
} }
_, err = query.FindAll() _, err = query.FindAll()
if hasOnlyIds { // 修正带宽统计数据
var newResult = []*Server{} for _, server := range result {
for _, one := range result { if len(server.BandwidthTime) > 0 && server.BandwidthBytes > 0 && server.BandwidthTime < prevTime {
server, err := this.Find(tx, one.Id) server.BandwidthBytes = 0
if err != nil {
return nil, err
}
if server == nil {
continue
}
newResult = append(newResult, server.(*Server))
} }
result = newResult
} }
return return
@@ -2578,6 +2561,22 @@ func (this *ServerDAO) FindUserServerClusterIds(tx *dbs.Tx, userId int64) ([]int
return clusterIds, nil return clusterIds, nil
} }
// UpdateServerBandwidth 更新服务带宽
// fullTime YYYYMMDDHHII
func (this *ServerDAO) UpdateServerBandwidth(tx *dbs.Tx, serverId int64, fullTime string, bandwidthBytes int64) error {
if serverId <= 0 {
return nil
}
if bandwidthBytes < 0 {
bandwidthBytes = 0
}
return this.Query(tx).
Pk(serverId).
Set("bandwidthTime", fullTime).
Set("bandwidthBytes", bandwidthBytes).
UpdateQuickly()
}
// NotifyUpdate 同步服务所在的集群 // NotifyUpdate 同步服务所在的集群
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error { func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
// 创建任务 // 创建任务

View File

@@ -48,53 +48,57 @@ type Server struct {
UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID
LastUserPlanId uint32 `field:"lastUserPlanId"` // 上一次使用的套餐 LastUserPlanId uint32 `field:"lastUserPlanId"` // 上一次使用的套餐
Uam dbs.JSON `field:"uam"` // UAM设置 Uam dbs.JSON `field:"uam"` // UAM设置
BandwidthTime string `field:"bandwidthTime"` // 带宽更新时间YYYYMMDDHHII
BandwidthBytes uint64 `field:"bandwidthBytes"` // 最近带宽峰值
} }
type ServerOperator struct { type ServerOperator struct {
Id interface{} // ID Id any // ID
IsOn interface{} // 是否启用 IsOn any // 是否启用
UserId interface{} // 用户ID UserId any // 用户ID
AdminId interface{} // 管理员ID AdminId any // 管理员ID
Type interface{} // 服务类型 Type any // 服务类型
Name interface{} // 名称 Name any // 名称
Description interface{} // 描述 Description any // 描述
PlainServerNames interface{} // 扁平化域名列表 PlainServerNames any // 扁平化域名列表
ServerNames interface{} // 域名列表 ServerNames any // 域名列表
AuditingAt interface{} // 审核提交时间 AuditingAt any // 审核提交时间
AuditingServerNames interface{} // 审核中的域名 AuditingServerNames any // 审核中的域名
IsAuditing interface{} // 是否正在审核 IsAuditing any // 是否正在审核
AuditingResult interface{} // 审核结果 AuditingResult any // 审核结果
Http interface{} // HTTP配置 Http any // HTTP配置
Https interface{} // HTTPS配置 Https any // HTTPS配置
Tcp interface{} // TCP配置 Tcp any // TCP配置
Tls interface{} // TLS配置 Tls any // TLS配置
Unix interface{} // Unix配置 Unix any // Unix配置
Udp interface{} // UDP配置 Udp any // UDP配置
WebId interface{} // WEB配置 WebId any // WEB配置
ReverseProxy interface{} // 反向代理配置 ReverseProxy any // 反向代理配置
GroupIds interface{} // 分组ID列表 GroupIds any // 分组ID列表
Config interface{} // 服务配置,自动生成 Config any // 服务配置,自动生成
ConfigMd5 interface{} // Md5 ConfigMd5 any // Md5
ClusterId interface{} // 集群ID ClusterId any // 集群ID
IncludeNodes interface{} // 部署条件 IncludeNodes any // 部署条件
ExcludeNodes interface{} // 节点排除条件 ExcludeNodes any // 节点排除条件
Version interface{} // 版本号 Version any // 版本号
CreatedAt interface{} // 创建时间 CreatedAt any // 创建时间
State interface{} // 状态 State any // 状态
DnsName interface{} // DNS名称 DnsName any // DNS名称
TcpPorts interface{} // 所包含TCP端口 TcpPorts any // 所包含TCP端口
UdpPorts interface{} // 所包含UDP端口 UdpPorts any // 所包含UDP端口
SupportCNAME interface{} // 允许CNAME不在域名名单 SupportCNAME any // 允许CNAME不在域名名单
TrafficLimit interface{} // 流量限制 TrafficLimit any // 流量限制
TrafficDay interface{} // YYYYMMDD TrafficDay any // YYYYMMDD
TrafficMonth interface{} // YYYYMM TrafficMonth any // YYYYMM
TotalDailyTraffic interface{} // 日流量 TotalDailyTraffic any // 日流量
TotalMonthlyTraffic interface{} // 月流量 TotalMonthlyTraffic any // 月流量
TrafficLimitStatus interface{} // 流量限制状态 TrafficLimitStatus any // 流量限制状态
TotalTraffic interface{} // 总流量 TotalTraffic any // 总流量
UserPlanId interface{} // 所属套餐ID UserPlanId any // 所属套餐ID
LastUserPlanId interface{} // 上一次使用的套餐 LastUserPlanId any // 上一次使用的套餐
Uam interface{} // UAM设置 Uam any // UAM设置
BandwidthTime any // 带宽更新时间YYYYMMDDHHII
BandwidthBytes any // 最近带宽峰值
} }
func NewServerOperator() *ServerOperator { func NewServerOperator() *ServerOperator {

View File

@@ -782,7 +782,7 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
if err != nil { if err != nil {
return nil, err return nil, err
} }
result := []*pb.Server{} var result = []*pb.Server{}
for _, server := range servers { for _, server := range servers {
clusterName, err := models.SharedNodeClusterDAO.FindNodeClusterName(tx, int64(server.ClusterId)) clusterName, err := models.SharedNodeClusterDAO.FindNodeClusterName(tx, int64(server.ClusterId))
if err != nil { if err != nil {
@@ -790,9 +790,9 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
} }
// 分组信息 // 分组信息
pbGroups := []*pb.ServerGroup{} var pbGroups = []*pb.ServerGroup{}
if models.IsNotNull(server.GroupIds) { if models.IsNotNull(server.GroupIds) {
groupIds := []int64{} var groupIds = []int64{}
err = json.Unmarshal(server.GroupIds, &groupIds) err = json.Unmarshal(server.GroupIds, &groupIds)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -827,7 +827,7 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
} }
// 审核结果 // 审核结果
auditingResult := &pb.ServerNameAuditingResult{} var auditingResult = &pb.ServerNameAuditingResult{}
if len(server.AuditingResult) > 0 { if len(server.AuditingResult) > 0 {
err = json.Unmarshal(server.AuditingResult, auditingResult) err = json.Unmarshal(server.AuditingResult, auditingResult)
if err != nil { if err != nil {
@@ -847,27 +847,6 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
return nil, err return nil, err
} }
// 当前统计
dailyStat, err := models.SharedServerDailyStatDAO.SumCurrentDailyStat(tx, int64(server.Id))
if err != nil {
return nil, err
}
var pbDailyStat *pb.ServerDailyStat
if dailyStat != nil {
pbDailyStat = &pb.ServerDailyStat{
Bytes: int64(dailyStat.Bytes),
CachedBytes: int64(dailyStat.CachedBytes),
AttackBytes: int64(dailyStat.AttackBytes),
CountRequests: int64(dailyStat.CountRequests),
CountCachedRequests: int64(dailyStat.CountCachedRequests),
CountAttackRequests: int64(dailyStat.CountAttackRequests),
Day: dailyStat.Day,
Hour: dailyStat.Hour,
TimeFrom: dailyStat.TimeFrom,
TimeTo: dailyStat.TimeTo,
}
}
result = append(result, &pb.Server{ result = append(result, &pb.Server{
Id: int64(server.Id), Id: int64(server.Id),
IsOn: server.IsOn, IsOn: server.IsOn,
@@ -895,9 +874,10 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
Id: int64(server.ClusterId), Id: int64(server.ClusterId),
Name: clusterName, Name: clusterName,
}, },
ServerGroups: pbGroups, ServerGroups: pbGroups,
User: pbUser, User: pbUser,
LatestServerDailyStat: pbDailyStat, BandwidthTime: server.BandwidthTime,
BandwidthBytes: int64(server.BandwidthBytes),
}) })
} }

View File

@@ -29,19 +29,36 @@ func init() {
goman.New(func() { goman.New(func() {
for range ticker.C { for range ticker.C {
func() { func() {
var tx *dbs.Tx
serverBandwidthStatsLocker.Lock() serverBandwidthStatsLocker.Lock()
var m = serverBandwidthStatsMap var m = serverBandwidthStatsMap
serverBandwidthStatsMap = map[string]*pb.ServerBandwidthStat{} serverBandwidthStatsMap = map[string]*pb.ServerBandwidthStat{}
serverBandwidthStatsLocker.Unlock() serverBandwidthStatsLocker.Unlock()
tx, err := models.SharedServerBandwidthStatDAO.Instance.Begin()
if err != nil {
remotelogs.Error("ServerBandwidthStatService", "begin transaction failed: "+err.Error())
return
}
defer func() {
_ = tx.Commit()
}()
for _, stat := range m { for _, stat := range m {
err := models.SharedServerBandwidthStatDAO.UpdateServerBandwidth(tx, stat.UserId, stat.ServerId, stat.Day, stat.TimeAt, stat.Bytes) // 更新服务的带宽峰值
if err != nil { if stat.ServerId > 0 {
remotelogs.Error("ServerBandwidthStatService", "dump bandwidth stats failed: "+err.Error()) err := models.SharedServerBandwidthStatDAO.UpdateServerBandwidth(tx, stat.UserId, stat.ServerId, stat.Day, stat.TimeAt, stat.Bytes)
if err != nil {
remotelogs.Error("ServerBandwidthStatService", "dump bandwidth stats failed: "+err.Error())
}
err = models.SharedServerDAO.UpdateServerBandwidth(tx, stat.ServerId, stat.Day+stat.TimeAt, stat.Bytes)
if err != nil {
remotelogs.Error("ServerBandwidthStatService", "update server bandwidth failed: "+err.Error())
}
} }
// 更新服务的带宽峰值
if stat.UserId > 0 { if stat.UserId > 0 {
err = models.SharedUserBandwidthStatDAO.UpdateUserBandwidth(tx, stat.UserId, stat.Day, stat.TimeAt, stat.Bytes) err = models.SharedUserBandwidthStatDAO.UpdateUserBandwidth(tx, stat.UserId, stat.Day, stat.TimeAt, stat.Bytes)
if err != nil { if err != nil {

View File

@@ -424,10 +424,6 @@ func (this *ServerStatBoardService) ComposeServerStatBoard(ctx context.Context,
if err != nil { if err != nil {
return nil, err return nil, err
} }
if bytes == 0 {
// 尝试从缓存中读取
bytes = ServerBandwidthGetCacheBytes(req.ServerId, day, minute)
}
if bytes > 0 { if bytes > 0 {
result.MinutelyPeekBandwidthBytes = bytes result.MinutelyPeekBandwidthBytes = bytes

File diff suppressed because one or more lines are too long