Add more badges

This commit is contained in:
Peter Stockings
2024-12-26 16:25:48 +11:00
parent cec37fe1f7
commit 9ed9aee471

View File

@@ -1,3 +1,4 @@
from collections import defaultdict
import csv import csv
from io import StringIO from io import StringIO
import io import io
@@ -83,38 +84,75 @@ def dashboard():
return systolic_avg, diastolic_avg, heart_rate_avg return systolic_avg, diastolic_avg, heart_rate_avg
# Helper function to calculate progress badges # Helper function to calculate progress badges
def calculate_progress_badges(readings, weekly_readings): def calculate_progress_badges(readings):
"""Calculate badges based on user reading activity."""
badges = [] badges = []
now = datetime.utcnow().date()
# General badges # Prepare sorted readings by timestamp
if len(readings) >= 10: sorted_readings = sorted(readings, key=lambda r: r.timestamp)
badges.append("10 Readings Logged")
if len(readings) >= 100:
badges.append("100 Readings Milestone")
if len(weekly_readings) >= 7:
badges.append("Logged Readings for 7 Days")
# Streak badge # Incremental milestone badge
if readings: def highest_milestone(total_readings, milestones):
streak_count = 1 # Start streak count with today's reading """Determine the highest milestone achieved."""
today = datetime.now().date() for milestone in reversed(milestones):
if total_readings >= milestone:
return f"{milestone} Readings Logged"
return None
# Sort readings by timestamp in ascending order highest_milestone_badge = highest_milestone(len(readings), [10, 50, 100, 500, 1000, 5000, 10000])
sorted_readings = sorted(readings, key=lambda r: r.timestamp) if highest_milestone_badge:
badges.append(highest_milestone_badge)
for i in range(len(sorted_readings) - 1, 0, -1): # Streaks and calendar month badges
current_day = sorted_readings[i].timestamp.date() if sorted_readings:
previous_day = sorted_readings[i - 1].timestamp.date() streak_count = 1
daily_streak = True
monthly_tracker = defaultdict(int)
# Check if days are consecutive # Start with the first reading
if (current_day - previous_day).days == 1: previous_date = sorted_readings[0].timestamp.date()
for reading in sorted_readings[1:]:
current_date = reading.timestamp.date()
# Check for consecutive daily streaks
if (current_date - previous_date).days == 1:
streak_count += 1 streak_count += 1
elif (current_day - previous_day).days > 1: elif (current_date - previous_date).days > 1:
break # Streak is broken daily_streak = False
# Add streak badge if the streak is greater than 1 # Track monthly activity
if streak_count > 1 and sorted_readings[-1].timestamp.date() == today: monthly_tracker[current_date.strftime('%Y-%m')] += 1
previous_date = current_date
# Add streak badges
if daily_streak and streak_count >= 1:
badges.append(f"Current Streak: {streak_count} Days") badges.append(f"Current Streak: {streak_count} Days")
if daily_streak and streak_count >= 7:
badges.append("Logged Every Day for a Week")
if daily_streak and streak_count >= 30 and previous_date == now:
badges.append("Monthly Streak")
# Add calendar month streak badges
for month, count in monthly_tracker.items():
if count >= 30:
badges.append(f"Full Month of Logging: {month}")
# Time-specific badges (morning/night logging)
def is_morning(reading_time):
return 5 <= reading_time.hour < 12
def is_night(reading_time):
return 18 <= reading_time.hour <= 23
if all(is_morning(r.timestamp) for r in sorted_readings[-7:]):
badges.append("Morning Riser: Logged Readings Every Morning for a Week")
if all(is_night(r.timestamp) for r in sorted_readings[-7:]):
badges.append("Night Owl: Logged Readings Every Night for a Week")
return badges return badges
@@ -147,8 +185,7 @@ def dashboard():
# Calculate weekly summary and progress badges # Calculate weekly summary and progress badges
systolic_avg, diastolic_avg, heart_rate_avg = calculate_weekly_summary(readings) systolic_avg, diastolic_avg, heart_rate_avg = calculate_weekly_summary(readings)
weekly_readings = [r for r in readings if r.timestamp >= (datetime.now() - timedelta(days=7))] badges = calculate_progress_badges(readings)
badges = calculate_progress_badges(readings, weekly_readings)
# Prepare graph data # Prepare graph data
timestamps = [reading.timestamp.strftime('%b %d') for reading in readings] timestamps = [reading.timestamp.strftime('%b %d') for reading in readings]