1.4.5.2
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package trafficstats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
ServerId int64
|
||||
Unit string
|
||||
}) {
|
||||
// 是否显示带宽
|
||||
config, err := configloaders.LoadUIConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
if config == nil || !config.ShowBandwidthCharts {
|
||||
return
|
||||
}
|
||||
|
||||
var defaultDateRangeCode = systemconfigs.DefaultBandwidthDateRangeCode
|
||||
var percentile = systemconfigs.DefaultBandwidthPercentile
|
||||
|
||||
if len(config.TrafficStats.DefaultBandwidthDateRange) > 0 {
|
||||
defaultDateRangeCode = config.TrafficStats.DefaultBandwidthDateRange
|
||||
}
|
||||
if config.TrafficStats.BandwidthPercentile > 0 {
|
||||
percentile = config.TrafficStats.BandwidthPercentile
|
||||
}
|
||||
|
||||
this.Data["percentile"] = percentile
|
||||
var defaultDateRange = systemconfigs.FindBandwidthDateRange(defaultDateRangeCode)
|
||||
if defaultDateRange != nil {
|
||||
this.Data["dayFrom"] = defaultDateRange.DayFrom
|
||||
this.Data["dayTo"] = defaultDateRange.DayTo
|
||||
} else {
|
||||
this.Data["dayFrom"] = timeutil.Format("Y-m-d", time.Now().AddDate(0, 0, -29 /** 加上今天是30天 **/))
|
||||
this.Data["dayTo"] = timeutil.Format("Y-m-d")
|
||||
}
|
||||
|
||||
this.Data["serverId"] = params.ServerId
|
||||
this.Data["unit"] = params.Unit
|
||||
|
||||
// 区域
|
||||
var regionMaps = []maps.Map{}
|
||||
regionsResp, err := this.RPC().NodeRegionRPC().FindAllAvailableNodeRegions(this.UserContext(), &pb.FindAllAvailableNodeRegionsRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
for _, region := range regionsResp.NodeRegions {
|
||||
regionMaps = append(regionMaps, maps.Map{
|
||||
"id": region.Id,
|
||||
"name": region.Name,
|
||||
})
|
||||
}
|
||||
this.Data["regions"] = regionMaps
|
||||
|
||||
this.Data["dayRanges"] = systemconfigs.FindAllBandwidthDateRanges()
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunPost(params struct {
|
||||
DayFrom string
|
||||
DayTo string
|
||||
ServerId int64
|
||||
RegionId int64
|
||||
}) {
|
||||
params.DayFrom = strings.ReplaceAll(params.DayFrom, "-", "")
|
||||
params.DayTo = strings.ReplaceAll(params.DayTo, "-", "")
|
||||
|
||||
// 百分位
|
||||
var percentile = systemconfigs.DefaultBandwidthPercentile
|
||||
userConfig, _ := configloaders.LoadUIConfig()
|
||||
if userConfig != nil && userConfig.TrafficStats.BandwidthPercentile > 0 {
|
||||
percentile = userConfig.TrafficStats.BandwidthPercentile
|
||||
}
|
||||
|
||||
// 带宽统计
|
||||
bandwidthStatsResp, err := this.RPC().ServerBandwidthStatRPC().FindDailyServerBandwidthStatsBetweenDays(this.UserContext(), &pb.FindDailyServerBandwidthStatsBetweenDaysRequest{
|
||||
ServerId: params.ServerId,
|
||||
DayFrom: params.DayFrom,
|
||||
DayTo: params.DayTo,
|
||||
Percentile: percentile,
|
||||
NodeRegionId: params.RegionId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var bandwidthMaps = []maps.Map{}
|
||||
|
||||
var maxBits int64 = 0
|
||||
var maxBitsTime = ""
|
||||
var maxTime = ""
|
||||
for _, stat := range bandwidthStatsResp.Stats {
|
||||
if stat.Bits > maxBits {
|
||||
maxBits = stat.Bits
|
||||
maxBitsTime = stat.Day[:4] + "-" + stat.Day[4:6] + "-" + stat.Day[6:] + " " + stat.TimeAt[:2] + ":" + stat.TimeAt[2:]
|
||||
}
|
||||
|
||||
if len(maxTime) == 0 || maxTime < stat.TimeAt {
|
||||
maxTime = stat.TimeAt
|
||||
}
|
||||
|
||||
bandwidthMaps = append(bandwidthMaps, maps.Map{
|
||||
"day": stat.Day,
|
||||
"time": stat.TimeAt,
|
||||
"bytes": stat.Bytes,
|
||||
"bits": stat.Bits,
|
||||
"mbps": fmt.Sprintf("%.4f", float64(stat.Bits)/(1<<20)),
|
||||
"gbps": fmt.Sprintf("%.4f", float64(stat.Bits)/(1<<30)),
|
||||
})
|
||||
}
|
||||
|
||||
// 补充当天剩余的数据
|
||||
var today = timeutil.Format("Ymd")
|
||||
if params.DayFrom == params.DayTo && params.DayFrom == today {
|
||||
if len(maxTime) == 0 {
|
||||
maxTime = "0000"
|
||||
}
|
||||
minutes, err := utils.RangeTimes(maxTime, "2359", 5)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if len(minutes) > 1 {
|
||||
minutes = minutes[1:]
|
||||
for _, minute := range minutes {
|
||||
bandwidthMaps = append(bandwidthMaps, maps.Map{
|
||||
"day": today,
|
||||
"time": minute,
|
||||
"bytes": 0,
|
||||
"bits": 0,
|
||||
"mbps": "0",
|
||||
"gbps": "0",
|
||||
"isNull": true, // 表示当前数据为空
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.Data["bandwidthStats"] = bandwidthMaps
|
||||
this.Data["maxBandwidthBits"] = maxBits
|
||||
this.Data["maxBandwidthTime"] = maxBitsTime
|
||||
|
||||
// 95th
|
||||
this.Data["bandwidth95thBits"] = 0
|
||||
if bandwidthStatsResp.NthStat != nil {
|
||||
this.Data["bandwidth95thBits"] = bandwidthStatsResp.NthStat.Bits
|
||||
}
|
||||
|
||||
// 总流量
|
||||
this.Data["totalTrafficBytes"] = 0
|
||||
this.Data["totalTrafficRequests"] = 0
|
||||
{
|
||||
sumTrafficResp, err := this.RPC().ServerDailyStatRPC().SumServerDailyStats(this.UserContext(), &pb.SumServerDailyStatsRequest{
|
||||
UserId: this.UserId(),
|
||||
ServerId: params.ServerId,
|
||||
NodeRegionId: params.RegionId,
|
||||
DayFrom: params.DayFrom,
|
||||
DayTo: params.DayTo,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var stat = sumTrafficResp.ServerDailyStat
|
||||
if stat != nil {
|
||||
this.Data["totalTrafficBytes"] = stat.Bytes
|
||||
this.Data["totalTrafficRequests"] = stat.CountRequests
|
||||
}
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user