Skip to content

Commit

Permalink
Merge branch 'master' into new_pr
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyo authored Jan 13, 2022
2 parents 6793607 + c996a87 commit fe7fe9c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 10 deletions.
17 changes: 17 additions & 0 deletions common/utils/convert.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion sql/engines/pgsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sql/engines/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 9 additions & 1 deletion sql/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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]

Expand Down
7 changes: 5 additions & 2 deletions sql/resource_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down
13 changes: 10 additions & 3 deletions sql/templates/instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ <h4 class="modal-title" id="myModalLabel">清理Binlog日志</h4>
pagination: true, //是否显示分页*
sortable: true, //是否启用排序
sortOrder: "asc", //排序方式
sortName: 'id', //排序字段
sidePagination: "server", //分页方式client客户端分页server服务端分页*
pageNumber: 1, //初始化加载第一页默认第一页,并记录
pageSize: 14, //每页的记录行数*
Expand Down Expand Up @@ -122,15 +123,19 @@ <h4 class="modal-title" id="myModalLabel">清理Binlog日志</h4>
type: $("#type").val(),
db_type: $("#db_type").val(),
tags: $("#tags").val(),
sortName: params.sort,
sortOrder: params.order
}
},
columns: [
{
title: 'ID',
field: 'id'
field: 'id',
sortable: true
}, {
title: '实例名称',
field: 'instance_name',
sortable: true,
formatter: function (value, row, index) {
return "<a target=\"_blank\" href=\"/admin/sql/instance/" + row.id + "/change/\">" + value + "</a>"
}
Expand All @@ -139,10 +144,12 @@ <h4 class="modal-title" id="myModalLabel">清理Binlog日志</h4>
field: 'type'
}, {
title: '数据库类型',
field: 'db_type'
field: 'db_type',
sortable: true
}, {
title: 'HOST',
field: 'host'
field: 'host',
sortable: true
}, {
title: 'PORT',
field: 'port'
Expand Down
5 changes: 3 additions & 2 deletions sql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from common.config import SysConfig
from sql.engines import get_engine
from common.utils.permission import superuser_required
from common.utils.convert import Convert
from sql.engines.models import ReviewResult, ReviewSet
from sql.utils.tasks import task_info

Expand Down Expand Up @@ -68,7 +69,7 @@ def sqlworkflow(request):
else:
filter_dict['engineer'] = user.username
instance_id = SqlWorkflow.objects.filter(**filter_dict).values('instance_id').distinct()
instance = Instance.objects.filter(pk__in=instance_id)
instance = Instance.objects.filter(pk__in=instance_id).order_by(Convert('instance_name', 'gbk').asc())
resource_group_id = SqlWorkflow.objects.filter(**filter_dict).values('group_id').distinct()
resource_group = ResourceGroup.objects.filter(group_id__in=resource_group_id)

Expand Down Expand Up @@ -342,7 +343,7 @@ def archive(request):
"""归档列表页面"""
# 获取资源组
group_list = user_groups(request.user)
ins_list = user_instances(request.user, db_type=['mysql'])
ins_list = user_instances(request.user, db_type=['mysql']).order_by(Convert('instance_name', 'gbk').asc())
return render(request, 'archive.html', {'group_list': group_list, 'ins_list': ins_list})


Expand Down

0 comments on commit fe7fe9c

Please sign in to comment.