Skip to content

Commit 1d06a83

Browse files
committed
175: Add Work Description to Anticipated EA Schedule.
Refactors some code. Exception error handling, updated template.
1 parent 96f1fdf commit 1d06a83

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

epictrack-api/src/api/reports/anticipated_schedule_report.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Classes for specific report types."""
22
from datetime import datetime, timedelta
33

4-
from flask import jsonify
4+
from flask import jsonify, current_app
55
from pytz import timezone
66
from sqlalchemy import and_, func, select
77
from sqlalchemy.dialects.postgresql import INTERVAL
@@ -28,7 +28,7 @@
2828

2929
from .cdog_client import CDOGClient
3030
from .report_factory import ReportFactory
31-
31+
from api.utils.util import process_data
3232

3333
# pylint:disable=not-callable
3434

@@ -48,6 +48,7 @@ def __init__(self, filters, color_intensity):
4848
"ea_act",
4949
"substitution_act",
5050
"project_description",
51+
"report_description",
5152
"anticipated_decision_date",
5253
"additional_info",
5354
"ministry_name",
@@ -66,6 +67,7 @@ def __init__(self, filters, color_intensity):
6667

6768
def _fetch_data(self, report_date):
6869
"""Fetches the relevant data for EA Anticipated Schedule Report"""
70+
current_app.logger.info(f"Fetching data for {self.report_title} report")
6971
start_date = report_date + timedelta(days=-7)
7072
report_date = report_date.astimezone(timezone('US/Pacific'))
7173
eac_decision_by = aliased(Staff)
@@ -140,6 +142,7 @@ def _fetch_data(self, report_date):
140142
EAAct.name.label("ea_act"),
141143
SubstitutionAct.name.label("substitution_act"),
142144
Project.description.label("project_description"),
145+
Work.report_description.label("report_description"),
143146
(
144147
Event.anticipated_date + func.cast(func.concat(Event.number_of_days, " DAYS"), INTERVAL)
145148
).label("anticipated_decision_date"),
@@ -166,23 +169,30 @@ def _fetch_data(self, report_date):
166169

167170
def generate_report(self, report_date, return_type):
168171
"""Generates a report and returns it"""
172+
current_app.logger.info(f"Generating {self.report_title} report for {report_date}")
169173
data = self._fetch_data(report_date)
170174
data = self._format_data(data)
171175
data = self._update_staleness(data, report_date)
172-
if return_type == "json" and data:
173-
return {"data": data}, None
174-
if not data:
175-
return {}, None
176+
177+
if return_type == "json" or not data:
178+
return process_data(data, return_type)
179+
176180
api_payload = {
177181
"report_data": data,
178182
"report_title": self.report_title,
179183
"report_date": report_date,
180184
}
181185
template = self.generate_template()
182-
report_client = CDOGClient()
183-
report = report_client.generate_document(
184-
self.report_title, jsonify(api_payload).json, template
185-
)
186+
# Calls out to the common services document generation service. Make sure your envs are set properly.
187+
try:
188+
report_client = CDOGClient()
189+
report = report_client.generate_document(self.report_title, jsonify(api_payload).json, template)
190+
except EnvironmentError as e:
191+
# Fall through to return empty response if CDOGClient fails, but log the error
192+
current_app.logger.error(f"Error initializing CDOGClient: {e}.")
193+
return {}, None
194+
195+
current_app.logger.info(f"Generated {self.report_title} report for {report_date}")
186196
return report, f"{self.report_title}_{report_date:%Y_%m_%d}.pdf"
187197

188198
def _get_next_pcp_query(self, start_date):

epictrack-api/src/api/reports/cdog_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class CDOGClient: # pylint: disable=too-few-public-methods
2626

2727
def __init__(self) -> None:
2828
"""Constructor"""
29+
if not all([
30+
os.environ.get('CDOGS_API_ENDPOINT'),
31+
os.environ.get('CDOGS_TOKEN_END_POINT'),
32+
os.environ.get('CDOGS_CLIENT_ID'),
33+
os.environ.get('CDOGS_CLIENT_SECRET')
34+
]):
35+
raise EnvironmentError("CDOGS environment variables are not set properly.")
2936
self.api_url = os.environ.get('CDOGS_API_ENDPOINT')
3037

3138
def _authorize(self):
Binary file not shown.

epictrack-api/src/api/reports/thirty_sixty_ninety_report.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from api.schemas import response as res
3030
from api.utils.constants import CANADA_TIMEZONE
3131
from api.utils.enums import StalenessEnum
32+
from api.utils.util import process_data
3233

3334
from .report_factory import ReportFactory
3435

@@ -245,10 +246,8 @@ def generate_report(
245246
data = self._fetch_data(report_date)
246247
data = self._format_data(data)
247248
data = self._update_staleness(data, report_date)
248-
if return_type == "json" and data:
249-
return {"data": data}, None
250-
if not data:
251-
return {}, None
249+
if return_type == "json" or not data:
250+
return process_data(data, return_type)
252251
pdf_stream = BytesIO()
253252
stylesheet = getSampleStyleSheet()
254253
doc = BaseDocTemplate(pdf_stream, pagesize=A4)

epictrack-api/src/api/utils/util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ def generate_title(project_name, work_type_name, simple_title=''):
5353
if simple_title:
5454
parts.append(simple_title)
5555
return ' - '.join(parts)
56+
57+
58+
def process_data(data, return_type):
59+
"""Process data based on the return type."""
60+
if return_type == "json" and data:
61+
return {"data": data}, None
62+
if not data:
63+
return {}, None

0 commit comments

Comments
 (0)