This commit is contained in:
unknown
2026-02-04 20:27:13 +08:00
commit 3b042d1dad
9410 changed files with 1488147 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package trafficstats
import (
"bytes"
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/types"
"github.com/tealeg/xlsx/v3"
"strconv"
"strings"
)
type DownloadAction struct {
actionutils.ParentAction
}
func (this *DownloadAction) Init() {
this.Nav("", "", "")
}
func (this *DownloadAction) RunGet(params struct {
DayFrom string
DayTo string
UserId int64
ServerId int64
}) {
// 用户名
var username = "unknown"
if params.UserId > 0 {
userResp, err := this.RPC().UserRPC().FindEnabledUser(this.AdminContext(), &pb.FindEnabledUserRequest{UserId: params.UserId})
if err != nil {
this.ErrorPage(err)
return
}
if userResp.User != nil {
username = userResp.User.Username
}
}
params.DayFrom = strings.ReplaceAll(params.DayFrom, "-", "")
params.DayTo = strings.ReplaceAll(params.DayTo, "-", "")
bandwidthStatsResp, err := this.RPC().ServerBandwidthStatRPC().FindDailyServerBandwidthStatsBetweenDays(this.AdminContext(), &pb.FindDailyServerBandwidthStatsBetweenDaysRequest{
UserId: params.UserId,
ServerId: params.ServerId,
DayFrom: params.DayFrom,
DayTo: params.DayTo,
})
if err != nil {
this.ErrorPage(err)
return
}
var wb = xlsx.NewFile()
sheet, err := wb.AddSheet("default")
if err != nil {
this.ErrorPage(err)
return
}
// 头部
{
var row = sheet.AddRow()
row.SetHeight(25)
row.AddCell().SetString("时间")
row.AddCell().SetString("带宽Mbps")
row.AddCell().SetString("带宽Gbps")
}
var stats = bandwidthStatsResp.Stats
lists.Reverse(stats)
for _, stat := range stats {
var row = sheet.AddRow()
row.SetHeight(25)
row.AddCell().SetString(stat.Day[:4] + "-" + stat.Day[4:6] + "-" + stat.Day[6:] + " " + stat.TimeAt[:2] + ":" + stat.TimeAt[2:])
row.AddCell().SetString(fmt.Sprintf("%.4f", float64(stat.Bits)/(1<<20)))
row.AddCell().SetString(fmt.Sprintf("%.4f", float64(stat.Bits)/(1<<30)))
}
this.AddHeader("Content-Type", "application/vnd.ms-excel")
this.AddHeader("Content-Disposition", "attachment; filename=\"BANDWIDTH-"+username+"-"+types.String(params.ServerId)+"-"+params.DayFrom+"-"+params.DayTo+".xlsx\"")
this.AddHeader("Cache-Control", "max-age=0")
var buf = bytes.NewBuffer([]byte{})
err = wb.Write(buf)
if err != nil {
this.ErrorPage(err)
return
}
this.AddHeader("Content-Length", strconv.Itoa(buf.Len()))
_, _ = this.Write(buf.Bytes())
}

View File

@@ -0,0 +1,207 @@
// 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()
}

View File

@@ -0,0 +1,26 @@
//go:build plus
package trafficstats
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/plus"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Helper(plus.NewBasicHelper()).
Data("teaMenu", "servers").
Data("teaSubMenu", "trafficStat").
Prefix("/servers/traffic-stats").
GetPost("", new(IndexAction)).
Get("/download", new(DownloadAction)).
Post("/serverOptions", new(ServerOptionsAction)).
GetPost("/setting", new(SettingAction)).
EndAll()
})
}

View File

@@ -0,0 +1,45 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package trafficstats
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
type ServerOptionsAction struct {
actionutils.ParentAction
}
func (this *ServerOptionsAction) RunPost(params struct {
UserId int64
}) {
if params.UserId == 0 {
this.Data["servers"] = []maps.Map{}
return
}
serversResp, err := this.RPC().ServerRPC().FindAllUserServers(this.AdminContext(), &pb.FindAllUserServersRequest{UserId: params.UserId})
if err != nil {
this.ErrorPage(err)
return
}
var serverMaps = []maps.Map{
{"id": 0, "name": this.Lang(codes.ServerTrafficStat_AllServers, len(serversResp.Servers))},
}
for _, server := range serversResp.Servers {
if len(server.FirstServerName) > 0 {
server.Name = server.FirstServerName
}
serverMaps = append(serverMaps, maps.Map{
"id": server.Id,
"name": server.Name,
})
}
this.Data["servers"] = serverMaps
this.Success()
}

View File

@@ -0,0 +1,94 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//go:build plus
package trafficstats
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type SettingAction struct {
actionutils.ParentAction
}
func (this *SettingAction) Init() {
this.Nav("", "", "setting")
}
func (this *SettingAction) RunGet(params struct{}) {
this.Data["config"] = maps.Map{
"bandwidthPercentile": systemconfigs.DefaultBandwidthPercentile,
"defaultBandwidthDateRange": systemconfigs.DefaultBandwidthDateRangeCode,
"bandwidthAlgo": systemconfigs.BandwidthAlgoSecondly,
}
userConfig, _ := configloaders.LoadUserUIConfig()
if userConfig != nil {
var config = userConfig.TrafficStats
if config.BandwidthPercentile <= 0 {
config.BandwidthPercentile = systemconfigs.DefaultBandwidthPercentile
}
if len(config.DefaultBandwidthDateRange) == 0 {
config.DefaultBandwidthDateRange = systemconfigs.DefaultBandwidthDateRangeCode
}
if len(config.BandwidthAlgo) == 0 {
config.BandwidthAlgo = systemconfigs.BandwidthAlgoSecondly
}
this.Data["config"] = config
}
this.Data["dateRanges"] = systemconfigs.FindAllBandwidthDateRanges()
this.Show()
}
func (this *SettingAction) RunPost(params struct {
BandwidthPercentile int32
DefaultBandwidthDateRange string
BandwidthAlgo string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
config, err := configloaders.LoadUserUIConfig()
if err != nil {
this.ErrorPage(err)
return
}
if params.BandwidthPercentile < 0 {
params.BandwidthPercentile = 0
}
if params.BandwidthPercentile > 100 {
params.BandwidthPercentile = 95
}
config.TrafficStats.BandwidthPercentile = params.BandwidthPercentile
config.TrafficStats.DefaultBandwidthDateRange = params.DefaultBandwidthDateRange
config.TrafficStats.BandwidthAlgo = params.BandwidthAlgo
err = configloaders.UpdateUserUIConfig(config)
if err != nil {
this.ErrorPage(err)
return
}
// 财务配置
priceConfig, err := configloaders.LoadUserPriceConfig()
if err != nil {
this.ErrorPage(err)
return
}
if priceConfig != nil && priceConfig.DefaultBandwidthPriceConfig != nil && priceConfig.DefaultBandwidthPriceConfig.BandwidthAlgo != params.BandwidthAlgo {
priceConfig.DefaultBandwidthPriceConfig.BandwidthAlgo = params.BandwidthAlgo
err = configloaders.UpdateUserPriceConfig(priceConfig)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Success()
}