添加 submit 表记录每次提交配置;顺序排列提交配置
This commit is contained in:
27
actions/submit.go
Normal file
27
actions/submit.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"zzman/clients/jd"
|
||||||
|
"zzman/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RecordSubmit(time time.Time, gatewat model.Gateway, edges []jd.EdgeInfo) error {
|
||||||
|
config, err := json.Marshal(edges)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("序列化提交数据失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = model.DB.Create(&model.Submit{
|
||||||
|
Time: time,
|
||||||
|
Gateway: gatewat.Macaddr,
|
||||||
|
Config: string(config),
|
||||||
|
}).Error
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("保存提交记录失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -70,20 +70,23 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
|
|||||||
// 查找需要更新的配置
|
// 查找需要更新的配置
|
||||||
oldConfigs := make(map[model.City][]model.Config)
|
oldConfigs := make(map[model.City][]model.Config)
|
||||||
newConfigs := make(map[model.Gateway][]ConfigInfo)
|
newConfigs := make(map[model.Gateway][]ConfigInfo)
|
||||||
|
for _, gateway := range gateways {
|
||||||
|
newConfigs[gateway] = make([]ConfigInfo, 250) // 预分配空间,减少扩容
|
||||||
|
}
|
||||||
for _, config := range configs {
|
for _, config := range configs {
|
||||||
city := findCity[config.Cityhash]
|
city := findCity[config.Cityhash]
|
||||||
gateway := findGateway[config.GatewayMac]
|
gateway := findGateway[config.GatewayMac]
|
||||||
if config.IsChange == 1 {
|
if config.IsChange == 1 {
|
||||||
oldConfigs[city] = append(oldConfigs[city], config)
|
oldConfigs[city] = append(oldConfigs[city], config)
|
||||||
} else {
|
} else {
|
||||||
newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{
|
newConfigs[gateway][config.Table-1] = ConfigInfo{
|
||||||
Change: false,
|
Change: false,
|
||||||
Remote: jd.EdgeInfo{
|
Remote: jd.EdgeInfo{
|
||||||
Mac: config.EdgeMac,
|
Mac: config.EdgeMac,
|
||||||
City: config.Cityhash,
|
City: config.Cityhash,
|
||||||
Network: config.Network,
|
Network: config.Network,
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +129,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
|
|||||||
|
|
||||||
slog.Debug(fmt.Sprintf("网关配置变更,网关:%s,旧节点:%s,新节点:%s", gateway.Macaddr, config.EdgeMac, edge.Macaddr))
|
slog.Debug(fmt.Sprintf("网关配置变更,网关:%s,旧节点:%s,新节点:%s", gateway.Macaddr, config.EdgeMac, edge.Macaddr))
|
||||||
|
|
||||||
newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{
|
newConfigs[gateway][config.Table-1] = ConfigInfo{
|
||||||
Change: true,
|
Change: true,
|
||||||
Remote: jd.EdgeInfo{
|
Remote: jd.EdgeInfo{
|
||||||
Mac: edge.Macaddr,
|
Mac: edge.Macaddr,
|
||||||
@@ -139,7 +142,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
|
|||||||
IsChange: u.P(0),
|
IsChange: u.P(0),
|
||||||
IsOnline: u.P(1),
|
IsOnline: u.P(1),
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
changesCreate = append(changesCreate, model.Change{
|
changesCreate = append(changesCreate, model.Change{
|
||||||
Time: now,
|
Time: now,
|
||||||
CityId: city.Id,
|
CityId: city.Id,
|
||||||
@@ -199,6 +202,12 @@ func update(tx *gorm.DB, arg UpdateArgs) error {
|
|||||||
return fmt.Errorf("更新网关 %s 本地配置失败:%w", gateway.Macaddr, err)
|
return fmt.Errorf("更新网关 %s 本地配置失败:%w", gateway.Macaddr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 记录提交配置
|
||||||
|
err = RecordSubmit(now, gateway, edges)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("记录网关 %s 提交配置失败:%w", gateway.Macaddr, err)
|
||||||
|
}
|
||||||
|
|
||||||
// 提交配置到云端:配置版本 gateway.ConfigVersion
|
// 提交配置到云端:配置版本 gateway.ConfigVersion
|
||||||
if !arg.Mock {
|
if !arg.Mock {
|
||||||
err := jd.GatewayConfigSet(gateway.ConfigVersion, gateway.Macaddr, edges)
|
err := jd.GatewayConfigSet(gateway.ConfigVersion, gateway.Macaddr, edges)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package model
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Id int `gorm:"column:id;primaryKey"`
|
Id int `gorm:"column:id;primaryKey"`
|
||||||
GatewayMac string `gorm:"column:macaddr"`
|
GatewayMac string `gorm:"column:macaddr"`
|
||||||
Table string `gorm:"column:table"`
|
Table int `gorm:"column:table"`
|
||||||
EdgeMac string `gorm:"column:edge"`
|
EdgeMac string `gorm:"column:edge"`
|
||||||
Network string `gorm:"column:network"`
|
Network string `gorm:"column:network"`
|
||||||
Cityhash string `gorm:"column:cityhash"`
|
Cityhash string `gorm:"column:cityhash"`
|
||||||
|
|||||||
14
model/submit.go
Normal file
14
model/submit.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Submit struct {
|
||||||
|
Id int `gorm:"column:id;primaryKey"`
|
||||||
|
Time time.Time `gorm:"column:time"`
|
||||||
|
Gateway string `gorm:"column:gateway"`
|
||||||
|
Config string `gorm:"column:config"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (Submit) TableName() string {
|
||||||
|
return "submit"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user