66import time
77from datetime import datetime
88
9+ from api .fake_data import sl_mock_data
10+
911try :
1012 from secrets_dict import SHELTERLUV_SECRET_TOKEN
1113except ImportError :
1214 # Not running locally
1315 print ("Couldn't get SHELTERLUV_SECRET_TOKEN from file, trying environment **********" )
14- from os import environ
15-
16- try :
17- SHELTERLUV_SECRET_TOKEN = environ ['SHELTERLUV_SECRET_TOKEN' ]
18- except KeyError :
19- # Nor in environment
20- # You're SOL for now
21- print ("Couldn't get secrets from file or environment" )
22-
16+ from os import getenv
2317
18+ SHELTERLUV_SECRET_TOKEN = getenv ('SHELTERLUV_SECRET_TOKEN' )
19+ if not SHELTERLUV_SECRET_TOKEN :
20+ print ("Couldn't get secrets from file or environment" ,
21+ "Defaulting to Fake Data" )
2422
2523from api import jwt_ops
2624
27-
2825@common_api .route ('/api/timeout_test/<duration>' , methods = ['GET' ])
2926def get_timeout (duration ):
3027 start = datetime .now ().strftime ("%H:%M:%S" );
@@ -178,14 +175,17 @@ def get_360(matching_id):
178175
179176 return jsonify ({'result' : result })
180177
181-
182178@common_api .route ('/api/person/<matching_id>/animals' , methods = ['GET' ])
179+ @jwt_ops .jwt_required ()
183180def get_animals (matching_id ):
184181 result = {
185182 "person_details" : {},
186183 "animal_details" : {}
187184 }
188185
186+ if not SHELTERLUV_SECRET_TOKEN :
187+ return jsonify (sl_mock_data ('animals' ))
188+
189189 with engine .connect () as connection :
190190 query = text ("select * from pdp_contacts where matching_id = :matching_id and source_type = 'shelterluvpeople' and archived_date is null" )
191191 query_result = connection .execute (query , matching_id = matching_id )
@@ -206,19 +206,15 @@ def get_animals(matching_id):
206206 return result
207207
208208
209- @common_api .route ('/api/animal/<animal_id>/events' , methods = ['GET' ])
210- def get_animal_events (animal_id ):
211- result = {}
212- animal_url = f"http://shelterluv.com/api/v1/animals/{ animal_id } /events"
213- event_details = requests .get (animal_url , headers = {"x-api-key" : SHELTERLUV_SECRET_TOKEN }).json ()
214- result [animal_id ] = event_details ["events" ]
215- return result
216-
217-
218209@common_api .route ('/api/person/<matching_id>/animal/<animal_id>/events' , methods = ['GET' ])
210+ @jwt_ops .jwt_required ()
219211def get_person_animal_events (matching_id , animal_id ):
220212 result = {}
221213 events = []
214+
215+ if not SHELTERLUV_SECRET_TOKEN :
216+ return jsonify (sl_mock_data ('events' ))
217+
222218 with engine .connect () as connection :
223219 query = text ("select * from pdp_contacts where matching_id = :matching_id and source_type = 'shelterluvpeople' and archived_date is null" )
224220 query_result = connection .execute (query , matching_id = matching_id )
@@ -237,6 +233,7 @@ def get_person_animal_events(matching_id, animal_id):
237233 return result
238234
239235@common_api .route ('/api/person/<matching_id>/support' , methods = ['GET' ])
236+ @jwt_ops .jwt_required ()
240237def get_support_oview (matching_id ):
241238 """Return these values for the specified match_id:
242239 largest gift, date for first donation, total giving, number of gifts,
@@ -385,3 +382,31 @@ def get_support_oview(matching_id):
385382 current_app .logger .debug ('No SF contact IDs found for matching_id ' + str (matching_id ))
386383 oview_fields ['number_of_gifts' ] = 0 # Marker for no data
387384 return jsonify (oview_fields )
385+
386+
387+ @common_api .route ('/api/last_analysis' , methods = ['GET' ])
388+ @jwt_ops .jwt_required ()
389+ def get_last_analysis ():
390+ """ Return the UTC string (e.g., '2021-12-11T02:29:14.830371') representing
391+ when the last analysis run succesfully completed.
392+ Returns an empty string if no results.
393+ """
394+
395+ last_run = ''
396+
397+ last_stamp = """
398+ select update_stamp
399+ from execution_status
400+ where stage = 'flow' and status = 'complete'
401+ order by update_stamp desc
402+ limit 1;
403+ """
404+
405+ with engine .connect () as connection :
406+ result = connection .execute (last_stamp )
407+ if result .rowcount :
408+ row = result .fetchone ()
409+ last_run_dt = row [0 ] # We get as a datetime object
410+ last_run = last_run_dt .isoformat ()
411+
412+ return last_run
0 commit comments