Commit 2e7fdf7b authored by zhengjinlei's avatar zhengjinlei

概览统计加速

parent f2f867e9
...@@ -5,7 +5,7 @@ from rest_framework import viewsets ...@@ -5,7 +5,7 @@ from rest_framework import viewsets
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.request import Request from rest_framework.request import Request
from rest_framework.response import Response from rest_framework.response import Response
from inspect_report.models import Tasks, CheckSession from inspect_report.models import Tasks, CheckSession, RulesStat
import json import json
from datetime import datetime, timedelta from datetime import datetime, timedelta
from config.config import TABLE_PRE from config.config import TABLE_PRE
...@@ -493,6 +493,58 @@ class TasksApi(viewsets.ViewSet): ...@@ -493,6 +493,58 @@ class TasksApi(viewsets.ViewSet):
@action(['post'], detail=False) @action(['post'], detail=False)
def static_rule(self, req: Request): def static_rule(self, req: Request):
"""
首页概述-违规项统计
:param req:
:return:
"""
task_id = req.data.get('task', '')
start_date = req.data.get('start_date', (datetime.now() + timedelta(days=-1)).strftime('%Y-%m-%d'))
end_date = req.data.get('end_date', datetime.now().strftime('%Y-%m-%d'))
task_condition = {'hasCheck': 1, 'createdAt__gte': start_date, 'createdAt__lt': end_date + ' 23:59:59'}
if task_id:
task_condition['name__startswith'] = task_id
tasks = Tasks.objects.filter(**task_condition).values('id', 'sessionCollectionId')
task_dict = {}
for t in tasks:
if t['sessionCollectionId'] in task_dict.keys():
task_dict[t['sessionCollectionId']].append(t['id'])
else:
task_dict[t['sessionCollectionId']] = [t['id'], ]
return_data = {}
call_count = 0
for k, v in task_dict.items():
table_name = TABLE_PRE + k
tn = CheckSession.set_table(table_name)
session_condition = {'taskId__in': v, 'violationRuleCount__gt': 0}
validate_all = tn.objects.filter(**session_condition).aggregate(Count("id"))
call_count += validate_all['id__count']
q_start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
q_end_date = (datetime.strptime(end_date, '%Y-%m-%d') + + timedelta(days=1)).date()
task_condition = {'create_date__gte': q_start_date, 'create_date__lt': q_end_date}
if task_id:
task_condition['rule_name'] = task_id
tasks = RulesStat.objects.filter(**task_condition).values('rule_name', 'from_table', 'rule_num')
for t in tasks:
name = t['rule_name']
if name in return_data.keys():
return_data[name]['count'] += int(t['rule_num'])
else:
return_data[name] = {'count': 1, 'tables': []}
if t['from_table'] not in return_data[name]['tables']:
return_data[name]['tables'].append(t['from_table'])
data_sort = []
for k, v in return_data.items():
v['rule'] = k
v['ratio'] = round(v['count']/call_count, 2) if call_count else 0
v['call_count'] = call_count
data_sort.append(v)
data_sort = sorted(data_sort, key=lambda x: x['count'], reverse=True)
return Response({'code': 0, 'msg': 'success', 'data': data_sort})
@action(['post'], detail=False)
def static_rule_bak(self, req: Request):
""" """
首页概述-违规项统计 首页概述-违规项统计
:param req: :param req:
...@@ -525,10 +577,6 @@ class TasksApi(viewsets.ViewSet): ...@@ -525,10 +577,6 @@ class TasksApi(viewsets.ViewSet):
for check in checks: for check in checks:
call_count += 1 call_count += 1
result = check['checkResult'] result = check['checkResult']
# detail = {'agentName': check['agentName'], 'customName': check['customName'],
# 'remainTime': check['remainTime'], 'score': check['score'], 'sessionId': check['sessionId'],
# 'sessionCollectionId': k, 'taskId': check['taskId']}
# session_id = check['id']
if result: if result:
data = json.loads(result) data = json.loads(result)
for d in data: for d in data:
......
...@@ -24,7 +24,7 @@ def rule_stat(start_date, end_date): ...@@ -24,7 +24,7 @@ def rule_stat(start_date, end_date):
return_data = {} return_data = {}
table_name = TABLE_PRE + t['sessionCollectionId'] table_name = TABLE_PRE + t['sessionCollectionId']
tn = CheckSession.set_table(table_name) tn = CheckSession.set_table(table_name)
session_condition = {'taskId': t['id']} session_condition = {'taskId': t['id'], 'violationRuleCount__gt': 0}
checks = tn.objects.filter(**session_condition).values('id', 'checkResult') checks = tn.objects.filter(**session_condition).values('id', 'checkResult')
for check in checks: for check in checks:
result = check['checkResult'] result = check['checkResult']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment