Skip to content

Commit

Permalink
feat: Add pagination to list_alerts_by_assigned_to_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwalton committed Aug 19, 2024
1 parent 60c43e3 commit 5691650
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 7 additions & 3 deletions backend/app/incidents/routes/db_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,13 @@ async def list_alerts_by_status_endpoint(
return AlertOutResponse(alerts=await list_alert_by_status(status.value, db, page=page, page_size=page_size), success=True, message="Alerts retrieved successfully")

@incidents_db_operations_router.get("/alerts/assigned-to/{assigned_to}", response_model=AlertOutResponse)
async def list_alerts_by_assigned_to_endpoint(assigned_to: str, db: AsyncSession = Depends(get_db)):
return AlertOutResponse(alerts=await list_alert_by_assigned_to(assigned_to, db), success=True, message="Alerts retrieved successfully")

async def list_alerts_by_assigned_to_endpoint(
assigned_to: str,
page: int = Query(1, ge=1),
page_size: int = Query(25, ge=1),
db: AsyncSession = Depends(get_db)
):
return AlertOutResponse(alerts=await list_alert_by_assigned_to(assigned_to, db, page=page, page_size=page_size), success=True, message="Alerts retrieved successfully")

@incidents_db_operations_router.get("/alerts/asset/{asset_name}", response_model=AlertOutResponse)
async def list_alerts_by_asset_name_endpoint(
Expand Down
7 changes: 5 additions & 2 deletions backend/app/incidents/services/db_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,8 @@ async def list_alerts_by_asset_name(asset_name: str, db: AsyncSession, page: int
return alerts_out


async def list_alert_by_assigned_to(assigned_to: str, db: AsyncSession) -> List[AlertOut]:
async def list_alert_by_assigned_to(assigned_to: str, db: AsyncSession, page: int = 1, page_size: int = 25) -> List[AlertOut]:
offset = (page - 1) * page_size
result = await db.execute(
select(Alert)
.where(Alert.assigned_to == assigned_to)
Expand All @@ -891,7 +892,9 @@ async def list_alert_by_assigned_to(assigned_to: str, db: AsyncSession) -> List[
selectinload(Alert.assets),
selectinload(Alert.cases),
selectinload(Alert.tags).selectinload(AlertToTag.tag),
),
)
.offset(offset)
.limit(page_size)
)
alerts = result.scalars().all()
alerts_out = []
Expand Down

0 comments on commit 5691650

Please sign in to comment.