mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-12-25 11:36:34 +08:00
增加服务流量统计
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package stat
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
@@ -20,140 +20,34 @@ func (this *IndexAction) Init() {
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
ServerId int64
|
||||
Month string
|
||||
}) {
|
||||
month := params.Month
|
||||
if len(month) != 6 {
|
||||
month = timeutil.Format("Ym")
|
||||
}
|
||||
this.Data["month"] = month
|
||||
|
||||
serverTypeResp, err := this.RPC().ServerRPC().FindEnabledServerType(this.AdminContext(), &pb.FindEnabledServerTypeRequest{ServerId: params.ServerId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
serverType := serverTypeResp.Type
|
||||
|
||||
statIsOn := false
|
||||
|
||||
// 是否已开启
|
||||
if serverconfigs.IsHTTPServerType(serverType) {
|
||||
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
||||
{
|
||||
resp, err := this.RPC().ServerDailyStatRPC().FindServerHourlyStats(this.AdminContext(), &pb.FindServerHourlyStatsRequest{
|
||||
ServerId: params.ServerId,
|
||||
Hours: 24,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if webConfig != nil && webConfig.StatRef != nil {
|
||||
statIsOn = webConfig.StatRef.IsOn
|
||||
}
|
||||
} else {
|
||||
this.WriteString("此类型服务暂不支持统计")
|
||||
return
|
||||
}
|
||||
|
||||
this.Data["statIsOn"] = statIsOn
|
||||
|
||||
// 统计数据
|
||||
countryStatMaps := []maps.Map{}
|
||||
provinceStatMaps := []maps.Map{}
|
||||
cityStatMaps := []maps.Map{}
|
||||
|
||||
if statIsOn {
|
||||
// 地区
|
||||
{
|
||||
resp, err := this.RPC().ServerRegionCountryMonthlyStatRPC().FindTopServerRegionCountryMonthlyStats(this.AdminContext(), &pb.FindTopServerRegionCountryMonthlyStatsRequest{
|
||||
Month: month,
|
||||
ServerId: params.ServerId,
|
||||
Offset: 0,
|
||||
Size: 10,
|
||||
sort.Slice(resp.Stats, func(i, j int) bool {
|
||||
stat1 := resp.Stats[i]
|
||||
stat2 := resp.Stats[j]
|
||||
return stat1.Hour < stat2.Hour
|
||||
})
|
||||
statMaps := []maps.Map{}
|
||||
for _, stat := range resp.Stats {
|
||||
statMaps = append(statMaps, maps.Map{
|
||||
"day": stat.Hour[:4] + "-" + stat.Hour[4:6] + "-" + stat.Hour[6:8],
|
||||
"hour": stat.Hour[8:],
|
||||
"bytes": stat.Bytes,
|
||||
"cachedBytes": stat.CachedBytes,
|
||||
"countRequests": stat.CountRequests,
|
||||
"countCachedRequests": stat.CountCachedRequests,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
for _, stat := range resp.Stats {
|
||||
countryStatMaps = append(countryStatMaps, maps.Map{
|
||||
"count": stat.Count,
|
||||
"country": maps.Map{
|
||||
"id": stat.RegionCountry.Id,
|
||||
"name": stat.RegionCountry.Name,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 省份
|
||||
{
|
||||
resp, err := this.RPC().ServerRegionProvinceMonthlyStatRPC().FindTopServerRegionProvinceMonthlyStats(this.AdminContext(), &pb.FindTopServerRegionProvinceMonthlyStatsRequest{
|
||||
Month: month,
|
||||
ServerId: params.ServerId,
|
||||
Offset: 0,
|
||||
Size: 10,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
for _, stat := range resp.Stats {
|
||||
provinceStatMaps = append(provinceStatMaps, maps.Map{
|
||||
"count": stat.Count,
|
||||
"country": maps.Map{
|
||||
"id": stat.RegionCountry.Id,
|
||||
"name": stat.RegionCountry.Name,
|
||||
},
|
||||
"province": maps.Map{
|
||||
"id": stat.RegionProvince.Id,
|
||||
"name": stat.RegionProvince.Name,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 城市
|
||||
{
|
||||
resp, err := this.RPC().ServerRegionCityMonthlyStatRPC().FindTopServerRegionCityMonthlyStats(this.AdminContext(), &pb.FindTopServerRegionCityMonthlyStatsRequest{
|
||||
Month: month,
|
||||
ServerId: params.ServerId,
|
||||
Offset: 0,
|
||||
Size: 10,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
for _, stat := range resp.Stats {
|
||||
cityStatMaps = append(cityStatMaps, maps.Map{
|
||||
"count": stat.Count,
|
||||
"country": maps.Map{
|
||||
"id": stat.RegionCountry.Id,
|
||||
"name": stat.RegionCountry.Name,
|
||||
},
|
||||
"province": maps.Map{
|
||||
"id": stat.RegionProvince.Id,
|
||||
"name": stat.RegionProvince.Name,
|
||||
},
|
||||
"city": maps.Map{
|
||||
"id": stat.RegionCity.Id,
|
||||
"name": stat.RegionCity.Name,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.Data["countryStats"] = countryStatMaps
|
||||
this.Data["provinceStats"] = provinceStatMaps
|
||||
this.Data["cityStats"] = cityStatMaps
|
||||
|
||||
// 记录最近使用
|
||||
_, err = this.RPC().LatestItemRPC().IncreaseLatestItem(this.AdminContext(), &pb.IncreaseLatestItemRequest{
|
||||
ItemType: "server",
|
||||
ItemId: params.ServerId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
this.Data["hourlyStats"] = statMaps
|
||||
}
|
||||
|
||||
this.Show()
|
||||
|
||||
Reference in New Issue
Block a user