mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-10 21:03:01 +00:00
Feature/background season data (#46)
* Fix NCAAFB ranking display issue - Remove duplicate ranking system that was drawing rankings behind team logos - Old system (_get_rank) was drawing rankings at top of logos - New system (_fetch_team_rankings) correctly draws rankings in bottom corners - Remove old ranking calls from live, recent, and upcoming game drawing functions - Remove unnecessary _fetch_rankings() calls from update methods - Rankings now only appear in designated corner positions, not overlapping logos Fixes issue where team rankings/betting lines were being drawn behind team logos instead of replacing team records in the corners. * Add missing show_ranking and show_records options to NCAAFB web UI - Add show_ranking option to NCAAFB scoreboard config template - Add show_records and show_ranking toggle switches to NCAAFB web UI - Update JavaScript form collection to include new fields - Users can now control whether to show team records or rankings via web interface This completes the fix for NCAAFB ranking display - users can now enable show_ranking in the web UI to see AP Top 25 rankings instead of team records. * Implement Background Threading for Season Data Fetching Phase 1: Background Season Data Fetching - COMPLETED Key Features: - Created BackgroundDataService class with thread-safe operations - Implemented automatic retry logic with exponential backoff - Modified NFL manager to use background service - Added immediate partial data return for non-blocking display - Comprehensive logging and statistics tracking Performance Benefits: - Main display loop no longer blocked by API calls - Season data always fresh with background updates - Better user experience during data fetching Files Added/Modified: - src/background_data_service.py (NEW) - src/nfl_managers.py (updated) - config/config.template.json (updated) - test_background_service.py (NEW) - BACKGROUND_SERVICE_README.md (NEW) * Fix data validation issues in background service - Add comprehensive data structure validation in NFL managers - Handle malformed events gracefully with proper error logging - Validate cached data format and handle legacy formats - Add data validation in background service response parsing - Fix TypeError: string indices must be integers, not 'str' This fixes the error where events were being treated as strings instead of dictionaries, causing crashes in recent/upcoming games. * Phase 2: Apply Background Service to Major Sport Managers ✅ Applied background service support to: - NCAAFB Manager (College Football) - NBA Manager (Basketball) - NHL Manager (Hockey) - MLB Manager (Baseball) 🔧 Key Features Added: - Background service initialization for each sport - Configurable workers, timeouts, and retry settings - Graceful fallback when background service is disabled - Comprehensive logging for monitoring ⚙️ Configuration Updates: - Added background_service config section to NBA - Added background_service config section to NHL - Added background_service config section to NCAAFB - Each sport can independently enable/disable background service 📈 Performance Benefits: - Season data fetching no longer blocks display loops - Immediate response with cached/partial data - Background threads handle heavy API calls - Better responsiveness across all supported sports Next: Apply to remaining managers (MiLB, Soccer, etc.) * Fix Python compatibility issue in BackgroundDataService shutdown 🐛 Bug Fix: - Fixed TypeError in ThreadPoolExecutor.shutdown() for older Python versions - Added try/catch to handle timeout parameter compatibility - Fallback gracefully for Python < 3.9 that doesn't support timeout parameter 🔧 Technical Details: - ThreadPoolExecutor.shutdown(timeout=) was added in Python 3.9 - Older versions only support shutdown(wait=) - Added compatibility layer with proper error handling ✅ Result: - No more shutdown exceptions on older Python versions - Graceful degradation for different Python environments - Maintains full functionality on newer Python versions * Phase 2 Complete: Background Service Applied to All Sport Managers 🎉 MAJOR MILESTONE: Complete Background Service Rollout ✅ All Sport Managers Now Support Background Service: - MiLB Manager (Minor League Baseball) - Soccer Manager (Multiple leagues: Premier League, La Liga, etc.) - Leaderboard Manager (Multi-sport standings) - Odds Ticker Manager (Live betting odds) 🔧 Technical Implementation: - Background service initialization in all managers - Configurable workers, timeouts, and retry settings - Graceful fallback when background service is disabled - Comprehensive logging for monitoring and debugging - Thread-safe operations with proper error handling ⚙️ Configuration Support Added: - MiLB: background_service config section - Soccer: background_service config section - Leaderboard: background_service config section - Odds Ticker: background_service config section - Each manager can independently enable/disable background service 📈 Performance Benefits Achieved: - Non-blocking data fetching across ALL sport managers - Immediate response with cached/partial data - Background threads handle heavy API calls - Significantly improved responsiveness - Better user experience during data loading 🚀 Production Ready: - All major sport managers now support background threading - Comprehensive configuration options - Robust error handling and fallback mechanisms - Ready for production deployment Next: Phase 3 - Advanced features (priority queuing, analytics) * Update wiki submodule with Background Service documentation 📚 Wiki Documentation Added: - Complete Background Service Guide with architecture diagrams - Configuration examples and best practices - Performance benefits and troubleshooting guide - Migration guide and advanced features 🔧 Navigation Updates: - Added to sidebar under Technical section - Updated home page with performance section - Highlighted as NEW feature with ⚡ icon The wiki now includes comprehensive documentation for the new background threading system that improves performance across all sport managers. * Fix CacheManager constructor in test script 🐛 Bug Fix: - Fixed CacheManager initialization in test_background_service.py - CacheManager no longer takes config_manager parameter - Updated constructor call to match current implementation ✅ Result: - Test script now works with current CacheManager API - Background service testing can proceed without errors * Move test_background_service.py to test/ directory 📁 Organization Improvement: - Moved test_background_service.py from root to test/ directory - Updated import paths to work from new location - Fixed sys.path to correctly reference src/ directory - Updated imports to use relative paths 🔧 Technical Changes: - Changed sys.path from 'src' to '../src' (go up from test/) - Updated imports to remove 'src.' prefix - Maintains all functionality while improving project structure ✅ Benefits: - Better project organization - Test files properly grouped in test/ directory - Cleaner root directory structure - Follows standard Python project layout * Remove old test_background_service.py from root directory 📁 Cleanup: - Removed test_background_service.py from root directory - File has been moved to test/ directory for better organization - Maintains clean project structure * Fix NCAA FB team ranking display functionality - Add missing _fetch_team_rankings() calls to all update methods (live, recent, upcoming) - Add ranking display logic to live manager scorebug layout - Remove unused old _fetch_rankings() method and top_25_rankings variable - Rankings now properly display as #X format when show_ranking is enabled - Fixes non-functional ranking feature despite existing UI and configuration options
This commit is contained in:
@@ -12,6 +12,7 @@ from src.cache_manager import CacheManager # Keep CacheManager import
|
||||
from src.config_manager import ConfigManager
|
||||
from src.odds_manager import OddsManager
|
||||
from src.logo_downloader import download_missing_logo
|
||||
from src.background_data_service import get_background_service
|
||||
import pytz
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib3.util.retry import Retry
|
||||
@@ -103,7 +104,20 @@ class BaseNCAAFBManager: # Renamed class
|
||||
self._rankings_cache_timestamp = 0
|
||||
self._rankings_cache_duration = 3600 # Cache rankings for 1 hour
|
||||
|
||||
self.top_25_rankings = []
|
||||
|
||||
# Initialize background data service
|
||||
background_config = self.ncaa_fb_config.get("background_service", {})
|
||||
if background_config.get("enabled", True): # Default to enabled
|
||||
max_workers = background_config.get("max_workers", 3)
|
||||
self.background_service = get_background_service(self.cache_manager, max_workers)
|
||||
self.background_fetch_requests = {} # Track background fetch requests
|
||||
self.background_enabled = True
|
||||
self.logger.info(f"[NCAAFB] Background service enabled with {max_workers} workers")
|
||||
else:
|
||||
self.background_service = None
|
||||
self.background_fetch_requests = {}
|
||||
self.background_enabled = False
|
||||
self.logger.info("[NCAAFB] Background service disabled")
|
||||
|
||||
self.logger.info(f"Initialized NCAAFB manager with display dimensions: {self.display_width}x{self.display_height}")
|
||||
self.logger.info(f"Logo directory: {self.logo_dir}")
|
||||
@@ -369,38 +383,6 @@ class BaseNCAAFBManager: # Renamed class
|
||||
else:
|
||||
return self._fetch_ncaa_fb_api_data(use_cache=True)
|
||||
|
||||
def _fetch_rankings(self):
|
||||
self.logger.info(f"[NCAAFB] Fetching current AP Top 25 rankings from ESPN API...")
|
||||
try:
|
||||
url = "http://site.api.espn.com/apis/site/v2/sports/football/college-football/rankings"
|
||||
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
# Grab rankings[0]
|
||||
rankings_0 = data.get("rankings", [])[0]
|
||||
|
||||
# Extract top 25 team abbreviations
|
||||
self.top_25_rankings = [
|
||||
entry["team"]["abbreviation"]
|
||||
for entry in rankings_0.get("ranks", [])[:25]
|
||||
]
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
self.logger.error(f"[NCAAFB] Error retrieving AP Top 25 rankings: {e}")
|
||||
|
||||
def _get_rank(self, team_to_check):
|
||||
i = 1
|
||||
if self.top_25_rankings:
|
||||
for team in self.top_25_rankings:
|
||||
if team == team_to_check:
|
||||
return i
|
||||
i += 1
|
||||
else:
|
||||
return 0
|
||||
else:
|
||||
return 0
|
||||
|
||||
def _load_fonts(self):
|
||||
"""Load fonts used by the scoreboard."""
|
||||
@@ -831,6 +813,10 @@ class NCAAFBLiveManager(BaseNCAAFBManager): # Renamed class
|
||||
if current_time - self.last_update >= interval:
|
||||
self.last_update = current_time
|
||||
|
||||
# Fetch rankings if enabled
|
||||
if self.show_ranking:
|
||||
self._fetch_team_rankings()
|
||||
|
||||
if self.test_mode:
|
||||
# Simulate clock running down in test mode
|
||||
if self.current_game and self.current_game["is_live"]:
|
||||
@@ -1105,6 +1091,86 @@ class NCAAFBLiveManager(BaseNCAAFBManager): # Renamed class
|
||||
if 'odds' in game and game['odds']:
|
||||
self._draw_dynamic_odds(draw_overlay, game['odds'], self.display_width, self.display_height)
|
||||
|
||||
# Draw records or rankings if enabled
|
||||
if self.show_records or self.show_ranking:
|
||||
try:
|
||||
record_font = ImageFont.truetype("assets/fonts/4x6-font.ttf", 6)
|
||||
self.logger.debug(f"Loaded 6px record font successfully")
|
||||
except IOError:
|
||||
record_font = ImageFont.load_default()
|
||||
self.logger.warning(f"Failed to load 6px font, using default font (size: {record_font.size})")
|
||||
|
||||
# Get team abbreviations
|
||||
away_abbr = game.get('away_abbr', '')
|
||||
home_abbr = game.get('home_abbr', '')
|
||||
|
||||
record_bbox = draw_overlay.textbbox((0,0), "0-0", font=record_font)
|
||||
record_height = record_bbox[3] - record_bbox[1]
|
||||
record_y = self.display_height - record_height
|
||||
self.logger.debug(f"Record positioning: height={record_height}, record_y={record_y}, display_height={self.display_height}")
|
||||
|
||||
# Display away team info
|
||||
if away_abbr:
|
||||
if self.show_ranking and self.show_records:
|
||||
# When both rankings and records are enabled, rankings replace records completely
|
||||
rankings = self._fetch_team_rankings()
|
||||
away_rank = rankings.get(away_abbr, 0)
|
||||
if away_rank > 0:
|
||||
away_text = f"#{away_rank}"
|
||||
else:
|
||||
# Show nothing for unranked teams when rankings are prioritized
|
||||
away_text = ''
|
||||
elif self.show_ranking:
|
||||
# Show ranking only if available
|
||||
rankings = self._fetch_team_rankings()
|
||||
away_rank = rankings.get(away_abbr, 0)
|
||||
if away_rank > 0:
|
||||
away_text = f"#{away_rank}"
|
||||
else:
|
||||
away_text = ''
|
||||
elif self.show_records:
|
||||
# Show record only when rankings are disabled
|
||||
away_text = game.get('away_record', '')
|
||||
else:
|
||||
away_text = ''
|
||||
|
||||
if away_text:
|
||||
away_record_x = 0
|
||||
self.logger.debug(f"Drawing away ranking '{away_text}' at ({away_record_x}, {record_y}) with font size {record_font.size if hasattr(record_font, 'size') else 'unknown'}")
|
||||
self._draw_text_with_outline(draw_overlay, away_text, (away_record_x, record_y), record_font)
|
||||
|
||||
# Display home team info
|
||||
if home_abbr:
|
||||
if self.show_ranking and self.show_records:
|
||||
# When both rankings and records are enabled, rankings replace records completely
|
||||
rankings = self._fetch_team_rankings()
|
||||
home_rank = rankings.get(home_abbr, 0)
|
||||
if home_rank > 0:
|
||||
home_text = f"#{home_rank}"
|
||||
else:
|
||||
# Show nothing for unranked teams when rankings are prioritized
|
||||
home_text = ''
|
||||
elif self.show_ranking:
|
||||
# Show ranking only if available
|
||||
rankings = self._fetch_team_rankings()
|
||||
home_rank = rankings.get(home_abbr, 0)
|
||||
if home_rank > 0:
|
||||
home_text = f"#{home_rank}"
|
||||
else:
|
||||
home_text = ''
|
||||
elif self.show_records:
|
||||
# Show record only when rankings are disabled
|
||||
home_text = game.get('home_record', '')
|
||||
else:
|
||||
home_text = ''
|
||||
|
||||
if home_text:
|
||||
home_record_bbox = draw_overlay.textbbox((0,0), home_text, font=record_font)
|
||||
home_record_width = home_record_bbox[2] - home_record_bbox[0]
|
||||
home_record_x = self.display_width - home_record_width
|
||||
self.logger.debug(f"Drawing home ranking '{home_text}' at ({home_record_x}, {record_y}) with font size {record_font.size if hasattr(record_font, 'size') else 'unknown'}")
|
||||
self._draw_text_with_outline(draw_overlay, home_text, (home_record_x, record_y), record_font)
|
||||
|
||||
# Composite the text overlay onto the main image
|
||||
main_img = Image.alpha_composite(main_img, overlay)
|
||||
main_img = main_img.convert('RGB') # Convert for display
|
||||
@@ -1141,6 +1207,10 @@ class NCAAFBRecentManager(BaseNCAAFBManager): # Renamed class
|
||||
|
||||
self.last_update = current_time # Update time even if fetch fails
|
||||
|
||||
# Fetch rankings if enabled
|
||||
if self.show_ranking:
|
||||
self._fetch_team_rankings()
|
||||
|
||||
try:
|
||||
data = self._fetch_data() # Uses shared cache
|
||||
if not data or 'events' not in data:
|
||||
@@ -1440,6 +1510,10 @@ class NCAAFBUpcomingManager(BaseNCAAFBManager): # Renamed class
|
||||
|
||||
self.last_update = current_time
|
||||
|
||||
# Fetch rankings if enabled
|
||||
if self.show_ranking:
|
||||
self._fetch_team_rankings()
|
||||
|
||||
try:
|
||||
data = self._fetch_data() # Uses shared cache
|
||||
if not data or 'events' not in data:
|
||||
|
||||
Reference in New Issue
Block a user