Portal FastAPI + 5 endpoints REST para Bootcamp Agentic AI con watsonx Orchestrate (FactorIT). Single container, Coolify-ready. - Landing brandeado FIT con formulario de registro (honeypot anti-bot) - Tokens itsdangerous para descargas (24h expiry) - 5 endpoints API: historical/available procedures, member-insights, schedule, generate-report (Jinja2 + Plotly) - SQLite con upsert-on-email para leads + log de descargas - Admin endpoints (HTTP Basic): leads.json, leads.csv, stats - 23 tests pytest pasando - Dockerfile listo para Coolify con volúmenes persistentes (/app/leads.db, /app/app/data/reports_output, /app/material) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
import pandas as pd
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
router = APIRouter(prefix="/api", tags=["benefits"])
|
|
|
|
_DATA_DIR = Path(__file__).parent / "data"
|
|
|
|
with (_DATA_DIR / "member_insights.json").open() as f:
|
|
_MEMBER_INSIGHTS = json.load(f)
|
|
|
|
_SCHEDULE_TEXT = (_DATA_DIR / "schedule_response.txt").read_text(encoding="utf-8").strip()
|
|
|
|
_HISTORICAL = pd.read_csv(_DATA_DIR / "historical_procedures.csv")
|
|
_AVAILABLE = pd.read_csv(_DATA_DIR / "available_procedures.csv")
|
|
|
|
|
|
_OPS = {
|
|
"equals": lambda s, v: s == v,
|
|
"ne": lambda s, v: s != v,
|
|
"contains": lambda s, v: s.astype(str).str.contains(str(v), case=False, na=False),
|
|
"gt": lambda s, v: s > v,
|
|
"lt": lambda s, v: s < v,
|
|
"ge": lambda s, v: s >= v,
|
|
"le": lambda s, v: s <= v,
|
|
}
|
|
|
|
|
|
def _apply_filters_and_group(df: pd.DataFrame, filters_raw, group_by_raw) -> list[dict]:
|
|
try:
|
|
filters = json.loads(filters_raw) if isinstance(filters_raw, str) else (filters_raw or [])
|
|
group_by = json.loads(group_by_raw) if isinstance(group_by_raw, str) else (group_by_raw or [])
|
|
except json.JSONDecodeError as exc:
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON in filters or group_by: {exc}")
|
|
|
|
result = df.copy()
|
|
for f in filters:
|
|
col, op, val = f.get("column"), f.get("operator"), f.get("value")
|
|
if col not in result.columns:
|
|
raise HTTPException(status_code=400, detail=f"Unknown column: {col}")
|
|
if op not in _OPS:
|
|
raise HTTPException(status_code=400, detail=f"Unsupported operator: {op}")
|
|
result = result[_OPS[op](result[col], val)]
|
|
|
|
if group_by:
|
|
missing = [c for c in group_by if c not in result.columns]
|
|
if missing:
|
|
raise HTTPException(status_code=400, detail=f"Unknown group_by columns: {missing}")
|
|
numeric_cols = result.select_dtypes(include="number").columns.tolist()
|
|
result = result.groupby(group_by)[numeric_cols].mean().reset_index()
|
|
|
|
return result.to_dict(orient="records")
|
|
|
|
|
|
@router.get("/member-insights")
|
|
def member_insights():
|
|
return _MEMBER_INSIGHTS
|
|
|
|
|
|
@router.get("/schedule")
|
|
def schedule():
|
|
return {"result": _SCHEDULE_TEXT}
|
|
|
|
|
|
@router.post("/historical-procedures")
|
|
def historical_procedures(payload: dict):
|
|
rows = _apply_filters_and_group(
|
|
_HISTORICAL,
|
|
payload.get("filters", "[]"),
|
|
payload.get("group_by", "[]"),
|
|
)
|
|
return {"result": rows}
|
|
|
|
|
|
@router.post("/available-procedures")
|
|
def available_procedures(payload: dict):
|
|
rows = _apply_filters_and_group(
|
|
_AVAILABLE,
|
|
payload.get("filters", "[]"),
|
|
payload.get("group_by", "[]"),
|
|
)
|
|
return {"result": rows}
|