Fix: 修复存储文件名和拓扑构建逻辑

- 修正存储文件名: network-topology.db -> devices.json
- 移除handleGetDevices中的重复builder重建逻辑
- 移除冗余的调试日志输出
- 统一日志术语: database -> storage
- 确保添加设备时正确更新builder和存储
This commit is contained in:
Your Name
2026-04-26 00:57:24 +08:00
parent 30b05709eb
commit 872ebc0376
2 changed files with 8 additions and 24 deletions
+8 -15
View File
@@ -31,10 +31,10 @@ type App struct {
// NewApp 创建应用
func NewApp(cfg *config.Config) *App {
// 初始化数据库
store, err := storage.NewStorage("network-topology.db")
// 初始化存储(使用JSON文件)
store, err := storage.NewStorage("devices.json")
if err != nil {
log.Printf("Warning: failed to initialize database: %v", err)
log.Printf("Warning: failed to initialize storage: %v", err)
}
app := &App{
@@ -257,21 +257,14 @@ func (app *App) handleTopology(w http.ResponseWriter, r *http.Request) {
func (app *App) handleGetDevices(w http.ResponseWriter, r *http.Request) {
var devices []models.Device
// 优先从数据库获取
// 优先从存储获取
if app.storage != nil {
var err error
devices, err = app.storage.GetAllDevices()
if err != nil {
log.Printf("Error: failed to get devices from database: %v", err)
log.Printf("Error: failed to get devices from storage: %v", err)
// 降级到 builder获取
devices = app.builder.GetDevices()
} else {
// 重新构建设备到builder中,确保拓扑正确
app.builder = topology.NewBuilder()
for _, dev := range devices {
app.builder.AddDevice(dev)
}
log.Printf("Rebuilt topology with %d devices from database", len(devices))
}
log.Printf("Returning %d devices from storage", len(devices))
} else {
@@ -319,12 +312,12 @@ func (app *App) handleAddDevice(w http.ResponseWriter, r *http.Request) {
app.builder.AddDevice(*dev)
// 保存到数据库
// 保存到存储
if app.storage != nil {
if err := app.storage.SaveDevice(dev); err != nil {
log.Printf("Error: failed to save device %s to database: %v", req.IP, err)
log.Printf("Error: failed to save device %s to storage: %v", req.IP, err)
} else {
log.Printf("Device %s saved to database successfully", req.IP)
log.Printf("Device %s saved to storage successfully", req.IP)
}
}
-9
View File
@@ -50,15 +50,6 @@ func (b *Builder) Build() models.TopologyGraph {
}
nodeMap[device.IP] = node
graph.Nodes = append(graph.Nodes, node)
// 调试信息
fmt.Printf("Node added: %s (%s) with %d neighbors\n",
device.IP, device.Type, len(device.Neighbors))
for i, neighbor := range device.Neighbors {
fmt.Printf(" Neighbor %d: RemoteIP=%s, RemoteDevice=%s, LocalIf=%s, RemoteIf=%s\n",
i+1, neighbor.RemoteIP, neighbor.RemoteDevice,
neighbor.LocalInterface, neighbor.RemoteInterface)
}
}
// 构建边(基于邻居信息)