Attempt to resolve filter omitting recent entries

This commit is contained in:
Peter Stockings
2025-01-06 23:14:21 +11:00
parent b51f204d8d
commit acad2def92

View File

@@ -30,7 +30,7 @@ def dashboard():
end_date = request.form.get('end_date') or (last_reading and last_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 # Fetch filtered readings
readings = fetch_readings(current_user.id, start_date, end_date) readings = fetch_readings(current_user.id, start_date, end_date, user_tz)
# Annotate readings with relative and localized timestamps # Annotate readings with relative and localized timestamps
annotate_readings(readings, user_tz) annotate_readings(readings, user_tz)
@@ -69,16 +69,26 @@ def get_reading_date_range(user_id, user_tz):
func.min(Reading.timestamp).label('first'), func.min(Reading.timestamp).label('first'),
func.max(Reading.timestamp).label('last') func.max(Reading.timestamp).label('last')
).filter(Reading.user_id == user_id).first() ).filter(Reading.user_id == user_id).first()
return utc.localize(result.first).astimezone(user_tz), utc.localize(result.last).astimezone(user_tz)
def fetch_readings(user_id, start_date, end_date): first = utc.localize(result.first).astimezone(user_tz) if result.first else None
last = utc.localize(result.last).astimezone(user_tz) if result.last else None
return first, last
def fetch_readings(user_id, start_date, end_date, user_tz):
"""Retrieve readings filtered by date range.""" """Retrieve readings filtered by date range."""
query = Reading.query.filter_by(user_id=user_id) query = Reading.query.filter_by(user_id=user_id)
if start_date and end_date: if start_date and end_date:
# Convert dates to the user's timezone
start_dt = user_tz.localize(datetime.strptime(start_date, '%Y-%m-%d')).astimezone(utc)
end_dt = user_tz.localize(datetime.strptime(end_date, '%Y-%m-%d')).astimezone(utc) + timedelta(days=1) - timedelta(seconds=1)
query = query.filter( query = query.filter(
Reading.timestamp >= datetime.strptime(start_date, '%Y-%m-%d'), Reading.timestamp >= start_dt,
Reading.timestamp <= datetime.strptime(end_date, '%Y-%m-%d') Reading.timestamp <= end_dt
) )
return query.order_by(Reading.timestamp.desc()).all() return query.order_by(Reading.timestamp.desc()).all()
def annotate_readings(readings, user_tz): def annotate_readings(readings, user_tz):