Commit 7ab2f372 authored by zhengjinlei's avatar zhengjinlei

报表提供下载功能

parent 8a1a71fa
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
# from .user import UserApi # from .user import UserApi
from .toolapi import ToolsApi from .toolapi import ToolsApi
from .tasksapi import TasksApi from .tasksapi import TasksApi
from .dataapi import DataApi
This diff is collapsed.
...@@ -199,11 +199,14 @@ class TasksApi(viewsets.ViewSet): ...@@ -199,11 +199,14 @@ class TasksApi(viewsets.ViewSet):
if agent_name: if agent_name:
task_condition['agentName'] = agent_name task_condition['agentName'] = agent_name
if sort == 'des': if sort == 'des':
seats = SeatStat.objects.filter(**task_condition).values('agentName', 'validate_session', 'total_session')\ seats = SeatStat.objects.filter(**task_condition).extra(select={'agentName': 'agentName'}) \
.order_by('-validate_session') .values('agentName').annotate(remainTime=Sum('remainTime'), validate_session=Sum('validate_session'),
total_session=Sum('total_session')).order_by('-validate_session')
else: else:
seats = SeatStat.objects.filter(**task_condition).values('agentName', 'validate_session', 'total_session') \ seats = SeatStat.objects.filter(**task_condition).extra(select={'agentName': 'agentName'}) \
.order_by('validate_session') .values('agentName').annotate(remainTime=Sum('remainTime'), validate_session=Sum('validate_session'),
total_session=Sum('total_session')).order_by('validate_session')
paginator = Paginator(seats, page_size) paginator = Paginator(seats, page_size)
total_count = paginator.count total_count = paginator.count
try: try:
...@@ -215,7 +218,8 @@ class TasksApi(viewsets.ViewSet): ...@@ -215,7 +218,8 @@ class TasksApi(viewsets.ViewSet):
return_data = [] return_data = []
for s in seats: for s in seats:
detail = {'agentName': s['agentName'], 'validate_session': s['validate_session'], detail = {'agentName': s['agentName'], 'validate_session': s['validate_session'],
'total_session': s['total_session'], 'ratio': round(s['validate_session']/s['total_session'], 2)} 'remainTime': s['remainTime'], 'total_session': s['total_session'],
'ratio': round(s['validate_session']/s['total_session'], 2)}
return_data.append(detail) return_data.append(detail)
if sort == 'asc': if sort == 'asc':
data_sort = sorted(return_data, key=lambda x: x['ratio']) data_sort = sorted(return_data, key=lambda x: x['ratio'])
......
...@@ -78,7 +78,8 @@ def seat_stat(start_date=None, end_date=None): ...@@ -78,7 +78,8 @@ def seat_stat(start_date=None, end_date=None):
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']}
checks = tn.objects.filter(**session_condition).values('id', 'agentName', 'taskId', 'violationRuleCount') checks = tn.objects.filter(**session_condition).values('id', 'agentName', 'remainTime', 'taskId',
'violationRuleCount')
seat_dict = {} seat_dict = {}
seat_list = [] seat_list = []
for check in checks: for check in checks:
...@@ -86,12 +87,14 @@ def seat_stat(start_date=None, end_date=None): ...@@ -86,12 +87,14 @@ def seat_stat(start_date=None, end_date=None):
if check['agentName'] in seat_dict.keys(): if check['agentName'] in seat_dict.keys():
seat = seat_dict.get(check['agentName']) seat = seat_dict.get(check['agentName'])
seat['total_session'] += 1 seat['total_session'] += 1
seat['remainTime'] = check['remainTime']
seat['validate_session'] += 1 if check['violationRuleCount'] > 0 else 0 seat['validate_session'] += 1 if check['violationRuleCount'] > 0 else 0
else: else:
seat_dict[check['agentName']] = {'create_date': start_date, 'agentName': check['agentName'], seat_dict[check['agentName']] = {'create_date': start_date, 'agentName': check['agentName'],
'taskId': check['taskId'], 'task': t['name'].split('_')[0], 'taskId': check['taskId'], 'task': t['name'].split('_')[0],
'sessionCollectionId': t['sessionCollectionId'], 'total_session': 1} 'sessionCollectionId': t['sessionCollectionId'], 'total_session': 1}
seat_dict[check['agentName']]['validate_session'] = 1 if check['violationRuleCount'] > 0 else 0 seat_dict[check['agentName']]['validate_session'] = 1 if check['violationRuleCount'] > 0 else 0
seat_dict[check['agentName']]['remainTime'] = check['remainTime']
for k, v in seat_dict.items(): for k, v in seat_dict.items():
seat_obj = {'agentName': k} seat_obj = {'agentName': k}
seat_obj.update(v) seat_obj.update(v)
......
...@@ -22,6 +22,7 @@ from . import api ...@@ -22,6 +22,7 @@ from . import api
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'tools', api.ToolsApi, base_name='tools') router.register(r'tools', api.ToolsApi, base_name='tools')
router.register(r'tasks', api.TasksApi, base_name='tasks') router.register(r'tasks', api.TasksApi, base_name='tasks')
router.register(r'data', api.DataApi, base_name='data')
# Wire up our API using automatic URL routing. # Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API. # Additionally, we include login URLs for the browsable API.
......
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