diff --git a/actions/submit.go b/actions/submit.go new file mode 100644 index 0000000..40a0a9a --- /dev/null +++ b/actions/submit.go @@ -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 +} diff --git a/actions/update.go b/actions/update.go index 1f83166..607baa5 100644 --- a/actions/update.go +++ b/actions/update.go @@ -70,20 +70,23 @@ func update(tx *gorm.DB, arg UpdateArgs) error { // 查找需要更新的配置 oldConfigs := make(map[model.City][]model.Config) newConfigs := make(map[model.Gateway][]ConfigInfo) + for _, gateway := range gateways { + newConfigs[gateway] = make([]ConfigInfo, 250) // 预分配空间,减少扩容 + } for _, config := range configs { city := findCity[config.Cityhash] gateway := findGateway[config.GatewayMac] if config.IsChange == 1 { oldConfigs[city] = append(oldConfigs[city], config) } else { - newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{ + newConfigs[gateway][config.Table-1] = ConfigInfo{ Change: false, Remote: jd.EdgeInfo{ Mac: config.EdgeMac, City: config.Cityhash, 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)) - newConfigs[gateway] = append(newConfigs[gateway], ConfigInfo{ + newConfigs[gateway][config.Table-1] = ConfigInfo{ Change: true, Remote: jd.EdgeInfo{ Mac: edge.Macaddr, @@ -139,7 +142,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error { IsChange: u.P(0), IsOnline: u.P(1), }, - }) + } changesCreate = append(changesCreate, model.Change{ Time: now, CityId: city.Id, @@ -199,6 +202,12 @@ func update(tx *gorm.DB, arg UpdateArgs) error { 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 if !arg.Mock { err := jd.GatewayConfigSet(gateway.ConfigVersion, gateway.Macaddr, edges) diff --git a/model/config.go b/model/config.go index 1db16d3..dae8717 100644 --- a/model/config.go +++ b/model/config.go @@ -3,7 +3,7 @@ package model type Config struct { Id int `gorm:"column:id;primaryKey"` GatewayMac string `gorm:"column:macaddr"` - Table string `gorm:"column:table"` + Table int `gorm:"column:table"` EdgeMac string `gorm:"column:edge"` Network string `gorm:"column:network"` Cityhash string `gorm:"column:cityhash"` @@ -55,4 +55,4 @@ type ConfigCreate struct { func (ConfigCreate) TableName() string { return "gateway" -} \ No newline at end of file +} diff --git a/model/submit.go b/model/submit.go new file mode 100644 index 0000000..d59bee1 --- /dev/null +++ b/model/submit.go @@ -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" +}