Skip to content

Commit

Permalink
fix: process results
Browse files Browse the repository at this point in the history
  • Loading branch information
magajh committed Jan 22, 2025
1 parent 53c01c6 commit 12ed527
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions eox_core/api/data/data_collector/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from celery import shared_task, Task
from eox_core.api.data.data_collector.utils import execute_query, post_data_to_api, serialize_data
from eox_core.api.data.data_collector.utils import execute_query, post_data_to_api, serialize_data, process_query_results
import yaml
import logging

Expand Down Expand Up @@ -59,7 +59,8 @@ def generate_report(self, destination_url, query_file_content, token_generation_
result = execute_query(query_sql)

serialized_result = serialize_data(result)
report_data[query_name] = serialized_result
processed_result = process_query_results(serialized_result)
report_data[query_name] = processed_result
except Exception as e:
logger.error(f"Failed to execute query '{query_name}': {e}")
continue
Expand Down
16 changes: 16 additions & 0 deletions eox_core/api/data/data_collector/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ def serialize_data(data):
return data


def process_query_results(raw_result):
"""
Process the raw result of a query.
Args:
raw_result: The result from the SQL query (list, scalar, or dictionary).
Returns:
The processed result, extracting scalar values from single-item lists,
or returning the original value for more complex data structures.
"""
if isinstance(raw_result, list) and len(raw_result) == 1:
return raw_result[0]
return raw_result


def post_data_to_api(api_url, report_data, token_generation_url, current_host):
"""
Sends the generated report data to the Shipyard API.
Expand Down

0 comments on commit 12ed527

Please sign in to comment.