Skip to content

Commit

Permalink
feat: add delete operation
Browse files Browse the repository at this point in the history
  • Loading branch information
martynia committed Dec 5, 2024
1 parent bf613c8 commit bd838a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions diracx-db/src/diracx/db/os/pilot_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class PilotLogsDB(BaseOSDB):
fields = {
"PilotStamp": {"type": "keyword"},
"PilotID": {"type": "long"},
"SubmissionTime": {"type": "date"},
"LineNumber": {"type": "long"},
"Message": {"type": "text"},
"VO": {"type": "keyword"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def policy(
pilot_db: PilotLogsDB | None = None,
pilot_ids: list[int] | None = None, # or pilot stamp list ?
):
print("user_info.properties:", user_info.properties)

assert action, "action is a mandatory parameter"
assert pilot_db, "pilot_db is a mandatory parameter"

Check warning on line 44 in diracx-routers/src/diracx/routers/pilot_logging/access_policies.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/access_policies.py#L43-L44

Added lines #L43 - L44 were not covered by tests

Expand Down
47 changes: 37 additions & 10 deletions diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

import datetime

from pydantic import BaseModel
from sqlalchemy import select

from diracx.core.exceptions import InvalidQueryError
from diracx.db.sql.pilot_agents.schema import PilotAgents
from diracx.db.sql.utils import BaseSQLDB

Expand All @@ -25,6 +28,12 @@ class LogMessage(BaseModel):
vo: str


class DateRange(BaseModel):
pilot_id: int | None = None
min: str | None = None # expects a string in ISO 8601 ("%Y-%m-%dT%H:%M:%S.%f%z")
max: str | None = None # expects a string in ISO 8601 ("%Y-%m-%dT%H:%M:%S.%f%z")


@router.post("/")
async def send_message(
data: LogMessage,
Expand All @@ -35,23 +44,27 @@ async def send_message(
await check_permissions(action=ActionType.CREATE, pilot_db=pilot_logs_db)

Check warning on line 44 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L43-L44

Added lines #L43 - L44 were not covered by tests

pilot_id = 0 # need to get pilot id from pilot_stamp (via PilotAgentsDB)

Check warning on line 46 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L46

Added line #L46 was not covered by tests
# also add a timestamp to be able to select and delete logs based on pilot creation dates, even if corresponding
# pilots have been already deleted from PilotAgentsDB (so the logs can live longer than pilots).
submission_time = datetime.datetime.fromtimestamp(0, datetime.timezone.utc)
piloAgentsDB = BaseSQLDB.available_implementations("PilotAgentsDB")[0]
url = BaseSQLDB.available_urls()["PilotAgentsDB"]
db = piloAgentsDB(url)

Check warning on line 52 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L49-L52

Added lines #L49 - L52 were not covered by tests

async with db.engine_context():
async with db:
stmt = select(PilotAgents.PilotID).where(
stmt = select(PilotAgents.PilotID, PilotAgents.SubmissionTime).where(

Check warning on line 56 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L54-L56

Added lines #L54 - L56 were not covered by tests
PilotAgents.PilotStamp == data.pilot_stamp
)
pilot_id = (await db.conn.execute(stmt)).scalar_one()
pilot_id, submission_time = (await db.conn.execute(stmt)).one()

Check warning on line 59 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L59

Added line #L59 was not covered by tests

docs = []
for line in data.lines:
docs.append(

Check warning on line 63 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L61-L63

Added lines #L61 - L63 were not covered by tests
{
"PilotStamp": data.pilot_stamp,
"PilotID": pilot_id,
"SubmissionTime": submission_time,
"VO": data.vo,
"LineNumber": line.line_no,
"Message": line.line,
Expand All @@ -72,21 +85,35 @@ async def get_logs(

result = await db.search(

Check warning on line 86 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L86

Added line #L86 was not covered by tests
["Message"],
[{"parameter": "PilotID", "operator": "eq"} | {"value": pilot_id}],
[{"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

Check warning on line 93 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L91-L93

Added lines #L91 - L93 were not covered by tests


@router.delete("/delete")
async def delete_by_pilot_id(
pilot_id: int,
@router.delete("/logs")
async def delete(
data: DateRange,
db: PilotLogsDB,
check_permissions: CheckPilotLogsPolicyCallable,
):
logger.warning(f"Deleting logs for pilot ID '{pilot_id}'")
) -> str:
"""Delete either logs for a specific PilotID or a creation date range."""
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"
if data.pilot_id:
await db.delete(

Check warning on line 105 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L103-L105

Added lines #L103 - L105 were not covered by tests
[{"parameter": "PilotID", "operator": "eq", "value": data.pilot_id}]
)
return f"Logs for pilot ID '{data.pilot_id}' successfully deleted"
if data.min and not data.max:
logger.warning(f"Deleting logs for pilots with submission data >='{data.min}'")
await db.delete(

Check warning on line 111 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L108-L111

Added lines #L108 - L111 were not covered by tests
[{"parameter": "SubmissionTime", "operator": "gt", "value": data.min}]
)
return f"Logs for for pilots with submission data >='{data.min}' successfully deleted"
if data.min and data.max:
raise InvalidQueryError(

Check warning on line 116 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L114-L116

Added lines #L114 - L116 were not covered by tests
"This query requires a range operater definition in DiracX"
)
return "no-op"

Check warning on line 119 in diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py

View check run for this annotation

Codecov / codecov/patch

diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py#L119

Added line #L119 was not covered by tests

0 comments on commit bd838a7

Please sign in to comment.