Add more badges
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from collections import defaultdict
|
||||
import csv
|
||||
from io import StringIO
|
||||
import io
|
||||
@@ -83,39 +84,76 @@ def dashboard():
|
||||
return systolic_avg, diastolic_avg, heart_rate_avg
|
||||
|
||||
# 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 = []
|
||||
|
||||
# General badges
|
||||
if len(readings) >= 10:
|
||||
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
|
||||
if readings:
|
||||
streak_count = 1 # Start streak count with today's reading
|
||||
today = datetime.now().date()
|
||||
now = datetime.utcnow().date()
|
||||
|
||||
# Sort readings by timestamp in ascending order
|
||||
sorted_readings = sorted(readings, key=lambda r: r.timestamp)
|
||||
# Prepare sorted readings by timestamp
|
||||
sorted_readings = sorted(readings, key=lambda r: r.timestamp)
|
||||
|
||||
for i in range(len(sorted_readings) - 1, 0, -1):
|
||||
current_day = sorted_readings[i].timestamp.date()
|
||||
previous_day = sorted_readings[i - 1].timestamp.date()
|
||||
# Incremental milestone badge
|
||||
def highest_milestone(total_readings, milestones):
|
||||
"""Determine the highest milestone achieved."""
|
||||
for milestone in reversed(milestones):
|
||||
if total_readings >= milestone:
|
||||
return f"{milestone} Readings Logged"
|
||||
return None
|
||||
|
||||
# Check if days are consecutive
|
||||
if (current_day - previous_day).days == 1:
|
||||
highest_milestone_badge = highest_milestone(len(readings), [10, 50, 100, 500, 1000, 5000, 10000])
|
||||
if highest_milestone_badge:
|
||||
badges.append(highest_milestone_badge)
|
||||
|
||||
# Streaks and calendar month badges
|
||||
if sorted_readings:
|
||||
streak_count = 1
|
||||
daily_streak = True
|
||||
monthly_tracker = defaultdict(int)
|
||||
|
||||
# Start with the first reading
|
||||
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
|
||||
elif (current_day - previous_day).days > 1:
|
||||
break # Streak is broken
|
||||
elif (current_date - previous_date).days > 1:
|
||||
daily_streak = False
|
||||
|
||||
# Add streak badge if the streak is greater than 1
|
||||
if streak_count > 1 and sorted_readings[-1].timestamp.date() == today:
|
||||
# Track monthly activity
|
||||
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")
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -147,8 +185,7 @@ def dashboard():
|
||||
|
||||
# Calculate weekly summary and progress badges
|
||||
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, weekly_readings)
|
||||
badges = calculate_progress_badges(readings)
|
||||
|
||||
# Prepare graph data
|
||||
timestamps = [reading.timestamp.strftime('%b %d') for reading in readings]
|
||||
|
||||
Reference in New Issue
Block a user