diff --git a/cdci_data_analysis/analysis/ivoa_helper.py b/cdci_data_analysis/analysis/ivoa_helper.py index e73da1b3..b865fcf2 100644 --- a/cdci_data_analysis/analysis/ivoa_helper.py +++ b/cdci_data_analysis/analysis/ivoa_helper.py @@ -1,4 +1,7 @@ -import antlr4 +import json +import os.path + +from black.lines import append_leaves from queryparser.adql import ADQLQueryTranslator from queryparser.mysql import MySQLQueryProcessor from queryparser.exceptions import QuerySyntaxError @@ -48,11 +51,13 @@ def run_ivoa_query(query, **kwargs): vo_mysql_pg_user = kwargs.get('vo_mysql_pg_user', None) vo_mysql_pg_password = kwargs.get('vo_mysql_pg_password', None) vo_mysql_pg_db = kwargs.get('vo_mysql_pg_db', None) + product_gallery_url = kwargs.get('product_gallery_url', None) result_list = run_ivoa_query_from_product_gallery(parsed_query_obj, vo_mysql_pg_host=vo_mysql_pg_host, vo_mysql_pg_user=vo_mysql_pg_user, vo_mysql_pg_password=vo_mysql_pg_password, - vo_mysql_pg_db=vo_mysql_pg_db) + vo_mysql_pg_db=vo_mysql_pg_db, + product_gallery_url=product_gallery_url) return result_list @@ -60,7 +65,8 @@ def run_ivoa_query_from_product_gallery(parsed_query_obj, vo_mysql_pg_host, vo_mysql_pg_user, vo_mysql_pg_password, - vo_mysql_pg_db + vo_mysql_pg_db, + product_gallery_url=None ): result_list = [] @@ -72,11 +78,23 @@ def run_ivoa_query_from_product_gallery(parsed_query_obj, database=vo_mysql_pg_db ) as connection: create_db_query = parsed_query_obj.get('mysql_query') - with connection.cursor() as cursor: + with connection.cursor(dictionary=True) as cursor: cursor.execute(create_db_query) - for db in cursor: - logger.info(db) - result_list.append(db) + for row in cursor: + if product_gallery_url is not None: + path = row.get('path', None) + if path is not None: + if path.startswith('/'): + path = path[1:] + row['path'] = os.path.join(product_gallery_url, path) + path_alias = row.get('path_alias', None) + if path_alias is not None: + if path_alias.startswith('/'): + path_alias = path_alias[1:] + row['path_alias'] = os.path.join(product_gallery_url, path_alias) + result_list.append(row) + # result_obj = cursor.fetchall() + except Error as e: sentry.capture_message(f"Error when connecting to MySQL: {str(e)}") @@ -86,4 +104,9 @@ def run_ivoa_query_from_product_gallery(parsed_query_obj, sentry.capture_message(f"Error when performing the mysql query to the product_gallery DB: {str(e)}") logger.error(f"Error when performing the mysql query to the product_gallery DB: {str(e)}") + finally: + if connection is not None and connection.is_connected(): + connection.close() + logger.info('MySQL connection closed') + return result_list diff --git a/cdci_data_analysis/flask_app/app.py b/cdci_data_analysis/flask_app/app.py index 37b9550f..b89ccd42 100644 --- a/cdci_data_analysis/flask_app/app.py +++ b/cdci_data_analysis/flask_app/app.py @@ -435,16 +435,16 @@ def run_adql_query(): vo_mysql_pg_user = app_config.vo_mysql_pg_user vo_mysql_pg_password = app_config.vo_mysql_pg_password vo_mysql_pg_db = app_config.vo_mysql_pg_db + product_gallery_url = app_config.product_gallery_url result_query = ivoa_helper.run_ivoa_query(adql_query, vo_mysql_pg_host=vo_mysql_pg_host, vo_mysql_pg_user=vo_mysql_pg_user, vo_mysql_pg_password=vo_mysql_pg_password, - vo_mysql_pg_db=vo_mysql_pg_db) - - output_request = json.dumps(result_query) + vo_mysql_pg_db=vo_mysql_pg_db, + product_gallery_url=product_gallery_url) - return output_request + return jsonify(result_query) @app.route('/run_analysis', methods=['POST', 'GET'])