Skip to content

Commit

Permalink
Mongo会话管理增加显示字段 (#2785)
Browse files Browse the repository at this point in the history
  • Loading branch information
feiazifeiazi authored Sep 25, 2024
1 parent 58c0145 commit 073560e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
12 changes: 12 additions & 0 deletions sql/engines/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down
19 changes: 14 additions & 5 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions sql/templates/dbdiagnostic.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,29 @@ <h4 class="modal-title text-danger">确定要终止所选会话吗?</h4>
title: 'type',
field: 'type',
sortable: true
}, {
title: 'active',
field: 'active',
sortable: true
}, {
title: 'desc',
field: 'desc',
sortable: true
}, {
title: 'ns',
field: 'ns',
sortable: true
}, {
title: 'effectiveUsers_user',
field: 'effectiveUsers_user',
sortable: true
}
, {
title: 'secs_running',
field: 'secs_running',
sortable: true
}
, {
title: 'microsecs_running',
field: 'microsecs_running',
sortable: true
Expand Down

0 comments on commit 073560e

Please sign in to comment.