Skip to content

Commit 06de52a

Browse files
authored
Merge pull request #217 from cuappdev/master
FCM Subscription Checking
2 parents 69ee787 + 09cd302 commit 06de52a

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

src/schema.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,9 @@ def mutate(self, info, fcm_token, days_of_week, gyms, capacity_percent):
863863
for day in validated_workout_days:
864864
topic_name = f"{gym}_{day}_{capacity_percent}"
865865
try:
866-
messaging.subscribe_to_topic(fcm_token, topic_name)
866+
response = messaging.subscribe_to_topic(fcm_token, topic_name)
867+
if response.success_count == 0:
868+
raise Exception(response.errors[0].reason)
867869
except Exception as error:
868870
raise GraphQLError(f"Error subscribing to topic for {topic_name}: {error}")
869871

@@ -916,7 +918,9 @@ def mutate(self, info, reminder_id, gyms, days_of_week, capacity_percent):
916918

917919
for topic in topics:
918920
try:
919-
messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
921+
response = messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
922+
if response.success_count == 0:
923+
raise Exception(response.errors[0].reason)
920924
except Exception as error:
921925
raise GraphQLError(f"Error subscribing to topic: {error}")
922926

@@ -925,7 +929,9 @@ def mutate(self, info, reminder_id, gyms, days_of_week, capacity_percent):
925929

926930
for topic in topics:
927931
try:
928-
messaging.subscribe_to_topic(reminder.fcm_token, topic)
932+
response = messaging.subscribe_to_topic(reminder.fcm_token, topic)
933+
if response.success_count == 0:
934+
raise Exception(response.errors[0].reason)
929935
except Exception as error:
930936
raise GraphQLError(f"Error subscribing to topic: {error}")
931937

@@ -954,7 +960,9 @@ def mutate(self, info, reminder_id):
954960

955961
for topic in topics:
956962
try:
957-
messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
963+
response = messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
964+
if response.success_count == 0:
965+
raise Exception(response.errors[0].reason)
958966
except Exception as error:
959967
raise GraphQLError(f"Error unsubscribing from topic {topic}: {error}")
960968

src/scrapers/capacities_scraper.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
import time
3+
import logging
34
from bs4 import BeautifulSoup
45
from collections import namedtuple
56
from 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

199201
def 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

Comments
 (0)