Skip to content

Commit

Permalink
build product gallery path and jsonify the response
Browse files Browse the repository at this point in the history
  • Loading branch information
burnout87 committed Jul 31, 2024
1 parent 1ee31a1 commit 40dc17e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
37 changes: 30 additions & 7 deletions cdci_data_analysis/analysis/ivoa_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import antlr4
import json

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'json' is not used.
import os.path

from black.lines import append_leaves

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'append_leaves' is not used.
from queryparser.adql import ADQLQueryTranslator
from queryparser.mysql import MySQLQueryProcessor
from queryparser.exceptions import QuerySyntaxError
Expand Down Expand Up @@ -48,19 +51,22 @@ 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


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 = []

Expand All @@ -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)}")
Expand All @@ -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
8 changes: 4 additions & 4 deletions cdci_data_analysis/flask_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down

0 comments on commit 40dc17e

Please sign in to comment.