Commit a3fa669b authored by zhengjinlei's avatar zhengjinlei

统计数据

parent 8519e533
......@@ -84,52 +84,24 @@ class TasksApi(viewsets.ViewSet):
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'))
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}
data_sort = []
return_data = {}
if task_id:
task_condition['task'] = task_id
if agent_name:
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'], ]
for k, v in task_dict.items():
table_name = TABLE_PRE + k
tn = CheckSession.set_table(table_name)
session_condition = {'taskId__in': v, 'agentName': agent_name}
checks = tn.objects.filter(**session_condition).values('id', 'checkResult')
for check in checks:
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
else:
return_data[name] = {'count': 1, 'tables': []}
if k not in return_data[name]['tables']:
return_data[name]['tables'].append(k)
else:
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['area'] = 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': int(t['rule_num']), 'tables': []}
if t['from_table'] not in return_data[name]['tables']:
return_data[name]['tables'].append(t['from_table'])
task_condition['agentName'] = agent_name
tasks = RulesStat.objects.filter(**task_condition).values('rule', 'sessionCollectionId', 'rule_num')
for t in tasks:
name = t['rule']
if name in return_data.keys():
return_data[name]['count'] += int(t['rule_num'])
else:
return_data[name] = {'count': int(t['rule_num']), 'tables': []}
if t['sessionCollectionId'] not in return_data[name]['tables']:
return_data[name]['tables'].append(t['sessionCollectionId'])
for k, v in return_data.items():
v['rule'] = k
......@@ -284,7 +256,6 @@ class TasksApi(viewsets.ViewSet):
:return:
"""
task_id = req.data.get('task', '')
num = req.data.get('num', '10')
a_name = req.data.get('agentName', '')
sort = req.data.get('sort', 'des')
start_date = req.data.get('start_date', (datetime.now()+timedelta(days=-1)).strftime('%Y-%m-%d'))
......@@ -588,16 +559,16 @@ class TasksApi(viewsets.ViewSet):
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['area'] = task_id
tasks = RulesStat.objects.filter(**task_condition).values('rule_name', 'from_table', 'rule_num')
task_condition['task'] = task_id
tasks = RulesStat.objects.filter(**task_condition).values('rule', 'sessionCollectionId', 'rule_num')
for t in tasks:
name = t['rule_name']
name = t['rule']
if name in return_data.keys():
return_data[name]['count'] += int(t['rule_num'])
else:
return_data[name] = {'count': int(t['rule_num']), 'tables': []}
if t['from_table'] not in return_data[name]['tables']:
return_data[name]['tables'].append(t['from_table'])
if t['sessionCollectionId'] not in return_data[name]['tables']:
return_data[name]['tables'].append(t['sessionCollectionId'])
data_sort = []
for k, v in return_data.items():
v['rule'] = k
......@@ -675,42 +646,26 @@ class TasksApi(viewsets.ViewSet):
page_size = req.data.get('page_size', '10')
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'))
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()
if not isinstance(tables, list):
table_list = json.loads(tables)
else:
table_list = tables
return_data = []
total_count = 0
for k in table_list:
table_name = TABLE_PRE + k
tn = CheckSession.set_table(table_name)
session_condition = {'checkResult__contains': rule_name, 'createdAt__gte': start_date,
'createdAt__lt': end_date + ' 23:59:59', 'violationRuleCount__gt': 0}
checks = tn.objects.filter(**session_condition).values('id', 'agentName', 'customName', 'checkResult',
'remainTime', 'score', 'sessionId', 'taskId')
paginator = Paginator(checks, page_size)
total_count += paginator.count
try:
seats = paginator.page(page)
except PageNotAnInteger:
seats = paginator.page(1)
except EmptyPage:
seats = paginator.page(paginator.num_pages)
for check in seats:
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() and d['rule']['name'] == rule_name:
detail = {'agentName': check['agentName'], 'customName': check['customName'],
'remainTime': check['remainTime'], 'score': check['score'],
'sessionId': check['sessionId'], 'sessionCollectionId': k,
'taskId': check['taskId']}
return_data.append(detail)
return Response({'code': 0, 'msg': 'success', 'count': total_count, 'data': return_data})
session_condition = {'create_date__gte': q_start_date, 'create_date__lt': q_end_date, 'rule': rule_name,
'sessionCollectionId__in': table_list}
checks = RulesStat.objects.filter(**session_condition).values('agentName', 'customName', 'remainTime', 'score',
'sessionCollectionId', 'sessionId', 'taskId')
paginator = Paginator(checks, page_size)
total_count += paginator.count
try:
seats = paginator.page(page)
except PageNotAnInteger:
seats = paginator.page(1)
except EmptyPage:
seats = paginator.page(paginator.num_pages)
return Response({'code': 0, 'msg': 'success', 'count': total_count, 'data': seats.object_list})
@xframe_options_exempt
......
......@@ -21,11 +21,11 @@ def rule_stat(start_date, end_date):
tasks = Tasks.objects.filter(**task_condition).values('id', 'name', 'sessionCollectionId')
stat_count = 0
for t in tasks:
return_data = {}
table_name = TABLE_PRE + t['sessionCollectionId']
tn = CheckSession.set_table(table_name)
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', 'agentName', 'customName', 'score',
'remainTime', 'taskId', 'sessionId')
for check in checks:
result = check['checkResult']
if result:
......@@ -33,18 +33,19 @@ def rule_stat(start_date, end_date):
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
else:
return_data[name] = {'count': 1}
for m, n in return_data.items():
rd = RulesStat()
rd.create_date = start_date
rd.from_table = t['sessionCollectionId']
rd.rule_name = m
rd.rule_num = n['count']
rd.area = t['name'].split('_')[0]
rd.save()
stat_count += 1
logger.info('统计数量为:%s' % stat_count)
rd = RulesStat()
rd.create_date = start_date
rd.sessionCollectionId = t['sessionCollectionId']
rd.rule = name
rd.rule_num = 1
rd.task = t['name'].split('_')[0]
rd.agentName = check['agentName']
rd.customName = check['customName']
rd.score = check['score']
rd.remainTime = check['remainTime']
rd.taskId = check['taskId']
rd.sessionId = check['sessionId']
rd.session_table_id = check['id']
rd.save()
stat_count += 1
logger.info('统计数量为:%s' % stat_count)
......@@ -25,14 +25,21 @@ class Tasks(models.Model):
class RulesStat(models.Model):
rule_name = models.CharField('违规名称', null=True, max_length=64, db_index=True)
from_table = models.CharField('所在表', null=True, max_length=32)
rule = models.CharField('违规名称', null=True, max_length=64, db_index=True)
sessionCollectionId = models.CharField('所在表', null=True, max_length=32)
rule_num = models.IntegerField('违规数量', default=0)
area = models.CharField('所在地市', null=True, max_length=24)
task = models.CharField('所在地市', null=True, max_length=24)
create_date = models.DateField('统计日期', null=True)
agentName = models.CharField('坐席名称', null=True, max_length=64)
customName = models.CharField('客户名称', null=True, max_length=64)
score = models.FloatField('对当前会话的评分', default=0)
remainTime = models.IntegerField('会话持续时间', default=0)
taskId = models.IntegerField('质检任务标识')
sessionId = models.CharField('会话唯一标识', max_length=64)
session_table_id = models.IntegerField('会话主键')
def __str__(self):
return self.rule_name
return self.rule
class Meta:
db_table = 'rules_datastat'
......
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