diff --git a/common/utils/convert.py b/common/utils/convert.py new file mode 100644 index 0000000000..d464e78c82 --- /dev/null +++ b/common/utils/convert.py @@ -0,0 +1,17 @@ +from django.db.models import Func + + +class Convert(Func): + """ + Description: 支持mysql的convert(field using gbk)语法,从而支持汉字排序 + Usage: queryset.order_by(Convert('name', 'gbk').asc()) + Reference: https://stackoverflow.com/questions/38517743/django-how-to-make-a-query-with-order-by-convert-name-using-gbk-asc + """ + + def __init__(self, expression, transcoding_name, **extra): + super(Convert, self).__init__(expression=expression, transcoding_name=transcoding_name, **extra) + + def as_mysql(self, compiler, connection): + self.function = 'CONVERT' + self.template = '%(function)s(%(expression)s USING %(transcoding_name)s)' + return super(Convert, self).as_sql(compiler, connection) diff --git a/sql/engines/pgsql.py b/sql/engines/pgsql.py index 27ef8272e8..1444116554 100644 --- a/sql/engines/pgsql.py +++ b/sql/engines/pgsql.py @@ -29,7 +29,7 @@ def get_connection(self, db_name=None): if self.conn: return self.conn self.conn = psycopg2.connect(host=self.host, port=self.port, user=self.user, - password=self.password, dbname=db_name) + password=self.password, dbname=db_name, connect_timeout=10) return self.conn @property diff --git a/sql/engines/redis.py b/sql/engines/redis.py index 64707ba814..99b90a94bb 100644 --- a/sql/engines/redis.py +++ b/sql/engines/redis.py @@ -26,7 +26,7 @@ class RedisEngine(EngineBase): def get_connection(self, db_name=None): db_name = db_name or self.db_name return redis.Redis(host=self.host, port=self.port, db=db_name, password=self.password, - encoding_errors='ignore', decode_responses=True) + encoding_errors='ignore', decode_responses=True, socket_connect_timeout=10) @property def name(self): diff --git a/sql/instance.py b/sql/instance.py index 6f1d355a32..22517fc57d 100644 --- a/sql/instance.py +++ b/sql/instance.py @@ -13,6 +13,7 @@ from common.config import SysConfig from common.utils.extend_json_encoder import ExtendJSONEncoder +from common.utils.convert import Convert from sql.engines import get_engine from sql.plugins.schemasync import SchemaSync from .models import Instance, ParamTemplate, ParamHistory @@ -28,6 +29,8 @@ def lists(request): tags = request.POST.getlist('tags[]') limit = offset + limit search = request.POST.get('search', '') + sortName = str(request.POST.get('sortName')) + sortOrder = str(request.POST.get('sortOrder')).lower() # 组合筛选项 filter_dict = dict() @@ -48,7 +51,12 @@ def lists(request): instances = instances.filter(instance_tag=tag, instance_tag__active=True) count = instances.count() - instances = instances[offset:limit].values("id", "instance_name", "db_type", "type", "host", "port", "user") + if sortName == 'instance_name': + instances = instances.order_by(getattr(Convert(sortName, 'gbk'), sortOrder)())[offset:limit] + else: + instances = instances.order_by('-' + sortName if sortOrder == 'desc' else sortName)[offset:limit] + instances = instances.values("id", "instance_name", "db_type", "type", "host", "port", "user") + # QuerySet 序列化 rows = [row for row in instances] diff --git a/sql/resource_group.py b/sql/resource_group.py index 915722af9f..e9875a14f5 100644 --- a/sql/resource_group.py +++ b/sql/resource_group.py @@ -9,6 +9,7 @@ from django.http import HttpResponse from common.utils.extend_json_encoder import ExtendJSONEncoder from common.utils.permission import superuser_required +from common.utils.convert import Convert from sql.models import ResourceGroup, Users, Instance from sql.utils.resource_group import user_instances from sql.utils.workflow_audit import Audit @@ -137,7 +138,8 @@ def instances(request): if tag_code: filter_dict['instance_tag__tag_code'] = tag_code filter_dict['instance_tag__active'] = True - ins = ins.filter(**filter_dict).values('id', 'type', 'db_type', 'instance_name') + ins = ins.filter(**filter_dict).order_by(Convert('instance_name', 'gbk').asc()).values( + 'id', 'type', 'db_type', 'instance_name') rows = [row for row in ins] result = {'status': 0, 'msg': 'ok', "data": rows} return HttpResponse(json.dumps(result), content_type='application/json') @@ -149,7 +151,8 @@ def user_all_instances(request): type = request.GET.get('type') db_type = request.GET.getlist('db_type[]') tag_codes = request.GET.getlist('tag_codes[]') - instances = user_instances(user, type, db_type, tag_codes).values('id', 'type', 'db_type', 'instance_name') + instances = user_instances(user, type, db_type, tag_codes).order_by( + Convert('instance_name', 'gbk').asc()).values('id', 'type', 'db_type', 'instance_name') rows = [row for row in instances] result = {'status': 0, 'msg': 'ok', "data": rows} return HttpResponse(json.dumps(result), content_type='application/json') diff --git a/sql/templates/instance.html b/sql/templates/instance.html index 243ddbe5cf..03c43adbf9 100644 --- a/sql/templates/instance.html +++ b/sql/templates/instance.html @@ -91,6 +91,7 @@