diff --git a/sql/db_diagnostic.py b/sql/db_diagnostic.py index 683d266d27..6f755c086f 100644 --- a/sql/db_diagnostic.py +++ b/sql/db_diagnostic.py @@ -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) @@ -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() diff --git a/sql/engines/__init__.py b/sql/engines/__init__.py index 09ee6f1e2b..939a100b13 100644 --- a/sql/engines/__init__.py +++ b/sql/engines/__init__.py @@ -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): """终止数据库连接""" diff --git a/sql/engines/cloud/aliyun_rds.py b/sql/engines/cloud/aliyun_rds.py index efb13eaf41..4f4b81f1cc 100644 --- a/sql/engines/cloud/aliyun_rds.py +++ b/sql/engines/cloud/aliyun_rds.py @@ -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" diff --git a/sql/engines/mongo.py b/sql/engines/mongo.py index a989067941..fe491c6057 100644 --- a/sql/engines/mongo.py +++ b/sql/engines/mongo.py @@ -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): """ 获取当前连接信息 diff --git a/sql/engines/mysql.py b/sql/engines/mysql.py index 369931791b..d47b2b8a92 100644 --- a/sql/engines/mysql.py +++ b/sql/engines/mysql.py @@ -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 diff --git a/sql/engines/oracle.py b/sql/engines/oracle.py index 747f84f8d1..4345431e1e 100644 --- a/sql/engines/oracle.py +++ b/sql/engines/oracle.py @@ -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, diff --git a/sql/engines/redis.py b/sql/engines/redis.py index 5e46c797b9..1f839988b2 100644 --- a/sql/engines/redis.py +++ b/sql/engines/redis.py @@ -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) diff --git a/sql/engines/tests.py b/sql/engines/tests.py index eea86f31c3..63147e6c0c 100644 --- a/sql/engines/tests.py +++ b/sql/engines/tests.py @@ -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") @@ -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"} @@ -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") diff --git a/sql/templates/dbdiagnostic.html b/sql/templates/dbdiagnostic.html index 2bb208f7ea..3d5e234ec5 100644 --- a/sql/templates/dbdiagnostic.html +++ b/sql/templates/dbdiagnostic.html @@ -1037,12 +1037,14 @@ $(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 @@ -1057,23 +1059,13 @@ 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 = ""; - // $("#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');