|
27 | 27 | from api.models.work_phase import WorkPhase
|
28 | 28 | from api.utils.constants import CANADA_TIMEZONE
|
29 | 29 | from api.utils.enums import StalenessEnum
|
30 |
| - |
| 30 | +from collections import namedtuple |
31 | 31 | from .cdog_client import CDOGClient
|
32 | 32 | from .report_factory import ReportFactory
|
33 | 33 | from api.utils.util import process_data
|
| 34 | +import json |
34 | 35 |
|
35 | 36 | # pylint:disable=not-callable
|
36 | 37 |
|
@@ -233,7 +234,30 @@ def _fetch_data(self, report_date):
|
233 | 234 | next_pecp_query.c.notes.label("next_pecp_short_description"),
|
234 | 235 | )
|
235 | 236 | )
|
236 |
| - return results_qry.all() |
| 237 | + results = results_qry.all() |
| 238 | + current_app.logger.debug(f"Fetched data: {results}") |
| 239 | + results_dict = [result._asdict() for result in results] |
| 240 | + # Processes the 'next_pecp_short_description' field in the results: |
| 241 | + # - Logs the short description if it exists. |
| 242 | + # - Attempts to parse the short description as JSON. |
| 243 | + # - If successful, extracts and concatenates text from JSON blocks. |
| 244 | + # - Logs a warning if JSON parsing fails. |
| 245 | + for result in results_dict: |
| 246 | + if 'next_pecp_short_description' in result and result['next_pecp_short_description'] is not None: |
| 247 | + current_app.logger.debug(f"Next PECP Short Description: {result['next_pecp_short_description']}") |
| 248 | + try: |
| 249 | + short_description_json = json.loads(result['next_pecp_short_description']) |
| 250 | + result['next_pecp_short_description'] = '' |
| 251 | + if 'blocks' in short_description_json: |
| 252 | + for block in short_description_json['blocks']: |
| 253 | + current_app.logger.debug(f"Block: {block}") |
| 254 | + if 'text' in block: |
| 255 | + result['next_pecp_short_description'] += block['text'] + '\n' |
| 256 | + except json.JSONDecodeError: |
| 257 | + current_app.logger.warning("Failed to decode JSON from next_pecp_short_description") |
| 258 | + data_result = namedtuple('data_result', results_dict[0].keys()) |
| 259 | + results = [data_result(**result) for result in results_dict] |
| 260 | + return results |
237 | 261 |
|
238 | 262 | def generate_report(self, report_date, return_type):
|
239 | 263 | """Generates a report and returns it"""
|
|
0 commit comments