Commit a88c4932 authored by zhengjinlei's avatar zhengjinlei

违规占比

parent d97fdb44
......@@ -502,66 +502,12 @@ class TasksApi(viewsets.ViewSet):
for k, v in return_data.items():
v['rule'] = k
v['ratio'] = round(v['count']/call_count, 2) if call_count else 0
v['ratio'] = '{0}%'.format(int(v['ratio'] * 100))
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:
:return:
"""
task_id = req.data.get('task', '')
agent_name = req.data.get('agentName', '')
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}
if agent_name:
session_condition['agentName'] = agent_name
# , 'agentName', 'customName', 'remainTime', 'score', 'sessionId', 'taskId'
checks = tn.objects.filter(**session_condition).values('id', 'checkResult')
for check in checks:
call_count += 1
result = check['checkResult']
if result:
data = json.loads(result)
for d in data:
if 'isViolation' in d.keys() and d['isViolation'] and 'rule' in d.keys() and 'name' in d['rule'].keys():
name = d['rule']['name']
if name in return_data.keys():
return_data[name]['count'] += 1
# return_data[name]['details'].append(detail)
else:
return_data[name] = {'count': 1, 'details': [], 'tables': []}
if k not in return_data[name]['tables']:
return_data[name]['tables'].append(k)
data_sort = []
for k, v in return_data.items():
v['rule'] = k
v['ratio'] = round(v['count']/call_count, 2)
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 rule_detail(self, req: Request):
"""
......
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