11import requests
22import time
3+ import logging
34from bs4 import BeautifulSoup
45from collections import namedtuple
56from datetime import datetime
@@ -71,7 +72,7 @@ def fetch_capacities():
7172
7273 # Map API name to database name
7374 if facility_name not in CAPACITY_MARKER_NAMES :
74- print (f"Warning: No name mapping for facility: { facility_name } " )
75+ logging . info (f"Warning: No name mapping for facility: { facility_name } " )
7576 continue
7677
7778 db_name = CAPACITY_MARKER_NAMES [facility_name ]
@@ -100,24 +101,25 @@ def fetch_capacities():
100101 facility = Facility .query .filter_by (id = facility_id ).first ()
101102
102103 if not facility or not facility .hours :
103- print (f"Warning: No hours found for facility ID { facility_id } " )
104+ logging . info (f"Warning: No hours found for facility ID { facility_id } " )
104105 continue
105106
106107 current_time = int (time .time ())
107-
108+
108109 is_open = any (hour .start_time <= current_time <= hour .end_time for hour in facility .hours )
109-
110+
110111 if is_open :
111112 topic_enum = gym_mapping [db_name ]
112113 check_and_send_capacity_reminders (topic_enum .name , db_name , percent , last_percent )
113-
114+
114115 add_single_capacity (count , facility_id , percent , updated )
115-
116+
116117 except Exception as e :
117- print (f"Error processing facility { facility .get ('LocationName' , 'unknown' )} : { str (e )} " )
118-
118+ logging .exception (f"Error processing facility { facility .get ('LocationName' , 'unknown' )} : { str (e )} " )
119+ # db_session.rollback()
120+
119121 except Exception as e :
120- print (f"Error fetching capacities: { str (e )} " )
122+ logging . error (f"Error fetching capacities: { str (e )} " )
121123 raise
122124
123125
@@ -177,10 +179,10 @@ def update_hourly_capacity(curDay, curHour):
177179 )
178180
179181 if hourly_average_capacity is not None :
180- print ("updating average" )
182+ logging . info ("updating average" )
181183 hourly_average_capacity .update_hourly_average (capacity .percent )
182184 else :
183- print ("No hourly capacity, creating new entry" )
185+ logging . info ("No hourly capacity, creating new entry" )
184186 hourly_average_capacity = HourlyAverageCapacity (
185187 facility_id = capacity .facility_id ,
186188 average_percent = capacity .percent ,
@@ -193,7 +195,7 @@ def update_hourly_capacity(curDay, curHour):
193195 db_session .commit ()
194196
195197 except Exception as e :
196- print (f"Error updating hourly average: { e } " )
198+ logging . error (f"Error updating hourly average: { e } " )
197199
198200
199201def check_and_send_capacity_reminders (facility_name , readable_name , current_percent , last_percent ):
@@ -214,5 +216,5 @@ def check_and_send_capacity_reminders(facility_name, readable_name, current_perc
214216
215217 for threshold in crossed_thresholds :
216218 topic_name = f"{ facility_name } _{ current_day_name } _{ threshold } "
217- print (f"Sending message to devices subscribed to { topic_name } " )
219+ logging . info (f"Sending message to devices subscribed to { topic_name } " )
218220 send_capacity_reminder (topic_name , readable_name , threshold )
0 commit comments