diff --git a/services/alert_processor/app/api/alerts.py b/services/alert_processor/app/api/alerts.py index 6bb2fd09..817c789a 100644 --- a/services/alert_processor/app/api/alerts.py +++ b/services/alert_processor/app/api/alerts.py @@ -3,15 +3,13 @@ Alerts API endpoints for dashboard and alert management """ -from fastapi import APIRouter, Depends, HTTPException, Query, Path -from sqlalchemy.ext.asyncio import AsyncSession +from fastapi import APIRouter, HTTPException, Query, Path from typing import List, Optional from pydantic import BaseModel, Field from uuid import UUID from datetime import datetime import structlog -from shared.database.base import get_db from app.repositories.alerts_repository import AlertsRepository from app.models.alerts import AlertSeverity, AlertStatus @@ -76,8 +74,7 @@ class AlertsListResponse(BaseModel): description="Get summary of alerts by severity and status for dashboard health indicator" ) async def get_alerts_summary( - tenant_id: UUID = Path(..., description="Tenant ID"), - db: AsyncSession = Depends(get_db) + tenant_id: UUID = Path(..., description="Tenant ID") ) -> AlertsSummaryResponse: """ Get alerts summary for dashboard @@ -85,10 +82,17 @@ async def get_alerts_summary( Returns counts of alerts grouped by severity and status. Critical count maps to URGENT severity for dashboard compatibility. """ + from app.config import AlertProcessorConfig + from shared.database.base import create_database_manager + try: - repo = AlertsRepository(db) - summary = await repo.get_alerts_summary(tenant_id) - return AlertsSummaryResponse(**summary) + config = AlertProcessorConfig() + db_manager = create_database_manager(config.DATABASE_URL, "alert-processor") + + async with db_manager.get_session() as session: + repo = AlertsRepository(session) + summary = await repo.get_alerts_summary(tenant_id) + return AlertsSummaryResponse(**summary) except Exception as e: logger.error("Error getting alerts summary", error=str(e), tenant_id=str(tenant_id)) @@ -107,8 +111,7 @@ async def get_alerts( status: Optional[str] = Query(None, description="Filter by status: active, resolved, acknowledged, ignored"), resolved: Optional[bool] = Query(None, description="Filter by resolved status: true=resolved only, false=unresolved only"), limit: int = Query(100, ge=1, le=1000, description="Maximum number of results"), - offset: int = Query(0, ge=0, description="Pagination offset"), - db: AsyncSession = Depends(get_db) + offset: int = Query(0, ge=0, description="Pagination offset") ) -> AlertsListResponse: """ Get filtered list of alerts @@ -119,6 +122,9 @@ async def get_alerts( - resolved: boolean filter for resolved status - pagination: limit and offset """ + from app.config import AlertProcessorConfig + from shared.database.base import create_database_manager + try: # Validate severity enum if severity and severity not in [s.value for s in AlertSeverity]: @@ -134,43 +140,47 @@ async def get_alerts( detail=f"Invalid status. Must be one of: {[s.value for s in AlertStatus]}" ) - repo = AlertsRepository(db) - alerts = await repo.get_alerts( - tenant_id=tenant_id, - severity=severity, - status=status, - resolved=resolved, - limit=limit, - offset=offset - ) + config = AlertProcessorConfig() + db_manager = create_database_manager(config.DATABASE_URL, "alert-processor") - # Convert to response models - alert_responses = [ - AlertResponse( - id=str(alert.id), - tenant_id=str(alert.tenant_id), - item_type=alert.item_type, - alert_type=alert.alert_type, - severity=alert.severity, - status=alert.status, - service=alert.service, - title=alert.title, - message=alert.message, - actions=alert.actions, - alert_metadata=alert.alert_metadata, - created_at=alert.created_at, - updated_at=alert.updated_at, - resolved_at=alert.resolved_at + async with db_manager.get_session() as session: + repo = AlertsRepository(session) + alerts = await repo.get_alerts( + tenant_id=tenant_id, + severity=severity, + status=status, + resolved=resolved, + limit=limit, + offset=offset ) - for alert in alerts - ] - return AlertsListResponse( - alerts=alert_responses, - total=len(alert_responses), # In a real implementation, you'd query the total count separately - limit=limit, - offset=offset - ) + # Convert to response models + alert_responses = [ + AlertResponse( + id=str(alert.id), + tenant_id=str(alert.tenant_id), + item_type=alert.item_type, + alert_type=alert.alert_type, + severity=alert.severity, + status=alert.status, + service=alert.service, + title=alert.title, + message=alert.message, + actions=alert.actions, + alert_metadata=alert.alert_metadata, + created_at=alert.created_at, + updated_at=alert.updated_at, + resolved_at=alert.resolved_at + ) + for alert in alerts + ] + + return AlertsListResponse( + alerts=alert_responses, + total=len(alert_responses), # In a real implementation, you'd query the total count separately + limit=limit, + offset=offset + ) except HTTPException: raise @@ -187,33 +197,39 @@ async def get_alerts( ) async def get_alert( tenant_id: UUID = Path(..., description="Tenant ID"), - alert_id: UUID = Path(..., description="Alert ID"), - db: AsyncSession = Depends(get_db) + alert_id: UUID = Path(..., description="Alert ID") ) -> AlertResponse: """Get a specific alert by ID""" + from app.config import AlertProcessorConfig + from shared.database.base import create_database_manager + try: - repo = AlertsRepository(db) - alert = await repo.get_alert_by_id(tenant_id, alert_id) + config = AlertProcessorConfig() + db_manager = create_database_manager(config.DATABASE_URL, "alert-processor") - if not alert: - raise HTTPException(status_code=404, detail="Alert not found") + async with db_manager.get_session() as session: + repo = AlertsRepository(session) + alert = await repo.get_alert_by_id(tenant_id, alert_id) - return AlertResponse( - id=str(alert.id), - tenant_id=str(alert.tenant_id), - item_type=alert.item_type, - alert_type=alert.alert_type, - severity=alert.severity, - status=alert.status, - service=alert.service, - title=alert.title, - message=alert.message, - actions=alert.actions, - alert_metadata=alert.alert_metadata, - created_at=alert.created_at, - updated_at=alert.updated_at, - resolved_at=alert.resolved_at - ) + if not alert: + raise HTTPException(status_code=404, detail="Alert not found") + + return AlertResponse( + id=str(alert.id), + tenant_id=str(alert.tenant_id), + item_type=alert.item_type, + alert_type=alert.alert_type, + severity=alert.severity, + status=alert.status, + service=alert.service, + title=alert.title, + message=alert.message, + actions=alert.actions, + alert_metadata=alert.alert_metadata, + created_at=alert.created_at, + updated_at=alert.updated_at, + resolved_at=alert.resolved_at + ) except HTTPException: raise