|
| 1 | +from collections import defaultdict |
| 2 | +from statistics import mean, pstdev |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from pymongo import UpdateOne |
| 6 | + |
| 7 | +from models.user_record import Role |
| 8 | +from services import mongodb_handler |
| 9 | +from services.mongodb_handler import Collection |
| 10 | + |
| 11 | +GLOBAL_FIELDS = {"resume", "hackathon_experience"} |
| 12 | + |
| 13 | + |
| 14 | +async def add_normalized_scores_to_all_hacker_applicants() -> None: |
| 15 | + """Calculates normalized scores and adds them to all hacker apps""" |
| 16 | + all_apps = await get_all_hacker_apps() |
| 17 | + reviewer_stats = get_reviewer_stats(all_apps) |
| 18 | + |
| 19 | + normalized_scores = get_normalized_scores_for_hacker_applicants( |
| 20 | + all_apps, reviewer_stats |
| 21 | + ) |
| 22 | + await update_hacker_applicants_in_collection(normalized_scores) |
| 23 | + |
| 24 | + |
| 25 | +async def get_all_hacker_apps() -> list[dict[str, object]]: |
| 26 | + return await mongodb_handler.retrieve( |
| 27 | + Collection.USERS, |
| 28 | + { |
| 29 | + "roles": Role.HACKER, |
| 30 | + "application_data.global_field_scores.resume": {"$gte": 0}, |
| 31 | + "application_data.global_field_scores.hackathon_experience": {"$gte": 0}, |
| 32 | + }, |
| 33 | + [ |
| 34 | + "_id", |
| 35 | + "status", |
| 36 | + "application_data.review_breakdown", |
| 37 | + "application_data.global_field_scores", |
| 38 | + ], |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def get_reviewer_stats(all_apps: list[dict[str, Any]]) -> dict[str, dict[str, float]]: |
| 43 | + """Compute mean and std for each reviewer across all applications.""" |
| 44 | + reviewer_totals: dict[str, list[float]] = defaultdict(list) |
| 45 | + |
| 46 | + for app in all_apps: |
| 47 | + breakdown = app.get("application_data", {}).get("review_breakdown", {}) |
| 48 | + for reviewer, scores_dict in breakdown.items(): |
| 49 | + total_score = sum( |
| 50 | + [ |
| 51 | + score |
| 52 | + for field, score in scores_dict.items() |
| 53 | + if field not in GLOBAL_FIELDS |
| 54 | + ] |
| 55 | + ) |
| 56 | + reviewer_totals[reviewer].append(total_score) |
| 57 | + |
| 58 | + reviewer_stats = { |
| 59 | + reviewer: { |
| 60 | + "mean": mean(scores), |
| 61 | + "std": pstdev(scores) or 1.0, # avoid divide-by-zero if all same |
| 62 | + } |
| 63 | + for reviewer, scores in reviewer_totals.items() |
| 64 | + } |
| 65 | + |
| 66 | + return reviewer_stats |
| 67 | + |
| 68 | + |
| 69 | +def get_normalized_scores_for_hacker_applicants( |
| 70 | + all_apps: list[dict[str, Any]], reviewer_stats: dict[str, dict[str, float]] |
| 71 | +) -> dict[str, dict[str, float]]: |
| 72 | + """ |
| 73 | + Compute normalized scores for each applicant and return a dict in the format: |
| 74 | + { |
| 75 | + "app1": {"ian": 0.5, "bob": -0.3}, |
| 76 | + "app2": {"ian": 1.2} |
| 77 | + } |
| 78 | +
|
| 79 | + - all_apps: list of applicant dicts |
| 80 | + - reviewer_stats: dict of reviewer mean/std |
| 81 | + """ |
| 82 | + result: dict[str, dict[str, float]] = {} |
| 83 | + |
| 84 | + for app in all_apps: |
| 85 | + app_id = app["_id"] |
| 86 | + breakdown = app.get("application_data", {}).get("review_breakdown", {}) |
| 87 | + normalized_scores: dict[str, float] = {} |
| 88 | + |
| 89 | + for reviewer, scores_dict in breakdown.items(): |
| 90 | + total_score = sum( |
| 91 | + score |
| 92 | + for field, score in scores_dict.items() |
| 93 | + if field not in GLOBAL_FIELDS # exclude global fields if needed |
| 94 | + ) |
| 95 | + stats = reviewer_stats.get(reviewer, {"mean": 0, "std": 1}) |
| 96 | + normalized = (total_score - stats["mean"]) / stats["std"] |
| 97 | + normalized_scores[reviewer] = normalized |
| 98 | + |
| 99 | + result[app_id] = normalized_scores |
| 100 | + |
| 101 | + return result |
| 102 | + |
| 103 | + |
| 104 | +async def update_hacker_applicants_in_collection( |
| 105 | + normalized_scores: dict[str, dict[str, float]] |
| 106 | +) -> None: |
| 107 | + operations = [ |
| 108 | + UpdateOne( |
| 109 | + {"_id": app_id}, {"$set": {"application_data.normalized_scores": scores}} |
| 110 | + ) |
| 111 | + for app_id, scores in normalized_scores.items() |
| 112 | + ] |
| 113 | + |
| 114 | + await mongodb_handler.bulk_update(Collection.USERS, operations) |
0 commit comments