From 7e7d706f84577a8106c190ea32a473813f6d6672 Mon Sep 17 00:00:00 2001 From: luorijun Date: Thu, 21 Aug 2025 18:34:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9B=B4=E6=96=B0=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E4=BB=A5=E6=8E=92=E6=9F=A5IP=E5=88=86=E9=85=8D?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/changes.go | 6 ++++- actions/update.go | 35 ++++++++++++++++++++--------- cmd/test.go | 8 +++---- model/change-city.go | 16 +++++++++++++ model/{change.go => change-edge.go} | 5 +++-- 5 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 model/change-city.go rename model/{change.go => change-edge.go} (77%) diff --git a/actions/changes.go b/actions/changes.go index bcdcf54..c2ff592 100644 --- a/actions/changes.go +++ b/actions/changes.go @@ -6,7 +6,7 @@ import ( "gorm.io/gorm" ) -func RecordChange(tx *gorm.DB, changes []model.Change) error { +func RecordChangeEdges(tx *gorm.DB, changes []model.ChangeEdge) error { if len(changes) == 0 { return nil } @@ -22,3 +22,7 @@ func RecordChange(tx *gorm.DB, changes []model.Change) error { } return nil } + +func RecordChangeCity(tx *gorm.DB, change model.ChangeCity) error { + return tx.Create(&change).Error +} diff --git a/actions/update.go b/actions/update.go index 7c28771..37b1b6e 100644 --- a/actions/update.go +++ b/actions/update.go @@ -68,7 +68,7 @@ func update(tx *gorm.DB, arg UpdateArgs) error { // 如果每个网关在此城市都有节点且无需改变,就不需要重新分配节点 // 相当于直接重新提交配置,此流程下配置更新是幂等的 - var gateways2Update []model.Gateway + var gateways2Change []model.Gateway for _, gateway := range gateways { oldConfig, exists := oldConfigsMap[gateway][city.Hash] if exists && oldConfig.IsChange != 1 { @@ -80,17 +80,17 @@ func update(tx *gorm.DB, arg UpdateArgs) error { }, } } else { - gateways2Update = append(gateways2Update, gateway) + gateways2Change = append(gateways2Change, gateway) } } - if len(gateways2Update) == 0 { + + count := len(gateways2Change) + if len(gateways2Change) == 0 { continue } // 否则获取足量新节点 offset := city.Offset - count := len(gateways2Update) - if count > city.EdgesCount { slog.Warn(fmt.Sprintf("城市节点数量不足,跳过本次更新,城市:%s,节点数:%d,网关数:%d", city.Name, city.EdgesCount, count)) continue @@ -113,9 +113,9 @@ func update(tx *gorm.DB, arg UpdateArgs) error { // 更新网关配置 var configs2Create []model.Config var configs2Update []model.ConfigUpdate - var changes []model.Change + var changes []model.ChangeEdge - for iGateway, gateway := range gateways2Update { + for iGateway, gateway := range gateways2Change { oldConfig, exists := oldConfigsMap[gateway][city.Hash] newConfig := edges[iGateway] @@ -129,8 +129,9 @@ func update(tx *gorm.DB, arg UpdateArgs) error { IsChange: u.P(0), }) - changes = append(changes, model.Change{ + changes = append(changes, model.ChangeEdge{ Time: now, + CityId: city.Id, Gateway: gateway.Macaddr, OldEdge: oldConfig.Macaddr, NewEdge: newConfig.Macaddr, @@ -157,8 +158,9 @@ func update(tx *gorm.DB, arg UpdateArgs) error { InnerIp: fmt.Sprintf("172.16.%d.%d", gateway.Id, iCity+2), }) - changes = append(changes, model.Change{ + changes = append(changes, model.ChangeEdge{ Time: now, + CityId: city.Id, Gateway: gateway.Macaddr, OldEdge: "", NewEdge: newConfig.Macaddr, @@ -186,9 +188,20 @@ func update(tx *gorm.DB, arg UpdateArgs) error { if err != nil { return fmt.Errorf("更新城市 %s 的偏移量失败:%w", city.Name, err) } - err = RecordChange(tx, changes) + + err = RecordChangeCity(tx, model.ChangeCity{ + Time: now, + CityId: city.Id, + Count: count, + OffsetOld: city.Offset, + OffsetNew: offset, + }) if err != nil { - return fmt.Errorf("记录变更失败:%w", err) + return fmt.Errorf("记录城市变更失败:%w", err) + } + err = RecordChangeEdges(tx, changes) + if err != nil { + return fmt.Errorf("记录节点变更失败:%w", err) } } diff --git a/cmd/test.go b/cmd/test.go index bd106b6..c88420c 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -26,9 +26,9 @@ func main() { defer clients.CloseRedis() // 测试功能 - // actions.Update(actions.UpdateArgs{ - // Mock: true, - // }) + actions.Update(actions.UpdateArgs{ + Mock: true, + }) - actions.Sync() + // actions.Sync() } diff --git a/model/change-city.go b/model/change-city.go new file mode 100644 index 0000000..e0866f6 --- /dev/null +++ b/model/change-city.go @@ -0,0 +1,16 @@ +package model + +import "time" + +type ChangeCity struct { + Id int `gorm:"column:id;primaryKey"` + Time time.Time `gorm:"column:time"` + CityId int `gorm:"column:city_id"` + Count int `gorm:"column:count"` + OffsetOld int `gorm:"column:offset_old"` + OffsetNew int `gorm:"column:offset_new"` +} + +func (ChangeCity) TableName() string { + return "change_city" +} diff --git a/model/change.go b/model/change-edge.go similarity index 77% rename from model/change.go rename to model/change-edge.go index 7b94c6b..3561241 100644 --- a/model/change.go +++ b/model/change-edge.go @@ -2,9 +2,10 @@ package model import "time" -type Change struct { +type ChangeEdge struct { Id int `gorm:"column:id;primaryKey"` Time time.Time `gorm:"column:time"` + CityId int `gorm:"column:city"` Gateway string `gorm:"column:macaddr"` OldEdge string `gorm:"column:edge_old"` NewEdge string `gorm:"column:edge_new"` @@ -12,6 +13,6 @@ type Change struct { Info string `gorm:"column:info"` } -func (Change) TableName() string { +func (ChangeEdge) TableName() string { return "change" }