From 68c80594663777b94f5fd5f73ddebd7c1394194e Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Mon, 30 Dec 2024 23:29:30 +1100 Subject: [PATCH] Fixc weekly graph --- app/routes/main.py | 46 +++++++++++++++++++++++++----------- app/templates/dashboard.html | 10 ++++---- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/app/routes/main.py b/app/routes/main.py index 688f37b..d5eb30f 100644 --- a/app/routes/main.py +++ b/app/routes/main.py @@ -25,18 +25,18 @@ def dashboard(): user_tz = timezone(current_user.profile.timezone or 'UTC') # Get date range for readings - first_reading, last_reading = get_reading_date_range(current_user.id) + first_reading, last_reading = get_reading_date_range(current_user.id, user_tz) start_date = request.form.get('start_date') or (first_reading and first_reading.strftime('%Y-%m-%d')) end_date = request.form.get('end_date') or (last_reading and last_reading.strftime('%Y-%m-%d')) # Fetch filtered readings readings = fetch_readings(current_user.id, start_date, end_date) - now = datetime.utcnow() # Annotate readings with relative and localized timestamps - annotate_readings(readings, now, user_tz) + annotate_readings(readings, user_tz) # Generate calendar view + week_view = generate_weekly_calendar(readings, datetime.now(user_tz), user_tz) month_view = generate_monthly_calendar(readings, datetime.now(user_tz), user_tz) # Calculate stats and badges @@ -58,18 +58,17 @@ def dashboard(): start_date=start_date, end_date=end_date, month=month_view, - date=date, - timedelta=timedelta, + week = week_view, **graph_data ) -def get_reading_date_range(user_id): +def get_reading_date_range(user_id, user_tz): """Fetch the earliest and latest reading timestamps for a user.""" result = db.session.query( func.min(Reading.timestamp).label('first'), func.max(Reading.timestamp).label('last') ).filter(Reading.user_id == user_id).first() - return result.first, result.last + return utc.localize(result.first).astimezone(user_tz), utc.localize(result.last).astimezone(user_tz) def fetch_readings(user_id, start_date, end_date): """Retrieve readings filtered by date range.""" @@ -81,8 +80,9 @@ def fetch_readings(user_id, start_date, end_date): ) return query.order_by(Reading.timestamp.desc()).all() -def annotate_readings(readings, now, user_tz): +def annotate_readings(readings, user_tz): """Add relative and localized timestamps to readings.""" + now = datetime.utcnow() for reading in readings: reading.relative_timestamp = humanize.naturaltime(now - reading.timestamp) reading.local_timestamp = utc.localize(reading.timestamp).astimezone(user_tz) @@ -116,11 +116,34 @@ def generate_monthly_calendar(readings, selected_date, local_tz): 'day': current_date.day, 'is_today': current_date == today, 'is_in_current_month': current_date.month == selected_date.month, - 'readings': readings_by_day.get(current_date, []), + 'readings': readings_by_day.get(current_date.date(), []), } for current_date in (start_date + timedelta(days=i) for i in range((end_date - start_date).days + 1)) ] +def generate_weekly_calendar(readings, selected_date, local_tz): + """Generate a weekly calendar view.""" + # Get the start of the week (Monday) and the end of the week (Sunday) + today = datetime.now(local_tz).date() + start_of_week = selected_date - timedelta(days=selected_date.weekday()) + end_of_week = start_of_week + timedelta(days=6) + + # Group readings by day + readings_by_day = defaultdict(list) + for reading in readings: + local_date = reading.timestamp.astimezone(local_tz).date() + readings_by_day[local_date].append(reading) + + # Build the weekly calendar view + return [ + { + 'date': current_date.strftime('%a, %b %d') , + 'is_today': current_date == today, + 'readings': readings_by_day.get(current_date.date(), []), + } + for current_date in (start_of_week + timedelta(days=i) for i in range(7)) + ] + def prepare_graph_data(readings): """Prepare data for graph rendering.""" return { @@ -174,11 +197,6 @@ def calculate_progress_badges(readings): if count >= 30: badges.append(f"Full Month of Logging: {month}") - if streak_count >= 7: - badges.append("Logged Every Day for a Week") - if streak_count >= 30 and previous_date == now: - badges.append("Monthly Streak") - if all(5 <= r.timestamp.hour < 12 for r in sorted_readings[-7:]): badges.append("Morning Riser: Logged Readings Every Morning for a Week") diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html index 4de1444..81bc0ca 100644 --- a/app/templates/dashboard.html +++ b/app/templates/dashboard.html @@ -148,13 +148,11 @@
- {% set today = date.today() %} - {% for i in range(7) %} - {% set day = today - timedelta(days=today.weekday() - i) %} + {% for day in week %}
-
{{ day.strftime('%a, %b %d') }}
- {% if day in readings_by_date %} - {% for reading in readings_by_date[day]|sort(attribute="timestamp", reverse = True) %} +
{{ day.date }}
+ {% if day.readings %} + {% for reading in day.readings %}