File size: 8,521 Bytes
c468cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import argparse
import os
from datetime import datetime

from dotenv import load_dotenv
from pymongo import MongoClient


TARGET_COLLECTIONS = {
    "branches": "academic_branches",
    "classes": "academic_classes",
    "classrooms": "academic_classrooms",
    "subjects": "academic_subjects",
    "assignments": "faculty_assignments",
}


def _now_str():
    return datetime.now().strftime("%Y-%m-%d %H:%M:%S")


def _clean_text(value):
    return str(value or "").strip()


def _norm_upper(value):
    return _clean_text(value).upper()


def _to_int(value):
    if value in (None, ""):
        return None
    try:
        return int(value)
    except Exception:
        return None


def _slug(value):
    raw = _norm_upper(value)
    chars = []
    prev_underscore = False
    for ch in raw:
        if ch.isalnum():
            chars.append(ch)
            prev_underscore = False
        else:
            if not prev_underscore:
                chars.append("_")
                prev_underscore = True
    out = "".join(chars).strip("_")
    return out or "SUBJECT"


def _subject_code(branch, semester, subject_name):
    base = _slug(subject_name)
    sem = "X" if semester is None else str(semester)
    return f"{_norm_upper(branch)}{sem}_{base}"[:40]


def _build_faculty_lookup(faculty_docs):
    by_email = {}
    by_name = {}
    for doc in faculty_docs:
        email = _clean_text(doc.get("email")).lower()
        name = _clean_text(doc.get("name"))
        if email:
            by_email[email] = doc
        if name:
            by_name[name.lower()] = doc
    return by_email, by_name


def analyze_data(db):
    timetable_docs = list(db["timetable"].find({}, {"_id": 0}))
    faculty_docs = list(db["faculty"].find({}, {"_id": 0, "name": 1, "email": 1}))
    student_docs = list(db["students"].find({}, {"_id": 0, "branch": 1, "semester": 1, "section": 1}))

    faculty_by_email, faculty_by_name = _build_faculty_lookup(faculty_docs)

    branches = {}
    classes = {}
    classrooms = {}
    subjects = {}
    assignments = {}

    now = _now_str()

    for doc in timetable_docs:
        branch = _norm_upper(doc.get("branch"))
        semester = _to_int(doc.get("semester"))
        section = _norm_upper(doc.get("section"))
        subject_name = _clean_text(doc.get("subject"))
        classroom = _clean_text(doc.get("classroom"))

        faculty_email = _clean_text(doc.get("faculty_email")).lower()
        faculty_name = _clean_text(doc.get("faculty_name"))

        if not faculty_email and faculty_name:
            found = faculty_by_name.get(faculty_name.lower())
            if found:
                faculty_email = _clean_text(found.get("email")).lower()

        if faculty_email and not faculty_name:
            found = faculty_by_email.get(faculty_email)
            if found:
                faculty_name = _clean_text(found.get("name"))

        if branch:
            branches[branch] = {
                "code": branch,
                "name": branch,
                "active": True,
                "updated_at": now,
            }

        if branch and semester is not None and section:
            key = (branch, semester, section)
            classes[key] = {
                "branch": branch,
                "semester": semester,
                "section": section,
                "label": f"{branch} / {semester} / {section}",
                "active": True,
                "updated_at": now,
            }

        if classroom:
            classrooms[classroom] = {
                "name": classroom,
                "type": "Classroom",
                "capacity": None,
                "active": True,
                "updated_at": now,
            }

        if branch and semester is not None and subject_name:
            code = _subject_code(branch, semester, subject_name)
            key = (branch, semester, code)
            subjects[key] = {
                "code": code,
                "name": subject_name,
                "branch": branch,
                "semester": semester,
                "type": "theory",
                "active": True,
                "updated_at": now,
            }

            if faculty_email and section:
                assignment_key = (faculty_email, branch, semester, section, code)
                assignments[assignment_key] = {
                    "faculty_email": faculty_email,
                    "faculty_name": faculty_name,
                    "branch": branch,
                    "semester": semester,
                    "section": section,
                    "subject_code": code,
                    "subject_name": subject_name,
                    "classroom": classroom,
                    "active": True,
                    "updated_at": now,
                }

    for doc in student_docs:
        branch = _norm_upper(doc.get("branch"))
        semester = _to_int(doc.get("semester"))
        section = _norm_upper(doc.get("section"))

        if branch and branch not in branches:
            branches[branch] = {
                "code": branch,
                "name": branch,
                "active": True,
                "updated_at": now,
            }

        if branch and semester is not None and section:
            key = (branch, semester, section)
            if key not in classes:
                classes[key] = {
                    "branch": branch,
                    "semester": semester,
                    "section": section,
                    "label": f"{branch} / {semester} / {section}",
                    "active": True,
                    "updated_at": now,
                }

    return {
        "branches": list(branches.values()),
        "classes": list(classes.values()),
        "classrooms": list(classrooms.values()),
        "subjects": list(subjects.values()),
        "assignments": list(assignments.values()),
    }


def _upsert_many(collection, docs, keys, apply_changes):
    inserted_or_updated = 0
    for doc in docs:
        filt = {k: doc[k] for k in keys}
        update = {
            "$set": doc,
            "$setOnInsert": {"created_at": _now_str()},
        }
        if apply_changes:
            collection.update_one(filt, update, upsert=True)
        inserted_or_updated += 1
    return inserted_or_updated


def run(apply_changes=False):
    load_dotenv()

    mongo_uri = os.environ.get("MONGO_URI")
    db_name = os.environ.get("MONGODB_DB", "attendance_db")

    if not mongo_uri:
        raise RuntimeError("MONGO_URI is not set. Add it to backend/.env")

    client = MongoClient(mongo_uri)
    db = client[db_name]

    data = analyze_data(db)

    print("Analysis summary:")
    print(f"  Timetable rows: {db['timetable'].count_documents({})}")
    print(f"  Faculty rows:   {db['faculty'].count_documents({})}")
    print(f"  Student rows:   {db['students'].count_documents({})}")
    print(f"  Branches:       {len(data['branches'])}")
    print(f"  Classes:        {len(data['classes'])}")
    print(f"  Classrooms:     {len(data['classrooms'])}")
    print(f"  Subjects:       {len(data['subjects'])}")
    print(f"  Assignments:    {len(data['assignments'])}")

    if not apply_changes:
        print("\nDry run only. Re-run with --apply to upsert data into MongoDB.")
        return

    mapping = {
        "branches": (db[TARGET_COLLECTIONS["branches"]], ("code",)),
        "classes": (db[TARGET_COLLECTIONS["classes"]], ("branch", "semester", "section")),
        "classrooms": (db[TARGET_COLLECTIONS["classrooms"]], ("name",)),
        "subjects": (db[TARGET_COLLECTIONS["subjects"]], ("code",)),
        "assignments": (
            db[TARGET_COLLECTIONS["assignments"]],
            ("faculty_email", "branch", "semester", "section", "subject_code"),
        ),
    }

    print("\nUpserting academic setup data...")
    for key, docs in data.items():
        collection, uniq_keys = mapping[key]
        changed = _upsert_many(collection, docs, uniq_keys, apply_changes=True)
        print(f"  {key}: upserted {changed}")

    print("\nDone. Refresh /admin/academic-setup in the frontend.")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Bootstrap academic setup collections from timetable/faculty/students data."
    )
    parser.add_argument(
        "--apply",
        action="store_true",
        help="Write changes to MongoDB. Without this flag, script runs in dry-run mode.",
    )
    args = parser.parse_args()

    run(apply_changes=args.apply)