deeep-sec commited on
Commit
019b7f2
·
verified ·
1 Parent(s): 3769597

Upload scripts/load_all_1800_students.py with huggingface_hub

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