From 243e24e1006b8d5ea5d2e9673323fe82345d630d Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Fri, 23 May 2025 22:22:49 +0200 Subject: [PATCH] fixes quota recalculation --- scipaperloader/blueprints/config.py | 19 +- scipaperloader/cache_utils.py | 36 ++- .../templates/config/schedule.html.jinja | 211 +++++++++++++----- scipaperloader/templates/papers.html.jinja | 6 + 4 files changed, 201 insertions(+), 71 deletions(-) diff --git a/scipaperloader/blueprints/config.py b/scipaperloader/blueprints/config.py index 49bf148..ca805df 100644 --- a/scipaperloader/blueprints/config.py +++ b/scipaperloader/blueprints/config.py @@ -49,6 +49,19 @@ def _update_volume(new_volume): ) db.session.commit() + + # Invalidate and recalculate the hourly quota cache + try: + # Import the calculation function from the scraper module + from ..blueprints.scraper import calculate_papers_for_current_hour + invalidate_hourly_quota_cache(calculate_papers_for_current_hour) + except Exception as e: + # Log the error but don't fail the update + ActivityLog.log_error( + error_message=f"Error invalidating hourly quota cache: {str(e)}", + source="_update_volume" + ) + return True, "Volume updated successfully!", volume_config except (ValueError, TypeError) as e: @@ -175,9 +188,11 @@ def _update_schedule(schedule_data): db.session.commit() - # Invalidate hourly quota cache using the cache_utils module + # Invalidate hourly quota cache and immediately recalculate try: - invalidate_hourly_quota_cache() + # Import the calculation function from the scraper module + from ..blueprints.scraper import calculate_papers_for_current_hour + invalidate_hourly_quota_cache(calculate_papers_for_current_hour) except Exception as e: # Log the error but don't fail the update ActivityLog.log_error( diff --git a/scipaperloader/cache_utils.py b/scipaperloader/cache_utils.py index b8743fe..536e65a 100644 --- a/scipaperloader/cache_utils.py +++ b/scipaperloader/cache_utils.py @@ -12,17 +12,37 @@ HOURLY_QUOTA_CACHE = { 'last_config_update': None, # Last time volume or schedule config was updated } -def invalidate_hourly_quota_cache(): - """Invalidate the hourly quota cache when configuration changes.""" +def invalidate_hourly_quota_cache(calculate_function=None): + """ + Invalidate the hourly quota cache when configuration changes. + + Args: + calculate_function (callable, optional): Function to recalculate quota immediately. + If None, recalculation will happen during next get_cached_hourly_quota() call. + """ global HOURLY_QUOTA_CACHE HOURLY_QUOTA_CACHE['last_config_update'] = None - # Log the cache invalidation - ActivityLog.log_scraper_activity( - action="cache_invalidated", - status="info", - description="Hourly quota cache was invalidated due to configuration changes" - ) + # If a calculation function is provided, recalculate immediately + if calculate_function: + current_hour = datetime.now().hour + quota = calculate_function() + HOURLY_QUOTA_CACHE['hour'] = current_hour + HOURLY_QUOTA_CACHE['quota'] = quota + HOURLY_QUOTA_CACHE['last_config_update'] = datetime.now() + + ActivityLog.log_scraper_activity( + action="cache_recalculated", + status="info", + description=f"Hourly quota immediately recalculated after config change: {quota} papers" + ) + else: + # Log the cache invalidation + ActivityLog.log_scraper_activity( + action="cache_invalidated", + status="info", + description="Hourly quota cache was invalidated due to configuration changes" + ) def get_cached_hourly_quota(calculate_function): """ diff --git a/scipaperloader/templates/config/schedule.html.jinja b/scipaperloader/templates/config/schedule.html.jinja index 89346e3..068ac32 100644 --- a/scipaperloader/templates/config/schedule.html.jinja +++ b/scipaperloader/templates/config/schedule.html.jinja @@ -30,6 +30,13 @@ font-size: 0.7rem; margin-top: 2px; } + + .weight-gradient { + width: 50px; + height: 15px; + background: linear-gradient(to right, hsl(210, 10%, 95%), hsl(210, 10%, 30%)); + border-radius: 2px; + }