33import logging
44
55from fastapi import HTTPException , status
6- from pydantic import BaseModel
76
8- from diracx .core .models import ScalarSearchOperator , ScalarSearchSpec
7+ from diracx .core .models import LogMessage
8+ from diracx .logic .pilots .logging import get_logs as get_logs_bl
9+ from diracx .logic .pilots .logging import send_message as send_message_bl
910
1011from ..access_policies import open_access
1112from ..dependencies import PilotAgentsDB , PilotLogsDB
1617router = DiracxRouter ()
1718
1819
19- class LogLine (BaseModel ):
20- line_no : int
21- line : str
22-
23-
24- class LogMessage (BaseModel ):
25- pilot_stamp : str
26- lines : list [LogLine ]
27- vo : str
28-
29-
30- class DateRange (BaseModel ):
31- min : str | None = None # expects a string in ISO 8601 ("%Y-%m-%dT%H:%M:%S.%f%z")
32- max : str | None = None # expects a string in ISO 8601 ("%Y-%m-%dT%H:%M:%S.%f%z")
33-
34-
3520@open_access
3621@router .post ("/" )
3722async def send_message (
@@ -41,55 +26,11 @@ async def send_message(
4126 # user_info: Annotated[AuthorizedUserInfo, Depends(verify_dirac_access_token)],
4227) -> int :
4328
44- # expecting exactly one row:
45- search_params = ScalarSearchSpec (
46- parameter = "PilotStamp" ,
47- operator = ScalarSearchOperator .EQUAL ,
48- value = data .pilot_stamp ,
49- )
50-
51- total , result = await pilot_agents_db .search (
52- ["PilotID" , "VO" , "SubmissionTime" ], [search_params ], []
53- )
54- if total != 1 :
55- logger .error (
56- "Cannot determine PilotID for requested PilotStamp: %r, (%d candidates)" ,
57- data .pilot_stamp ,
58- total ,
59- )
60- raise HTTPException (
61- status .HTTP_400_BAD_REQUEST , detail = f"Number of rows !=1: { total } "
62- )
63- pilot_id , vo , submission_time = (
64- result [0 ]["PilotID" ],
65- result [0 ]["VO" ],
66- result [0 ]["SubmissionTime" ],
67- )
68-
69- # await check_permissions(action=ActionType.CREATE, pilot_agent_db, pilot_id),
70-
71- docs = []
72- for line in data .lines :
73- docs .append (
74- {
75- "PilotStamp" : data .pilot_stamp ,
76- "PilotID" : pilot_id ,
77- "SubmissionTime" : submission_time ,
78- "VO" : vo ,
79- "LineNumber" : line .line_no ,
80- "Message" : line .line ,
81- }
82- )
83- await pilot_logs_db .bulk_insert (pilot_logs_db .index_name (pilot_id ), docs )
84- """
85- search_params = [{"parameter": "PilotID", "operator": "eq", "value": pilot_id}]
86-
87- result = await pilot_logs_db.search(
88- ["Message"],
89- search_params,
90- [{"parameter": "LineNumber", "direction": "asc"}],
91- )
92- """
29+ # await check_permissions(action=ActionType.CREATE, pilot_agent_db, pilot_id)
30+ try :
31+ pilot_id = await send_message_bl (data , pilot_logs_db , pilot_agents_db )
32+ except Exception as exc :
33+ raise HTTPException (status .HTTP_400_BAD_REQUEST , detail = str (exc )) from exc
9334 return pilot_id
9435
9536
@@ -107,13 +48,4 @@ async def get_logs(
10748 action = ActionType .QUERY , pilot_agents_db = pilot_agents_db , pilot_id = pilot_id
10849 )
10950
110- search_params = [{"parameter" : "PilotID" , "operator" : "eq" , "value" : pilot_id }]
111-
112- result = await db .search (
113- ["Message" ],
114- search_params ,
115- [{"parameter" : "LineNumber" , "direction" : "asc" }],
116- )
117- if not result :
118- return [{"Message" : f"No logs for pilot ID = { pilot_id } " }]
119- return result
51+ return await get_logs_bl (pilot_id , db )
0 commit comments