From 2c2650795cc74d7fe5b61706caa6a7e85aa2f265 Mon Sep 17 00:00:00 2001 From: Daniel Nemergut <dnemergu@nrao.edu> Date: Wed, 10 Jul 2024 00:15:34 -0400 Subject: [PATCH] Calling the CASA matrix in the metadata wresters --- .../aat_wrest/aat_wrest/metadata_wrester.py | 36 ++++++++++++++----- .../aat_wrest/aat_wrest/utilities.py | 26 ++++++++++++++ 2 files changed, 53 insertions(+), 9 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 58af3d10e..3c57f5fbb 100644 --- a/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py +++ b/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py @@ -23,7 +23,12 @@ import logging from typing import List import pendulum -from aat_wrest.utilities import PENDULUM_FORMAT, TIME_ZONE, MDDBConnector +from aat_wrest.utilities import ( + PENDULUM_FORMAT, + TIME_ZONE, + CasaMatrixClient, + MDDBConnector, +) class WrestWorkflowMetadata: @@ -32,10 +37,10 @@ class WrestWorkflowMetadata: """ def __init__( - self, - connection: MDDBConnector, - spl: List[str] = None, - sdm_id: str = None, + self, + connection: MDDBConnector, + spl: List[str] = None, + sdm_id: str = None, ): self.logger = logging.getLogger("aat_wrest") self.conn = connection @@ -67,6 +72,8 @@ class WrestWorkflowMetadata: cursor.execute(sql, {"spl": self.spl}) data = cursor.fetchall() if data: + # Append a CASA recipe from the matrix service using the calibration's CASA version + casa_recipe = CasaMatrixClient().casa_recipe("std_calibration") make_json = json.dumps( { "sdmId": data[0][0], @@ -75,6 +82,7 @@ class WrestWorkflowMetadata: "startTime": data[0][3], "observer": data[0][4], "telescope": data[0][5], + "casa_recipe": casa_recipe, "created_at": str(pendulum.now().in_timezone(TIME_ZONE).format(PENDULUM_FORMAT)), } ) @@ -101,7 +109,8 @@ class WrestWorkflowMetadata: e.starttime as startTime, (a.firstname || ' ' || a.lastname) as observer, telescope as telescope, - c.science_product_locator as cal_locator + c.science_product_locator as cal_locator, + c.casa_version as casa_version FROM execution_blocks e JOIN projects p on e.project_code = p.project_code JOIN authors a on p.project_code = a.project_code @@ -114,6 +123,8 @@ class WrestWorkflowMetadata: cursor.execute(sql, {"sdmId": self.sdm_id}) data = cursor.fetchall() if data: + # Append a CASA recipe from the matrix service using the calibration's CASA version + casa_recipe = CasaMatrixClient().casa_recipe("std_cms_imaging", data[0][7], data[0][5]) make_json = json.dumps( { "product_locator": data[0][0], @@ -123,6 +134,7 @@ class WrestWorkflowMetadata: "observer": data[0][4], "telescope": data[0][5], "cal_locator": data[0][6], + "casa_recipe": casa_recipe, "created_at": str(pendulum.now().in_timezone(TIME_ZONE).format(PENDULUM_FORMAT)), } ) @@ -155,8 +167,11 @@ class WrestWorkflowMetadata: """ cal_sql = f""" - SELECT external_name as calSdmId - from science_products WHERE science_product_locator = %(cal_spl)s + SELECT sp.external_name as calSdmId, + c.casa_version as casa_version + FROM science_products sp + LEFT JOIN calibrations c on sp.science_product_locator = c.science_product_locator + WHERE sp.science_product_locator = %(cal_spl)s """ make_json = {} @@ -167,6 +182,8 @@ class WrestWorkflowMetadata: cursor.execute(cal_sql, {"cal_spl": self.spl[1]}) cal_data = cursor.fetchall() if data and cal_data: + # Append a CASA recipe from the matrix service using the calibration's CASA version + casa_recipe = CasaMatrixClient().casa_recipe("restore_cms", cal_data[0][1], data[0][5]) make_json = json.dumps( { "sdmId": data[0][0], @@ -176,6 +193,7 @@ class WrestWorkflowMetadata: "startTime": data[0][3], "observer": data[0][4], "telescope": data[0][5], + "casa_recipe": casa_recipe, "created_at": str(pendulum.now().in_timezone(TIME_ZONE).format(PENDULUM_FORMAT)), } ) @@ -390,7 +408,7 @@ class WrestWorkflowMetadata: if data: product_type = str(data[0][0]).lower().replace(" ", "_") - if product_type != 'calibration': + if product_type != "calibration": telescope = data[0][1] else: # Need to query on the eb / alma_ous_id to get a telescope for calibrations diff --git a/apps/cli/utilities/aat_wrest/aat_wrest/utilities.py b/apps/cli/utilities/aat_wrest/aat_wrest/utilities.py index 0e0e7d599..c73a7f9d9 100644 --- a/apps/cli/utilities/aat_wrest/aat_wrest/utilities.py +++ b/apps/cli/utilities/aat_wrest/aat_wrest/utilities.py @@ -17,10 +17,12 @@ # along with Workspaces. If not, see <https://www.gnu.org/licenses/>. """ Conveniences for wresting detailed info from the archive MDDB """ +import json import logging import sys import psycopg2 as pg +import requests from pendulum import Duration from pycapo import CapoConfig from sqlalchemy.engine import Connection @@ -76,6 +78,30 @@ class MDDBConnector: self.connection.close() +class CasaMatrixClient: + """ + Methods for interacting with the CASA version matrix. + """ + + @staticmethod + def casa_recipe(capability: str, version: str | None = None, telescope: str | None = None) -> str: + recipe = "" + matrix_url = ( + CapoConfig().get("edu.nrao.workspaces.CapabilitySettings.externalServiceUrl") + "/casa_matrix/recipe" + ) + + params = {"capability": capability, "version": version, "telescope": telescope} + + response = requests.get(matrix_url, json=params) + + if response.ok: + recipe = json.loads(response.text) + else: + logger.critical(f"Unable to fetch a CASA recipe from the metrix service with params: {params}") + + return recipe + + def format_interval(interval: Duration) -> str: """ Format this time interval in a human-readable way -- GitLab