159 lines
4.1 KiB
Python
159 lines
4.1 KiB
Python
"""
|
|
Routes API pour les alertes.
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.dependencies import get_db, verify_api_key
|
|
from app.crud.alert import AlertRepository
|
|
from app.services import ws_manager
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class AlertCreate(BaseModel):
|
|
category: str = "general"
|
|
level: Optional[str] = "info"
|
|
title: Optional[str] = None
|
|
message: str
|
|
source: Optional[str] = None
|
|
|
|
|
|
@router.post("")
|
|
async def create_alert(
|
|
alert_data: AlertCreate,
|
|
api_key_valid: bool = Depends(verify_api_key),
|
|
db_session: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Crée une nouvelle alerte."""
|
|
repo = AlertRepository(db_session)
|
|
alert = await repo.create(
|
|
category=alert_data.category,
|
|
level=alert_data.level,
|
|
title=alert_data.title,
|
|
message=alert_data.message,
|
|
source=alert_data.source,
|
|
)
|
|
await db_session.commit()
|
|
|
|
await ws_manager.broadcast({
|
|
"type": "alert_created",
|
|
"data": {
|
|
"id": alert.id,
|
|
"title": alert.title,
|
|
"message": alert.message,
|
|
"level": alert.level,
|
|
}
|
|
})
|
|
|
|
return {
|
|
"id": alert.id,
|
|
"title": alert.title,
|
|
"message": alert.message,
|
|
"level": alert.level,
|
|
"category": alert.category,
|
|
"created_at": alert.created_at,
|
|
}
|
|
|
|
|
|
@router.get("")
|
|
async def get_alerts(
|
|
limit: int = 50,
|
|
offset: int = 0,
|
|
unread_only: bool = False,
|
|
api_key_valid: bool = Depends(verify_api_key),
|
|
db_session: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Récupère les alertes avec pagination."""
|
|
repo = AlertRepository(db_session)
|
|
alerts = await repo.list(limit=limit, offset=offset, unread_only=unread_only)
|
|
|
|
return {
|
|
"alerts": [
|
|
{
|
|
"id": a.id,
|
|
"title": a.title,
|
|
"message": a.message,
|
|
"level": a.level,
|
|
"source": a.source,
|
|
"category": a.category,
|
|
"read": a.read_at is not None,
|
|
"read_at": a.read_at,
|
|
"created_at": a.created_at,
|
|
}
|
|
for a in alerts
|
|
],
|
|
"count": len(alerts)
|
|
}
|
|
|
|
|
|
@router.get("/unread-count")
|
|
async def get_unread_count(
|
|
api_key_valid: bool = Depends(verify_api_key),
|
|
db_session: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Récupère le nombre d'alertes non lues."""
|
|
repo = AlertRepository(db_session)
|
|
count = await repo.count_unread()
|
|
return {"unread": count}
|
|
|
|
|
|
@router.post("/{alert_id}/read")
|
|
async def mark_as_read(
|
|
alert_id: str,
|
|
api_key_valid: bool = Depends(verify_api_key),
|
|
db_session: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Marque une alerte comme lue."""
|
|
repo = AlertRepository(db_session)
|
|
alert = await repo.get(alert_id)
|
|
|
|
if not alert:
|
|
raise HTTPException(status_code=404, detail="Alerte non trouvée")
|
|
|
|
await repo.mark_as_read(alert_id)
|
|
await db_session.commit()
|
|
|
|
return {"message": "Alerte marquée comme lue", "id": alert_id}
|
|
|
|
|
|
@router.post("/mark-all-read")
|
|
async def mark_all_as_read(
|
|
api_key_valid: bool = Depends(verify_api_key),
|
|
db_session: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Marque toutes les alertes comme lues."""
|
|
repo = AlertRepository(db_session)
|
|
count = await repo.mark_all_as_read()
|
|
await db_session.commit()
|
|
|
|
await ws_manager.broadcast({
|
|
"type": "alerts_cleared",
|
|
"data": {"count": count}
|
|
})
|
|
|
|
return {"message": f"{count} alerte(s) marquée(s) comme lue(s)"}
|
|
|
|
|
|
@router.delete("/{alert_id}")
|
|
async def delete_alert(
|
|
alert_id: str,
|
|
api_key_valid: bool = Depends(verify_api_key),
|
|
db_session: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Supprime une alerte."""
|
|
repo = AlertRepository(db_session)
|
|
alert = await repo.get(alert_id)
|
|
|
|
if not alert:
|
|
raise HTTPException(status_code=404, detail="Alerte non trouvée")
|
|
|
|
await repo.delete(alert_id)
|
|
await db_session.commit()
|
|
|
|
return {"message": "Alerte supprimée", "id": alert_id}
|