Spaces:
Paused
Paused
Download analytics.py from jobinv/persona_version_2: direct link, hf CLI and curl.
- Browser
- Download file 5.77 kB
-
https://huggingface.co/spaces/jobinv/persona_version_2/resolve/main/analytics.py
- Command line
-
hf download hf://spaces/jobinv/persona_version_2/analytics.py
-
curl -L -o analytics.py https://huggingface.co/spaces/jobinv/persona_version_2/resolve/main/analytics.py
5.77 kB
| from collections import defaultdict | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| from sqlalchemy import func, select | |
| from db import db_session | |
| from models import Cohort, EmployabilitySnapshot, JobApplication, Membership, PlacementRecord, UserAccount | |
| from services import require_membership | |
| def student_trend_figure(history): | |
| fig, ax = plt.subplots(figsize=(7, 3.2)) | |
| if history: | |
| ax.plot([x["date"] for x in history], [x["overall"] for x in history], marker="o") | |
| ax.set_ylim(0, 100); ax.set_ylabel("Validated employability"); ax.set_title("Employability over time"); ax.grid(alpha=0.25) | |
| else: | |
| ax.text(0.5, 0.5, "No snapshots yet", ha="center", va="center"); ax.set_axis_off() | |
| fig.tight_layout(); return fig | |
| def institution_dashboard(token, membership_id): | |
| with db_session() as db: | |
| actor, admin = require_membership(db, token, membership_id, {"faculty", "placement_officer", "institution_admin"}) | |
| inst_id = admin.institution_id | |
| students = db.scalars(select(Membership).where(Membership.institution_id == inst_id, Membership.role == "student", Membership.active == True)).all() | |
| faculty_count = db.scalar(select(func.count(Membership.id)).where(Membership.institution_id == inst_id, Membership.role == "faculty", Membership.active == True)) or 0 | |
| cohort_count = db.scalar(select(func.count(Cohort.id)).where(Cohort.institution_id == inst_id, Cohort.active == True)) or 0 | |
| latest_rows = [] | |
| for student in students: | |
| snap = db.scalar(select(EmployabilitySnapshot).where(EmployabilitySnapshot.membership_id == student.id).order_by(EmployabilitySnapshot.captured_at.desc()).limit(1)) | |
| latest_rows.append((student, snap)) | |
| scores = [snap.overall_score for _, snap in latest_rows if snap] | |
| avg_score = round(sum(scores) / len(scores), 1) if scores else 0 | |
| ready = sum(1 for x in scores if x >= 70); excellence = sum(1 for x in scores if x >= 85) | |
| placed_ids = set(db.scalars(select(PlacementRecord.membership_id).join(Membership, PlacementRecord.membership_id == Membership.id).where(Membership.institution_id == inst_id, PlacementRecord.status == "placed")).all()) | |
| placed_count = len(placed_ids); placement_rate = round(100 * placed_count / max(1, len(students)), 1) | |
| summary = ( | |
| f"### Institution dashboard\n**Students:** {len(students)} | **Faculty:** {faculty_count} | **Cohorts:** {cohort_count} \n" | |
| f"**Average validated employability:** {avg_score}/100 \n**Students ≥70:** {ready} | **Students ≥85:** {excellence} \n" | |
| f"**Placed students:** {placed_count} | **Placement rate:** {placement_rate}%" | |
| ) | |
| cohort_vals = defaultdict(list) | |
| for student, snap in latest_rows: | |
| if not snap: continue | |
| name = "Unassigned" | |
| if student.cohort_id: | |
| c = db.get(Cohort, student.cohort_id) | |
| if c: name = c.name | |
| cohort_vals[name].append(snap.overall_score) | |
| names = list(cohort_vals); avgs = [sum(cohort_vals[n])/len(cohort_vals[n]) for n in names] | |
| fig1, ax1 = plt.subplots(figsize=(7, 3.2)) | |
| if names: | |
| ax1.bar(names, avgs); ax1.set_ylim(0,100); ax1.set_ylabel("Average validated score"); ax1.set_title("Employability by cohort"); ax1.tick_params(axis="x", rotation=25) | |
| else: | |
| ax1.text(0.5,0.5,"No cohort snapshots",ha="center",va="center"); ax1.set_axis_off() | |
| fig1.tight_layout() | |
| statuses = ["saved", "applied", "interview", "offer", "placed"]; counts=[] | |
| for status in statuses: | |
| if status == "placed": counts.append(placed_count) | |
| else: | |
| counts.append(db.scalar(select(func.count(JobApplication.id)).join(Membership, JobApplication.membership_id == Membership.id).where(Membership.institution_id == inst_id, JobApplication.status == status)) or 0) | |
| fig2, ax2 = plt.subplots(figsize=(7,3.2)); ax2.bar(statuses, counts); ax2.set_title("Placement / opportunity funnel"); ax2.set_ylabel("Records"); fig2.tight_layout() | |
| snaps = db.scalars(select(EmployabilitySnapshot).join(Membership, EmployabilitySnapshot.membership_id == Membership.id).where(Membership.institution_id == inst_id).order_by(EmployabilitySnapshot.captured_at)).all() | |
| month_map=defaultdict(list) | |
| for snap in snaps: month_map[snap.captured_at.strftime("%Y-%m")].append(snap.overall_score) | |
| months=sorted(month_map); monthly=[sum(month_map[m])/len(month_map[m]) for m in months] | |
| fig3, ax3 = plt.subplots(figsize=(7,3.2)) | |
| if months: | |
| ax3.plot(months, monthly, marker="o"); ax3.set_ylim(0,100); ax3.set_title("Institution employability trend"); ax3.set_ylabel("Average validated score"); ax3.tick_params(axis="x", rotation=30) | |
| else: | |
| ax3.text(0.5,0.5,"No longitudinal data yet",ha="center",va="center"); ax3.set_axis_off() | |
| fig3.tight_layout() | |
| rows=[] | |
| for student, snap in latest_rows: | |
| user=db.get(UserAccount, student.user_id); cohort=db.get(Cohort, student.cohort_id) if student.cohort_id else None | |
| rows.append({"Membership ID":student.id,"Student":user.display_name if user else "","HF Username":user.hf_username if user else "","Cohort":cohort.name if cohort else "","Discipline":student.discipline,"Latest score":snap.overall_score if snap else 0,"Assessment":snap.assessment_score if snap else 0,"Evidence":snap.evidence_score if snap else 0,"Placed":"Yes" if student.id in placed_ids else "No"}) | |
| table=pd.DataFrame(rows) | |
| if len(table): table=table.sort_values("Latest score",ascending=False) | |
| return summary, table, fig1, fig2, fig3 | |