Commit 909ec7bf authored by zhengjinlei's avatar zhengjinlei

统计任务

parent ff7bc26b
......@@ -21,11 +21,14 @@ def rule_stat(start_date, end_date):
tasks = Tasks.objects.filter(**task_condition).values('id', 'name', 'sessionCollectionId')
stat_count = 0
for t in tasks:
if RulesStat.objects.filter(taskId=t['id']).exists():
continue
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', 'agentName', 'customName', 'score',
'remainTime', 'taskId', 'sessionId')
rules_list = []
for check in checks:
result = check['checkResult']
if result:
......@@ -33,22 +36,30 @@ 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']
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()
rule_obj = {'create_date': start_date, 'sessionCollectionId': t['sessionCollectionId'],
'rule': name, 'rule_num': 1, 'task': t['name'].split('_')[0],
'agentName': check['agentName'], 'customName': check['customName'],
'score': check['score'], 'remainTime': check['remainTime'],
'taskId': check['taskId'], 'sessionId': check['sessionId'],
'session_table_id': check['id']}
rules_list.append(RulesStat(**rule_obj))
# 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)
RulesStat.objects.bulk_create(rules_list)
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}
tasks = Tasks.objects.filter(**task_condition).values('id', 'name', 'sessionCollectionId')
for t in tasks:
if SeatStat.objects.filter(taskId=t['id']).exists():
continue
table_name = TABLE_PRE + t['sessionCollectionId']
tn = CheckSession.set_table(table_name)
session_condition = {'taskId': t['id']}
checks = tn.objects.filter(**session_condition).values('id', 'agentName', 'taskId', 'violationRuleCount')
seat_dict = {}
seat_list = []
for check in checks:
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()
if check['agentName'] in seat_dict.keys():
seat = seat_dict.get(check['agentName'])
seat['total_session'] += 1
seat['validate_session'] += 1 if check['violationRuleCount'] > 0 else 0
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()
seat_dict[check['agentName']] = {'create_date': start_date, 'agentName': check['agentName'],
'taskId': check['taskId'], 'task': t['name'].split('_')[0],
'sessionCollectionId': t['sessionCollectionId'], 'total_session': 1}
seat_dict[check['agentName']]['validate_session'] = 1 if check['violationRuleCount'] > 0 else 0
for k, v in seat_dict.items():
seat_obj = {'agentName': k}
seat_obj.update(v)
seat_list.append(SeatStat(**seat_obj))
# 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