Files
waf-platform/EdgeUser/internal/web/actions/default/anti-ddos/packages/index.go

151 lines
4.5 KiB
Go

// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package packages
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct{}) {
// 高防产品
var allPackageMap = map[int64]*pb.ADPackage{} // packageId => *pb.ADPackage
packagesResp, err := this.RPC().ADPackageRPC().FindAllIdleADPackages(this.UserContext(), &pb.FindAllIdleADPackagesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
for _, p := range packagesResp.AdPackages {
allPackageMap[p.Id] = p
}
// 价格
pricesResp, err := this.RPC().ADPackagePriceRPC().FindAllADPackagePrices(this.UserContext(), &pb.FindAllADPackagePricesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var priceMaps = []maps.Map{}
for _, price := range pricesResp.AdPackagePrices {
if price.Price > 0 {
var packageId = price.AdPackageId
var periodId = price.AdPackagePeriodId
p, ok := allPackageMap[packageId]
if !ok {
continue
}
var networkId = p.AdNetworkId
var countIdleInstances = p.CountIdleADPackageInstances
if packageId > 0 && periodId > 0 && networkId > 0 && countIdleInstances > 0 {
priceMaps = append(priceMaps, maps.Map{
"networkId": networkId,
"packageId": packageId,
"protectionBandwidth": types.String(p.ProtectionBandwidthSize) + userconfigs.ADPackageSizeFullUnit(p.ProtectionBandwidthUnit),
"serverBandwidth": types.String(p.ServerBandwidthSize) + userconfigs.ADPackageSizeFullUnit(p.ServerBandwidthUnit),
"periodId": periodId,
"price": price.Price,
"maxInstances": countIdleInstances,
})
}
}
}
this.Data["prices"] = priceMaps
// 重新处理防护带宽和服务带宽
var allProtectionBandwidthSizes = []string{}
var allServerBandwidthSizes = []string{}
for _, p := range packagesResp.AdPackages {
var protectionSize = types.String(p.ProtectionBandwidthSize) + userconfigs.ADPackageSizeFullUnit(p.ProtectionBandwidthUnit)
var serverSize = types.String(p.ServerBandwidthSize) + userconfigs.ADPackageSizeFullUnit(p.ServerBandwidthUnit)
if !lists.ContainsString(allProtectionBandwidthSizes, protectionSize) {
var found = false
for _, price := range priceMaps {
if types.String(price["protectionBandwidth"]) == protectionSize {
found = true
}
}
if found {
allProtectionBandwidthSizes = append(allProtectionBandwidthSizes, protectionSize)
}
}
if !lists.ContainsString(allServerBandwidthSizes, serverSize) {
var found = false
for _, price := range priceMaps {
if types.String(price["serverBandwidth"]) == serverSize {
found = true
}
}
if found {
allServerBandwidthSizes = append(allServerBandwidthSizes, serverSize)
}
}
}
this.Data["allProtectionBandwidthSizes"] = allProtectionBandwidthSizes
this.Data["allServerBandwidthSizes"] = allServerBandwidthSizes
// 线路
var networkMaps = []maps.Map{}
networkResp, err := this.RPC().ADNetworkRPC().FindAllAvailableADNetworks(this.UserContext(), &pb.FindAllAvailableADNetworksRequest{})
if err != nil {
this.ErrorPage(err)
return
}
for _, network := range networkResp.AdNetworks {
var found = false
for _, price := range priceMaps {
if types.Int64(price["networkId"]) == network.Id {
found = true
}
}
if found {
networkMaps = append(networkMaps, maps.Map{
"id": network.Id,
"name": network.Name,
"description": network.Description,
})
}
}
this.Data["allNetworks"] = networkMaps
// 周期
var periodMaps = []maps.Map{}
periodResp, err := this.RPC().ADPackagePeriodRPC().FindAllAvailableADPackagePeriods(this.UserContext(), &pb.FindAllAvailableADPackagePeriodsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
for _, period := range periodResp.AdPackagePeriods {
var found = false
for _, price := range priceMaps {
if types.Int64(price["periodId"]) == period.Id {
found = true
}
}
if found {
periodMaps = append(periodMaps, maps.Map{
"id": period.Id,
"count": period.Count,
"unit": period.Unit,
"unitName": userconfigs.ADPackagePeriodUnitName(period.Unit),
})
}
}
this.Data["allPeriods"] = periodMaps
this.Show()
}