diff --git a/diracx-db/src/diracx/db/os/utils.py b/diracx-db/src/diracx/db/os/utils.py index 91ef65bf..439c3689 100644 --- a/diracx-db/src/diracx/db/os/utils.py +++ b/diracx-db/src/diracx/db/os/utils.py @@ -239,6 +239,15 @@ async def search( return hits + async def delete(self, query: list[dict[str, Any]]) -> None: + + # Delete multiple documents by query. + + body = {} + if query: + body["query"] = apply_search_filters(self.fields, query) + await self.client.delete_by_query(body=body, index=f"{self.index_prefix}*") + def require_type(operator, field_name, field_type, allowed_types): if field_type not in allowed_types: diff --git a/diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py b/diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py index b8eec760..55c64050 100644 --- a/diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py +++ b/diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py @@ -67,7 +67,7 @@ async def get_logs( db: PilotLogsDB, check_permissions: CheckPilotLogsPolicyCallable, ): - logger.warning(f"Retrieving message for pilot ID '{pilot_id}'") + logger.warning(f"Retrieving logs for pilot ID '{pilot_id}'") await check_permissions(action=ActionType.QUERY, pilot_db=db) result = await db.search( @@ -75,4 +75,18 @@ async def get_logs( [{"parameter": "PilotID", "operator": "eq"} | {"value": pilot_id}], [{"parameter": "LineNumber", "direction": "asc"}], ) + if not result: + return [f"No logs for pilot ID = {pilot_id}"] return result + + +@router.delete("/delete") +async def delete_by_pilot_id( + pilot_id: int, + db: PilotLogsDB, + check_permissions: CheckPilotLogsPolicyCallable, +): + logger.warning(f"Deleting logs for pilot ID '{pilot_id}'") + await check_permissions(action=ActionType.DELETE, pilot_db=db) + await db.delete([{"parameter": "PilotID", "operator": "eq"} | {"value": pilot_id}]) + return f"Logs for pilot ID '{pilot_id}' successfully deleted"