diff --git a/app/routes/data.py b/app/routes/data.py index 52f832d..e038e75 100644 --- a/app/routes/data.py +++ b/app/routes/data.py @@ -17,16 +17,17 @@ def manage_data(): try: csv_data = csv.reader(StringIO(file.read().decode('utf-8'))) next(csv_data) # Skip the header row + readings_to_add = [] for row in csv_data: timestamp, systolic, diastolic, heart_rate = row - reading = Reading( + readings_to_add.append(Reading( user_id=current_user.id, timestamp=datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S'), systolic=int(systolic), diastolic=int(diastolic), heart_rate=int(heart_rate), - ) - db.session.add(reading) + )) + db.session.bulk_save_objects(readings_to_add) db.session.commit() flash('Data imported successfully!', 'success') except Exception as e: diff --git a/app/routes/main.py b/app/routes/main.py index ec5d9c9..c9bed8a 100644 --- a/app/routes/main.py +++ b/app/routes/main.py @@ -288,12 +288,9 @@ def prepare_graph_data(readings): def calculate_progress_badges(user_id, user_tz): """Generate badges based on user activity and milestones using optimized queries.""" - total_readings = Reading.query.filter_by(user_id=user_id).count() - if total_readings == 0: - return [] - - # Fetch only timestamps (highly optimized compared to fetching full objects) + # Fetch only timestamps (index-only scan on the composite index) timestamps = db.session.query(Reading.timestamp).filter(Reading.user_id == user_id).order_by(Reading.timestamp.desc()).all() + total_readings = len(timestamps) return _compute_badges(total_readings, timestamps, user_tz) def _compute_badges(total_readings, timestamps, user_tz, now_local=None):