Fix quick stats to show dynamic data (Issue #7)

- Add calculate_quick_stats() to get real-time post counts
- Calculate posts from last 24 hours instead of hardcoded value
- Pass quick_stats to dashboard template
- Update template to display dynamic posts_today count

Fixes #7
This commit is contained in:
2025-10-11 23:38:06 -05:00
parent 63fa44ed2c
commit 6a1834bbd2
2 changed files with 26 additions and 3 deletions

27
app.py
View File

@@ -271,9 +271,32 @@ def check_first_user():
pass
def calculate_quick_stats():
"""Calculate quick stats for dashboard"""
from datetime import datetime, timedelta
cached_posts, _ = _load_posts_cache()
# Calculate posts from today (last 24 hours)
now = datetime.utcnow()
today_start = now - timedelta(hours=24)
today_timestamp = today_start.timestamp()
posts_today = sum(1 for post in cached_posts.values()
if post.get('timestamp', 0) >= today_timestamp)
return {
'posts_today': posts_today,
'total_posts': len(cached_posts)
}
@app.route('/')
def index():
"""Serve the main feed page"""
# Calculate stats
quick_stats = calculate_quick_stats()
if current_user.is_authenticated:
# Load user settings
try:
@@ -282,7 +305,7 @@ def index():
logger.warning(f"Invalid user settings JSON for user {current_user.id}: {e}")
user_settings = {}
return render_template('dashboard.html', user_settings=user_settings)
return render_template('dashboard.html', user_settings=user_settings, quick_stats=quick_stats)
else:
# Check if anonymous access is allowed
if app.config.get('ALLOW_ANONYMOUS_ACCESS', False):
@@ -297,7 +320,7 @@ def index():
'dark_patterns_opt_in': False
}
}
return render_template('dashboard.html', user_settings=user_settings, anonymous=True)
return render_template('dashboard.html', user_settings=user_settings, anonymous=True, quick_stats=quick_stats)
else:
# Redirect non-authenticated users to login
return redirect(url_for('login'))

View File

@@ -84,7 +84,7 @@
<h3>Quick Stats</h3>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-number">156</div>
<div class="stat-number">{{ quick_stats.posts_today if quick_stats else 0 }}</div>
<div class="stat-label">Posts Today</div>
</div>
<div class="stat-card">