mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-04 16:00:24 +08:00
服务列表增加下行带宽
This commit is contained in:
@@ -132,6 +132,23 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
|
||||
return nil
|
||||
}
|
||||
|
||||
// SumCurrentDailyStat 查找当前时刻的数据统计
|
||||
func (this *ServerDailyStatDAO) SumCurrentDailyStat(tx *dbs.Tx, serverId int64) (*ServerDailyStat, error) {
|
||||
var day = timeutil.Format("Ymd")
|
||||
var minute = timeutil.FormatTime("His", time.Now().Unix()/300*300-300)
|
||||
one, err := this.Query(tx).
|
||||
Result("MIN(id)", "MIN(serverId)", "SUM(bytes) AS bytes", "SUM(cachedBytes) AS cachedBytes", "SUM(attackBytes) AS attackBytes", "SUM(countRequests) AS countRequests", "SUM(countCachedRequests) AS countCachedRequests", "SUM(countAttackRequests) AS countAttackRequests").
|
||||
Attr("serverId", serverId).
|
||||
Attr("day", day).
|
||||
Attr("timeFrom", minute).
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return one.(*ServerDailyStat), nil
|
||||
}
|
||||
|
||||
// SumServerMonthlyWithRegion 根据服务计算某月合计
|
||||
// month 格式为YYYYMM
|
||||
func (this *ServerDailyStatDAO) SumServerMonthlyWithRegion(tx *dbs.Tx, serverId int64, regionId int64, month string) (int64, error) {
|
||||
|
||||
@@ -759,12 +759,11 @@ func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, ke
|
||||
// ListEnabledServersMatch 列出单页的服务
|
||||
// 参数:
|
||||
// groupId 分组ID,如果为-1,则搜索没有分组的服务
|
||||
func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32, protocolFamilies []string) (result []*Server, err error) {
|
||||
func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size int64, groupId int64, keyword string, userId int64, clusterId int64, auditingFlag int32, protocolFamilies []string, order string) (result []*Server, err error) {
|
||||
query := this.Query(tx).
|
||||
State(ServerStateEnabled).
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
DescPk().
|
||||
Slice(&result)
|
||||
|
||||
if groupId > 0 {
|
||||
@@ -807,7 +806,52 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
|
||||
query.Where("(" + strings.Join(protocolConds, " OR ") + ")")
|
||||
}
|
||||
|
||||
// 排序
|
||||
var day = timeutil.Format("Ymd")
|
||||
var minute = timeutil.FormatTime("His", time.Now().Unix()/300*300-300)
|
||||
var selfTable = this.Table
|
||||
var statTable = SharedServerDailyStatDAO.Table
|
||||
var hasOnlyIds = false
|
||||
switch order {
|
||||
case "trafficOutAsc":
|
||||
query.Result("id")
|
||||
query.Join(SharedServerDailyStatDAO, dbs.QueryJoinLeft, selfTable+".id="+statTable+".serverId AND "+statTable+".day=:day AND "+statTable+".timeFrom=:minute")
|
||||
query.Param("day", day)
|
||||
query.Param("minute", minute)
|
||||
query.Group(selfTable + ".id")
|
||||
query.Asc("SUM(" + statTable + ".bytes)").
|
||||
DescPk()
|
||||
hasOnlyIds = true
|
||||
case "trafficOutDesc":
|
||||
query.Result("id")
|
||||
query.Join(SharedServerDailyStatDAO, dbs.QueryJoinLeft, selfTable+".id="+statTable+".serverId AND "+statTable+".day=:day AND "+statTable+".timeFrom=:minute")
|
||||
query.Param("day", day)
|
||||
query.Param("minute", minute)
|
||||
query.Group(selfTable + ".id")
|
||||
query.Desc("SUM(" + statTable + ".bytes)").
|
||||
DescPk()
|
||||
hasOnlyIds = true
|
||||
default:
|
||||
query.DescPk()
|
||||
}
|
||||
|
||||
_, err = query.FindAll()
|
||||
|
||||
if hasOnlyIds {
|
||||
var newResult = []*Server{}
|
||||
for _, one := range result {
|
||||
server, err := this.Find(tx, one.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if server == nil {
|
||||
continue
|
||||
}
|
||||
newResult = append(newResult, server.(*Server))
|
||||
}
|
||||
result = newResult
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -624,7 +624,14 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
|
||||
req.UserId = userId
|
||||
}
|
||||
|
||||
servers, err := models.SharedServerDAO.ListEnabledServersMatch(tx, req.Offset, req.Size, req.ServerGroupId, req.Keyword, req.UserId, req.NodeClusterId, req.AuditingFlag, utils.SplitStrings(req.ProtocolFamily, ","))
|
||||
var order = ""
|
||||
if req.TrafficOutAsc {
|
||||
order = "trafficOutAsc"
|
||||
} else if req.TrafficOutDesc {
|
||||
order = "trafficOutDesc"
|
||||
}
|
||||
|
||||
servers, err := models.SharedServerDAO.ListEnabledServersMatch(tx, req.Offset, req.Size, req.ServerGroupId, req.Keyword, req.UserId, req.NodeClusterId, req.AuditingFlag, utils.SplitStrings(req.ProtocolFamily, ","), order)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -693,6 +700,27 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
|
||||
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{
|
||||
Id: int64(server.Id),
|
||||
IsOn: server.IsOn,
|
||||
@@ -722,6 +750,7 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
|
||||
},
|
||||
ServerGroups: pbGroups,
|
||||
User: pbUser,
|
||||
LatestServerDailyStat: pbDailyStat,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/stats"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"math"
|
||||
"time"
|
||||
@@ -220,3 +221,36 @@ func (this *ServerDailyStatService) FindLatestServerDailyStats(ctx context.Conte
|
||||
}
|
||||
return &pb.FindLatestServerDailyStatsResponse{Stats: result}, nil
|
||||
}
|
||||
|
||||
// SumCurrentServerDailyStats 查找单个服务当前统计数据
|
||||
func (this *ServerDailyStatService) SumCurrentServerDailyStats(ctx context.Context, req *pb.SumCurrentServerDailyStatsRequest) (*pb.SumCurrentServerDailyStatsResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx *dbs.Tx
|
||||
stat, err := models.SharedServerDailyStatDAO.SumCurrentDailyStat(tx, req.ServerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pbStat = &pb.ServerDailyStat{
|
||||
ServerId: req.ServerId,
|
||||
}
|
||||
if stat != nil {
|
||||
pbStat = &pb.ServerDailyStat{
|
||||
ServerId: req.ServerId,
|
||||
Bytes: int64(stat.Bytes),
|
||||
CachedBytes: int64(stat.CachedBytes),
|
||||
CountRequests: int64(stat.CountRequests),
|
||||
CountCachedRequests: int64(stat.CountCachedRequests),
|
||||
CountAttackRequests: int64(stat.CountAttackRequests),
|
||||
AttackBytes: int64(stat.AttackBytes),
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.SumCurrentServerDailyStatsResponse{
|
||||
ServerDailyStat: pbStat,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user