Skip to content

Commit

Permalink
feat: Add pagination to list_alerts_by_asset_name_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwalton committed Aug 19, 2024
1 parent cd59851 commit 60c43e3
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 @@ -396,9 +396,13 @@ async def list_alerts_by_assigned_to_endpoint(assigned_to: str, db: AsyncSession


@incidents_db_operations_router.get("/alerts/asset/{asset_name}", response_model=AlertOutResponse)
async def list_alerts_by_asset_name_endpoint(asset_name: str, db: AsyncSession = Depends(get_db)):
return AlertOutResponse(alerts=await list_alerts_by_asset_name(asset_name, db), success=True, message="Alerts retrieved successfully")

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

@incidents_db_operations_router.get("/cases", response_model=CaseOutResponse)
async def list_cases_endpoint(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 @@ -843,7 +843,8 @@ async def list_alert_by_status(status: str, db: AsyncSession, page: int = 1, pag
return alerts_out


async def list_alerts_by_asset_name(asset_name: str, db: AsyncSession) -> List[AlertOut]:
async def list_alerts_by_asset_name(asset_name: str, db: AsyncSession, page: int = 1, page_size: int = 25) -> List[AlertOut]:
offset = (page - 1) * page_size
result = await db.execute(
select(Alert)
.join(Asset)
Expand All @@ -853,7 +854,9 @@ async def list_alerts_by_asset_name(asset_name: str, db: AsyncSession) -> List[A
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 60c43e3

Please sign in to comment.