diff --git a/plugins/data_query/sql_mysql.py b/plugins/data_query/sql_mysql.py new file mode 100755 index 000000000..b110b35c7 --- /dev/null +++ b/plugins/data_query/sql_mysql.py @@ -0,0 +1,215 @@ +# coding:utf-8 + +import sys +import io +import os +import time +import re +import pymongo +import json + + +sys.path.append(os.getcwd() + "/class/core") +import mw + +def singleton(cls): + _instance = {} + + def inner(): + if cls not in _instance: + _instance[cls] = cls() + return _instance[cls] + return inner + + +@singleton +class nosqlMySQL(): + + __DB_PASS = None + __DB_USER = None + __DB_PORT = 6379 + __DB_HOST = '127.0.0.1' + __DB_CONN = None + __DB_ERR = None + + __DB_LOCAL = None + + def __init__(self): + self.__config = self.get_options(None) + + + def mgdb_conn(self): + + if self.__DB_HOST in ['127.0.0.1', 'localhost']: + my_path = "{}/mysql".format(mw.getServerDir()) + if not os.path.exists(my_path): return False + + if not self.__DB_LOCAL: + self.__DB_PORT = int(self.__config['port']) + + # print(self.__DB_HOST,self.__DB_PORT, self.__DB_PASS) + try: + self.__DB_CONN = pymongo.MongoClient(host=self.__DB_HOST, port=self.__DB_PORT, maxPoolSize=10) + self.__DB_CONN.admin.command('ping') + return self.__DB_CONN + except pymongo.errors.ConnectionFailure: + return False + except Exception: + self.__DB_ERR = mw.get_error_info() + return False + + # 获取配置项 + def get_options(self, get=None): + result = {} + mgdb_content = mw.readFile("{}/mysql/my.conf".format(mw.getServerDir())) + if not mgdb_content: return False + + keys = ["bind_ip", "port"] + + result['host'] = '127.0.0.1' + rep = 'port\s*=\s*(.*)' + ip_re = re.search(rep, mgdb_content) + if ip_re: + result['port'] = int(ip_re.groups()[0].strip()) + else: + result['port'] = 27017 + return result + + def set_host(self, host, port, name, username, password, prefix=''): + self.__DB_HOST = host + self.__DB_PORT = int(port) + self.__DB_NAME = name + if self.__DB_NAME: self.__DB_NAME = str(self.__DB_NAME) + self.__DB_USER = str(username) + self._USER = str(username) + self.__DB_PASS = str(password) + self.__DB_PREFIX = prefix + self.__DB_LOCAL = 1 + return self + + + +@singleton +class nosqlMySQLCtr(): + + def __init__(self): + pass + + def getInstanceBySid(self, sid = 0): + instance = nosqlMySQL() + return instance + + def getDbList(self, args): + sid = args['sid'] + mgdb_instance = self.getInstanceBySid(sid).mgdb_conn() + if mgdb_instance is False: + return mw.returnData(False,'无法链接') + + result = {} + doc_list = mgdb_instance.list_database_names() + rlist = [] + for x in doc_list: + if not x in ['admin', 'config', 'local']: + rlist.append(x) + result['list'] = rlist + return mw.returnData(True,'ok', result) + + def getCollectionsList(self, args): + sid = args['sid'] + name = args['name'] + + mgdb_instance = self.getInstanceBySid(sid).mgdb_conn() + if mgdb_instance is False: + return mw.returnData(False,'无法链接') + + result = {} + collections = mgdb_instance[name].list_collection_names() + result['collections'] = collections + return mw.returnData(True,'ok', result) + + def getDataList(self, args): + sid = args['sid'] + db = args['db'] + collection = args['collection'] + p = 1 + size = 10 + if 'p' in args: + p = args['p'] + + if 'size' in args: + size = args['size'] + + mgdb_instance = self.getInstanceBySid(sid).mgdb_conn() + if mgdb_instance is False: + return mw.returnData(False,'无法链接') + + db_instance = mgdb_instance[db] + collection_instance = db_instance[collection] + + start_index = (p - 1) * size + end_index = p * size + args_where = args['where'] + + where = {} + if 'field' in args_where: + mg_field = args_where['field'] + + if mg_field == '_id': + mg_value = ObjectId(args_where['value']) + where[mg_field] = mg_value + else: + mg_value = args_where['value'] + where[mg_field] = re.compile(mg_value) + + # print(where) + result = collection_instance.find(where).skip(start_index).limit(size).sort({'_id':-1}) + count = collection_instance.count_documents(where) + d = [] + for document in result: + d.append(document) + + doc_str_json = dumps(d) + result = json.loads(doc_str_json) + + + page_args = {} + page_args['count'] = count + page_args['tojs'] = 'mongodbDataList' + page_args['p'] = p + page_args['row'] = size + + rdata = {} + rdata['page'] = mw.getPage(page_args) + rdata['list'] = result + rdata['count'] = count + + rdata['soso_field'] = '' + if 'field' in args_where: + rdata['soso_field'] = args_where['field'] + + + return mw.returnData(True,'ok', rdata) + + +# ---------------------------------- run ---------------------------------- +# 获取 mysql 列表 +def get_db_list(args): + t = nosqlMySQLCtr() + return t.getDbList(args) + +# 获取 mysql 列表 +def get_collections_list(args): + t = nosqlMySQLCtr() + return t.getCollectionsList(args) + +def get_data_list(args): + t = nosqlMySQLCtr() + return t.getDataList(args) + +# 测试 +def test(args): + sid = args['sid'] + t = nosqlMySQLCtr() + print(t.get_options()) + print("test") + return 'ok' \ No newline at end of file diff --git a/plugins/data_query/static/html/index.html b/plugins/data_query/static/html/index.html index f2e206a97..4353fcf14 100644 --- a/plugins/data_query/static/html/index.html +++ b/plugins/data_query/static/html/index.html @@ -29,9 +29,15 @@ -
- + + +
+ +
+
@@ -60,7 +66,6 @@ 编号 操作类型 详情 - 操作 @@ -73,7 +78,16 @@ -
+
+
+
+ 进程 + 变量 + 状态 + 统计 +
+
+
@@ -194,7 +208,7 @@
-
+
0共0条
@@ -250,7 +264,7 @@
- +
diff --git a/plugins/data_query/static/js/app.js b/plugins/data_query/static/js/app.js index af0df8b98..797989099 100755 --- a/plugins/data_query/static/js/app.js +++ b/plugins/data_query/static/js/app.js @@ -89,6 +89,34 @@ function memPostCB(method, args, callback){ },'json'); } +function myPostCB(method, args, callback){ + var loadT = layer.msg('正在获取...', { icon: 16, time: 0, shade: 0.3 }); + + var req_data = {}; + req_data['name'] = 'data_query'; + req_data['func'] = method; + req_data['script']='sql_mysql'; + args['version'] = ''; + + if (typeof(args) == 'string' && args == ''){ + req_data['args'] = JSON.stringify(toArrayObject(args)); + } else { + req_data['args'] = JSON.stringify(args); + } + + $.post('/plugins/callback', req_data, function(data) { + layer.close(loadT); + if (!data.status){ + layer.msg(data.msg,{icon:0,time:2000,shade: [0.3, '#000']}); + return; + } + + if(typeof(callback) == 'function'){ + callback(data); + } + },'json'); +} + function selectTab(tab = 'redis'){ $('.tab-view-box .tab-con').addClass('hide').removeClass('show').removeClass('w-full'); @@ -108,6 +136,7 @@ function initTabFunc(tab){ case 'redis':initTabRedis();break; case 'mongodb':initTabMongodb();break; case 'memcached':initTabMemcached();break; + case 'mysql':initTabMySQL();break; case 'default:':initTabRedis();break; } } @@ -171,6 +200,8 @@ function initTabMongodb(){ mongodbGetList(); } + + function initTabMemcached(){ memcachedGetList(); @@ -181,7 +212,7 @@ function initTabMemcached(){ $('#memcached_clear_all').unbind('click').click(function(){ var sid = memcachedGetSid(); memPostCB('clear',{'sid':sid} ,function(rdata){ - console.log(rdata); + // console.log(rdata); showMsg(rdata.data.msg,function(){ if (rdata.data.status){ memcachedGetList(); @@ -191,6 +222,49 @@ function initTabMemcached(){ }); } +function initTabMySQL(){ + mysqlGetDbList(); +} + +// ------------------------- mysql start ------------------------------- +function mysqlGetSid(){ + return 0; +} + +function mysqlGetDbList(){ + var sid = mysqlGetSid(); + myPostCB('get_db_list',{'sid':sid} ,function(rdata){ + if (rdata.data.status){ + + var items = rdata.data.data['items']; + var content = ''; + for (var i = 0; i < items.length; i++) { + var name = items[i]; + if (i == 0){ + content += ''; + } else { + content += ''; + } + } + $('#memcached .item_list select').html(content); + $('#memcached .item_list select').change(function(){ + memcachedGetKeyList(1); + }); + closeInstallLayer(); + } else { + showInstallLayer(); + } + mysqlGetTableList(1); + }); +} + +function mysqlGetTableList(p){ + console.log('mysqlGetTableList',p); +} + + +// ------------------------- mysql start ------------------------------- + // ------------------------- memcached start ------------------------------- function memcachedGetSid(){ return 0; @@ -523,7 +597,7 @@ function mongodbDataList(p){ } header_field += '操作'; - $('.mongodb_table thead tr').html(header_field); + $('#mongodb .mongodb_table thead tr').html(header_field); var tbody = ''; for (var i = 0; i < dlist.length; i++) { @@ -552,10 +626,10 @@ function mongodbDataList(p){ // console.log($(window).width()-230); $('#mongodb_table').css('width', $(document).width()+240).parent().css('width', $(document).width()-240).css('overflow','scroll'); $('#mongodb').css('width',$(document).width()-240).css('overflow','hidden'); - $('.mongodb_table tbody').html(tbody); - $('.mongodb_list_page').html(data.page); + $('#mongodb .mongodb_table tbody').html(tbody); + $('#mongodb .mongodb_list_page').html(data.page); - $('.del').click(function(){ + $('#mongodb .del').click(function(){ var i = $(this).data('index'); mongodbDel(dlist[i]['_id']['$oid']); }); @@ -564,7 +638,7 @@ function mongodbDataList(p){ } function mongodbDel(mgdb_id){ - console.log(mgdb_id); + // console.log(mgdb_id); var sid = mongodbGetSid(); var db = mongodbGetDbName(); var collection = mongodbCollectionName();