File size: 3,555 Bytes
019b7f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | """
Ingests the complete 1,800 Synthetic Students Dataset into SQLite Database.
Creates student entities, backlog records, offers, and student login accounts.
"""
from __future__ import annotations
import os
import sys
import time
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if PROJECT_ROOT not in sys.path:
sys.path.insert(0, PROJECT_ROOT)
if "pyarrow" not in sys.modules:
sys.modules["pyarrow"] = None
from sqlalchemy import select
from backend.app.api.auth.hasher import hash_password
from backend.app.importers.student_importer import StudentImporter
from backend.app.storage.database import SessionLocal
from backend.app.storage.models.entities import StudentModel, UserModel
from backend.app.storage.repositories.mappers import student_domain_to_entity
def load_1800_students() -> None:
start_time = time.perf_counter()
csv_path = os.path.join(PROJECT_ROOT, "fixtures", "students", "synthetic_students_1800.csv")
if not os.path.exists(csv_path):
print(f"Error: CSV file not found at {csv_path}")
return
print(f"[*] Reading and parsing {csv_path}...")
with open(csv_path, "rb") as f:
content = f.read()
importer = StudentImporter()
report = importer.parse_file(content, "synthetic_students_1800.csv", dry_run=False)
print(f"[+] Successfully parsed {len(report.imported_students)} students from 9 branches.")
db = SessionLocal()
try:
# Precompute password hash once for all 1,800 student logins (admin123)
default_student_pwd_hash = hash_password("admin123")
# Existing roll numbers in DB
existing_rolls = set(db.execute(select(StudentModel.roll_no)).scalars().all())
existing_user_names = set(db.execute(select(UserModel.username)).scalars().all())
new_students_count = 0
new_users_count = 0
batch_size = 200
for i, s in enumerate(report.imported_students):
if s.roll_no not in existing_rolls:
entity = student_domain_to_entity(s)
db.add(entity)
existing_rolls.add(s.roll_no)
new_students_count += 1
if s.roll_no not in existing_user_names:
u = UserModel(
id=f"usr_{s.id}",
username=s.roll_no,
email=s.email,
password_hash=default_student_pwd_hash,
role="student",
student_id=s.id,
is_active=True,
)
db.add(u)
existing_user_names.add(s.roll_no)
new_users_count += 1
if (i + 1) % batch_size == 0:
db.commit()
print(f" --> Committed {i + 1}/{len(report.imported_students)} students...")
db.commit()
total_in_db = db.execute(select(StudentModel)).scalars().all()
elapsed = time.perf_counter() - start_time
print("=" * 65)
print(f"[OK] Complete Ingestion Finished in {elapsed:.2f} seconds!")
print(f" - Added new students: {new_students_count}")
print(f" - Added student logins: {new_users_count}")
print(f" - Total students in DB: {len(total_in_db)}")
print("=" * 65)
except Exception as exc:
db.rollback()
print(f"[!] Error during ingestion: {exc}")
raise
finally:
db.close()
if __name__ == "__main__":
load_1800_students()
|