208 lines
5.7 KiB
Go
208 lines
5.7 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
package trafficstats
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
|
"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("", "", "index")
|
|
}
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
UserId int64
|
|
ServerId int64
|
|
Unit string
|
|
}) {
|
|
var defaultDateRangeCode = systemconfigs.DefaultBandwidthDateRangeCode
|
|
var percentile = systemconfigs.DefaultBandwidthPercentile
|
|
userConfig, _ := configloaders.LoadUserUIConfig()
|
|
if userConfig != nil {
|
|
if len(userConfig.TrafficStats.DefaultBandwidthDateRange) > 0 {
|
|
defaultDateRangeCode = userConfig.TrafficStats.DefaultBandwidthDateRange
|
|
}
|
|
if userConfig.TrafficStats.BandwidthPercentile > 0 {
|
|
percentile = userConfig.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["userId"] = params.UserId
|
|
this.Data["serverId"] = params.ServerId
|
|
this.Data["unit"] = params.Unit
|
|
|
|
// 用户ID
|
|
if params.ServerId > 0 {
|
|
serverResp, err := this.RPC().ServerRPC().FindEnabledServer(this.AdminContext(), &pb.FindEnabledServerRequest{
|
|
ServerId: params.ServerId,
|
|
IgnoreSSLCerts: true,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var server = serverResp.Server
|
|
if server != nil && server.User != nil {
|
|
this.Data["userId"] = server.User.Id
|
|
}
|
|
}
|
|
|
|
// 区域
|
|
var regionMaps = []maps.Map{}
|
|
regionsResp, err := this.RPC().NodeRegionRPC().FindAllAvailableNodeRegions(this.AdminContext(), &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
|
|
UserId int64
|
|
ServerId int64
|
|
RegionId int64
|
|
}) {
|
|
params.DayFrom = strings.ReplaceAll(params.DayFrom, "-", "")
|
|
params.DayTo = strings.ReplaceAll(params.DayTo, "-", "")
|
|
|
|
// 百分位
|
|
var percentile = systemconfigs.DefaultBandwidthPercentile
|
|
userConfig, _ := configloaders.LoadUserUIConfig()
|
|
if userConfig != nil && userConfig.TrafficStats.BandwidthPercentile > 0 {
|
|
percentile = userConfig.TrafficStats.BandwidthPercentile
|
|
}
|
|
|
|
// 带宽统计
|
|
bandwidthStatsResp, err := this.RPC().ServerBandwidthStatRPC().FindDailyServerBandwidthStatsBetweenDays(this.AdminContext(), &pb.FindDailyServerBandwidthStatsBetweenDaysRequest{
|
|
UserId: params.UserId,
|
|
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.AdminContext(), &pb.SumServerDailyStatsRequest{
|
|
UserId: params.UserId,
|
|
ServerId: params.ServerId,
|
|
DayFrom: params.DayFrom,
|
|
DayTo: params.DayTo,
|
|
NodeRegionId: params.RegionId,
|
|
})
|
|
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()
|
|
}
|