42 lines
1.7 KiB
Python
42 lines
1.7 KiB
Python
"""Analysis API: get flow graph and summary for a case."""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.database import get_db
|
|
from app.models import Case, Transaction
|
|
from app.schemas import TransactionResponse, AnalysisSummaryResponse, FlowGraphResponse
|
|
from app.services.analyzer import build_flow_graph
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/{case_id}/transactions", response_model=list[TransactionResponse])
|
|
async def list_transactions(case_id: int, db: AsyncSession = Depends(get_db)):
|
|
r = await db.execute(select(Case).where(Case.id == case_id))
|
|
if not r.scalar_one_or_none():
|
|
raise HTTPException(status_code=404, detail="Case not found")
|
|
r = await db.execute(
|
|
select(Transaction).where(Transaction.case_id == case_id).order_by(Transaction.transaction_time, Transaction.id)
|
|
)
|
|
txns = r.scalars().all()
|
|
return [TransactionResponse.model_validate(t) for t in txns]
|
|
|
|
|
|
@router.get("/{case_id}/analysis")
|
|
async def get_analysis(case_id: int, db: AsyncSession = Depends(get_db)):
|
|
r = await db.execute(select(Case).where(Case.id == case_id))
|
|
if not r.scalar_one_or_none():
|
|
raise HTTPException(status_code=404, detail="Case not found")
|
|
r = await db.execute(select(Transaction).where(Transaction.case_id == case_id))
|
|
txns = r.scalars().all()
|
|
items = [TransactionResponse.model_validate(t) for t in txns]
|
|
graph, summary = build_flow_graph(items)
|
|
return {"summary": summary.model_dump(), "graph": graph.model_dump()}
|
|
|
|
|
|
@router.post("/{case_id}/analysis")
|
|
async def run_analysis(case_id: int, db: AsyncSession = Depends(get_db)):
|
|
return await get_analysis(case_id, db)
|