1
1
"""Classes for specific report types."""
2
2
from datetime import datetime , timedelta
3
3
4
- from flask import jsonify
4
+ from flask import jsonify , current_app
5
5
from pytz import timezone
6
6
from sqlalchemy import and_ , func , select
7
7
from sqlalchemy .dialects .postgresql import INTERVAL
28
28
29
29
from .cdog_client import CDOGClient
30
30
from .report_factory import ReportFactory
31
-
31
+ from api . utils . util import process_data
32
32
33
33
# pylint:disable=not-callable
34
34
@@ -48,6 +48,7 @@ def __init__(self, filters, color_intensity):
48
48
"ea_act" ,
49
49
"substitution_act" ,
50
50
"project_description" ,
51
+ "report_description" ,
51
52
"anticipated_decision_date" ,
52
53
"additional_info" ,
53
54
"ministry_name" ,
@@ -66,6 +67,7 @@ def __init__(self, filters, color_intensity):
66
67
67
68
def _fetch_data (self , report_date ):
68
69
"""Fetches the relevant data for EA Anticipated Schedule Report"""
70
+ current_app .logger .info (f"Fetching data for { self .report_title } report" )
69
71
start_date = report_date + timedelta (days = - 7 )
70
72
report_date = report_date .astimezone (timezone ('US/Pacific' ))
71
73
eac_decision_by = aliased (Staff )
@@ -140,6 +142,7 @@ def _fetch_data(self, report_date):
140
142
EAAct .name .label ("ea_act" ),
141
143
SubstitutionAct .name .label ("substitution_act" ),
142
144
Project .description .label ("project_description" ),
145
+ Work .report_description .label ("report_description" ),
143
146
(
144
147
Event .anticipated_date + func .cast (func .concat (Event .number_of_days , " DAYS" ), INTERVAL )
145
148
).label ("anticipated_decision_date" ),
@@ -166,23 +169,30 @@ def _fetch_data(self, report_date):
166
169
167
170
def generate_report (self , report_date , return_type ):
168
171
"""Generates a report and returns it"""
172
+ current_app .logger .info (f"Generating { self .report_title } report for { report_date } " )
169
173
data = self ._fetch_data (report_date )
170
174
data = self ._format_data (data )
171
175
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
+
176
180
api_payload = {
177
181
"report_data" : data ,
178
182
"report_title" : self .report_title ,
179
183
"report_date" : report_date ,
180
184
}
181
185
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 } " )
186
196
return report , f"{ self .report_title } _{ report_date :%Y_%m_%d} .pdf"
187
197
188
198
def _get_next_pcp_query (self , start_date ):
0 commit comments