From b1b330fdb9b7d0fd779a659abbe1650c9a411cab Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Wed, 27 Oct 2021 16:10:32 +0200
Subject: [PATCH] Much more chatty about constant loading

---
 src/calng/AgipdCorrection.py |  1 +
 src/calng/DsscCorrection.py  |  1 +
 src/calng/calcat_utils.py    | 66 +++++++++++++++++++++++-------------
 3 files changed, 44 insertions(+), 24 deletions(-)

diff --git a/src/calng/AgipdCorrection.py b/src/calng/AgipdCorrection.py
index df102741..b6c4cb4a 100644
--- a/src/calng/AgipdCorrection.py
+++ b/src/calng/AgipdCorrection.py
@@ -394,6 +394,7 @@ class AgipdCorrection(BaseCorrection):
             self.set(f"corrections.{field_name}.available", True)
 
         self._update_correction_flags()
+        self.log_status_info(f"Done loading {constant.name} to GPU")
 
     def _update_pulse_filter(self, filter_string):
         """Called whenever the pulse filter changes, typically followed by
diff --git a/src/calng/DsscCorrection.py b/src/calng/DsscCorrection.py
index d70f172d..ae9f4f33 100644
--- a/src/calng/DsscCorrection.py
+++ b/src/calng/DsscCorrection.py
@@ -177,3 +177,4 @@ class DsscCorrection(BaseCorrection):
             self.set("corrections.offset.available", True)
 
         self._update_correction_flags()
+        self.log_status_info(f"Done loading {constant.name} to GPU")
diff --git a/src/calng/calcat_utils.py b/src/calng/calcat_utils.py
index e6c6a8f1..ed6ae21f 100644
--- a/src/calng/calcat_utils.py
+++ b/src/calng/calcat_utils.py
@@ -49,20 +49,6 @@ class CalibrationClientConfigError(Exception):
     pass
 
 
-def _check_resp(resp, exception=Exception):
-    # TODO: probably verify using "info" that exception is the right one
-    if not resp["success"]:
-        # TODO: probably more types of app_info errors?
-        if resp["app_info"]:
-            if "not found" in resp["info"]:
-                # this was likely the exception exception
-                raise exception(resp["info"])
-            else:
-                # but could also be authorization or similar issue
-                raise CalibrationClientConfigError(resp["app_info"])
-        raise exception(resp["info"])
-
-
 def _add_status_schema_from_enum(schema, prefix, enum_class):
     for constant in enum_class:
         constant_node = f"{prefix}.{constant.name}"
@@ -323,16 +309,20 @@ class BaseCalcatFriend:
 
     @functools.cached_property
     def detector_id(self):
-        resp = Detector.get_by_identifier(self.client, self._get_param("detectorName"))
-        _check_resp(resp, DetectorNotFound)
+        detector_name = self._get_param("detectorName")
+        resp = Detector.get_by_identifier(self.client, detector_name)
+        self._check_resp(resp, DetectorNotFound, f"Detector {detector_name} not found")
         res = resp["data"]["id"]
         self._set_param("detectorId", str(res))
         return res
 
     @functools.cached_property
     def detector_type_id(self):
-        resp = DetectorType.get_by_name(self.client, self._get_param("detectorType"))
-        _check_resp(resp, DetectorNotFound)
+        detector_type = self._get_param("detectorType")
+        resp = DetectorType.get_by_name(self.client, detector_type)
+        self._check_resp(
+            resp, DetectorNotFound, f"Detector type {detector_type} not found"
+        )
         res = resp["data"]["id"]
         self._set_param("detectorTypeId", str(res))
         return res
@@ -342,7 +332,7 @@ class BaseCalcatFriend:
         resp = PhysicalDetectorUnit.get_all_by_detector(
             self.client, self.detector_id, self._get_param("deviceMappingSnapshotAt")
         )
-        _check_resp(resp)
+        self._check_resp(resp, warning="Failed to retrieve module mapping")
         for irrelevant_key in ("detector", "detector_type", "flg_available"):
             for pdu in resp["data"]:
                 del pdu[irrelevant_key]
@@ -365,7 +355,9 @@ class BaseCalcatFriend:
     @utils.threadsafe_cache
     def calibration_id(self, calibration_name: str):
         resp = Calibration.get_by_name(self.client, calibration_name)
-        _check_resp(resp, CalibrationNotFound)
+        self._check_resp(
+            resp, CalibrationNotFound, f"Calibration type {calibration_name} not found!"
+        )
         return resp["data"]["id"]
 
     @utils.threadsafe_cache
@@ -377,7 +369,11 @@ class BaseCalcatFriend:
         resp = self.client.search_possible_conditions_from_dict(
             "", condition_with_detector.encode()
         )
-        _check_resp(resp, ConditionNotFound)
+        self._check_resp(
+            resp,
+            ConditionNotFound,
+            f"Failed to find condition {condition} for pdu {pdu}",
+        )
         return resp["data"][0]["id"]
 
     @utils.threadsafe_cache
@@ -388,7 +384,7 @@ class BaseCalcatFriend:
             detector_type_id=self.detector_type_id,
             condition_id=condition_id,
         )
-        _check_resp(resp)
+        self._check_resp(resp, warning="Failed to retrieve constant ID")
         return resp["data"]["id"]
 
     def get_constant_version(self, constant):
@@ -397,6 +393,9 @@ class BaseCalcatFriend:
         self.device.log_status_info(f"Attempting to find {constant} for {karabo_da}")
 
         if karabo_da not in self._karabo_da_to_float_uuid:
+            self.device.log_status_warn(
+                f"Module {karabo_da} not found in mapping, check configuration!"
+            )
             raise ModuleNotFound(f"Module map did not include {karabo_da}")
         self._set_param("moduleId", str(self._karabo_da_to_id[karabo_da]))
 
@@ -424,7 +423,7 @@ class BaseCalcatFriend:
             event_at=self._get_param("constantVersionEventAt"),
             snapshot_at=None,
         )
-        _check_resp(resp)
+        self._check_resp(resp, warning="Failed to find calibration constant version")
         # TODO: replace with start date and end date
         timestamp = resp["data"]["begin_validity_at"]
         self._set_status(constant, "validFrom", timestamp)
@@ -438,6 +437,7 @@ class BaseCalcatFriend:
             constant_data = np.array(fd[resp["data"]["data_set_name"]]["data"])
         self.cached_constants[constant] = constant_data
         self._set_status(constant, "found", True)
+        self.device.log_status_info(f"Done finding {constant} for {karabo_da}")
 
         return constant_data
 
@@ -449,7 +449,7 @@ class BaseCalcatFriend:
         )
 
         resp = CalibrationConstantVersion.get_by_id(self.client, constant_version_id)
-        _check_resp(resp)
+        self._check_resp(resp, warning="Failed to find calibration constant version")
         file_path = (
             self.caldb_store / resp["data"]["path_to_file"] / resp["data"]["file_name"]
         )
@@ -494,6 +494,24 @@ class BaseCalcatFriend:
             self._set_status(constant, "validFrom", "")
             self._set_status(constant, "found", False)
 
+    def _check_resp(self, resp, exception=Exception, warning=None):
+        # TODO: probably verify using "info" that exception is the right one
+        to_raise = None
+        if not resp["success"]:
+            # TODO: probably more types of app_info errors?
+            if resp["app_info"]:
+                if "not found" in resp["info"]:
+                    # this was likely the exception exception
+                    to_raise = exception(resp["info"])
+                else:
+                    # but could also be authorization or similar issue
+                    to_raise = CalibrationClientConfigError(resp["app_info"])
+            to_raise = exception(resp["info"])
+        if to_raise is not None:
+            if warning is not None:
+                self.device.log_status_warn(warning)
+                raise to_raise
+
 
 class AgipdConstants(enum.Enum):
     SlopesFF = enum.auto()
-- 
GitLab