feat(scans): added count endpoint to scans

This commit is contained in:
2025-01-24 15:48:22 +01:00
parent a0924b3360
commit 6a4557c0c4
2 changed files with 32 additions and 0 deletions

View File

@ -268,6 +268,8 @@ class MongoDBDatasource:
# pipeline = self.add_user_filter(user, pipeline)
out = self.db[collection].aggregate(pipeline)
if dtype is None:
return list(out)
return [dtype(**x) for x in out]
def add_user_filter(

View File

@ -36,6 +36,13 @@ class ScanRouter(BaseRouter):
description="Update the user data of a scan",
response_model=dict,
)
self.router.add_api_route(
"/scans/count",
self.count_scans,
methods=["GET"],
description="Count the number of scans",
response_model=dict,
)
async def scans(
self,
@ -137,3 +144,26 @@ class ScanRouter(BaseRouter):
if out is None:
return {"message": "Scan not found."}
return {"message": "Scan user data updated."}
async def count_scans(
self, filter: str | None = None, current_user: UserInfo = Depends(get_current_user)
) -> int:
"""
Count the number of scans.
Args:
filter (str): JSON filter for the query, e.g. '{"name": "test"}'
current_user (UserInfo): The current user
Returns:
int: The number of scans
"""
pipeline = []
if filter:
filter = json.loads(filter)
pipeline.append({"$match": filter})
pipeline.append({"$count": "count"})
out = self.db.aggregate("scans", pipeline=pipeline, dtype=None, user=current_user)
if out:
return out[0]