@@ -52,22 +52,25 @@ def fetch_data(self, model: str):
52
52
53
53
runs = self .session .query (CallLogs ).filter (CallLogs .model == model ,
54
54
CallLogs .org_id == self .organisation_id ).all ()
55
- for run in runs :
56
- agent = self .session .query (Agent ).filter (Agent .id == run .agent_id ).first ()
57
55
58
- toolkit = None
59
- tool = self .session .query (Tool ).filter (Tool .name == run .tool_used ).first ()
60
- if tool :
61
- toolkit = self .session .query (Toolkit ).filter (Toolkit .id == tool .toolkit_id ).first ()
56
+ run_agent_ids = [run .agent_id for run in runs ]
57
+ agents = self .session .query (Agent ).filter (Agent .id .in_ (run_agent_ids )).all ()
58
+ agent_id_name_map = {agent .id : agent .name for agent in agents }
59
+ tools_used = [run .tool_used for run in runs ]
60
+ toolkit_ids_allowed = self .session .query (Toolkit .id ).filter (Toolkit .organisation_id == self .organisation_id ).all ()
61
+ tools = self .session .query (Tool ).filter (Tool .id .in_ (tools_used ), Tool .toolkit_id .in_ (toolkit_ids_allowed ))\
62
+ .all ()
63
+ tools_name_toolkit_id_map = {tool .name : tool .toolkit_id for tool in tools }
62
64
65
+ for run in runs :
63
66
model_data ['runs' ].append ({
64
67
'id' : run .id ,
65
68
'agent_execution_name' : run .agent_execution_name ,
66
69
'agent_id' : run .agent_id ,
67
- 'agent_name' : agent . name if agent is not None else None ,
70
+ 'agent_name' : agent_id_name_map [ run . agent_id ] if run . agent_id in agent_id_name_map else None ,
68
71
'tokens_consumed' : run .tokens_consumed ,
69
72
'tool_used' : run .tool_used ,
70
- 'toolkit_name' : toolkit . name if toolkit is not None else None ,
73
+ 'toolkit_name' : tools_name_toolkit_id_map [ run . tool_used ] if run . tool_used in tools_name_toolkit_id_map else None ,
71
74
'org_id' : run .org_id ,
72
75
'created_at' : run .created_at ,
73
76
'updated_at' : run .updated_at ,
@@ -79,4 +82,4 @@ def fetch_data(self, model: str):
79
82
80
83
except SQLAlchemyError as err :
81
84
logging .error (f"Error while fetching call log data: { str (err )} " )
82
- return None
85
+ return None
0 commit comments