29 lines
1010 B
Python
29 lines
1010 B
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, desc
|
|
from app.database import get_db
|
|
from app.models.user import User
|
|
from app.models.alert import Alert
|
|
from app.utils.auth import get_current_user
|
|
from app.services.risk_engine import calculate_risk_score
|
|
|
|
router = APIRouter(prefix="/api/alerts", tags=["alerts"])
|
|
|
|
@router.get("")
|
|
async def get_alerts(user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
|
|
risk = await calculate_risk_score(user.id, db)
|
|
|
|
# Get stored alerts
|
|
alerts = (await db.execute(
|
|
select(Alert).where(Alert.user_id == user.id).order_by(desc(Alert.created_at)).limit(20)
|
|
)).scalars().all()
|
|
|
|
return {
|
|
"current_risk": risk,
|
|
"alerts": [
|
|
{"id": str(a.id), "type": a.type, "severity": a.severity,
|
|
"message": a.message, "is_read": a.is_read, "created_at": a.created_at.isoformat()}
|
|
for a in alerts
|
|
]
|
|
}
|