diff --git a/sql/engines/mongo.py b/sql/engines/mongo.py index fe491c6057..534971fd8f 100644 --- a/sql/engines/mongo.py +++ b/sql/engines/mongo.py @@ -1253,6 +1253,18 @@ def processlist(self, command_type, **kwargs): "client" ] + # 获取此会话的用户名 + effective_users_key = "effectiveUsers_user" + effective_users = operation.get("effectiveUsers", []) + if isinstance(effective_users, list) and effective_users: + first_user = effective_users[0] + if isinstance(first_user, dict): + operation[effective_users_key] = first_user.get("user", []) + else: + operation[effective_users_key] = None + else: + operation[effective_users_key] = None + # client_s 只是处理的mongos,并不是实际客户端 # client 在sharding获取不到? if command_type in ["Full"]: diff --git a/sql/engines/tests.py b/sql/engines/tests.py index 4638708213..2cd119b761 100644 --- a/sql/engines/tests.py +++ b/sql/engines/tests.py @@ -1841,16 +1841,25 @@ def test_fill_query_columns(self): @patch("sql.engines.mongo.MongoEngine.get_connection") def test_processlist(self, mock_get_connection): - class Aggregate: + # 模拟 MongoDB aggregate 的游标行为 + class AggregateCursor: def __enter__(self): - yield {"client": "single_client"} - yield {"clientMetadata": {"mongos": {"client": "sharding_client"}}} + yield { + "client": "single_client", + "effectiveUsers": [{"user": "user_1"}], + "clientMetadata": {"mongos": {"client": "sharding_client"}}, + } + yield { + "clientMetadata": {"mongos": {}}, + "effectiveUsers": [{"user": "user_2"}], + } + yield {"effectiveUsers": []} - def __exit__(self, *arg, **kwargs): + def __exit__(self, exc_type, exc_value, traceback): pass mock_conn = Mock() - mock_conn.admin.aggregate.return_value = Aggregate() + mock_conn.admin.aggregate.return_value = AggregateCursor() mock_get_connection.return_value = mock_conn command_types = ["Full", "All", "Inner", "Active"] for command_type in command_types: diff --git a/sql/templates/dbdiagnostic.html b/sql/templates/dbdiagnostic.html index 76eb34aaf1..6f5f263495 100644 --- a/sql/templates/dbdiagnostic.html +++ b/sql/templates/dbdiagnostic.html @@ -197,11 +197,29 @@