Commit a3fa669b authored by zhengjinlei's avatar zhengjinlei

统计数据

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