2222# subcontract 1658085.
2323#
2424from typing import List
25- import re
25+ import ast
26+
2627from fastapi import APIRouter , Depends
2728from fastapi import status
2829from fastapi_pagination import Page , Params
2930from fastapi_pagination .ext .async_sqlalchemy import paginate
3031from sqlalchemy import select , and_
3132from sqlalchemy .engine import Result
32- from io import StringIO
33+
3334from urllib .parse import unquote
3435
3536from anms .components .schemas import ARIs
3637from anms .models .relational import get_async_session , get_session
3738
3839from anms .models .relational .report import Report
3940from anms .models .relational .execution_set import ExecutionSet
41+ from anms .models .relational .registered_agent import RegisteredAgent
42+
4043from anms .shared .opensearch_logger import OpenSearchLogger
4144import io
4245
4346import anms .routes .transcoder as transcoder
4447
4548# for handling report set and exec set
4649import ace
47- import ace .models
48-
4950
5051logger = OpenSearchLogger (__name__ , log_console = True )
5152
@@ -69,47 +70,70 @@ async def all_report_name():
6970
7071@router .get ("/entry/name/{agent_id}" , status_code = status .HTTP_200_OK , response_model = list ,
7172 tags = ["REPORTS" ])
72- async def report_def_by_id (agent_id : str ):
73+ async def report_def_by_id (agent_id : int ):
7374 # select all reports belonging to the agent
74- agent_id = agent_id .strip ()
7575 final_res = []
76+ agent_id_str = ""
7677 stmt = select (Report ).where (Report .agent_id == agent_id )
78+ agent_id_stmt = select (RegisteredAgent ).where (RegisteredAgent .registered_agents_id == agent_id )
7779 async with get_async_session () as session :
7880 result : Result = await session .scalars (stmt )
81+ # Execution set uses URI as agent_id
82+ result_agent : Result = await session .scalars (agent_id_stmt )
83+ agent_id_str = result_agent .one_or_none ()
84+ agent_id_str = agent_id_str .agent_endpoint_uri
7985 for res in result .all ():
8086 # 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 )
93-
87+ try :
88+ nonce_cbor = res .nonce_cbor
89+ stmt = select (ExecutionSet ).where (and_ (ExecutionSet .agent_id == agent_id_str , ExecutionSet .nonce_cbor == nonce_cbor ) )
90+ result : Result = await session .scalars (stmt )
91+ exc_set = result .all ()
92+ for res in exc_set :
93+ ari_val = ""
94+ if (res ):
95+ hex_str = res .entries .hex ()
96+ hex_str = "0x" + hex_str .upper ()
97+ ari_val = await transcoder .transcoder_put_cbor_await (hex_str )
98+ ari_val = ari_val ['data' ]
99+ logger .info (str (nonce_cbor ))
100+ addition = {'exec_set' : ari_val ,'nonce_cbor' :str (nonce_cbor )}
101+ if addition not in final_res :
102+ final_res .append (addition )
103+ except Exception as e :
104+ logger .error (f"Error { e } , while processing nonce:{ nonce_cbor } for agent: { agent_id_str } " )
105+
94106 return final_res
95107
96108
97109# entries tabulated returns header and values in correct order
98- @router .get ("/entries/table/{agent_id}/{correlator_nonce }" , status_code = status .HTTP_200_OK ,
110+ @router .get ("/entries/table/{agent_id}/{nonce_cbor }" , status_code = status .HTTP_200_OK ,
99111 response_model = list )
100- async def report_ac (agent_id : str , correlator_nonce : int ):
101- agent_id = agent_id .strip ()
102- final_res = []
112+ async def report_ac (agent_id : int , nonce_cbor : str ):
113+
103114 ari = None
104115 dec = ace .ari_cbor .Decoder ()
105- buf = io .StringIO ()
116+ enc = ace .ari_text .Encoder ()
117+ try :
118+ nonce_cbor = ast .literal_eval (nonce_cbor )
119+ except Exception as e :
120+ logger .error (f"{ e } while processing nonce" )
121+ return []
122+
123+ agent_id_str = ""
124+ agent_id_stmt = select (RegisteredAgent ).where (RegisteredAgent .registered_agents_id == agent_id )
125+ async with get_async_session () as session :
126+ result_agent : Result = await session .scalars (agent_id_stmt )
127+ agent_id_str = result_agent .one_or_none ()
128+ agent_id_str = agent_id_str .agent_endpoint_uri
129+
106130 # Load in adms
107131 # 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 ) )
132+ stmt = select (ExecutionSet ).where (and_ (ExecutionSet .agent_id == agent_id_str , ExecutionSet .nonce_cbor == nonce_cbor ) )
109133 async with get_async_session () as session :
110134 result : Result = await session .scalars (stmt )
111135
112- # there should only be one execution per agent per correlator_nonce
136+ # there should only be one execution per agent per nonce_cbor
113137 # in the event that two occur pull the latest one
114138 result = result .all ()
115139 exec_set_dir = {}
@@ -124,13 +148,13 @@ async def report_ac(agent_id: str, correlator_nonce: int):
124148 ari = dec .decode (io .BytesIO (in_bytes ))
125149
126150 except Exception as err :
127- logger .info (err )
151+ logger .error (err )
128152
129153 # current ARI should be an exection set
130154 if ari :
131155 if type (ari .value ) == ace .ari .ExecutionSet :
132156 try :
133- enc = ace . ari_text . Encoder ()
157+
134158 # run through targets and their parameters to get all things parts translated
135159 for targ in ari .value .targets :
136160 buf = io .StringIO ()
@@ -149,12 +173,12 @@ async def report_ac(agent_id: str, correlator_nonce: int):
149173 exec_set_dir [out_text_targ ] = [exec_set_entry ]
150174
151175 except Exception as err :
152- logger .info (err )
176+ logger .error (err )
153177
154178
155179 # final_res.append(exec_set_entry)
156180 ari = None
157- stmt = select (Report ).where (and_ (Report .agent_id == agent_id , Report .correlator_nonce == correlator_nonce ) )
181+ stmt = select (Report ).where (and_ (Report .agent_id == agent_id , Report .nonce_cbor == nonce_cbor ) )
158182 async with get_async_session () as session :
159183 result : Result = await session .scalars (stmt )
160184 for res in result .all ():
@@ -188,9 +212,7 @@ async def report_ac(agent_id: str, correlator_nonce: int):
188212
189213 exec_set_dir [out_text ].append (addition )
190214 except Exception as err :
191- logger .error (err )
192-
193-
215+ logger .error (err )
194216
195217 return list (exec_set_dir .values ())
196218
0 commit comments