Skip to content

Commit adb8e41

Browse files
Issues fixing (#1330)
* fix call_log_helper.py and updated test_call_log_helper.py --------- Co-authored-by: Fluder-Paradyne <[email protected]>
1 parent c02fe30 commit adb8e41

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

superagi/apm/call_log_helper.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,25 @@ def fetch_data(self, model: str):
5252

5353
runs = self.session.query(CallLogs).filter(CallLogs.model == model,
5454
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()
5755

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}
6264

65+
for run in runs:
6366
model_data['runs'].append({
6467
'id': run.id,
6568
'agent_execution_name': run.agent_execution_name,
6669
'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,
6871
'tokens_consumed': run.tokens_consumed,
6972
'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,
7174
'org_id': run.org_id,
7275
'created_at': run.created_at,
7376
'updated_at': run.updated_at,
@@ -79,4 +82,4 @@ def fetch_data(self, model: str):
7982

8083
except SQLAlchemyError as err:
8184
logging.error(f"Error while fetching call log data: {str(err)}")
82-
return None
85+
return None

tests/unit_tests/apm/test_call_log_helper.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ def test_fetch_data_success(call_log_helper, mock_session):
5555
model='test_model',
5656
org_id=1
5757
)]
58-
agent = Agent(name='test_agent')
59-
tool = Tool(name='test_tool', toolkit_id=1)
60-
toolkit = Toolkit(name='test_toolkit')
58+
agents = [Agent(name='test_agent')]
59+
tools = [Tool(name='test_tool', toolkit_id=1)]
60+
toolkits = [Toolkit(name='test_toolkit')]
6161

6262
# setup return values for the mock methods
63-
mock_session.query().filter().first.side_effect = [summary_result, agent, tool, toolkit]
64-
mock_session.query().filter().all.return_value = runs
63+
mock_session.query().filter().first.side_effect = [summary_result, runs, agents, toolkits, tools]
6564

6665
result = call_log_helper.fetch_data('test_model')
6766

0 commit comments

Comments
 (0)