diff --git a/common/utils/telemetry_client.py b/common/utils/telemetry_client.py index df16b54..bbb7c12 100644 --- a/common/utils/telemetry_client.py +++ b/common/utils/telemetry_client.py @@ -203,21 +203,24 @@ def get_widget_data( logger.error(f"Error fetching widget data for entity {entity_id}: {str(e)}") raise - def get_entity_properties( + def get_entity_properties_batch( self, - device_id: str, + device_ids: list[str], organization_slug: str, - ) -> list[dict]: + ) -> dict[str, list[dict]]: """ - Fetch device entities with latest values from telemetry service in one call + Fetch device entities with latest values for multiple devices in one call """ - endpoint = f"{self.base_url}/api/telemetry/v1/data/entity-properties" - params = {"device_id": device_id} + ids = sorted({str(device_id) for device_id in device_ids if device_id}) + if not ids: + return {} + + endpoint = f"{self.base_url}/api/telemetry/v1/data/entity-properties/batch" try: - response = requests.get( + response = requests.post( endpoint, - params=params, + json={"device_ids": ids}, timeout=self.timeout, headers={ "Content-Type": "application/json", @@ -227,24 +230,20 @@ def get_entity_properties( ) logger.info( - f"Device entity properties response status: {response.status_code}" + f"Device entity properties batch response status: {response.status_code}" ) if response.status_code == 404: - logger.warning( - f"404 - No device entity properties found for device {device_id}" - ) - return [] + logger.warning("404 - No device entity properties found for batch") + return {} response.raise_for_status() data = response.json() - return data if isinstance(data, list) else [] + return data if isinstance(data, dict) else {} except RequestException as e: - logger.error( - f"Error fetching device entity properties for device {device_id}: {str(e)}" - ) + logger.error(f"Error fetching device entity properties batch: {str(e)}") raise def check_health(self) -> bool: diff --git a/common/utils/tranformer_client.py b/common/utils/tranformer_client.py index 698be6c..212fb3d 100644 --- a/common/utils/tranformer_client.py +++ b/common/utils/tranformer_client.py @@ -17,19 +17,36 @@ def __init__(self, base_url: str | None = None): ) self.timeout = 30 - def get_device_model(self, device_model_id: str): - """Fetch a specific device model by ID from the Transformer Service API""" - # Try to get from cache first - cache_key = f"transformer:device_model:{device_model_id}" # noqa - cached = cache.get(cache_key) - if cached is not None: - return cached - - # Fetch from direct endpoint - endpoint = f"{self.base_url}/api/device-models/{device_model_id}" + def get_device_profiles_by_model_ids(self, device_model_ids): + """Fetch multiple device models by ID from the Transformer Service API""" + ids = sorted( + { + str(device_model_id) + for device_model_id in device_model_ids + if device_model_id + } + ) + if not ids: + return {} + + cached_profiles = {} + missing_ids = [] + for device_model_id in ids: + cache_key = f"transformer:device_model:{device_model_id}" # noqa + cached = cache.get(cache_key) + if cached is not None: + cached_profiles[device_model_id] = cached + else: + missing_ids.append(device_model_id) + + if not missing_ids: + return cached_profiles + + endpoint = f"{self.base_url}/api/device-models/batch" try: - response = requests.get( + response = requests.post( endpoint, + json={"device_model_ids": missing_ids}, timeout=self.timeout, headers={ "Content-Type": "application/json", @@ -37,9 +54,18 @@ def get_device_model(self, device_model_id: str): }, ) response.raise_for_status() - model = response.json() - cache.set(cache_key, model, timeout=300) - return model + payload = response.json() except RequestException as e: - logger.error(f"Error fetching device model {device_model_id}: {str(e)}") - return None + logger.error(f"Error fetching device models batch: {str(e)}") + return cached_profiles + + profiles = payload.get("results", payload if isinstance(payload, list) else []) + for profile in profiles: + device_model_id = str(profile.get("id") or "").strip() + if device_model_id: + cached_profiles[device_model_id] = profile + cache.set( + f"transformer:device_model:{device_model_id}", profile, timeout=300 + ) + + return cached_profiles