Skip to content

Commit

Permalink
feat: Add pagination to list_alerts_by_status_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwalton committed Aug 19, 2024
1 parent 318d963 commit cd59851
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 @@ -380,11 +380,15 @@ async def delete_alert_endpoint(alert_id: int, db: AsyncSession = Depends(get_db


@incidents_db_operations_router.get("/alerts/status/{status}", response_model=AlertOutResponse)
async def list_alerts_by_status_endpoint(status: AlertStatus, db: AsyncSession = Depends(get_db)):
async def list_alerts_by_status_endpoint(
status: AlertStatus,
page: int = Query(1, ge=1),
page_size: int = Query(25, ge=1),
db: AsyncSession = Depends(get_db)
):
if status not in AlertStatus:
raise HTTPException(status_code=400, detail="Invalid status")
return AlertOutResponse(alerts=await list_alert_by_status(status.value, db), success=True, message="Alerts retrieved successfully")

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)):
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 @@ -805,7 +805,8 @@ async def list_alerts_by_tag(tag: str, db: AsyncSession) -> List[AlertOut]:
return alerts_out


async def list_alert_by_status(status: str, db: AsyncSession) -> List[AlertOut]:
async def list_alert_by_status(status: 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.status == status)
Expand All @@ -814,7 +815,9 @@ async def list_alert_by_status(status: str, db: AsyncSession) -> List[AlertOut]:
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 cd59851

Please sign in to comment.