From c7489eeba0ba2d1cbc4467a83136a1f1dce5bf5c Mon Sep 17 00:00:00 2001
From: Daniel Nemergut <dnemergu@nrao.edu>
Date: Fri, 24 May 2024 12:04:36 -0400
Subject: [PATCH] Removed cal join in the first statement and moved all the
 logic to the second query for the cal telescope

---
 .../aat_wrest/aat_wrest/metadata_wrester.py   | 37 +++++++++----------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py b/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py
index 82bf47c2b..69e60fc3b 100644
--- a/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py
+++ b/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py
@@ -361,12 +361,10 @@ class WrestWorkflowMetadata:
         SELECT sp.science_product_type,
         COALESCE(eb.telescope,im.telescope) as telescope,
         spp.project_code,
-        c.execution_block_id
         FROM science_products sp
         JOIN science_products_projects spp ON sp.science_product_locator = spp.science_product_locator
         LEFT JOIN execution_blocks eb ON sp.science_product_locator = eb.science_product_locator
         LEFT JOIN images im ON sp.science_product_locator = im.science_product_locator
-        LEFT JOIN calibrations c ON sp.science_product_locator = c.science_product_locator
         WHERE sp.science_product_locator = %(spl)s;
         """
         make_json = {}
@@ -392,27 +390,28 @@ class WrestWorkflowMetadata:
             if data:
                 product_type = str(data[0][0]).lower().replace(" ", "_")
 
-                # Get the telescope from a calibration's exec block (if applicable)
                 if product_type != 'calibration':
                     telescope = data[0][1]
                 else:
-                    if not data[0][3]:
-                        # ALMA doesn't have a calibrations.execution_block_id, could query with the alm_ous_id, but meh
-                        telescope = 'ALMA'
+                    # Need to query on the eb / alma_ous_id to get a telescope for calibrations
+                    scope_query = """
+                    SELECT telescope
+                    FROM execution_blocks
+                    JOIN calibrations c_eb ON execution_blocks.execution_block_id = c_eb.execution_block_id
+                    WHERE c_eb.science_product_locator = %(spl)s
+                    UNION
+                    SELECT telescope
+                    FROM execution_blocks
+                    JOIN calibrations c_ous ON execution_blocks.alma_ous_id = c_ous.alma_ous_id
+                    WHERE c_ous.science_product_locator = %(spl)s
+                    """
+                    cursor.execute(scope_query, {"spl": spl})
+                    scope_data = cursor.fetchall()
+                    if scope_data:
+                        telescope = data[0][0]
                     else:
-                        # Can get it from the EB
-                        scope_query = """
-                        SELECT telescope
-                        FROM execution_blocks
-                        WHERE execution_block_id = %(eb_id)s;
-                        """
-                        cursor.execute(scope_query, {"pg_id": data[0][3]})
-                        scope_data = cursor.fetchall()
-                        if scope_data:
-                            telescope = data[0][0]
-                        else:
-                            self.logger.error(f"ERROR: Failed to determine telescope from calibration's EB, got {data}")
-                            return make_json
+                        self.logger.error(f"ERROR: Failed to determine telescope from calibration's EB, got {data}")
+                        return make_json
 
                 make_json = {
                     "product_type": product_type,
-- 
GitLab