Skip to content

Commit

Permalink
processlist方法已提升为父类方法
Browse files Browse the repository at this point in the history
  • Loading branch information
feiazifeiazi committed Sep 20, 2024
1 parent ed135b9 commit e8f1297
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 39 deletions.
21 changes: 7 additions & 14 deletions sql/db_diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
def process(request):
instance_name = request.POST.get("instance_name")
command_type = request.POST.get("command_type")
request_post_kwargs = {
key: value for key, value in request.POST.items() if key != "command_type"
}

try:
instance = user_instances(request.user).get(instance_name=instance_name)
Expand All @@ -31,20 +34,10 @@ def process(request):

query_engine = get_engine(instance=instance)
query_result = None
if instance.db_type in ["mysql", "redis"]:
query_result = query_engine.processlist(command_type)
elif instance.db_type == "mongo":
query_result = query_engine.current_op(command_type)
elif instance.db_type == "oracle":
query_result = query_engine.session_list(command_type)
else:
result = {
"status": 1,
"msg": "暂时不支持{}类型数据库的进程列表查询".format(instance.db_type),
"data": [],
}
return HttpResponse(json.dumps(result), content_type="application/json")

# processlist方法已提升为父类方法,简化此处的逻辑。进程添加新数据库支持时,改前端即可。
query_result = query_engine.processlist(
command_type=command_type, **request_post_kwargs
)
if query_result:
if not query_result.error:
processlist = query_result.to_dict()
Expand Down
4 changes: 4 additions & 0 deletions sql/engines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def server_version(self):
"""返回引擎服务器版本,返回对象为tuple (x,y,z)"""
return tuple()

def processlist(self, command_type, **kwargs) -> ResultSet:
"""获取连接信息"""
return ResultSet()

def kill_connection(self, thread_id):
"""终止数据库连接"""

Expand Down
2 changes: 1 addition & 1 deletion sql/engines/cloud/aliyun_rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, instance=None):
self.instance_name = instance.instance_name

# 将sql/aliyun_rds.py的函数迁移值此
def processlist(self, command_type):
def processlist(self, command_type, **kwargs):
if command_type is None or command_type == "":
command_type = "Query"

Expand Down
2 changes: 1 addition & 1 deletion sql/engines/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ def fill_query_columns(cursor, columns):
cols.append(key)
return cols

def current_op(self, command_type):
def processlist(self, command_type, **kwargs):
"""
获取当前连接信息
Expand Down
2 changes: 1 addition & 1 deletion sql/engines/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def osc_control(self, **kwargs):
"""
return self.inc_engine.osc_control(**kwargs)

def processlist(self, command_type):
def processlist(self, command_type, **kwargs):
"""获取连接信息"""
base_sql = "select id, user, host, db, command, time, state, ifnull(info,'') as info from information_schema.processlist"
# escape
Expand Down
2 changes: 1 addition & 1 deletion sql/engines/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ def execute(self, db_name=None, sql="", close_conn=True, parameters=None):
self.close()
return result

def session_list(self, command_type):
def processlist(self, command_type, **kwargs):
"""获取会话信息"""
base_sql = """select
s.sid,
Expand Down
2 changes: 1 addition & 1 deletion sql/engines/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def query_check(self, db_name=None, sql="", limit_num=0):
result["msg"] = "禁止执行该命令!"
return result

def processlist(self, command_type):
def processlist(self, command_type, **kwargs):
"""获取连接信息"""
sql = "client list"
result_set = ResultSet(full_sql=sql)
Expand Down
8 changes: 4 additions & 4 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1497,11 +1497,11 @@ def test_execute(self, _connect, _cursor, _execute):
self.assertIsInstance(execute_result, ResultSet)

@patch("sql.engines.oracle.OracleEngine.query")
def test_session_list(self, _query):
def test_processlist(self, _query):
new_engine = OracleEngine(instance=self.ins)
_query.return_value = ResultSet()
for command_type in ["All", "Active", "Others"]:
r = new_engine.session_list(command_type)
r = new_engine.processlist(command_type)
self.assertIsInstance(r, ResultSet)

@patch("sql.engines.oracle.OracleEngine.query")
Expand Down Expand Up @@ -1803,7 +1803,7 @@ def test_fill_query_columns(self):
self.assertEqual(cols, ["_id", "title", "tags", "likes", "text", "author"])

@patch("sql.engines.mongo.MongoEngine.get_connection")
def test_current_op(self, mock_get_connection):
def test_processlist(self, mock_get_connection):
class Aggregate:
def __enter__(self):
yield {"client": "single_client"}
Expand All @@ -1817,7 +1817,7 @@ def __exit__(self, *arg, **kwargs):
mock_get_connection.return_value = mock_conn
command_types = ["Full", "All", "Inner", "Active"]
for command_type in command_types:
result_set = self.engine.current_op(command_type)
result_set = self.engine.processlist(command_type)
self.assertIsInstance(result_set, ResultSet)

@patch("sql.engines.mongo.MongoEngine.get_connection")
Expand Down
24 changes: 8 additions & 16 deletions sql/templates/dbdiagnostic.html
Original file line number Diff line number Diff line change
Expand Up @@ -1037,12 +1037,14 @@ <h4 class="modal-title text-danger">确定要终止所选会话吗?</h4>
$(document).ready(function () {
//获取用户实例列表
$(function () {
// 会话管理-支持的数据库类型
supportedDbType=['mysql','mongo', 'oracle','redis','opensearch']
$.ajax({
type: "get",
url: "/group/user_all_instances/",
dataType: "json",
data: {
db_type: ['mysql','mongo', 'oracle','redis']
db_type: supportedDbType
},
complete: function () {
//如果已选择instance_name进入页面自动填充并且重置激活id
Expand All @@ -1057,23 +1059,13 @@ <h4 class="modal-title text-danger">确定要终止所选会话吗?</h4>
if (data.status === 0) {
let result = data['data'];
allInstances = result;
// $("#instance_name").empty();
$("#optgroup-mysql").empty();
$("#optgroup-mongo").empty();
$("#optgroup-oracle").empty();
$("#optgroup-redis").empty();
supportedDbType.forEach(function(db) {
$("#optgroup-" + db).empty();
});
for (let i = 0; i < result.length; i++) {
let instance = "<option value=\"" + result[i]['instance_name'] + "\">" + result[i]['instance_name'] + "</option>";
// $("#instance_name").append(instance);
if (result[i]['db_type'] === 'mysql') {
$("#optgroup-mysql").append(instance);
} else if (result[i]['db_type'] === 'mongo') {
$("#optgroup-mongo").append(instance);
} else if (result[i]['db_type'] === 'oracle') {
$("#optgroup-oracle").append(instance);
} else if (result[i]['db_type'] === 'redis') {
$("#optgroup-redis").append(instance);
}
var dbType = result[i]['db_type'];
$("#optgroup-" + dbType).append(instance);
}
$('#instance_name').selectpicker('render');
$('#instance_name').selectpicker('refresh');
Expand Down

0 comments on commit e8f1297

Please sign in to comment.