2222# subcontract 1658085.
2323#
2424from typing import List
25- import re
25+
2626from fastapi import APIRouter , Depends
2727from fastapi import status
2828from fastapi_pagination import Page , Params
2929from fastapi_pagination .ext .async_sqlalchemy import paginate
3030from sqlalchemy import select , and_
3131from sqlalchemy .engine import Result
32- from io import StringIO
32+
3333from urllib .parse import unquote
3434
3535from anms .components .schemas import ARIs
3636from anms .models .relational import get_async_session , get_session
3737
3838from anms .models .relational .report import Report
3939from anms .models .relational .execution_set import ExecutionSet
40+ from anms .models .relational .registered_agent import RegisteredAgent
41+
4042from anms .shared .opensearch_logger import OpenSearchLogger
4143import io
4244
4345import anms .routes .transcoder as transcoder
4446
4547# for handling report set and exec set
4648import ace
47- import ace .models
48-
4949
5050logger = OpenSearchLogger (__name__ , log_console = True )
5151
@@ -69,47 +69,64 @@ async def all_report_name():
6969
7070@router .get ("/entry/name/{agent_id}" , status_code = status .HTTP_200_OK , response_model = list ,
7171 tags = ["REPORTS" ])
72- async def report_def_by_id (agent_id : str ):
72+ async def report_def_by_id (agent_id : int ):
7373 # select all reports belonging to the agent
74- agent_id = agent_id .strip ()
7574 final_res = []
75+ agent_id_str = ""
7676 stmt = select (Report ).where (Report .agent_id == agent_id )
77+ agent_id_stmt = select (RegisteredAgent ).where (RegisteredAgent .registered_agents_id == agent_id )
7778 async with get_async_session () as session :
7879 result : Result = await session .scalars (stmt )
80+ # Execution set uses URI as agent_id
81+ result_agent : Result = await session .scalars (agent_id_stmt )
82+ agent_id_str = result_agent .one_or_none ()
83+ agent_id_str = agent_id_str .agent_endpoint_uri
7984 for res in result .all ():
8085 # select from exec_set
81- correlator_nonce = res .correlator_nonce
82- stmt = select (ExecutionSet ).where (and_ (ExecutionSet .agent_id == agent_id , ExecutionSet .correlator_nonce == correlator_nonce ) )
83- result : Result = await session .scalars (stmt )
84- exc_set = result .all ()
85- for res in exc_set :
86- ari_val = ""
87- if (res ):
88- ari_val = await transcoder .transcoder_put_cbor_await ("0x" + res .entries .hex ())
89- ari_val = ari_val ['data' ]
90- addition = {'exec_set' : ari_val ,'correlator_nonce' :correlator_nonce }
91- if addition not in final_res :
92- final_res .append (addition )
86+ nonce_cbor = res .nonce_cbor
87+ try :
88+ stmt = select (ExecutionSet ).where (and_ (ExecutionSet .agent_id == agent_id_str , ExecutionSet .nonce_cbor == nonce_cbor ) )
89+ result : Result = await session .scalars (stmt )
90+ exc_set = result .all ()
91+ for res in exc_set :
92+ ari_val = ""
93+ if (res ):
94+ hex_str = "0x" + res .entries .hex ()
95+ hex_str = hex_str .upper ()
96+ ari_val = await transcoder .transcoder_put_cbor_await (hex_str )
97+ ari_val = ari_val ['data' ]
98+ addition = {'exec_set' : ari_val ,'nonce_cbor' :nonce_cbor }
99+ if addition not in final_res :
100+ final_res .append (addition )
101+ except Exception as e :
102+ logger .error (f"Error { e } , while processing nonce:{ nonce_cbor } for agent: { agent_id_str } " )
93103
94104 return final_res
95105
96106
97107# entries tabulated returns header and values in correct order
98- @router .get ("/entries/table/{agent_id}/{correlator_nonce }" , status_code = status .HTTP_200_OK ,
108+ @router .get ("/entries/table/{agent_id}/{nonce_cbor }" , status_code = status .HTTP_200_OK ,
99109 response_model = list )
100- async def report_ac (agent_id : str , correlator_nonce : int ):
101- agent_id = agent_id .strip ()
110+ async def report_ac (agent_id : int , nonce_cbor : bytes ):
102111 final_res = []
103112 ari = None
104113 dec = ace .ari_cbor .Decoder ()
105- buf = io .StringIO ()
114+ buf = nonce_cbor
115+
116+ agent_id_str = ""
117+ agent_id_stmt = select (RegisteredAgent ).where (RegisteredAgent .registered_agents_id == agent_id )
118+ async with get_async_session () as session :
119+ result_agent : Result = await session .scalars (agent_id_stmt )
120+ agent_id_str = result_agent .one_or_none ()
121+ agent_id_str = agent_id_str .agent_endpoint_uri
122+
106123 # Load in adms
107124 # get command that made the report as first entry
108- stmt = select (ExecutionSet ).where (and_ (ExecutionSet .agent_id == agent_id , ExecutionSet .correlator_nonce == correlator_nonce ) )
125+ stmt = select (ExecutionSet ).where (and_ (ExecutionSet .agent_id == agent_id_str , ExecutionSet .nonce_cbor == nonce_cbor ) )
109126 async with get_async_session () as session :
110127 result : Result = await session .scalars (stmt )
111128
112- # there should only be one execution per agent per correlator_nonce
129+ # there should only be one execution per agent per nonce_cbor
113130 # in the event that two occur pull the latest one
114131 result = result .all ()
115132 exec_set_dir = {}
@@ -154,7 +171,7 @@ async def report_ac(agent_id: str, correlator_nonce: int):
154171
155172 # final_res.append(exec_set_entry)
156173 ari = None
157- stmt = select (Report ).where (and_ (Report .agent_id == agent_id , Report .correlator_nonce == correlator_nonce ) )
174+ stmt = select (Report ).where (and_ (Report .agent_id == agent_id , Report .nonce_cbor == nonce_cbor ) )
158175 async with get_async_session () as session :
159176 result : Result = await session .scalars (stmt )
160177 for res in result .all ():
0 commit comments