mirror of
https://github.com/bec-project/bec_atlas.git
synced 2025-07-14 07:01:48 +02:00
feat(scans): added count endpoint to scans
This commit is contained in:
@ -268,6 +268,8 @@ class MongoDBDatasource:
|
|||||||
# pipeline = self.add_user_filter(user, pipeline)
|
# pipeline = self.add_user_filter(user, pipeline)
|
||||||
|
|
||||||
out = self.db[collection].aggregate(pipeline)
|
out = self.db[collection].aggregate(pipeline)
|
||||||
|
if dtype is None:
|
||||||
|
return list(out)
|
||||||
return [dtype(**x) for x in out]
|
return [dtype(**x) for x in out]
|
||||||
|
|
||||||
def add_user_filter(
|
def add_user_filter(
|
||||||
|
@ -36,6 +36,13 @@ class ScanRouter(BaseRouter):
|
|||||||
description="Update the user data of a scan",
|
description="Update the user data of a scan",
|
||||||
response_model=dict,
|
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(
|
async def scans(
|
||||||
self,
|
self,
|
||||||
@ -137,3 +144,26 @@ class ScanRouter(BaseRouter):
|
|||||||
if out is None:
|
if out is None:
|
||||||
return {"message": "Scan not found."}
|
return {"message": "Scan not found."}
|
||||||
return {"message": "Scan user data updated."}
|
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]
|
||||||
|
Reference in New Issue
Block a user