From 69e88b3b353f1a2ab7a78259b480e8afbd87669c Mon Sep 17 00:00:00 2001 From: TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> Date: Wed, 1 Apr 2026 17:46:47 +0900 Subject: fix: snapshot delay, env fields, alembic creds, API healthcheck and error handling --- services/api/src/trading_api/routers/portfolio.py | 64 ++++++++++++++--------- 1 file changed, 38 insertions(+), 26 deletions(-) (limited to 'services/api/src/trading_api/routers/portfolio.py') diff --git a/services/api/src/trading_api/routers/portfolio.py b/services/api/src/trading_api/routers/portfolio.py index 3b30e1d..d76d85d 100644 --- a/services/api/src/trading_api/routers/portfolio.py +++ b/services/api/src/trading_api/routers/portfolio.py @@ -1,42 +1,54 @@ """Portfolio endpoints.""" -from fastapi import APIRouter, Request +import logging + +from fastapi import APIRouter, HTTPException, Request from shared.sa_models import PositionRow from sqlalchemy import select +logger = logging.getLogger(__name__) + router = APIRouter() @router.get("/positions") async def get_positions(request: Request): """Get all current positions.""" - db = request.app.state.db - async with db.get_session() as session: - result = await session.execute(select(PositionRow)) - rows = result.scalars().all() - return [ - { - "symbol": r.symbol, - "quantity": float(r.quantity), - "avg_entry_price": float(r.avg_entry_price), - "current_price": float(r.current_price), - "unrealized_pnl": float(r.quantity * (r.current_price - r.avg_entry_price)), - } - for r in rows - ] + try: + db = request.app.state.db + async with db.get_session() as session: + result = await session.execute(select(PositionRow)) + rows = result.scalars().all() + return [ + { + "symbol": r.symbol, + "quantity": float(r.quantity), + "avg_entry_price": float(r.avg_entry_price), + "current_price": float(r.current_price), + "unrealized_pnl": float(r.quantity * (r.current_price - r.avg_entry_price)), + } + for r in rows + ] + except Exception as exc: + logger.error("Failed to get positions: %s", exc) + raise HTTPException(status_code=500, detail="Failed to retrieve positions") @router.get("/snapshots") async def get_snapshots(request: Request, days: int = 30): """Get portfolio snapshots for the last N days.""" - db = request.app.state.db - snapshots = await db.get_portfolio_snapshots(days=days) - return [ - { - "total_value": float(s["total_value"]), - "realized_pnl": float(s["realized_pnl"]), - "unrealized_pnl": float(s["unrealized_pnl"]), - "snapshot_at": s["snapshot_at"].isoformat(), - } - for s in snapshots - ] + try: + db = request.app.state.db + snapshots = await db.get_portfolio_snapshots(days=days) + return [ + { + "total_value": float(s["total_value"]), + "realized_pnl": float(s["realized_pnl"]), + "unrealized_pnl": float(s["unrealized_pnl"]), + "snapshot_at": s["snapshot_at"].isoformat(), + } + for s in snapshots + ] + except Exception as exc: + logger.error("Failed to get snapshots: %s", exc) + raise HTTPException(status_code=500, detail="Failed to retrieve snapshots") -- cgit v1.2.3