Fix discovery count doubling and apply results errors

- Fix total_discovered_endpoints count to use DISTINCT device_id instead of counting all endpoints
- Fix apply_discovery_results endpoint to handle dictionary data correctly
- Replace object attribute access with dict.get() methods for scan results

Resolves issues where discovery count doubled with each scan and
apply results failed with '[object Object]' errors.
This commit is contained in:
openhands 2025-11-07 10:59:19 +00:00
parent a41d638268
commit c741ac8553
2 changed files with 9 additions and 9 deletions

View File

@ -1105,23 +1105,23 @@ async def apply_discovery_results(scan_id: str, station_id: str, pump_id: str, d
if not result: if not result:
raise HTTPException(status_code=404, detail=f"Discovery scan {scan_id} not found") raise HTTPException(status_code=404, detail=f"Discovery scan {scan_id} not found")
if result.status != DiscoveryStatus.COMPLETED: if result.get("status") != "completed":
raise HTTPException(status_code=400, detail="Cannot apply incomplete discovery scan") raise HTTPException(status_code=400, detail="Cannot apply incomplete discovery scan")
created_mappings = [] created_mappings = []
errors = [] errors = []
for endpoint in result.discovered_endpoints: for endpoint in result.get("discovered_endpoints", []):
try: try:
# Create protocol mapping from discovered endpoint # Create protocol mapping from discovered endpoint
mapping_id = f"{endpoint.device_id}_{data_type}" mapping_id = f"{endpoint.get('device_id')}_{data_type}"
protocol_mapping = ProtocolMapping( protocol_mapping = ProtocolMapping(
id=mapping_id, id=mapping_id,
station_id=station_id, station_id=station_id,
pump_id=pump_id, pump_id=pump_id,
protocol_type=endpoint.protocol_type, protocol_type=endpoint.get("protocol_type"),
protocol_address=endpoint.address, protocol_address=endpoint.get("address"),
data_type=data_type, data_type=data_type,
db_source=db_source db_source=db_source
) )
@ -1132,10 +1132,10 @@ async def apply_discovery_results(scan_id: str, station_id: str, pump_id: str, d
if success: if success:
created_mappings.append(mapping_id) created_mappings.append(mapping_id)
else: else:
errors.append(f"Failed to create mapping for {endpoint.device_name}") errors.append(f"Failed to create mapping for {endpoint.get('device_name')}")
except Exception as e: except Exception as e:
errors.append(f"Error creating mapping for {endpoint.device_name}: {str(e)}") errors.append(f"Error creating mapping for {endpoint.get('device_name')}: {str(e)}")
return { return {
"success": True, "success": True,

View File

@ -97,9 +97,9 @@ class PersistentProtocolDiscoveryService:
for row in result for row in result
] ]
# Get total discovered endpoints # Get total discovered endpoints (count unique endpoints across all scans)
query = text(""" query = text("""
SELECT COUNT(*) SELECT COUNT(DISTINCT endpoint->>'device_id')
FROM discovery_results dr, FROM discovery_results dr,
jsonb_array_elements(dr.discovered_endpoints) AS endpoint jsonb_array_elements(dr.discovered_endpoints) AS endpoint
WHERE dr.status = 'completed' WHERE dr.status = 'completed'