Reduce NBA manager logging frequency with 5-minute cooldown for status messages

This commit is contained in:
ChuckBuilds
2025-04-19 14:47:18 -05:00
parent b45a99d3b1
commit 7a71260cab

View File

@@ -18,6 +18,7 @@ class BaseNBAManager:
_no_data_warning_logged = False _no_data_warning_logged = False
_last_warning_time = 0 _last_warning_time = 0
_warning_cooldown = 60 # Only log warnings once per minute _warning_cooldown = 60 # Only log warnings once per minute
_last_log_times = {} # Track last log time for each message type
def __init__(self, config: Dict[str, Any], display_manager: DisplayManager): def __init__(self, config: Dict[str, Any], display_manager: DisplayManager):
self.display_manager = display_manager self.display_manager = display_manager
@@ -51,6 +52,16 @@ class BaseNBAManager:
self.logger.info(f"Initialized NBA manager with display dimensions: {self.display_width}x{self.display_height}") self.logger.info(f"Initialized NBA manager with display dimensions: {self.display_width}x{self.display_height}")
self.logger.info(f"Logo directory: {self.logo_dir}") self.logger.info(f"Logo directory: {self.logo_dir}")
def _should_log(self, message_type: str, cooldown: int = 300) -> bool:
"""Check if a message should be logged based on cooldown period."""
current_time = time.time()
last_time = self._last_log_times.get(message_type, 0)
if current_time - last_time >= cooldown:
self._last_log_times[message_type] = current_time
return True
return False
def _load_test_data(self) -> Dict: def _load_test_data(self) -> Dict:
"""Load test data for development and testing.""" """Load test data for development and testing."""
self.logger.info("[NBA] Loading test data") self.logger.info("[NBA] Loading test data")
@@ -620,10 +631,12 @@ class NBARecentManager(BaseNBAManager):
# Fetch data from ESPN API # Fetch data from ESPN API
data = self._fetch_data() data = self._fetch_data()
if not data or 'events' not in data: if not data or 'events' not in data:
if self._should_log("no_events", 300):
self.logger.warning("[NBA] No events found in ESPN API response") self.logger.warning("[NBA] No events found in ESPN API response")
return return
events = data['events'] events = data['events']
if self._should_log("fetch_success", 300):
self.logger.info(f"[NBA] Successfully fetched {len(events)} events from ESPN API") self.logger.info(f"[NBA] Successfully fetched {len(events)} events from ESPN API")
# Process games # Process games
@@ -639,6 +652,7 @@ class NBARecentManager(BaseNBAManager):
if game['home_abbr'] in self.favorite_teams or if game['home_abbr'] in self.favorite_teams or
game['away_abbr'] in self.favorite_teams] game['away_abbr'] in self.favorite_teams]
if self._should_log("team_games", 300):
self.logger.info(f"[NBA] Found {len(team_games)} recent games for favorite teams") self.logger.info(f"[NBA] Found {len(team_games)} recent games for favorite teams")
if not team_games: if not team_games:
self.logger.info("[NBA] No recent games found for favorite teams") self.logger.info("[NBA] No recent games found for favorite teams")
@@ -698,10 +712,12 @@ class NBAUpcomingManager(BaseNBAManager):
# Fetch data from ESPN API # Fetch data from ESPN API
data = self._fetch_data() data = self._fetch_data()
if not data or 'events' not in data: if not data or 'events' not in data:
if self._should_log("no_events", 300):
self.logger.warning("[NBA] No events found in ESPN API response") self.logger.warning("[NBA] No events found in ESPN API response")
return return
events = data['events'] events = data['events']
if self._should_log("fetch_success", 300):
self.logger.info(f"[NBA] Successfully fetched {len(events)} events from ESPN API") self.logger.info(f"[NBA] Successfully fetched {len(events)} events from ESPN API")
# Process games # Process games
@@ -717,6 +733,7 @@ class NBAUpcomingManager(BaseNBAManager):
if game['home_abbr'] in self.favorite_teams or if game['home_abbr'] in self.favorite_teams or
game['away_abbr'] in self.favorite_teams] game['away_abbr'] in self.favorite_teams]
if self._should_log("team_games", 300):
self.logger.info(f"[NBA] Found {len(team_games)} upcoming games for favorite teams") self.logger.info(f"[NBA] Found {len(team_games)} upcoming games for favorite teams")
if not team_games: if not team_games:
self.logger.info("[NBA] No upcoming games found for favorite teams") self.logger.info("[NBA] No upcoming games found for favorite teams")