Commit 909ec7bf authored by zhengjinlei's avatar zhengjinlei

统计任务

parent ff7bc26b
...@@ -21,11 +21,14 @@ def rule_stat(start_date, end_date): ...@@ -21,11 +21,14 @@ 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:
if RulesStat.objects.filter(taskId=t['id']).exists():
continue
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', 'agentName', 'customName', 'score', checks = tn.objects.filter(**session_condition).values('id', 'checkResult', 'agentName', 'customName', 'score',
'remainTime', 'taskId', 'sessionId') 'remainTime', 'taskId', 'sessionId')
rules_list = []
for check in checks: for check in checks:
result = check['checkResult'] result = check['checkResult']
if result: if result:
...@@ -33,22 +36,30 @@ def rule_stat(start_date, end_date): ...@@ -33,22 +36,30 @@ 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']
rd = RulesStat() rule_obj = {'create_date': start_date, 'sessionCollectionId': t['sessionCollectionId'],
rd.create_date = start_date 'rule': name, 'rule_num': 1, 'task': t['name'].split('_')[0],
rd.sessionCollectionId = t['sessionCollectionId'] 'agentName': check['agentName'], 'customName': check['customName'],
rd.rule = name 'score': check['score'], 'remainTime': check['remainTime'],
rd.rule_num = 1 'taskId': check['taskId'], 'sessionId': check['sessionId'],
rd.task = t['name'].split('_')[0] 'session_table_id': check['id']}
rd.agentName = check['agentName'] rules_list.append(RulesStat(**rule_obj))
rd.customName = check['customName'] # rd = RulesStat()
rd.score = check['score'] # rd.create_date = start_date
rd.remainTime = check['remainTime'] # rd.sessionCollectionId = t['sessionCollectionId']
rd.taskId = check['taskId'] # rd.rule = name
rd.sessionId = check['sessionId'] # rd.rule_num = 1
rd.session_table_id = check['id'] # rd.task = t['name'].split('_')[0]
rd.save() # 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 stat_count += 1
logger.info('统计数量为:%s' % stat_count) logger.info('统计数量为:%s' % stat_count)
RulesStat.objects.bulk_create(rules_list)
def seat_stat(start_date, end_date): def seat_stat(start_date, end_date):
...@@ -60,25 +71,43 @@ def seat_stat(start_date, end_date): ...@@ -60,25 +71,43 @@ def seat_stat(start_date, end_date):
task_condition = {'hasCheck': 1, 'createdAt__gte': start_date, 'createdAt__lt': end_date} task_condition = {'hasCheck': 1, 'createdAt__gte': start_date, 'createdAt__lt': end_date}
tasks = Tasks.objects.filter(**task_condition).values('id', 'name', 'sessionCollectionId') tasks = Tasks.objects.filter(**task_condition).values('id', 'name', 'sessionCollectionId')
for t in tasks: for t in tasks:
if SeatStat.objects.filter(taskId=t['id']).exists():
continue
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', 'taskId', 'violationRuleCount')
seat_dict = {}
seat_list = []
for check in checks: for check in checks:
seat_condition = {'create_date': start_date, 'agentName': check['agentName']} if check['agentName'] in seat_dict.keys():
seats = SeatStat.objects.filter(**seat_condition) seat = seat_dict.get(check['agentName'])
if seats: seat['total_session'] += 1
seat = seats[0] seat['validate_session'] += 1 if check['violationRuleCount'] > 0 else 0
seat.total_session += 1
seat.validate_session += 1 if check['violationRuleCount'] > 0 else 0
seat.save()
else: else:
seat = SeatStat() seat_dict[check['agentName']] = {'create_date': start_date, 'agentName': check['agentName'],
seat.create_date = start_date 'taskId': check['taskId'], 'task': t['name'].split('_')[0],
seat.agentName = check['agentName'] 'sessionCollectionId': t['sessionCollectionId'], 'total_session': 1}
seat.taskId = check['taskId'] seat_dict[check['agentName']]['validate_session'] = 1 if check['violationRuleCount'] > 0 else 0
seat.task = t['name'].split('_')[0] for k, v in seat_dict.items():
seat.sessionCollectionId = t['sessionCollectionId'] seat_obj = {'agentName': k}
seat.total_session = 1 seat_obj.update(v)
seat.validate_session = 1 if check['violationRuleCount'] > 0 else 0 seat_list.append(SeatStat(**seat_obj))
seat.save() # seat_condition = {'create_date': start_date, 'agentName': check['agentName']}
# seats = SeatStat.objects.filter(**seat_condition)
# if seats:
# seat = seats[0]
# seat.total_session += 1
# seat.validate_session += 1 if check['violationRuleCount'] > 0 else 0
# seat.save()
# else:
# seat = SeatStat()
# seat.create_date = start_date
# seat.agentName = check['agentName']
# seat.taskId = check['taskId']
# seat.task = t['name'].split('_')[0]
# seat.sessionCollectionId = t['sessionCollectionId']
# seat.total_session = 1
# seat.validate_session = 1 if check['violationRuleCount'] > 0 else 0
# seat.save()
SeatStat.objects.bulk_create(seat_list)
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