-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable remote pilot logging system
- Loading branch information
Showing
9 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
from __future__ import annotations | ||
|
||
__all__ = ("JobParametersDB",) | ||
__all__ = ( | ||
"JobParametersDB", | ||
"PilotLogsDB", | ||
) | ||
|
||
from .job_parameters import JobParametersDB | ||
from .pilot_logs import PilotLogsDB |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from __future__ import annotations | ||
|
||
from diracx.db.os.utils import BaseOSDB | ||
|
||
|
||
class PilotLogsDB(BaseOSDB): | ||
fields = { | ||
"PilotStamp": {"type": "keyword"}, | ||
"LineNumber": {"type": "long"}, | ||
"Message": {"type": "text"}, | ||
"VO": {"type": "keyword"}, | ||
"timestamp": {"type": "date"}, | ||
} | ||
index_prefix = "pilot_logs" | ||
|
||
def index_name(self, doc_id: int) -> str: | ||
# TODO decide how to define the index name | ||
return f"{self.index_prefix}_0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) |
58 changes: 58 additions & 0 deletions
58
diracx-routers/src/diracx/routers/pilot_logging/access_policies.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import annotations | ||
|
||
from enum import StrEnum, auto | ||
from typing import Annotated, Callable | ||
|
||
from fastapi import Depends, HTTPException, status | ||
|
||
from diracx.core.properties import GENERIC_PILOT, OPERATOR, PILOT, SERVICE_ADMINISTRATOR | ||
from diracx.db.os import PilotLogsDB | ||
from diracx.routers.access_policies import BaseAccessPolicy | ||
|
||
from ..utils.users import AuthorizedUserInfo | ||
|
||
|
||
class ActionType(StrEnum): | ||
#: Create/update pilot log records | ||
CREATE = auto() | ||
#: download pilot logs | ||
READ = auto() | ||
#: delete pilot logs | ||
DELETE = auto() | ||
#: Search | ||
QUERY = auto() | ||
|
||
|
||
class PilotLogsAccessPolicy(BaseAccessPolicy): | ||
"""ToDo | ||
---- | ||
""" | ||
|
||
@staticmethod | ||
async def policy( | ||
policy_name: str, | ||
user_info: AuthorizedUserInfo, | ||
/, | ||
*, | ||
action: ActionType | None = None, | ||
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" | ||
|
||
if GENERIC_PILOT in user_info.properties: | ||
return | ||
if PILOT in user_info.properties: | ||
return | ||
if SERVICE_ADMINISTRATOR in user_info.properties: | ||
return | ||
if OPERATOR in user_info.properties: | ||
return | ||
|
||
raise HTTPException(status.HTTP_403_FORBIDDEN, detail=user_info.properties) | ||
|
||
|
||
CheckPilotLogsPolicyCallable = Annotated[Callable, Depends(PilotLogsAccessPolicy.check)] |
46 changes: 46 additions & 0 deletions
46
diracx-routers/src/diracx/routers/pilot_logging/remote_logger.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from __future__ import annotations | ||
|
||
from pydantic import BaseModel | ||
|
||
from ..dependencies import PilotLogsDB | ||
from ..fastapi_classes import DiracxRouter | ||
from ..pilot_logging import logger | ||
from .access_policies import ActionType, CheckPilotLogsPolicyCallable | ||
|
||
router = DiracxRouter() | ||
|
||
|
||
class LogLine(BaseModel): | ||
line_no: int | ||
line: str | ||
|
||
|
||
class LogMessage(BaseModel): | ||
pilot_stamp: str | ||
lines: list[LogLine] | ||
vo: str | ||
|
||
|
||
@router.post("/") | ||
async def send_message( | ||
data: LogMessage, | ||
pilot_logs_db: PilotLogsDB, | ||
check_permissions: CheckPilotLogsPolicyCallable, | ||
): | ||
logger.warning(f"Message received '{data}'") | ||
await check_permissions(action=ActionType.CREATE, pilot_db=pilot_logs_db) | ||
|
||
pilot_id = 1234 # need to get pilot id from pilot_stamp (via pilot DB) | ||
|
||
docs = [] | ||
for line in data.lines: | ||
docs.append( | ||
{ | ||
"PilotStamp": data.pilot_stamp, | ||
"VO": data.vo, | ||
"LineNumber": line.line_no, | ||
"Message": line.line, | ||
} | ||
) | ||
await pilot_logs_db.bulk_insert(pilot_logs_db.index_name(pilot_id), docs) | ||
return data |