Debug: 添加详细日志以诊断设备列表和数据显示问题
- 添加设备时记录接口和邻居数量 - 获取设备列表时记录返回数量 - 数据库保存成功/失败都有明确日志 - 创建API测试脚本便于调试
这个提交包含在:
+13
-3
@@ -262,12 +262,14 @@ func (app *App) handleGetDevices(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
devices, err = app.storage.GetAllDevices()
|
||||
if err != nil {
|
||||
log.Printf("Warning: failed to get devices from database: %v", err)
|
||||
// 降级到从builder获取
|
||||
log.Printf("Error: failed to get devices from database: %v", err)
|
||||
// 降级到 builder获取
|
||||
devices = app.builder.GetDevices()
|
||||
}
|
||||
log.Printf("Returning %d devices from storage", len(devices))
|
||||
} else {
|
||||
devices = app.builder.GetDevices()
|
||||
log.Printf("Returning %d devices from builder", len(devices))
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -294,20 +296,28 @@ func (app *App) handleAddDevice(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
deviceType := models.DeviceType(req.Type)
|
||||
log.Printf("Adding device: %s (type: %s)", req.IP, req.Type)
|
||||
|
||||
dev, err := device.DiscoverDevice(req.IP, deviceType, req.Username, req.Password)
|
||||
if err != nil {
|
||||
log.Printf("Failed to discover device %s: %v", req.IP, err)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Device discovered: %s, interfaces: %d, neighbors: %d",
|
||||
dev.IP, len(dev.Interfaces), len(dev.Neighbors))
|
||||
|
||||
app.builder.AddDevice(*dev)
|
||||
|
||||
// 保存到数据库
|
||||
if app.storage != nil {
|
||||
if err := app.storage.SaveDevice(dev); err != nil {
|
||||
log.Printf("Warning: failed to save device to database: %v", err)
|
||||
log.Printf("Error: failed to save device %s to database: %v", req.IP, err)
|
||||
} else {
|
||||
log.Printf("Device %s saved to database successfully", req.IP)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
@echo off
|
||||
chcp 65001 >nul 2>&1
|
||||
echo ========================================
|
||||
echo Testing API Endpoints
|
||||
echo ========================================
|
||||
echo.
|
||||
|
||||
echo [1] Testing /api/devices endpoint...
|
||||
curl -s http://localhost:8080/api/devices | python -m json.tool
|
||||
echo.
|
||||
|
||||
echo [2] Testing /api/topology endpoint...
|
||||
curl -s http://localhost:8080/api/topology | python -m json.tool
|
||||
echo.
|
||||
|
||||
pause
|
||||
在新工单中引用
屏蔽一个用户