Skip to content

Commit

Permalink
draft: 添加sql优化详情的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Grain-Yu committed Nov 23, 2023
1 parent b85d169 commit 7637db3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ class SlowQuery(models.Model):
reviewed_by = models.CharField(max_length=20, blank=True, null=True)
reviewed_on = models.DateTimeField(blank=True, null=True)
comments = models.TextField(blank=True, null=True)
reviewed_status = models.CharField(max_length=24, blank=True, null=True)

class Meta:
managed = False
Expand Down
6 changes: 4 additions & 2 deletions sql/slowlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def slowquery_review(request):
fingerprint__icontains=search,
**filter_kwargs
)
.annotate(SQLText=F("fingerprint"), SQLId=F("checksum"))
.values("SQLText", "SQLId")
.annotate(SQLText=F("fingerprint"), SQLId=F("checksum"), ReviewedBy=F("reviedwd_by"), ReviewedOn=F("reviewed_on"), Comments=F("comments"), ReviewedStatus=F("reviewed_status"))
.values("SQLText", "SQLId", "ReviewedBy", "ReviewedOn", "Comments", "ReviewedStatus")
.annotate(
CreateTime=Max("slowqueryhistory__ts_max"),
DBName=Max("slowqueryhistory__db_max"), # 数据库
Expand Down Expand Up @@ -157,6 +157,7 @@ def slowquery_review_history(request):
sample__icontains=search,
**filter_kwargs
).annotate(
SQLChecksum=F("checksum"), # SQL语句校验和
ExecutionStartTime=F("ts_min"), # 本次统计(每5分钟一次)该类型sql语句出现的最小时间
DBName=F("db_max"), # 数据库名
HostAddress=Concat(
Expand All @@ -175,6 +176,7 @@ def slowquery_review_history(request):
slow_sql_record_list = slow_sql_record_obj.order_by(
"-" + sortName if "desc".__eq__(sortOrder) else sortName
)[offset:limit].values(
"SQLChecksum",
"ExecutionStartTime",
"DBName",
"HostAddress",
Expand Down
36 changes: 36 additions & 0 deletions sql/templates/slowquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!-- 自定义操作按钮-->
<div id="sqladvisor-toolbar" class="btn-group right" style="display:none;">
<button id=sqladvisor class="btn btn-default" style="" type="button">获取优化建议</button>
<button id=sqlreview class="btn btn-default" style="" type="button">编辑优化详情</button>
</div>
<!-- Nav tabs -->
<ul id="nav-tabs" class="nav nav-tabs" role="tablist">
Expand Down Expand Up @@ -269,6 +270,25 @@
}
});
});

//点击编辑按钮编辑优化详情
$(function () {
$("#sqlreview").click(function () {
if ($("#slowsqlinfo-list").bootstrapTable('getSelections')[0]) {
var review_sql_content = $("#slowsqlinfo-list").bootstrapTable('getSelections')[0]['SQLText'] + ';';
var review_db_name = $("#slowsqlinfo-list").bootstrapTable('getSelections')[0]['DBName'];
var review_instance_name = $("#instance_name").val();
var review_checksum = $("#slowsqlinfo-list").bootstrapTable('getSelections')[0]['SQLChecksum']
sessionStorage.setItem('review_sql_content ', review_sql_content );
sessionStorage.setItem('review_db_name', review_db_name);
sessionStorage.setItem('review_instance_name', review_instance_name);
sessionStorage.setItem('review_checksum', review_checksum);
location.href = '/slowquery_review/';
} else {
alert("请先选择需要优化的sql")
}
});
});
</script>
<!-- 获取列表数据 -->
<script>
Expand Down Expand Up @@ -427,6 +447,22 @@
title: '平均返回行数',
field: 'ReturnRowAvg',
sortable:true
}, {
title: '优化人',
field: 'ReviewedBy',
sortable:true
}, {
title: '优化日期',
field: 'ReviewedOn',
sortable:true
}, {
title: '优化详情',
field: 'Comments',
sortable:true
}, {
title: '优化状态',
field: 'ReviewedStatus',
sortable:true
}],
locale: 'zh-CN',
onLoadError: onLoadErrorCallback,
Expand Down
1 change: 1 addition & 0 deletions sql/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
path("sqlquery/", views.sqlquery),
path("slowquery/", views.slowquery),
path("sqladvisor/", views.sqladvisor),
path("sqlreview/", views.sqlreview),
path("slowquery_advisor/", views.sqladvisor),
path("queryapplylist/", views.queryapplylist),
path(
Expand Down
4 changes: 4 additions & 0 deletions sql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ def slowquery(request):
"""SQL慢日志页面"""
return render(request, "slowquery.html")

@permission_required("sql.menu_sqlreview", raise_exception=True)
def sqlreview(request):
"""SQL优化详情页面"""
return render(request, "sqlreview.html")

@permission_required("sql.menu_instance", raise_exception=True)
def instance(request):
Expand Down

0 comments on commit 7637db3

Please sign in to comment.