Skip to content

Commit

Permalink
解决nginx反向代理django应用非80端口,跳转错误的问题
Browse files Browse the repository at this point in the history
sqladvisor获取优化建议新增选择,可选择是否查看详细优化过程
  • Loading branch information
hhyo authored and lihuanhuan committed Apr 16, 2018
1 parent 7e5cccb commit e454034
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 23 deletions.
2 changes: 2 additions & 0 deletions archer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

ALLOWED_HOSTS = ['*']

# 解决nginx部署跳转404
USE_X_FORWARDED_HOST = True

# Application definition

Expand Down
60 changes: 47 additions & 13 deletions sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def getdbNameList(request):
if is_master:
try:
master_info = master_config.objects.get(cluster_name=clusterName)
# 取出该集群的连接方式,为了后面连进去获取所有databases
# 取出该集群主库的连接方式,为了后面连进去获取所有databases
listDb = dao.getAlldbByCluster(master_info.master_host, master_info.master_port, master_info.master_user,
prpCryptor.decrypt(master_info.master_password))
# 要把result转成JSON存进数据库里,方便SQL单子详细信息展示
Expand Down Expand Up @@ -226,14 +226,31 @@ def getdbNameList(request):
def getTableNameList(request):
clusterName = request.POST.get('cluster_name')
db_name = request.POST.get('db_name')
is_master = request.POST.get('is_master')
result = {'status': 0, 'msg': 'ok', 'data': []}

slave_info = slave_config.objects.get(cluster_name=clusterName)
# 取出该集群的连接方式,为了后面连进去获取所有的表
listTb = dao.getAllTableByDb(slave_info.slave_host, slave_info.slave_port, slave_info.slave_user,
prpCryptor.decrypt(slave_info.slave_password), db_name)
# 要把result转成JSON存进数据库里,方便SQL单子详细信息展示
result['data'] = listTb
if is_master:
try:
master_info = master_config.objects.get(cluster_name=clusterName)
# 取出该集群主库的连接方式,为了后面连进去获取所有的表
listTb = dao.getAllTableByDb(master_info.master_host, master_info.master_port, master_info.master_user,
prpCryptor.decrypt(master_info.master_password), db_name)
# 要把result转成JSON存进数据库里,方便SQL单子详细信息展示
result['data'] = listTb
except Exception:
result['status'] = 1
result['msg'] = '找不到对应的主库配置信息,请配置'
else:
try:
slave_info = slave_config.objects.get(cluster_name=clusterName)
# 取出该集群从库的连接方式,为了后面连进去获取所有的表
listTb = dao.getAllTableByDb(slave_info.slave_host, slave_info.slave_port, slave_info.slave_user,
prpCryptor.decrypt(slave_info.slave_password), db_name)
# 要把result转成JSON存进数据库里,方便SQL单子详细信息展示
result['data'] = listTb
except Exception:
result['status'] = 1
result['msg'] = '找不到对应的从库配置信息,请配置'
return HttpResponse(json.dumps(result), content_type='application/json')


Expand All @@ -243,14 +260,31 @@ def getColumnNameList(request):
clusterName = request.POST.get('cluster_name')
db_name = request.POST.get('db_name')
tb_name = request.POST.get('tb_name')
is_master = request.POST.get('is_master')
result = {'status': 0, 'msg': 'ok', 'data': []}

slave_info = slave_config.objects.get(cluster_name=clusterName)
# 取出该集群的连接方式,为了后面连进去获取表的所有字段
listCol = dao.getAllColumnsByTb(slave_info.slave_host, slave_info.slave_port, slave_info.slave_user,
prpCryptor.decrypt(slave_info.slave_password), db_name, tb_name)
# 要把result转成JSON存进数据库里,方便SQL单子详细信息展示
result['data'] = listCol
if is_master:
try:
master_info = master_config.objects.get(cluster_name=clusterName)
# 取出该集群主库的连接方式,为了后面连进去获取所有字段
listCol = dao.getAllColumnsByTb(master_info.master_host, master_info.master_port, master_info.master_user,
prpCryptor.decrypt(master_info.master_password), db_name, tb_name)
# 要把result转成JSON存进数据库里,方便SQL单子详细信息展示
result['data'] = listCol
except Exception:
result['status'] = 1
result['msg'] = '找不到对应的主库配置信息,请配置'
else:
try:
slave_info = slave_config.objects.get(cluster_name=clusterName)
# 取出该集群的连接方式,为了后面连进去获取表的所有字段
listCol = dao.getAllColumnsByTb(slave_info.slave_host, slave_info.slave_port, slave_info.slave_user,
prpCryptor.decrypt(slave_info.slave_password), db_name, tb_name)
# 要把result转成JSON存进数据库里,方便SQL单子详细信息展示
result['data'] = listCol
except Exception:
result['status'] = 1
result['msg'] = '找不到对应的从库配置信息,请配置'
return HttpResponse(json.dumps(result), content_type='application/json')


Expand Down
21 changes: 19 additions & 2 deletions sql/static/sqladvisor.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
<option value="is-empty" disabled="" selected="selected">请选择数据库:</option>
</select>
</div>
<div class="form-group">
<select id="verbose" name="verbose"
class="form-control selectpicker show-tick bs-select-hidden" data-name="是否查看优化过程"
data-placeholder="是否查看优化过程:" required>
<option value="is-empty" disabled="" selected="selected">是否查看优化过程:</option>
<option value="1"></option>
<option value="0"></option>
</select>
</div>
<div class="form-group">
<input id="btn-format" type="button" class="btn btn-info" value="美化"/>
<input id="btn-explain" type="button" class="btn btn-warning" value="执行计划"/>
Expand Down Expand Up @@ -66,6 +75,7 @@
<script src="{% static 'ace/ace_init.js' %}"></script>
<script src="{% static 'dist/js/sql-formatter.min.js' %}"></script>
<script>
// 表单校验
function sqladvisor_validate() {
var result = true;
var sqlContent = editor.getValue();
Expand All @@ -85,6 +95,7 @@
return result;
}

// 获取优化建议
$("#btn-sqladvisor").click(function () {
//先做表单验证,成功了提交优化请求
if (sqladvisor_validate()) {
Expand All @@ -95,7 +106,7 @@
}
);

//先做表单验证,验证成功再成功提交格式化sql
//格式化sql
$("#btn-format").click(function () {
var select_sqlContent = editor.session.getTextRange(editor.getSelectionRange());
if (select_sqlContent) {
Expand All @@ -110,6 +121,8 @@
editor.clearSelection();
}
);

//获取执行计划
$("#btn-explain").click(function () {
//先做表单验证,成功了提交优化请求
if (sqladvisor_validate()) {
Expand All @@ -120,10 +133,12 @@
}
);

//集群变动获取数据库
$("#cluster_name").change(function () {
get_db_list()
});

//获取主库数据库
function get_db_list() {
//将数据通过ajax提交给获取db_name
$.ajax({
Expand Down Expand Up @@ -175,6 +190,7 @@
var sqlContent = editor.getValue();
var clusterName = $("#cluster_name").val();
var dbName = $("#db_name").val();
var verbose = $("#verbose").val();

//适配从慢查询过来的优化请求
var pathname = window.location.pathname;
Expand All @@ -190,7 +206,8 @@
data: {
sql_content: sqlContent,
cluster_name: clusterName,
db_name: dbName
db_name: dbName,
verbose: verbose

},
complete: function () {
Expand Down
16 changes: 9 additions & 7 deletions sql/views_ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from .const import Const, WorkflowDict
from .inception import InceptionDao
from .aes_decryptor import Prpcrypt
from .models import users, slave_config, workflow
from .models import users, master_config, workflow
from sql.sendmail import MailSender
import logging
from .workflow import Workflow
Expand Down Expand Up @@ -419,10 +419,12 @@ def sqladvisorcheck(request):
sqlContent = request.POST.get('sql_content')
clusterName = request.POST.get('cluster_name')
dbName = request.POST.get('db_name')
verbose = request.POST.get('verbose')
else:
sqlContent = request.POST['sql_content']
clusterName = request.POST['cluster_name']
dbName = request.POST.get('db_name')
dbName = request.POST.POST['db_name']
verbose = request.POST.POST['verbose']
finalResult = {'status': 0, 'msg': 'ok', 'data': []}

# 服务器端参数验证
Expand All @@ -437,16 +439,16 @@ def sqladvisorcheck(request):
finalResult['msg'] = 'SQL语句结尾没有以;结尾,请重新修改并提交!'
return HttpResponse(json.dumps(finalResult), content_type='application/json')

# 取出从库的连接信息
cluster_info = slave_config.objects.get(cluster_name=clusterName)
# 取出主库的连接信息
cluster_info = master_config.objects.get(cluster_name=clusterName)

# 提交给sqladvisor获取审核结果
sqladvisor_path = getattr(settings, 'SQLADVISOR')
sqlContent = sqlContent.rstrip().replace('"', '\\"').replace('`', '\`').replace('\n', ' ')
try:
p = subprocess.Popen(sqladvisor_path + ' -h %s -P %s -u %s -p \'%s\' -d %s -v 1 -q "%s"' % (
str(cluster_info.slave_host), str(cluster_info.slave_port), str(cluster_info.slave_user),
str(prpCryptor.decrypt(cluster_info.slave_password),), str(dbName), sqlContent), stdin=subprocess.PIPE,
p = subprocess.Popen(sqladvisor_path + ' -h "%s" -P "%s" -u "%s" -p "%s\" -d "%s" -v %s -q "%s"' % (
str(cluster_info.master_host), str(cluster_info.master_port), str(cluster_info.master_user),
str(prpCryptor.decrypt(cluster_info.master_password),), str(dbName), verbose, sqlContent), stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, universal_newlines=True)
stdout, stderr = p.communicate()
finalResult['data'] = stdout
Expand Down
1 change: 1 addition & 0 deletions src/docker/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ http {
location / {
proxy_pass http://127.0.0.1:8888;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host:9123;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
Expand Down
2 changes: 1 addition & 1 deletion startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ python3 manage.py collectstatic -v0 --noinput

settings=${1:-"archer.settings"}
ip=${2:-"127.0.0.1"}
port=${3:-8000}
port=${3:-8888}

gunicorn -w 2 --env DJANGO_SETTINGS_MODULE=${settings} --error-logfile=/tmp/archer.err -b ${ip}:${port} --daemon archer.wsgi:application

0 comments on commit e454034

Please sign in to comment.