Skip to content

Commit

Permalink
新增发起SQL上线页面'帮我检查SQL'功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jly8866 committed Apr 20, 2017
1 parent 9e885e8 commit 9b4d4ee
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 45 deletions.
10 changes: 5 additions & 5 deletions archer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,27 @@
'NAME': 'archer',
'USER': 'archer_rw',
'PASSWORD': 'archer_rw',
'HOST': '127.0.0.1',
'HOST': '172.21.139.1',
'PORT': '5000'
}
}

#inception组件所在的地址
INCEPTION_HOST = '192.168.1.11'
INCEPTION_HOST = '172.16.5.7'
INCEPTION_PORT = '6100'

#查看回滚SQL时候会用到,这里要告诉archer去哪个mysql里读取inception备份的回滚信息和SQL.
#注意这里要和inception组件的inception.conf里的inception_remote_XX部分保持一致.
INCEPTION_REMOTE_BACKUP_HOST='192.168.1.12'
INCEPTION_REMOTE_BACKUP_HOST='172.16.5.10'
INCEPTION_REMOTE_BACKUP_PORT=5621
INCEPTION_REMOTE_BACKUP_USER='inception'
INCEPTION_REMOTE_BACKUP_PASSWORD='inception'

#是否开启邮件提醒功能:发起SQL上线后会发送邮件提醒审核人审核,执行完毕会发送给DBA. on是开,off是关,配置为其他值均会被archer认为不开启邮件功能
MAIL_ON_OFF='on'

MAIL_REVIEW_SMTP_SERVER='mail.xxx.com'
MAIL_REVIEW_SMTP_SERVER='172.21.129.215'
MAIL_REVIEW_SMTP_PORT=25
MAIL_REVIEW_FROM_ADDR='[email protected]' #发件人,也是登录SMTP server需要提供的用户名
MAIL_REVIEW_FROM_PASSWORD='' #发件人邮箱密码,如果为空则不需要login SMTP server
MAIL_REVIEW_DBA_ADDR=['zhangsan@abc.com', 'lisi01@abc.com'] #DBA地址,执行完毕会发邮件给DBA,以list形式保存
MAIL_REVIEW_DBA_ADDR=['jialiyang@baijiahulian.com', 'zhangpeng@baijiahulian.com'] #DBA地址,执行完毕会发邮件给DBA,以list形式保存
2 changes: 1 addition & 1 deletion sql/inception.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self):
except ValueError as v:
print("Error: %s" % v)

def sqlautoReview(self, sqlContent, clusterName, isBackup):
def sqlautoReview(self, sqlContent, clusterName, isBackup='否'):
'''
将sql交给inception进行自动审核,并返回审核结果。
'''
Expand Down
2 changes: 1 addition & 1 deletion sql/static/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h4 class="modal-title" id="myModalLabel">
<!-- 包括所有已编译的插件 -->
<script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
<script src="{% static 'dist/js/bootstrap-select.js' %}"></script>
<script src="{% static 'user/js/user1.js' %}"></script>
<script src="{% static 'user/js/autoreview.js' %}"></script>
</body>

</html>
52 changes: 47 additions & 5 deletions sql/static/submitSql.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<form action="/autoreview/" method="post" class="form-horizontal" role="form">
{% csrf_token %}
<div class="col-md-8 column">
<textarea name="sql_content" class="form-control" placeholder="请在此提交SQL,请以分号结尾。例如:use test; create table t1(id int)engine=innodb;" rows=20 required></textarea>
<textarea id="sql_content" name="sql_content" class="form-control" placeholder="请在此提交SQL,请以分号结尾。例如:use test; create table t1(id int)engine=innodb;" rows=20 required></textarea>
</div>

<div class="col-md-4 column">
Expand All @@ -29,26 +29,68 @@
</select>
</div>
<div class="form-group">
<select id="basic" name="is_backup" class="selectpicker show-tick form-control bs-select-hidden" data-name="是否选择备份" data-placeholder="请选择是否要备份:" required>
<select id="is_backup" name="is_backup" class="selectpicker show-tick form-control bs-select-hidden" data-name="是否选择备份" data-placeholder="请选择是否要备份:" required>
<option disabled="" selected="selected">请选择是否要备份:</option>
<option value=""></option>
<option value=""></option>
</select>
</div>
<div class="form-group">
<select id="basic" name="review_man" class="selectpicker show-tick form-control bs-select-hidden" data-name="审核人" data-placeholder="请选择RD审核人:" required>
<select id="review_man" name="review_man" class="selectpicker show-tick form-control bs-select-hidden" data-name="审核人" data-placeholder="请选择RD审核人:" required>
<option disabled="" selected="selected">请选择审核人:</option>
{% for man in reviewMen %}
<option value="{{man}}">{{man}}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">提交SQL</button>
<button type="reset" class="btn btn-default">清空重填</button>
<button type="submit" class="btn btn-primary">提交SQL</button>
<input type="button" id="btn-autoreview" class="btn btn-info" value="帮我检查" />
<button type="reset" class="btn btn-default">清空重填</button>
</div>
</form>
</div>
</div>

<div id="inception-result" style="display:none;" class="row clearfix">
<br/>
<div id="inception-result-col" class="col-md-12">
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">
×
</button>
<table class="">
<tr>
<td width="800px">CREATE TABLE `dba_z120` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', `cc_video_id` varchar(120) NOT NULL DEFAULT '0' COMMENT '视频id', `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户id', `v_status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态值,1为正常,0为删除', `duration` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '片长', `cover` varchar(255) NOT NULL DEFAULT '' COMMENT '封面', `status` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间', PRIMARY KEY (`id`), KEY `idx_user_id` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户视频表'</td>
<td><strong>自动审核结果:</strong><span id="result1">incetipinf ffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span></td>
</tr>
</table>
</div>
<div class="alert alert-dismissable alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">
×
</button>
<table class="">
<tr>
<td width="800px">create table t222(id int)engine=innodb </td>
<td><strong>自动审核结果:</strong>incetipinf ffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td>
</tr>
</table>
</div>
<!--<div class="alert alert-dismissable alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">
×
</button>
<table class="">
<tr>
<td width="800px">create table t222(id int)engine=innodb </td>
<td>自动审核结果:incetipinf ffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td>
</tr>
</table>
</div> -->

</div>
</div>
</div>
</div>
{% endblock content %}
76 changes: 76 additions & 0 deletions sql/static/user/js/autoreview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
$("#btn-autoreview").click(function(){
autoreview();
});

function authenticateUser111() {
var inputUsername = $('#inputUsername');
var inputPassword = $('#inputPassword');
$.ajax({
type: "post",
url: "/authenticate/",
dataType: "json",
data: {
username: inputUsername.val(),
password: inputPassword.val()
},
complete: function () {
},
success: function (data) {
if (data.status == 0) {
$(location).attr('href','/allworkflow/');
} else {
$('#wrongpwd-modal-body').html(data.msg);
$('#wrongpwd-modal').modal({
keyboard: true
});
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
};

function autoreview() {
var sqlContent = $("#sql_content");
var clusterName = $("#cluster_name");

//将数据通过ajax提交给后端进行检查
$.ajax({
type: "post",
url: "/simplecheck/",
dataType: "json",
data: {
sql_content: sqlContent.val(),
cluster_name: clusterName.val()
},
complete: function() {

},
success: function (data) {
if (data.status == 0) {
//console.log(data.data);
var result = data.data;
var finalHtml = "";
for (var i=0; i<result.length; i++) {
//索引5是SQL,4是审核建议
alertStyle = "alert-success";
if (result[i][4] != "None") {
alertStyle = "alert-danger";
}
finalHtml += "<div class='alert alert-dismissable " + alertStyle + "'> <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>x</button> <table class=''> <tr> <td width='800px'>" + result[i][5] + "</td> <td><strong>自动审核结果:</strong>" + result[i][4] + "</td> </tr> </table> </div>";
}

$("#inception-result-col").html(finalHtml);
//填充内容后展现出来
$("#inception-result").show();
} else {
alert("status: " + data.status + "\nmsg: " + data.msg + data.data);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});

}
6 changes: 4 additions & 2 deletions sql/urls.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# -*- coding: UTF-8 -*-

from django.conf.urls import url,include
from . import views
from . import views, views_ajax

urlpatterns = [
url(r'^$', views.allworkflow, name='allworkflow'),
url(r'^index/$', views.allworkflow, name='allworkflow'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
url(r'^authenticate/$', views.authenticate, name='authenticate'),
url(r'^submitsql/$', views.submitSql, name='submitSql'),
url(r'^allworkflow/$', views.allworkflow, name='allworkflow'),

Expand All @@ -18,4 +17,7 @@
url(r'^cancel/$', views.cancel, name='cancel'),
url(r'^rollback/$', views.rollback, name='rollback'),
url(r'^dbaprinciples/$', views.dbaprinciples, name='dbaprinciples'),

url(r'^authenticate/$', views_ajax.authenticate, name='authenticate'),
url(r'^simplecheck/$', views_ajax.simplecheck, name='simplecheck'),
]
35 changes: 4 additions & 31 deletions sql/views.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
# -*- coding: UTF-8 -*-


import re
import json
import time
import multiprocessing

from django.db.models import Q
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from django.contrib.auth.hashers import check_password
from django.conf import settings

from .dao import Dao
from .const import Const
from .sendmail import MailSender
from .inception import InceptionDao
from .models import users, master_config, workflow
from .aes_decryptor import Prpcrypt
from django.db.models import Q
from .models import users, master_config, workflow

dao = Dao()
inceptionDao = InceptionDao()
mailSender = MailSender()
prpCryptor = Prpcrypt()

# Create your views here.
def login(request):
return render(request, 'login.html')

Expand All @@ -35,31 +33,6 @@ def logout(request):
del request.session['login_username']
return render(request, 'login.html')

#ajax接口,登录页面调用,用来验证用户名密码
@csrf_exempt
def authenticate(request):
"""认证机制做的非常简单"""
if request.is_ajax():
strUsername = request.POST.get('username')
strPassword = request.POST.get('password')
else:
strUsername = request.POST['username']
strPassword = request.POST['password']

result = {}
#服务端二次验证参数
if strUsername == "" or strPassword == "" or strUsername is None or strPassword is None:
result = {'status':2, 'msg':'登录用户名或密码为空,请重新输入!', 'data':''}

correct_users = users.objects.filter(username=strUsername)
if len(correct_users) == 1 and check_password(strPassword, correct_users[0].password) == True:
#调用了django内置函数check_password函数检测输入的密码是否与django默认的PBKDF2算法相匹配
request.session['login_username'] = strUsername
result = {'status':0, 'msg':'ok', 'data':''}
else:
result = {'status':1, 'msg':'用户名或密码错误,请重新输入!', 'data':''}
return HttpResponse(json.dumps(result), content_type='application/json')

#首页,也是查看所有SQL工单页面,具备翻页功能
def allworkflow(request):
#一个页面展示
Expand Down
84 changes: 84 additions & 0 deletions sql/views_ajax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: UTF-8 -*-

import re
import json
import time
import multiprocessing

from django.db.models import Q
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.hashers import check_password

from .dao import Dao
from .const import Const
from .inception import InceptionDao
from .aes_decryptor import Prpcrypt
from .models import users, master_config, workflow

dao = Dao()
inceptionDao = InceptionDao()
prpCryptor = Prpcrypt()

#ajax接口,登录页面调用,用来验证用户名密码
@csrf_exempt
def authenticate(request):
"""认证机制做的非常简单"""
if request.is_ajax():
strUsername = request.POST.get('username')
strPassword = request.POST.get('password')
else:
strUsername = request.POST['username']
strPassword = request.POST['password']

result = {}
#服务端二次验证参数
if strUsername == "" or strPassword == "" or strUsername is None or strPassword is None:
result = {'status':2, 'msg':'登录用户名或密码为空,请重新输入!', 'data':''}

correct_users = users.objects.filter(username=strUsername)
if len(correct_users) == 1 and check_password(strPassword, correct_users[0].password) == True:
#调用了django内置函数check_password函数检测输入的密码是否与django默认的PBKDF2算法相匹配
request.session['login_username'] = strUsername
result = {'status':0, 'msg':'ok', 'data':''}
else:
result = {'status':1, 'msg':'用户名或密码错误,请重新输入!', 'data':''}
return HttpResponse(json.dumps(result), content_type='application/json')


#提交SQL给inception进行自动审核
@csrf_exempt
def simplecheck(request):
if request.is_ajax():
sqlContent = request.POST.get('sql_content')
clusterName = request.POST.get('cluster_name')
else:
sqlContent = request.POST['sql_content']
clusterName = request.POST['cluster_name']

finalResult = {'status':0, 'msg':'ok', 'data':[]}
#服务器端参数验证
if sqlContent is None or clusterName is None:
finalResult['status'] = 1
finalResult['msg'] = '页面提交参数可能为空'
return HttpResponse(json.dumps(finalResult), content_type='application/json')

sqlContent = sqlContent.rstrip()
if sqlContent[-1] != ";":
finalResult['status'] = 1
finalResult['msg'] = 'SQL语句结尾没有以;结尾,请重新修改并提交!'
return HttpResponse(json.dumps(finalResult), content_type='application/json')

#交给inception进行自动审核
result = inceptionDao.sqlautoReview(sqlContent, clusterName)
if result is None or len(result) == 0:
finalResult['status'] = 1
finalResult['msg'] = 'inception返回的结果集为空!可能是SQL语句有语法错误'
return HttpResponse(json.dumps(finalResult), content_type='application/json')
#要把result转成JSON存进数据库里,方便SQL单子详细信息展示
finalResult['data'] = result
return HttpResponse(json.dumps(finalResult), content_type='application/json')

0 comments on commit 9b4d4ee

Please sign in to comment.