Skip to content

Commit

Permalink
增加实例标签功能
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyo committed May 3, 2019
1 parent ce7a19a commit c7e7528
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 20 deletions.
30 changes: 24 additions & 6 deletions sql/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Register your models here.
from .models import Users, Instance, SqlWorkflow, SqlWorkflowContent, QueryLog, DataMaskingColumns, DataMaskingRules, \
AliyunAccessKey, AliyunRdsConfig, ResourceGroup, ResourceGroupRelations, QueryPrivilegesApply, QueryPrivileges, \
WorkflowAudit, WorkflowLog, ParamTemplate, ParamHistory
WorkflowAudit, WorkflowLog, ParamTemplate, ParamHistory, InstanceTag, InstanceTagRelations


# 用户管理
Expand Down Expand Up @@ -39,18 +39,36 @@ class ResourceGroupRelationsAdmin(admin.ModelAdmin):
list_display = ('object_type', 'object_id', 'object_name', 'group_id', 'group_name', 'create_time')


# 阿里云实例配置
class AliRdsConfigInline(admin.TabularInline):
model = AliyunRdsConfig
# 实例标签配置
@admin.register(InstanceTag)
class InstanceTagAdmin(admin.ModelAdmin):
list_display = ('id', 'tag_code', 'tag_name', 'active', 'create_time')
list_display_links = ('id', 'tag_code',)


# 实例标签关系配置
@admin.register(InstanceTagRelations)
class InstanceTagRelationsAdmin(admin.ModelAdmin):
list_display = ('instance', 'instance_tag', 'active', 'create_time')
list_filter = ('instance', 'instance_tag', 'active')


# 实例管理
@admin.register(Instance)
class InstanceAdmin(admin.ModelAdmin):
list_display = ('id', 'instance_name', 'db_type', 'type', 'host', 'port', 'user', 'create_time')
search_fields = ['instance_name', 'host', 'port', 'user']
list_filter = ('db_type', 'type',)
inlines = [AliRdsConfigInline]
list_filter = ('db_type', 'type')

# 阿里云实例关系配置
class AliRdsConfigInline(admin.TabularInline):
model = AliyunRdsConfig

# 实例标签关系配置
class InstanceTagRelationsInline(admin.TabularInline):
model = InstanceTagRelations

inlines = [InstanceTagRelationsInline, AliRdsConfigInline]


# SQL工单内容
Expand Down
22 changes: 22 additions & 0 deletions sql/fixtures/initial_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"model": "sql.InstanceTag",
"pk": 1,
"fields": {
"tag_code": "write",
"tag_name": "支持上线",
"active": true,
"create_time": "2019-05-03"
}
},
{
"model": "sql.InstanceTag",
"pk": 2,
"fields": {
"tag_code": "read",
"tag_name": "支持查询",
"active": true,
"create_time": "2019-05-03"
}
}
]
12 changes: 9 additions & 3 deletions sql/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
from .models import Instance, ParamTemplate, ParamHistory


# 获取实例列表
@permission_required('sql.menu_instance', raise_exception=True)
def lists(request):
"""获取实例列表"""
limit = int(request.POST.get('limit'))
offset = int(request.POST.get('offset'))
type = request.POST.get('type')
db_type = request.POST.get('db_type')
tags = request.POST.getlist('tags[]')
limit = offset + limit
search = request.POST.get('search', '')

Expand All @@ -34,6 +35,10 @@ def lists(request):
# 过滤数据库类型
if db_type:
instances = instances.filter(db_type=db_type)
# 过滤标签,返回同时包含全部标签的实例,循环会生成多表JOIN,如果数据量大会存在效率问题
if tags:
for tag in tags:
instances = instances.filter(instancetagrelations__instance_tag=tag)

count = instances.count()
instances = instances[offset:limit].values("id", "instance_name", "db_type", "type", "host", "port", "user")
Expand All @@ -45,9 +50,9 @@ def lists(request):
content_type='application/json')


# 获取实例用户列表
@permission_required('sql.menu_instance', raise_exception=True)
def users(request):
"""获取实例用户列表"""
instance_id = request.POST.get('instance_id')
try:
instance = Instance.objects.get(id=instance_id)
Expand Down Expand Up @@ -184,9 +189,9 @@ def param_edit(request):
return HttpResponse(json.dumps(result), content_type='application/json')


# 对比实例schema信息
@permission_required('sql.menu_schemasync', raise_exception=True)
def schemasync(request):
"""对比实例schema信息"""
instance_name = request.POST.get('instance_name')
db_name = request.POST.get('db_name')
target_instance_name = request.POST.get('target_instance_name')
Expand Down Expand Up @@ -317,6 +322,7 @@ def instance_resource(request):


def describe(request):
"""获取表结构"""
instance_name = request.POST.get('instance_name')
try:
instance = Instance.objects.get(instance_name=instance_name)
Expand Down
31 changes: 31 additions & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,37 @@ def save(self, *args, **kwargs):
super(Instance, self).save(*args, **kwargs)


class InstanceTag(models.Model):
"""实例标签配置"""
tag_code = models.CharField('标签代码', max_length=20, unique=True)
tag_name = models.CharField('标签名称', max_length=20, unique=True)
active = models.BooleanField('激活状态', default=True)
create_time = models.DateTimeField('创建时间', auto_now_add=True)

def __str__(self):
return self.tag_name

class Meta:
managed = True
db_table = 'sql_instance_tag'
verbose_name = u'实例标签'
verbose_name_plural = u'实例标签'


class InstanceTagRelations(models.Model):
"""实例标签关系"""
instance = models.ForeignKey(Instance, on_delete=models.CASCADE)
instance_tag = models.ForeignKey(InstanceTag, on_delete=models.CASCADE)
active = models.BooleanField('激活状态', default=True)
create_time = models.DateTimeField('创建时间', auto_now_add=True)

class Meta:
managed = True
db_table = 'sql_instance_tag_relations'
verbose_name = u'实例标签关系'
verbose_name_plural = u'实例标签关系'


SQL_WORKFLOW_CHOICES = (
('workflow_finish', _('workflow_finish')),
('workflow_abort', _('workflow_abort')),
Expand Down
35 changes: 27 additions & 8 deletions sql/templates/instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@

{% block content %}
<!-- 自定义操作按钮-->
<div id="toolbar" class="form-inline">
<div class="form-group bootstrap-select ">
<select id="type" class="dropdown-menu-right selectpicker ">
<option value="" selected="selected">全部实例类型</option>
<div id="toolbar" class="form-inline pull-left">
<div class="form-group">
<select id="type" class="form-control selectpicker">
<option value="" selected="selected">实例类型</option>
<option value="master">MASTER</option>
<option value="slave">SLAVE</option>
</select>
</div>
<div class="form-group bootstrap-select ">
<select id="db_type" class="dropdown-menu-right selectpicker ">
<option value="" selected="selected">全部数据库类型</option>
<div class="form-group">
<select id="db_type" class="form-control selectpicker">
<option value="" selected="selected">数据库类型</option>
<option value="mysql">MySQL</option>
<option value="mssql">MsSQL</option>
<option value="redis">Redis</option>
<option value="pgsql">PgSQL</option>
</select>
</div>
<div class="form-group">
<select id="tags" class="form-control selectpicker" data-live-search="true"
multiple data-selected-text-format="count > 2" data-none-selected-text="标签筛选">
{% for tag in tags %}
<option value="{{ tag.id }}">{{ tag.tag_name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group ">
<button id="btn_add_master" type="button" class="btn btn-default"
onclick="window.location.href='/admin/sql/instance/add/'">
Expand Down Expand Up @@ -66,6 +74,12 @@ <h4 class="modal-title" id="myModalLabel">清理Binlog日志</h4>
<script src="{% static 'bootstrap-table/js/bootstrap-table-export.min.js' %}"></script>
<script src="{% static 'bootstrap-table/js/tableExport.min.js' %}"></script>
<script>
//选择控件初始化
$("#tag").selectpicker({
countSelectedText: "已选中{0}项",
selectedTextFormat: "count > 2"
});

//获取列表
function instancelist() {
//采取异步请求
Expand Down Expand Up @@ -109,7 +123,8 @@ <h4 class="modal-title" id="myModalLabel">清理Binlog日志</h4>
offset: params.offset,
search: params.search,
type: $("#type").val(),
db_type: $("#db_type").val()
db_type: $("#db_type").val(),
tags: $("#tags").val(),
}
},
columns: [
Expand Down Expand Up @@ -249,6 +264,10 @@ <h4 class="modal-title" id="myModalLabel">清理Binlog日志</h4>
instancelist();
});

$("#tags").change(function () {
instancelist();
});

//初始化数据
$(document).ready(function () {
instancelist()
Expand Down
6 changes: 4 additions & 2 deletions sql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sql.utils.tasks import task_info

from .models import Users, SqlWorkflow, QueryPrivileges, ResourceGroup, \
QueryPrivilegesApply, Config, SQL_WORKFLOW_CHOICES
QueryPrivilegesApply, Config, SQL_WORKFLOW_CHOICES, InstanceTag
from sql.utils.workflow_audit import Audit
from sql.utils.sql_review import can_execute, can_timingtask, can_cancel
from common.utils.const import Const, WorkflowDict
Expand Down Expand Up @@ -304,7 +304,9 @@ def groupmgmt(request, group_id):
# 实例管理页面
@permission_required('sql.menu_instance', raise_exception=True)
def instance(request):
return render(request, 'instance.html')
# 获取实例标签
tags = InstanceTag.objects.filter(active=True)
return render(request, 'instance.html', {'tags': tags})


# 实例用户管理页面
Expand Down
37 changes: 36 additions & 1 deletion src/init_sql/v1.5.3_v1.5.4.sql
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
ALTER TABLE param_history CHANGE update_time create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '参数修改时间';
-- 变更字段名
ALTER TABLE param_history CHANGE update_time create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '参数修改时间';

-- 变更字符集为utf8mb4,本身是utf8mb4的无需执行该语句
ALTER TABLE sql_workflow_content
modify `sql_content` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '提交的SQL文本',
modify `review_content` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '自动审核内容的JSON格式',
modify `execute_result` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '执行结果的JSON格式';

ALTER TABLE query_log
modify `sqllog` longtext CHARACTER SET utf8mb4 NOT NULL COMMENT '执行的sql查询';

-- 增加实例标签配置
CREATE TABLE `sql_instance_tag` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '标签id',
`tag_code` varchar(20) NOT NULL COMMENT '标签代码',
`tag_name` varchar(20) NOT NULL COMMENT '标签名称',
`active` tinyint(1) NOT NULL COMMENT '激活状态',
`create_time` datetime(6) NOT NULL COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_tag_code` (`tag_code`),
UNIQUE KEY `uniq_tag_name` (`tag_name`)
) COMMENT '实例标签配置' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `sql_instance_tag_relations` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '关联id',
`instance_id` int(11) NOT NULL COMMENT '关联实例ID',
`instance_tag_id` int(11) NOT NULL COMMENT '关联标签ID',
`active` tinyint(1) NOT NULL COMMENT '激活状态',
`create_time` datetime(6) NOT NULL COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_instance_id` (`instance_id`),
KEY `idx_instance_tag_id` (`instance_tag_id`),
CONSTRAINT `fk_itr_instance` FOREIGN KEY (`instance_id`) REFERENCES `sql_instance` (`id`),
CONSTRAINT `fk_itr_instance_tag` FOREIGN KEY (`instance_tag_id`) REFERENCES `sql_instance_tag` (`id`)
) COMMENT '实例标签关系' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

0 comments on commit c7e7528

Please sign in to comment.