perf: remove redundant COUNT query and use bulk insert for CSV imports

This commit is contained in:
Peter Stockings
2026-03-15 00:24:26 +11:00
parent 25aa7de043
commit 25d1774e53
2 changed files with 6 additions and 8 deletions

View File

@@ -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: