Skip to content

Commit

Permalink
Merge pull request #260 from yzypals/etime
Browse files Browse the repository at this point in the history
增加sql上线可执行时间选择,且审核人可修改这个时间
  • Loading branch information
hhyo authored Jun 16, 2019
2 parents 9a96d3b + 0c51bf7 commit afee887
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 13 deletions.
2 changes: 2 additions & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class SqlWorkflow(models.Model):
engineer_display = models.CharField('发起人中文名', max_length=50, default='')
status = models.CharField(max_length=50, choices=SQL_WORKFLOW_CHOICES)
audit_auth_groups = models.CharField('审批权限组列表', max_length=255)
run_date_start = models.DateTimeField('可执行起始时间', null=True, blank=True)
run_date_end = models.DateTimeField('可执行结束时间', null=True, blank=True)
create_time = models.DateTimeField('创建时间', auto_now_add=True)
finish_time = models.DateTimeField('结束时间', null=True, blank=True)
is_manual = models.IntegerField('是否原生执行', choices=((0, '否'), (1, '是')), default=0)
Expand Down
56 changes: 50 additions & 6 deletions sql/sql_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sql.models import ResourceGroup, Users
from sql.utils.resource_group import user_groups, user_instances
from sql.utils.tasks import add_sql_schedule, del_schedule
from sql.utils.sql_review import can_timingtask, can_cancel, can_execute
from sql.utils.sql_review import can_timingtask, can_cancel, can_execute, on_correct_time_period
from sql.utils.workflow_audit import Audit
from .models import SqlWorkflow, SqlWorkflowContent, Instance
from django_q.tasks import async_task
Expand Down Expand Up @@ -115,17 +115,19 @@ def check(request):
@permission_required('sql.sql_submit', raise_exception=True)
def submit(request):
"""正式提交SQL, 此处生成工单"""
sql_content = request.POST['sql_content'].strip()
workflow_title = request.POST['workflow_name']
sql_content = request.POST.get('sql_content').strip()
workflow_title = request.POST.get('workflow_name')
# 检查用户是否有权限涉及到资源组等, 比较复杂, 可以把检查权限改成一个独立的方法
group_name = request.POST['group_name']
group_name = request.POST.get('group_name')
group_id = ResourceGroup.objects.get(group_name=group_name).group_id
instance_name = request.POST['instance_name']
instance_name = request.POST.get('instance_name')
instance = Instance.objects.get(instance_name=instance_name)
db_name = request.POST.get('db_name')
is_backup = True if request.POST.get('is_backup') == 'True' else False
notify_users = request.POST.getlist('notify_users')
list_cc_addr = [email['email'] for email in Users.objects.filter(username__in=notify_users).values('email')]
run_date_start = request.POST.get('run_date_start')
run_date_end = request.POST.get('run_date_end')

# 服务器端参数验证
if None in [sql_content, db_name, instance_name, db_name, is_backup]:
Expand Down Expand Up @@ -178,7 +180,9 @@ def submit(request):
db_name=db_name,
is_manual=0,
syntax_type=check_result.syntax_type,
create_time=timezone.now()
create_time=timezone.now(),
run_date_start=run_date_start or None,
run_date_end=run_date_end or None
)
SqlWorkflowContent.objects.create(workflow=sql_workflow,
sql_content=sql_content,
Expand All @@ -205,6 +209,38 @@ def submit(request):
return HttpResponseRedirect(reverse('sql:detail', args=(workflow_id,)))


@permission_required('sql.sql_review', raise_exception=True)
def alter_run_date(request):
"""
审核人修改可执行时间
:param request:
:return:
"""
workflow_id = int(request.POST.get('workflow_id', 0))
run_date_start = request.POST.get('run_date_start')
run_date_end = request.POST.get('run_date_end')
if workflow_id == 0:
context = {'errMsg': 'workflow_id参数为空.'}
return render(request, 'error.html', context)

user = request.user
if Audit.can_review(user, workflow_id, 2) is False:
context = {'errMsg': '你无权操作当前工单!'}
return render(request, 'error.html', context)

try:
# 存进数据库里
SqlWorkflow(id=workflow_id,
run_date_start=run_date_start or None,
run_date_end=run_date_end or None
).save(update_fields=['run_date_start', 'run_date_end'])
except Exception as msg:
context = {'errMsg': msg}
return render(request, 'error.html', context)

return HttpResponseRedirect(reverse('sql:detail', args=(workflow_id,)))


@permission_required('sql.sql_review', raise_exception=True)
def passed(request):
"""
Expand Down Expand Up @@ -264,6 +300,10 @@ def execute(request):
if can_execute(request.user, workflow_id) is False:
context = {'errMsg': '你无权操作当前工单!'}
return render(request, 'error.html', context)

if on_correct_time_period(workflow_id) is False:
context = {'errMsg': '不在可执行时间范围内,如果需要修改执行时间请重新提交工单!'}
return render(request, 'error.html', context)
# 根据执行模式进行对应修改
mode = request.POST.get('mode')
if mode == "auto":
Expand Down Expand Up @@ -325,6 +365,10 @@ def timing_task(request):
run_date = datetime.datetime.strptime(run_date, "%Y-%m-%d %H:%M")
task_name = f"{Const.workflowJobprefix['sqlreview']}-{workflow_id}"

if on_correct_time_period(workflow_id, run_date) is False:
context = {'errMsg': '不在可执行时间范围内,如果需要修改执行时间请重新提交工单!'}
return render(request, 'error.html', context)

# 使用事务保持数据一致性
try:
with transaction.atomic():
Expand Down
94 changes: 92 additions & 2 deletions sql/templates/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h4 style="display: inline;"><span>{{ workflow_detail.workflow_name }}</span></h
<input type="hidden" id="editSqlContent" value="{{ workflow_detail.sqlworkflowcontent.sql_content }}"/>
<hr>
<table data-toggle="table" class="table table-striped table-hover"
style="table-layout:inherit;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">
style="table-layout:inherit;overflow:hidden;text-overflow:ellipsis;">
<thead>
<tr>
<th>
Expand All @@ -35,6 +35,9 @@ <h4 style="display: inline;"><span>{{ workflow_detail.workflow_name }}</span></h
<th>
发起时间
</th>
<th>
可执行时间范围
</th>
<th>
结束时间
</th>
Expand Down Expand Up @@ -72,6 +75,13 @@ <h4 style="display: inline;"><span>{{ workflow_detail.workflow_name }}</span></h
<td>
{{ workflow_detail.create_time }}
</td>
<td>
{% if workflow_detail.run_date_start and workflow_detail.run_date_end %}
{{ workflow_detail.run_date_start }} / {{ workflow_detail.run_date_end }}
{% else %}
无限制
{% endif %}
</td>
<td>
{{ workflow_detail.finish_time }}
</td>
Expand Down Expand Up @@ -154,6 +164,16 @@ <h4 style="display: inline;"><span>{{ workflow_detail.workflow_name }}</span></h
placeholder="请填写审核备注/终止原因" rows=3></textarea>
<br>
{% endif %}
<!--审核人修改可执行时间按钮-->
{% if is_can_review %}
<form id="from-passed" action="/passed/" method="post" style="display:inline-block;">
{% csrf_token %}
<input type="hidden" name="workflow_id" value="{{ workflow_detail.id }}">
<input type="hidden" id="audit_remark" name="audit_remark" value="">
<input type="button" class="btn btn-info" data-toggle="modal" data-target="#Executabletime"
value="可执行时间变更"/>
</form>
{% endif %}
<!--审核通过按钮-->
{% if is_can_review %}
<form id="from-passed" action="/passed/" method="post" style="display:inline-block;">
Expand Down Expand Up @@ -280,16 +300,51 @@ <h4 class="modal-title text-danger">执行进度</h4>
</div>
</div>
</div>
<!-- 修改可执行时间弹出框 -->
<form id="from-alterexecutabletime" action="/alter_run_date/" method="post" style="display:inline-block;">
<div class="modal fade" id="Executabletime" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header ">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4 class="modal-title text-danger">修改可执行时间</h4>
</div>
{% csrf_token %}
<div class="modal-body">
<!--可执行范围时间-->
<div class='form-group input-group'>
<input type="hidden" id="run_date_start" name="run_date_start">
<input type="hidden" id="run_date_end" name="run_date_end">
<input type="text" id="run_date_range" readonly
value="请选择可执行时间范围"
style="background-color: #fff;color: #999 "
class="form-control "/>
<span class="input-group-addon"><span class="fa fa-calendar"></span></span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal">取消</button>

<input type="hidden" name="workflow_id" value="{{ workflow_detail.id }}">
<input type="button" id="btnalterExecutabletime" class="btn btn-danger" value="确认提交"/>
</div>

</div>
</div>
</div>
</form>

{% endblock content %}

{% block js %}
{% load staticfiles %}
<link href="{% static 'datetimepicker/css/bootstrap-datetimepicker.css' %}" rel="stylesheet" type="text/css"/>
<link href="{% static 'daterangepicker/css/daterangepicker.css' %}" rel="stylesheet" type="text/css"/>
<script src="{% static 'daterangepicker/js/moment.min.js' %}"></script>
<script src="{% static 'datetimepicker/js/bootstrap-datetimepicker.js' %}"></script>
<script src="{% static 'datetimepicker/js/bootstrap-datetimepicker.zh-CN.js' %}"></script>
<script src="{% static 'daterangepicker/js/daterangepicker.js' %}"></script>
<!--初始化时间控件 -->
<script>
var date = new Date();
Expand All @@ -305,8 +360,35 @@ <h4 class="modal-title text-danger">执行进度</h4>
inline: true,
sideBySide: true,
minuteStep: 5,
})
});
$("#run_date_range").daterangepicker({
timePicker: true,
timePicker24Hour: true,
autoApply: true,
autoUpdateInput: false,
opens: "left",
drops: "down",
minDate: moment().startOf('minutes'),
startDate: moment().startOf('hour'),
endDate: moment().startOf('hour'),
locale: {
"applyLabel": "确定",
"cancelLabel": "清空",
"daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
"monthNames": ["一月", "二月", "三月", "四月", "五月", "六", "七月", "八月", "九月", "十月", "十一月", "十二月"],
"firstDay": 1
},
}).on('apply.daterangepicker', function (ev, picker) {
$(this).css("color", "#333");
$("#run_date_start").val(picker.startDate.format('YYYY-MM-DD HH:mm'));
$("#run_date_end").val(picker.endDate.format('YYYY-MM-DD HH:mm'));
$(this).val(picker.startDate.format('YYYY-MM-DD HH:mm') + ' / ' + picker.endDate.format('YYYY-MM-DD HH:mm'));
}).on('cancel.daterangepicker', function (ev, picker) {
$(this).css("color", "#999");
$(this).val('请选择可执行时间范围');
});
</script>

<!--按钮控制 -->
<script>
var editWorkflowNname = "{{ workflow_detail.workflow_name }}";
Expand Down Expand Up @@ -398,6 +480,14 @@ <h4 class="modal-title text-danger">执行进度</h4>
}
});

// 修改可执行时间
$("#btnalterExecutabletime").click(function () {
//获取form对象判断输入通过则提交
var formAlterexecutabletime = $("#from-alterexecutabletime");
loading(this);
formAlterexecutabletime.submit();
});

// 按钮禁用
function loading(obj) {
$(obj).button('loading').delay(2500).queue(function () {
Expand Down
43 changes: 42 additions & 1 deletion sql/templates/sqlsubmit.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,22 @@
<select id="is_backup" name="is_backup"
class="selectpicker show-tick form-control bs-select-hidden"
data-name="是否选择备份"
title="请选择是否需要备份"
data-placeholder="请选择是否要备份" required>
<option value="is-empty" disabled="">请选择是否需要备份:</option>
<option value=True selected="selected">备份SQL</option>
<option value=False>不备份SQL</option>
</select>
</div>
{% endif %}
<!--可执行范围时间-->
<div class='form-group'>
<input type="hidden" id="run_date_start" name="run_date_start">
<input type="hidden" id="run_date_end" name="run_date_end">
<input type="text" id="run_date_range" readonly
value="请选择可执行时间范围"
style="background-color: #fff;color: #999 "
class="form-control "/>
</div>
<!--增加通知人-->
<div class="form-group">
<select id="notify_users" name="notify_users"
Expand Down Expand Up @@ -148,6 +157,7 @@ <h4 class="modal-title text-danger">提交信息确认</h4>
{% block js %}
{% load staticfiles %}
<link href="{% static 'bootstrap-fileinput/css/fileinput.min.css' %}" rel="stylesheet">
<link href="{% static 'daterangepicker/css/daterangepicker.css' %}" rel="stylesheet" type="text/css"/>
<script src="{% static 'ace/ace.js' %}"></script>
<script src="{% static 'ace/ext-language_tools.js' %}"></script>
<script src="{% static 'ace/mode-mysql.js' %}"></script>
Expand All @@ -156,7 +166,38 @@ <h4 class="modal-title text-danger">提交信息确认</h4>
<script src="{% static 'ace/ace-init.js' %}"></script>
<script src="{% static 'bootstrap-fileinput/js/fileinput.min.js' %}"></script>
<script src="{% static 'bootstrap-fileinput/js/locales/zh.js' %}"></script>
<script src="{% static 'daterangepicker/js/moment.min.js' %}"></script>
<script src="{% static 'daterangepicker/js/daterangepicker.js' %}"></script>

<!--初始化时间控件 -->
<script>
$("#run_date_range").daterangepicker({
timePicker: true,
timePicker24Hour: true,
autoApply: true,
autoUpdateInput: false,
opens: "left",
drops: "up",
minDate: moment().startOf('minutes'),
startDate: moment().startOf('hour'),
endDate: moment().startOf('hour'),
locale: {
"applyLabel": "确定",
"cancelLabel": "清空",
"daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
"monthNames": ["一月", "二月", "三月", "四月", "五月", "六", "七月", "八月", "九月", "十月", "十一月", "十二月"],
"firstDay": 1
},
}).on('apply.daterangepicker', function (ev, picker) {
$(this).css("color", "#333");
$("#run_date_start").val(picker.startDate.format('YYYY-MM-DD HH:mm'));
$("#run_date_end").val(picker.endDate.format('YYYY-MM-DD HH:mm'));
$(this).val(picker.startDate.format('MM-DD HH:mm') + ' / ' + picker.endDate.format('MM-DD HH:mm'));
}).on('cancel.daterangepicker', function (ev, picker) {
$(this).css("color", "#999");
$(this).val('请选择可执行时间范围');
});
</script>
<!--upload -->
<script>
//初始化上传控件
Expand Down
Loading

0 comments on commit afee887

Please sign in to comment.