Commit 7dd3c6c4 authored by zhengjinlei's avatar zhengjinlei

首页统计

parent 7ab2f372
......@@ -243,38 +243,19 @@ class TasksApi(viewsets.ViewSet):
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'}
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()
session_condition = {'create_date__gte': q_start_date, 'create_date__lt': q_end_date}
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 = {'total_session': 0, 'validate_session': 0}
total_score = 0
for k, v in task_dict.items():
table_name = TABLE_PRE + k
tn = CheckSession.set_table(table_name)
session_condition = {'taskId__in': v}
# checks = tn.objects.filter(**session_condition).values('violationRuleCount', 'score')
# for check in checks:
# total_score += check['score']
# return_data['total_session'] += 1
# if check['violationRuleCount'] > 0:
# return_data['validate_session'] += 1
session_all = tn.objects.filter(**session_condition).aggregate(Count("id"))
score_all = tn.objects.filter(**session_condition).aggregate(Sum("score"))
session_condition['violationRuleCount__gt'] = 0
validate_all = tn.objects.filter(**session_condition).aggregate(Count("id"))
return_data['total_session'] += session_all['id__count']
return_data['validate_session'] += validate_all['id__count']
total_score += score_all['score__sum']
if return_data['total_session']:
return_data['ratio'] = round(return_data['validate_session']/return_data['total_session'], 2)
return_data['avg_score'] = round(total_score / return_data['total_session'], 2)
session_condition['task'] = task_id
seat_data = SeatStat.objects.filter(**session_condition).aggregate(
validate_session_sum=Sum('validate_session'), total_session_sum=Sum('total_session'))
avg_score = ScoreStat.objects.filter(**session_condition).aggregate(score_avg=Round(Avg('score'), 2))
return_data = {'validate_session': seat_data['validate_session_sum'],
'total_session': seat_data['total_session_sum']}
if seat_data['total_session_sum']:
return_data['ratio'] = round(seat_data['validate_session_sum']/seat_data['total_session_sum'], 2)
return_data['avg_score'] = avg_score['score_avg']
else:
return_data['ratio'] = 0.0
return_data['avg_score'] = 0.0
......
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