不同地区提取合并自动配置

This commit is contained in:
2025-04-02 17:24:12 +08:00
parent 13794c2d27
commit 68e96793fe
3 changed files with 113 additions and 23 deletions

View File

@@ -1,6 +1,78 @@
package main
import (
"encoding/json"
"platform/pkg/orm"
m "platform/web/models"
q "platform/web/queries"
"time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
func main() {
println(rune('w'))
println(rune('m'))
open, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
panic(err)
}
err = open.AutoMigrate(&m.Resource{}, &m.ResourcePss{})
if err != nil {
panic(err)
}
q.SetDefault(open)
var r = &m.Resource{
ID: 1,
UserID: 101,
Active: true,
}
open.Create(r)
var resourcePss = &m.ResourcePss{
ID: 1,
ResourceID: 1,
Type: 1,
Live: 180,
Expire: time.Now().AddDate(1, 0, 0),
DailyLimit: 10000,
}
open.Create(resourcePss)
var resource = new(ResourceInfo)
data := q.Resource.As("data")
pss := q.ResourcePss.As("pss")
err = data.Scopes(orm.Alias(data)).
Select(
data.ID, data.UserID, data.Active,
pss.Type, pss.Live, pss.DailyUsed, pss.DailyLimit, pss.DailyLast, pss.Quota, pss.Used, pss.Expire,
).
LeftJoin(q.ResourcePss.As("pss"), pss.ResourceID.EqCol(data.ID)).
Where(data.ID.Eq(1)).
Scan(&resource)
if err != nil {
panic(err)
}
bytes, err := json.MarshalIndent(resource, "", " ")
if err != nil {
panic(err)
}
println(string(bytes))
}
type ResourceInfo struct {
Id int32
UserId int32
Active bool
Type int32
Live int32
DailyLimit int32
DailyUsed int32
DailyLast time.Time
Quota int32
Used int32
Expire time.Time
}

View File

@@ -308,6 +308,16 @@ func (s *channelService) CreateChannel(
// 更新套餐使用记录
step = time.Now()
toUpdate := models.ResourcePss{
Used: resource.Used + int32(count),
DailyLast: now,
}
last := resource.DailyLast
if now.Year() != last.Year() || now.Month() != last.Month() || now.Day() != last.Day() {
toUpdate.DailyUsed = int32(count)
} else {
toUpdate.DailyUsed = resource.DailyUsed + int32(count)
}
_, err = q.ResourcePss.
Where(q.ResourcePss.ResourceID.Eq(resourceId)).
Select(
@@ -315,11 +325,7 @@ func (s *channelService) CreateChannel(
q.ResourcePss.DailyUsed,
q.ResourcePss.DailyLast,
).
Updates(models.ResourcePss{
Used: resource.Used + int32(count),
DailyUsed: resource.DailyUsed + int32(count),
DailyLast: now,
})
Updates(toUpdate)
if err != nil {
return err
}
@@ -468,15 +474,33 @@ func assignEdge(count int, filter NodeFilterConfig) (*AssignEdgeResult, error) {
step = time.Now()
slog.Debug("新增新节点", "proxy", info.proxy.Name, "used", info.used, "count", info.count)
rConfigs := rProxyConfigs[info.proxy.Name]
var newConfig = remote.AutoConfig{
Province: filter.Prov,
City: filter.City,
Isp: filter.Isp,
Count: int(math.Ceil(float64(info.used) * 11 / 10)),
}
var newConfigs []remote.AutoConfig
var update = false
for _, rConfig := range rConfigs {
if rConfig.Isp == filter.Isp && rConfig.City == filter.City && rConfig.Province == filter.Prov {
newConfigs = append(newConfigs, newConfig)
update = true
} else {
newConfigs = append(newConfigs, rConfig)
}
}
if !update {
newConfigs = append(newConfigs, newConfig)
}
err := remote.Cloud.CloudConnect(remote.CloudConnectReq{
Uuid: info.proxy.Name,
Edge: nil,
AutoConfig: []remote.AutoConfig{{
Province: filter.Prov,
City: filter.City,
Isp: filter.Isp,
Count: int(math.Ceil(float64(info.used) * 11 / 10)),
}},
Uuid: info.proxy.Name,
Edge: nil,
AutoConfig: newConfigs,
})
if err != nil {
return nil, err

View File

@@ -475,6 +475,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
wantErr: true,
wantErrContains: "端口数量不足",
},
// todo 跨天用量更新
// todo 多地区混杂条件提取
}
for _, tt := range tests {
@@ -510,14 +512,6 @@ func Test_channelService_CreateChannel(t *testing.T) {
return
}
// 检查返回地址格式
for _, addr := range got {
protocol := string(tt.args.protocol)
if !strings.HasPrefix(addr, protocol+"://") {
t.Errorf("CreateChannel() 地址 %v 不是有效的 %s 地址", addr, protocol)
}
}
// 查询创建的通道
var channels []models.Channel
db.Where(