diff --git a/dev-requirements.txt b/dev-requirements.txt index 0b20185751..2c1e386094 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -3,4 +3,4 @@ pytest-django pytest-mock pytest-cov codecov -flake8 \ No newline at end of file +flake8 diff --git a/pyproject.toml b/pyproject.toml index a53e3a2af6..042172e084 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,4 +25,4 @@ omit = [ # omit this single file "sql/migrations/*", "venv*" -] \ No newline at end of file +] diff --git a/sql/models.py b/sql/models.py index 405692459d..af716302ed 100755 --- a/sql/models.py +++ b/sql/models.py @@ -981,6 +981,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 diff --git a/sql/slowlog.py b/sql/slowlog.py index 3db96c6142..2dc2f100bc 100644 --- a/sql/slowlog.py +++ b/sql/slowlog.py @@ -6,7 +6,7 @@ from django.contrib.auth.decorators import permission_required from django.db.models import F, Sum, Value as V, Max from django.db.models.functions import Concat -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse from django.views.decorators.cache import cache_page from pyecharts.charts import Line from pyecharts import options as opts @@ -14,6 +14,9 @@ from sql.engines import get_engine from sql.utils.resource_group import user_instances +from sql.utils.instance_management import ( + SUPPORTED_MANAGEMENT_DB_TYPE, +) from common.utils.extend_json_encoder import ExtendJSONEncoder from .models import Instance, SlowQuery, SlowQueryHistory, AliyunRdsConfig @@ -68,8 +71,22 @@ 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("reviewed_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"), # 数据库 @@ -162,6 +179,7 @@ def slowquery_review_history(request): sample__icontains=search, **filter_kwargs ).annotate( + SQLChecksum=F("checksum"), # SQL语句校验和 ExecutionStartTime=F( "ts_min" ), # 本次统计(每5分钟一次)该类型sql语句出现的最小时间 @@ -182,6 +200,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", @@ -251,3 +270,47 @@ def report(request): result = {"status": 0, "msg": "", "data": line.render_embed()} return HttpResponse(json.dumps(result), content_type="application/json") + + +@permission_required("sql.menu_slowquery", raise_exception=True) +def listreview(request): + """获取优化详情""" + checksum = request.POST.get("review_checksum") + if not checksum: + return JsonResponse({"status": 0, "msg": "Checksum获取失败", "data": []}) + review_details = SlowQuery.objects.filter(checksum=checksum).values( + "checksum", "reviewed_by", "reviewed_on", "comments", "reviewed_status" + ) + rows = [row for row in review_details] + if not rows: + result = {"status": 1, "msg": "优化详情获取失败"} + else: + result = {"status": 0, "msg": "ok", "rows": rows} + return HttpResponse( + json.dumps(result, cls=ExtendJSONEncoder, bigint_as_string=True), + content_type="application/json", + ) + + +@permission_required("sql.menu_slowquery", raise_exception=True) +def editreview(request): + """更新优化详情""" + checksum = request.POST.get("checksum") + reviewed_by = request.POST.get("reviewed_by", "") + reviewed_on = request.POST.get("reviewed_on", "") + comments = request.POST.get("comments", "") + reviewed_status = request.POST.get("reviewed_status", "") + if not all([checksum, reviewed_by, reviewed_on, comments, reviewed_status]): + return JsonResponse( + {"status": 1, "msg": "参数不完整,请确认后提交", "data": []} + ) + record = SlowQuery.objects.filter(checksum=checksum) + if not record: + return JsonResponse({"status": 1, "msg": "记录不存在", "data": []}) + record.update( + reviewed_by=reviewed_by, + reviewed_on=reviewed_on, + comments=comments, + reviewed_status=reviewed_status, + ) + return JsonResponse({"status": 0, "msg": "", "data": []}) diff --git a/sql/templates/slowquery.html b/sql/templates/slowquery.html index 0219c89419..38e818e319 100644 --- a/sql/templates/slowquery.html +++ b/sql/templates/slowquery.html @@ -4,6 +4,7 @@