Compare commits

...

3 Commits

Author SHA1 Message Date
f11131df8d fix(redis): connect during health checks 2026-05-27 16:50:25 +01:00
047225e3ae fix(graph-tree): class_code column, SubjectClass expansion handler
- _query_teacher_classes: fix code -> class_code (Supabase column name), add section_id param
- _build_timetable_section: tag class nodes with section_id=timetable
- _build_classes_section: tag class nodes with section_id=classes
- SubjectClass handler: section=timetable -> taught_lessons for class; section=classes -> enrolled students

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-27 13:30:53 +01:00
caae5b799d fix(graph-tree): switch class/timetable data source to Supabase
- _query_teacher_classes: now queries class_teachers + class_students tables instead of non-existent Neo4j TEACHER_HAS_CLASS relationship
- _build_classes_section: updated signature to (user_id, institute_id, institute_db)
- _build_timetable_section: updated signature; loads classes from Supabase, not Neo4j TIMETABLE_HAS_CLASS
- TeacherTimetable lazy handler: simplified (classes pre-loaded in section builder)
- AcademicWeek timetable-term: Supabase taught_lessons query by date range instead of Neo4j
- expose supabase_institute_id from _resolve_institute call

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-27 12:58:47 +01:00
3 changed files with 194 additions and 94 deletions

View File

@ -491,7 +491,9 @@ class RedisManager:
try:
if not self.client:
raise Exception("No Redis connection")
logger.info("Redis health check has no active client; connecting now")
if not self.connect():
raise Exception("No Redis connection")
# Test connection
self.client.ping()

View File

@ -166,27 +166,66 @@ def _query_month_days(month_uuid: str) -> List[Dict]:
return []
def _query_teacher_classes(institute_db: str, teacher_uuid: str) -> List[Dict]:
def _query_teacher_classes(
user_id: str, institute_id: str, institute_db: str, section_id: str = ""
) -> List[Dict]:
"""Query classes for a teacher or student from Supabase (source of truth)."""
try:
with driver_tools.get_session(database=institute_db) as session:
result = session.run(
"MATCH (t:Teacher {uuid_string: $uuid})-[:TEACHER_HAS_CLASS]->(c:SubjectClass) "
"RETURN c ORDER BY c.name",
uuid=teacher_uuid,
)
return [
{
"neo4j_node_id": r["c"]["uuid_string"],
"label": r["c"].get("name") or "Class",
"node_type": "SubjectClass",
"neo4j_db_name": institute_db,
"is_section": False,
"has_children": True,
}
for r in result
]
sb = _sb()
teacher_rows = (
sb.supabase.table("class_teachers")
.select("class_id, is_primary")
.eq("teacher_id", user_id)
.execute()
.data or []
)
teacher_class_ids = {r["class_id"] for r in teacher_rows}
student_rows = (
sb.supabase.table("class_students")
.select("class_id")
.eq("student_id", user_id)
.eq("status", "active")
.execute()
.data or []
)
student_class_ids = {r["class_id"] for r in student_rows}
all_ids = list(teacher_class_ids | student_class_ids)
if not all_ids:
return []
classes = (
sb.supabase.table("classes")
.select("id, name, class_code, subject")
.in_("id", all_ids)
.eq("institute_id", institute_id)
.eq("is_active", True)
.order("name")
.execute()
.data or []
)
result = []
for c in classes:
role = "teacher" if c["id"] in teacher_class_ids else "student"
label = c.get("class_code") or c.get("name") or "Class"
node: Dict = {
"neo4j_node_id": c["id"],
"label": label,
"node_type": "SubjectClass",
"neo4j_db_name": institute_db,
"is_section": False,
"has_children": True,
"neo4j_props": {
"role": role,
"subject": c.get("subject") or "",
"name": c.get("name") or "",
"class_code": c.get("class_code") or "",
},
}
if section_id:
node["section_id"] = section_id
result.append(node)
return result
except Exception as e:
logger.warning(f"Could not query classes for teacher {teacher_uuid}: {e}")
logger.warning(f"Could not query classes for user {user_id}: {e}")
return []
@ -235,8 +274,8 @@ def _build_calendar_section() -> Dict:
return _section("calendar", "Calendar", "classroomcopilot", "empty")
def _build_timetable_section(institute_db: Optional[str], teacher_uuid: Optional[str]) -> Dict:
if not institute_db or not teacher_uuid:
def _build_timetable_section(user_id: str, institute_id: Optional[str], institute_db: Optional[str], teacher_uuid: Optional[str]) -> Dict:
if not institute_db or not teacher_uuid or not institute_id:
return _section("timetable", "My Timetable", "", "no_school")
try:
@ -249,27 +288,8 @@ def _build_timetable_section(institute_db: Optional[str], teacher_uuid: Optional
if rec:
tt = rec["tt"]
tt_uuid = tt["uuid_string"]
classes = []
try:
cls_result = session.run(
"MATCH (tt2:TeacherTimetable {uuid_string: $id})"
"-[:TIMETABLE_HAS_CLASS]->(c:SubjectClass) "
"RETURN c ORDER BY c.name",
id=tt_uuid,
)
classes = [
{
"neo4j_node_id": r["c"]["uuid_string"],
"label": r["c"].get("name") or "Class",
"node_type": "SubjectClass",
"neo4j_db_name": institute_db,
"is_section": False,
"has_children": True,
}
for r in cls_result
]
except Exception:
pass
# Load classes from Supabase (source of truth)
classes = _query_teacher_classes(user_id, institute_id, institute_db, section_id="timetable")
return {
**_section("timetable", "My Timetable", institute_db, "populated",
has_children=True, children=classes if classes else None),
@ -283,11 +303,11 @@ def _build_timetable_section(institute_db: Optional[str], teacher_uuid: Optional
return _section("timetable", "My Timetable", institute_db, "empty")
def _build_classes_section(institute_db: Optional[str], teacher_uuid: Optional[str]) -> Dict:
if not institute_db or not teacher_uuid:
def _build_classes_section(user_id: str, institute_id: Optional[str], institute_db: Optional[str]) -> Dict:
if not institute_db or not institute_id:
return _section("classes", "My Classes", "", "no_school")
classes = _query_teacher_classes(institute_db, teacher_uuid)
classes = _query_teacher_classes(user_id, institute_id, institute_db, section_id="classes")
if classes:
return _section("classes", "My Classes", institute_db, "populated",
has_children=True, children=classes)
@ -413,28 +433,9 @@ def _get_children_for_node(
# TeacherTimetable lazy-load (fallback if not pre-loaded, or for By-Term view)
if node_type == "TeacherTimetable" and neo4j_db_name:
if section_id in ("", "timetable"):
try:
with driver_tools.get_session(database=neo4j_db_name) as session:
result = session.run(
"MATCH (tt:TeacherTimetable {uuid_string: $id})"
"-[:TIMETABLE_HAS_CLASS]->(c:SubjectClass) "
"RETURN c ORDER BY c.name",
id=neo4j_node_id,
)
return [
{
"neo4j_node_id": r["c"]["uuid_string"],
"label": r["c"].get("name") or "Class",
"node_type": "SubjectClass",
"neo4j_db_name": neo4j_db_name,
"is_section": False,
"has_children": True,
}
for r in result
]
except Exception as e:
logger.warning(f"TeacherTimetable class children failed: {e}")
return []
# Classes are pre-loaded in _build_timetable_section via Supabase.
# Lazy expansion here is not needed in normal flow.
return []
if section_id == "timetable-term":
try:
with driver_tools.get_session(database=neo4j_db_name) as session:
@ -461,6 +462,84 @@ def _get_children_for_node(
logger.warning(f"TeacherTimetable term children failed: {e}")
return []
# SubjectClass — expand to taught lessons (timetable context) or members (classes context)
if node_type == "SubjectClass":
class_id = neo4j_node_id # Supabase class UUID
try:
sb = _sb()
if section_id == "timetable" and user_email:
# Resolve teacher profile_id from email
prof = sb.supabase.table("profiles").select("id").eq("email", user_email).single().execute()
teacher_id = (prof.data or {}).get("id")
if teacher_id:
lessons = (
sb.supabase.table("taught_lessons")
.select("id, date, period_code, class_name, subject")
.eq("class_id", class_id)
.eq("teacher_profile_id", teacher_id)
.order("date")
.order("period_code")
.execute()
.data or []
)
return [
{
"neo4j_node_id": tl["id"],
"label": "{} {}".format(
tl.get("period_code") or "",
tl.get("date") or ""
).strip(),
"node_type": "TaughtLesson",
"neo4j_db_name": neo4j_db_name,
"is_section": False,
"has_children": False,
"section_id": "timetable",
}
for tl in lessons
]
return []
if section_id == "classes":
# Class members: students enrolled in this class
members = (
sb.supabase.table("class_students")
.select("student_id, status, enrolled_at")
.eq("class_id", class_id)
.order("enrolled_at")
.execute()
.data or []
)
if not members:
return []
student_ids = [m["student_id"] for m in members]
status_map = {m["student_id"]: m["status"] for m in members}
profiles = (
sb.supabase.table("profiles")
.select("id, email, first_name, last_name, user_type")
.in_("id", student_ids)
.order("last_name")
.execute()
.data or []
)
return [
{
"neo4j_node_id": p["id"],
"label": "{} {} ({})".format(
p.get("first_name") or "",
p.get("last_name") or "",
status_map.get(p["id"], ""),
).strip(),
"node_type": "Student",
"neo4j_db_name": neo4j_db_name,
"is_section": False,
"has_children": False,
"section_id": "classes",
}
for p in profiles
]
except Exception as e:
logger.warning(f"SubjectClass children failed: {e}")
return []
# Section containers that need lazy loading
if node_type == "Section":
if section_id == "timetable" and neo4j_db_name:
@ -566,33 +645,52 @@ def _get_children_for_node(
# AcademicWeek → days (or TaughtLessons in timetable-term context)
if node_type == "AcademicWeek" and neo4j_db_name:
if section_id == "timetable-term" and user_email:
# Supabase: get week date range from Neo4j, then query taught_lessons
try:
week_start = None
with driver_tools.get_session(database=neo4j_db_name) as session:
result = session.run(
rec = session.run(
"MATCH (w:AcademicWeek {uuid_string: $id}) "
"-[:ACADEMIC_WEEK_HAS_ACADEMIC_DAY]->(d:AcademicDay) "
"-[:ACADEMIC_DAY_HAS_PERIOD]->(p:AcademicPeriod) "
"-[:ACADEMIC_PERIOD_HAS_TAUGHT_LESSON]->(tl:TaughtLesson) "
"WHERE tl.teacher_email = "
"RETURN tl, d.date AS date ORDER BY d.date, p.start_time",
"RETURN w.start_date AS start_date",
id=neo4j_node_id,
email=user_email,
)
return [
{
"neo4j_node_id": r["tl"]["uuid_string"],
"label": (r["tl"].get("period_code") or "")
+ ""
+ (r["tl"].get("class_name") or r["tl"].get("subject_class") or "Lesson"),
"node_type": "TaughtLesson",
"neo4j_db_name": neo4j_db_name,
"is_section": False,
"has_children": False,
}
for r in result
]
).single()
if rec:
week_start = rec["start_date"]
if week_start:
from datetime import datetime, timedelta
start_dt = datetime.strptime(str(week_start)[:10], "%Y-%m-%d")
end_dt = start_dt + timedelta(days=6)
sb = _sb()
prof = sb.supabase.table("profiles").select("id").eq("email", user_email).single().execute()
teacher_id = (prof.data or {}).get("id")
if teacher_id:
lessons = (
sb.supabase.table("taught_lessons")
.select("id, date, period_code, class_name, subject")
.eq("teacher_profile_id", teacher_id)
.gte("date", start_dt.strftime("%Y-%m-%d"))
.lte("date", end_dt.strftime("%Y-%m-%d"))
.order("date")
.order("period_code")
.execute()
.data or []
)
return [
{
"neo4j_node_id": tl["id"],
"label": "{}{}".format(
tl.get("period_code") or "",
tl.get("class_name") or tl.get("subject") or "Lesson"
),
"node_type": "TaughtLesson",
"neo4j_db_name": neo4j_db_name,
"is_section": False,
"has_children": False,
}
for tl in lessons
]
except Exception as e:
logger.warning(f"AcademicWeek timetable-term lessons failed: {e}")
logger.warning(f"AcademicWeek timetable-term Supabase lessons failed: {e}")
return []
try:
with driver_tools.get_session(database=neo4j_db_name) as session:
@ -691,12 +789,12 @@ async def get_teacher_graph_tree(
"has_children": True,
}
_, institute_db, teacher_node_uuid = _resolve_institute(user_id, user_email)
supabase_institute_id, institute_db, teacher_node_uuid = _resolve_institute(user_id, user_email)
sections = [
_build_calendar_section(),
_build_timetable_section(institute_db, teacher_node_uuid),
_build_classes_section(institute_db, teacher_node_uuid),
_build_timetable_section(user_id, supabase_institute_id, institute_db, teacher_node_uuid),
_build_classes_section(user_id, supabase_institute_id, institute_db),
_build_curriculum_section(institute_db),
_build_journal_section(teacher_db),
_build_planner_section(teacher_db),

View File

@ -16,7 +16,7 @@ logging = logger.get_logger(
from fastapi import APIRouter, HTTPException
from langchain_classic.chains import GraphCypherQAChain
from langchain_community.graphs import Neo4jGraph
from langchain_community.chat_models import ChatOpenAI
from langchain_openai import ChatOpenAI
from langchain_classic.prompts.prompt import PromptTemplate
from routers.llm.private.ollama.ollama_wrapper import OllamaWrapper
from modules.database.tools.neontology.utils import get_node_types, get_rels_by_type