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:
27
app.py
27
app.py
@@ -271,9 +271,32 @@ def check_first_user():
|
|||||||
pass
|
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('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
"""Serve the main feed page"""
|
"""Serve the main feed page"""
|
||||||
|
# Calculate stats
|
||||||
|
quick_stats = calculate_quick_stats()
|
||||||
|
|
||||||
if current_user.is_authenticated:
|
if current_user.is_authenticated:
|
||||||
# Load user settings
|
# Load user settings
|
||||||
try:
|
try:
|
||||||
@@ -282,7 +305,7 @@ def index():
|
|||||||
logger.warning(f"Invalid user settings JSON for user {current_user.id}: {e}")
|
logger.warning(f"Invalid user settings JSON for user {current_user.id}: {e}")
|
||||||
user_settings = {}
|
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:
|
else:
|
||||||
# Check if anonymous access is allowed
|
# Check if anonymous access is allowed
|
||||||
if app.config.get('ALLOW_ANONYMOUS_ACCESS', False):
|
if app.config.get('ALLOW_ANONYMOUS_ACCESS', False):
|
||||||
@@ -297,7 +320,7 @@ def index():
|
|||||||
'dark_patterns_opt_in': False
|
'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:
|
else:
|
||||||
# Redirect non-authenticated users to login
|
# Redirect non-authenticated users to login
|
||||||
return redirect(url_for('login'))
|
return redirect(url_for('login'))
|
||||||
|
|||||||
@@ -84,7 +84,7 @@
|
|||||||
<h3>Quick Stats</h3>
|
<h3>Quick Stats</h3>
|
||||||
<div class="stats-grid">
|
<div class="stats-grid">
|
||||||
<div class="stat-card">
|
<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 class="stat-label">Posts Today</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="stat-card">
|
<div class="stat-card">
|
||||||
|
|||||||
Reference in New Issue
Block a user