@@ -74,6 +74,11 @@ async def report_def_by_id(agent_id: int):
7474 # select all reports belonging to the agent
7575 final_res = []
7676 agent_id_str = ""
77+ dec = ace .ari_cbor .Decoder ()
78+ enc = ace .ari_text .Encoder ()
79+ adms = ace .AdmSet ()
80+ adms .load_default_dirs ()
81+ nn_func = ace .nickname .Converter (ace .nickname .Mode .FROM_NN , adms .db_session (), False )
7782 stmt = select (Report ).where (Report .agent_id == agent_id )
7883 agent_id_stmt = select (RegisteredAgent ).where (RegisteredAgent .registered_agents_id == agent_id )
7984 async with get_async_session () as session :
@@ -86,104 +91,83 @@ async def report_def_by_id(agent_id: int):
8691 # select from exec_set
8792 try :
8893 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 )
94+ if (nonce_cbor != b'\xf6 ' ): # not a null nonce
95+ stmt = select (ExecutionSet ).where (and_ (ExecutionSet .agent_id == agent_id_str , ExecutionSet .nonce_cbor == nonce_cbor ) )
96+ result : Result = await session .scalars (stmt )
97+ exc_set = result .all ()
98+ for res_exec in exc_set :
99+ ari_val = ""
100+ if (res_exec ):
101+ hex_str = res_exec .entries .hex ()
102+ hex_str = "0x" + hex_str .upper ()
103+ ari_val = await transcoder .transcoder_put_cbor_await (hex_str )
104+ ari_val = ari_val ['data' ]
105+ addition = {'exec_set' : ari_val ,'nonce_cbor' :str (nonce_cbor )}
106+ if addition not in final_res :
107+ final_res .append (addition )
108+ else : #null nonce use report source
109+ rpt_set = res .report_list_cbor .hex ()
110+ # Using Ace to translate CBOR into ARI object to process individual parts
111+ in_text = '0x' + rpt_set
112+ ari_rpt = None
113+ try :
114+ in_bytes = ace .cborutil .from_hexstr (in_text )
115+ ari_rpt = dec .decode (io .BytesIO (in_bytes ))
116+ except Exception as err :
117+ logger .error (err )
118+
119+ # running through and translating all parts of rptset
120+ for rpt in ari_rpt .value .reports :
121+ try :
122+ enc = ace .ari_text .Encoder ()
123+ buf = io .StringIO ()
124+ enc .encode (rpt .source , buf )
125+ out_text = buf .getvalue ()
126+ ari_val = out_text
127+ # TODO look at better way to handle storing nonce with null
128+ addition = {'exec_set' : ari_val ,'nonce_cbor' :str (nonce_cbor )}
129+ if addition not in final_res :
130+ final_res .append (addition )
131+ except Exception as err :
132+ logger .error (err )
133+
103134 except Exception as e :
104135 logger .error (f"Error { e } , while processing nonce:{ nonce_cbor } for agent: { agent_id_str } " )
105136
106137 return final_res
107138
108-
109139# entries tabulated returns header and values in correct order
110- @router .get ("/entries/table/{agent_id}/{nonce_cbor}" , status_code = status .HTTP_200_OK ,
111- response_model = list )
112- async def report_ac (agent_id : int , nonce_cbor : str ):
113-
140+ # handling if nonce_cbor is null
141+ @router .get ("/entries/table/{agent_id}/{nonce_cbor}" , status_code = status .HTTP_200_OK )
142+ async def report_ac (agent_id : int , nonce_cbor : str ) -> dict :
114143 ari = None
115144 dec = ace .ari_cbor .Decoder ()
116145 enc = ace .ari_text .Encoder ()
146+ exec_set_dir = {}
147+ logger .info (nonce_cbor )
148+ logger .info (type (nonce_cbor ))
117149 try :
150+ store_nonce = nonce_cbor
118151 nonce_cbor = ast .literal_eval (nonce_cbor )
119152 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-
130- # Load in adms
131- # get command that made the report as first entry
132- stmt = select (ExecutionSet ).where (and_ (ExecutionSet .agent_id == agent_id_str , ExecutionSet .nonce_cbor == nonce_cbor ) )
133- async with get_async_session () as session :
134- result : Result = await session .scalars (stmt )
153+ try :
154+ nonce_cbor = ast .literal_eval (str (bytes .fromhex (nonce_cbor )))
155+ except Exception as e :
156+ logger .error (f"{ e } while processing nonce" )
157+ return []
135158
136- # there should only be one execution per agent per nonce_cbor
137- # in the event that two occur pull the latest one
138- result = result .all ()
139- exec_set_dir = {}
140-
141- if result :
142- result = result [- 1 ]
143- exec_set = result .entries .hex ()
144- # use ACE to handle report set decoding
145- in_text = '0x' + exec_set
146- try :
147- in_bytes = ace .cborutil .from_hexstr (in_text )
148- ari = dec .decode (io .BytesIO (in_bytes ))
149-
150- except Exception as err :
151- logger .error (err )
152-
153- # current ARI should be an exection set
154- if ari :
155- if type (ari .value ) == ace .ari .ExecutionSet :
156- try :
157-
158- # run through targets and their parameters to get all things parts translated
159- for targ in ari .value .targets :
160- buf = io .StringIO ()
161- exec_set_entry = ["time" ]
162- enc .encode (targ , buf )
163- out_text_targ = buf .getvalue ()
164- if targ is ace .LiteralARI and targ .type_id is ace .StructType .AC :
165- for part in targ .value :
166- buf = io .StringIO ()
167- enc .encode (part , buf )
168- out_text = buf .getvalue ()
169- exec_set_entry .append (out_text )
170- else :
171- exec_set_entry .append (out_text_targ )
172-
173- exec_set_dir [out_text_targ ] = [exec_set_entry ]
174-
175- except Exception as err :
176- logger .error (err )
177-
178-
179- # final_res.append(exec_set_entry)
159+
160+ # process each report in the rpt set and place inside appropiate nonce case or if null use source as key
161+ # TODO use td off set in report set to update actual time
162+ #
180163 ari = None
181164 stmt = select (Report ).where (and_ (Report .agent_id == agent_id , Report .nonce_cbor == nonce_cbor ) )
182165 async with get_async_session () as session :
183166 result : Result = await session .scalars (stmt )
184167 for res in result .all ():
185168 # used to hold final report set
186- addition = [res .reference_time ]
169+ curr_time = res .reference_time
170+ # addition = {time:}
187171 rpt_set = res .report_list_cbor .hex ()
188172 # Using Ace to translate CBOR into ARI object to process individual parts
189173 in_text = '0x' + rpt_set
@@ -197,22 +181,45 @@ async def report_ac(agent_id: int, nonce_cbor: str):
197181 # current ARI should be an report set
198182 if ari :
199183 if type (ari .value ) == ace .ari .ReportSet :
184+ # for each report in a rptset
185+ # add to the top level nonce dict or to source dict if nonce is null null
200186 for rpt in ari .value .reports :
201187 try :
188+ # structure for the reports
189+ # time: source_name:{[values of reprots ]}
190+ buf = io .StringIO ()
191+ enc .encode (rpt .source , buf )
192+ rpt_src = buf .getvalue ()
193+ addition = {"time" :curr_time , rpt_src :[]}
194+ rpt_entries = []
202195 enc = ace .ari_text .Encoder ()
203196 # running through and translating all parts of rptset
204197 for item in rpt .items :
205- buf = io .StringIO ()
206- enc .encode (item , buf )
207- out_text = buf .getvalue ()
208- addition .append (out_text )
209- buf = io .StringIO ()
210- enc .encode (rpt .source , buf )
211- out_text = buf .getvalue ()
198+ # using ace to decode the components
199+ # item = dec.decode(item)
200+ if type (item .value ) == ace .ari .Table :
201+ table_vals = []
202+ for tab_val in item .value :
203+ table_vals .append ([t .value for t in tab_val ])
204+ rpt_entries .append (table_vals )
205+ else :#handle values as normal
206+ buf = io .StringIO ()
207+ enc .encode (item , buf )
208+ out_text = buf .getvalue ()
209+ rpt_entries .append (out_text )
210+
211+ # placing all the values in the sources section
212+ addition [rpt_src ] = rpt_entries
212213
213- exec_set_dir [out_text ].append (addition )
214+ if (nonce_cbor == b'\xf6 ' ):
215+ curr_dic = exec_set_dir .get (rpt_src ,[])
216+ curr_dic .append (addition )
217+ exec_set_dir [rpt_src ] = curr_dic
218+ else :
219+ curr_dic = exec_set_dir .get (store_nonce ,[])
220+ curr_dic .append (addition )
221+ exec_set_dir [store_nonce ] = curr_dic
214222 except Exception as err :
215223 logger .error (err )
216-
217- return list (exec_set_dir .values ())
224+ return exec_set_dir
218225
0 commit comments