From c741ac8553c792ae6a28c6ca8012eda49a29333c Mon Sep 17 00:00:00 2001 From: openhands Date: Fri, 7 Nov 2025 10:59:19 +0000 Subject: [PATCH] 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. --- src/dashboard/api.py | 14 +++++++------- src/discovery/protocol_discovery_persistent.py | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/dashboard/api.py b/src/dashboard/api.py index 9e7f6d5..81986bb 100644 --- a/src/dashboard/api.py +++ b/src/dashboard/api.py @@ -1105,23 +1105,23 @@ async def apply_discovery_results(scan_id: str, station_id: str, pump_id: str, d if not result: 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") created_mappings = [] errors = [] - for endpoint in result.discovered_endpoints: + for endpoint in result.get("discovered_endpoints", []): try: # 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( id=mapping_id, station_id=station_id, pump_id=pump_id, - protocol_type=endpoint.protocol_type, - protocol_address=endpoint.address, + protocol_type=endpoint.get("protocol_type"), + protocol_address=endpoint.get("address"), data_type=data_type, 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: created_mappings.append(mapping_id) 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: - 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 { "success": True, diff --git a/src/discovery/protocol_discovery_persistent.py b/src/discovery/protocol_discovery_persistent.py index f6ada1e..165c41b 100644 --- a/src/discovery/protocol_discovery_persistent.py +++ b/src/discovery/protocol_discovery_persistent.py @@ -97,9 +97,9 @@ class PersistentProtocolDiscoveryService: for row in result ] - # Get total discovered endpoints + # Get total discovered endpoints (count unique endpoints across all scans) query = text(""" - SELECT COUNT(*) + SELECT COUNT(DISTINCT endpoint->>'device_id') FROM discovery_results dr, jsonb_array_elements(dr.discovered_endpoints) AS endpoint WHERE dr.status = 'completed'