File size: 3,008 Bytes
effde1c |
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 |
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from backend.db.session import SessionLocal
from backend.db.models import Universe, Axiom, Theorem
from backend.api.auth import get_api_key
from backend.core.logging_config import get_logger
router = APIRouter()
logger = get_logger("visualization_routes")
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@router.get("/visualization/universe/{universe_id}")
def get_universe_graph(universe_id: int, db: Session = Depends(get_db), api_key: str = Depends(get_api_key)):
try:
universe = db.query(Universe).filter(Universe.id == universe_id).first()
axioms = db.query(Axiom).filter(Axiom.universe_id == universe_id).all()
theorems = db.query(Theorem).filter(Theorem.universe_id == universe_id).all()
nodes = [{"id": ax.id, "type": "axiom", "label": ax.statement} for ax in axioms] + \
[{"id": th.id, "type": "theorem", "label": th.statement} for th in theorems]
edges = []
for th in theorems:
for ax in axioms:
if ax.statement in th.proof:
edges.append({"source": ax.id, "target": th.id, "type": "proof"})
logger.info(f"Visualization graph for universe {universe_id} generated.")
return {
"universe": {"id": universe.id, "name": universe.name, "type": universe.universe_type},
"nodes": nodes,
"edges": edges
}
except Exception as e:
logger.error(f"Visualization error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/visualization/universes")
def get_all_universe_graphs(db: Session = Depends(get_db), api_key: str = Depends(get_api_key)):
try:
universes = db.query(Universe).all()
result = []
for universe in universes:
axioms = db.query(Axiom).filter(Axiom.universe_id == universe.id).all()
theorems = db.query(Theorem).filter(Theorem.universe_id == universe.id).all()
nodes = [{"id": ax.id, "type": "axiom", "label": ax.statement} for ax in axioms] + \
[{"id": th.id, "type": "theorem", "label": th.statement} for th in theorems]
edges = []
for th in theorems:
for ax in axioms:
if ax.statement in th.proof:
edges.append({"source": ax.id, "target": th.id, "type": "proof"})
result.append({
"universe": {"id": universe.id, "name": universe.name, "type": universe.universe_type},
"nodes": nodes,
"edges": edges
})
logger.info("Visualization graphs for all universes generated.")
return result
except Exception as e:
logger.error(f"Visualization error: {e}")
raise HTTPException(status_code=500, detail=str(e))
|