Commit f2f867e9 authored by zhengjinlei's avatar zhengjinlei

概览统计

parent 6e47c439
......@@ -520,8 +520,8 @@ class TasksApi(viewsets.ViewSet):
session_condition = {'taskId__in': v}
if agent_name:
session_condition['agentName'] = agent_name
checks = tn.objects.filter(**session_condition).values('id', 'checkResult', 'agentName', 'customName',
'remainTime', 'score', 'sessionId', 'taskId')
# , 'agentName', 'customName', 'remainTime', 'score', 'sessionId', 'taskId'
checks = tn.objects.filter(**session_condition).values('id', 'checkResult')
for check in checks:
call_count += 1
result = check['checkResult']
......
# coding: utf-8
from rest_framework.request import Request
from datetime import datetime, timedelta
from inspect_report.models import Tasks, CheckSession, RulesStat
from config.config import TABLE_PRE
import json
import logging
"""定时任务
参考:https://github.com/jgorset/django-kronos
"""
logger = logging.getLogger("app_file")
def rule_stat(start_date, end_date):
"""
首页概述-违规项统计
:return:
"""
task_condition = {'hasCheck': 1, 'createdAt__gte': start_date, 'createdAt__lt': 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']}
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}
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)
......@@ -24,6 +24,20 @@ class Tasks(models.Model):
db_table = 'check_tasks'
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_num = models.IntegerField('违规数量', default=0)
area = models.CharField('所在地市', null=True, max_length=24)
create_date = models.DateField('统计日期', null=True)
def __str__(self):
return self.rule_name
class Meta:
db_table = 'rules_datastat'
class CheckSession(models.Model):
taskId = models.IntegerField('质检任务标识')
sessionId = models.CharField('会话唯一标识')
......
......@@ -16,6 +16,7 @@ Including another URLconf
from django.contrib import admin
from django.urls import path
from .api.tasksapi import rule, seat, group, score, check
from .views import stat_rules_every
urlpatterns = [
# path('admin/', admin.site.urls),
......@@ -24,4 +25,5 @@ urlpatterns = [
path('inspect/group/', group),
path('inspect/score/', score),
path('inspect/check/', check),
path('inspect/stat/', stat_rules_every),
]
from django.shortcuts import render
from datetime import datetime, timedelta
from .cron import rule_stat
from django.http.response import JsonResponse
# Create your views here.
def stat_rules_every(request):
date = request.GET.get('date')
start_date = datetime.strptime(date, '%Y-%m-%d')
end_date = datetime.strptime(date + ' 23:59:59', '%Y-%m-%d %H:%M:%S')
rule_stat(start_date, end_date)
return JsonResponse({'code': 0, 'msg': 'stat success'})
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