Spaces:
Sleeping
Sleeping
File size: 5,416 Bytes
2b18225 | 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 | """
Per-organization Google Sheet sync.
Each university keeps its own Sheet and edits it directly — no training, no
account on our side, no export step. This module walks the organizations that
have a `sheet_url` configured and syncs each one into THAT university's own
collection, so a registrar editing a deadline changes the answer their own
students get and nobody else's.
Why an orchestrator rather than changes inside SheetSync: SheetSync's job is
"sync one Sheet into one collection", and it does that well already — version
markers, chunking, pruning. This module's job is "do that once per
university, and make sure one university's failure doesn't stop the others."
Keeping them separate means the hard part (per-org isolation) is testable
without touching the part that already works.
Isolation properties this guarantees:
- Each organization's rows are written to its own collection, resolved
through org_registry — the same path retrieval and uploads now use.
- Each organization has its own version marker, so an unchanged Sheet costs
one small HTTP request and no re-embedding. Sharing one marker would make
the first org's version look current for everyone and silently skip the
rest.
- A failure for one organization is caught, logged and reported, and the
loop continues. A university with a broken Sheet cannot stop another
university's update, and its own previous good data stays in place.
"""
from __future__ import annotations
import logging
from typing import Any, Dict, List, Optional
logger = logging.getLogger(__name__)
def sync_all_organizations(
alu_organization_id: Optional[str] = None,
force: bool = False,
) -> Dict[str, Any]:
"""Sync every organization that has a Sheet configured.
Returns a per-organization summary. Never raises: a caller on a startup
hook or a poll loop should not be taken down by one bad Sheet.
"""
import db
import org_registry
from sheet_sync import SheetSync
try:
orgs: List[dict] = db.list_organizations_with_sheets()
except Exception as e:
logger.warning("[org_sheet_sync] could not list organizations: %s", e)
return {"ok": False, "error": str(e), "organizations": []}
if not orgs:
logger.info("[org_sheet_sync] no organizations have a sheet_url configured")
return {"ok": True, "organizations": []}
results: List[Dict[str, Any]] = []
for org in orgs:
org_id = str(org.get("id"))
name = org.get("name") or org_id
entry: Dict[str, Any] = {"organization_id": org_id, "name": name}
try:
engine = org_registry.get_engine_for_org(
org_id, alu_organization_id=alu_organization_id
)
if engine is None:
raise RuntimeError("no retrieval engine available")
syncer = SheetSync(
engine,
json_url=org.get("sheet_url"),
organization_id=org_id,
)
result = syncer.refresh(force=force)
entry["result"] = result.to_dict()
entry["ok"] = result.ok
logger.info(
"[org_sheet_sync] %s: ok=%s seen=%s upserted=%s deleted=%s skipped=%s",
name, result.ok, result.entries_seen, result.entries_upserted,
result.entries_deleted, result.skipped_unchanged,
)
except Exception as e:
# Deliberately swallowed: one university's misconfigured Sheet must
# not stop the others from updating. Their previous data is
# untouched, so the failure degrades to "stale", never to "wrong".
entry["ok"] = False
entry["error"] = str(e)
logger.warning("[org_sheet_sync] %s failed: %s", name, e)
results.append(entry)
return {
"ok": all(r.get("ok") for r in results),
"organizations": results,
"synced": sum(1 for r in results if r.get("ok")),
"failed": sum(1 for r in results if not r.get("ok")),
}
def sync_one_organization(
organization_id: str,
alu_organization_id: Optional[str] = None,
force: bool = True,
) -> Dict[str, Any]:
"""Sync a single organization's Sheet on demand.
Used by the admin "refresh now" action so a registrar who has just edited
their Sheet doesn't have to wait for the next poll to see the change.
"""
import db
import org_registry
from sheet_sync import SheetSync
try:
orgs = db.list_organizations_with_sheets()
except Exception as e:
return {"ok": False, "error": str(e)}
match = next((o for o in orgs if str(o.get("id")) == str(organization_id)), None)
if match is None:
return {"ok": False, "error": "organization has no sheet_url configured"}
try:
engine = org_registry.get_engine_for_org(
organization_id, alu_organization_id=alu_organization_id
)
if engine is None:
return {"ok": False, "error": "no retrieval engine available"}
syncer = SheetSync(
engine, json_url=match.get("sheet_url"), organization_id=str(organization_id)
)
return syncer.refresh(force=force).to_dict()
except Exception as e:
logger.warning("[org_sheet_sync] single sync failed for %s: %s", organization_id, e)
return {"ok": False, "error": str(e)}
|