|
@@ -1,16 +1,20 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
+import time
|
|
|
+
|
|
|
+from django.db.models import Count,Q
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
from django.views.generic import TemplateView
|
|
|
from django.utils.decorators import method_decorator
|
|
|
from django.contrib.auth.hashers import make_password # 对密码加密模块
|
|
|
-from Model.models import Device_Info, Role, UserExModel
|
|
|
+from Model.models import Device_Info, Role, UserExModel, User_Brand, UidSetModel, UserAppFrequencyModel, \
|
|
|
+ AppFrequencyStatisticsModel, AppFrequencyYearStatisticsModel
|
|
|
from Service.ModelService import ModelService
|
|
|
from django.utils import timezone
|
|
|
from Model.models import Access_Log, Device_User
|
|
|
from django.views.decorators.http import require_http_methods
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
-from Ansjer.config import OFF_LINE_TIME_DELTA
|
|
|
+from Ansjer.config import OFF_LINE_TIME_DELTA, DEVICE_TYPE
|
|
|
import datetime, simplejson as json
|
|
|
from Service.CommonService import CommonService
|
|
|
from Object.RedisObject import RedisObject
|
|
@@ -64,6 +68,16 @@ class AdminManage(TemplateView):
|
|
|
return self.getAllUID(userID, response)
|
|
|
if operation == 'getAllDeviceArea':
|
|
|
return self.getAllDeviceArea(userID, response)
|
|
|
+ if operation == 'getUserBrand':
|
|
|
+ return self.getUserBrand(userID, response)
|
|
|
+ if operation == 'getAreaDeviceType':
|
|
|
+ return self.getAreaDeviceType(userID, response)
|
|
|
+ if operation == 'getDeviceType':
|
|
|
+ return self.getDeviceType(userID, response)
|
|
|
+ if operation == 'getAppFrequency':
|
|
|
+ return self.getAppFrequency(userID, request_dict, response)
|
|
|
+ if operation == 'getHistoryAppFrequency':
|
|
|
+ return self.getAllAppFrequency(userID, response)
|
|
|
|
|
|
def resetUserPwd(self, request_dict, userID, response):
|
|
|
own_permission = ModelService.check_perm(userID=userID, permID=50)
|
|
@@ -213,6 +227,204 @@ class AdminManage(TemplateView):
|
|
|
res = {'count': uid_list.count(), 'uid_list': list(uid_list)}
|
|
|
return response.json(0, res)
|
|
|
|
|
|
+ def getUserBrand(self, userID, response):
|
|
|
+ own_permission = ModelService.check_perm(userID=userID, permID=30)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+ # 手机型号统计
|
|
|
+ print('手机型号统计:')
|
|
|
+ ub_qs = User_Brand.objects.values('deviceSupplier', 'deviceModel').annotate(value=Count('id')).order_by()
|
|
|
+ res = {
|
|
|
+ 'value': 0,
|
|
|
+ 'data': []
|
|
|
+ }
|
|
|
+ data = res['data']
|
|
|
+ tmpDict = {}
|
|
|
+ for ub in ub_qs:
|
|
|
+ deviceSupplier = ub['deviceSupplier']
|
|
|
+ if not tmpDict.__contains__(deviceSupplier):
|
|
|
+ tmpDict[deviceSupplier] = {
|
|
|
+ 'name': deviceSupplier,
|
|
|
+ 'value': 0,
|
|
|
+ 'children': []
|
|
|
+ }
|
|
|
+
|
|
|
+ item = tmpDict[deviceSupplier]
|
|
|
+ item['value'] = item['value'] + ub['value']
|
|
|
+ res['value'] = res['value'] + item['value']
|
|
|
+ model = {
|
|
|
+ 'name': ub['deviceModel'],
|
|
|
+ 'value': ub['value']
|
|
|
+ }
|
|
|
+ item['children'].append(model)
|
|
|
+
|
|
|
+ for k, v in tmpDict.items():
|
|
|
+ data.append(v)
|
|
|
+
|
|
|
+ print(res)
|
|
|
+ return response.json(0, res)
|
|
|
+
|
|
|
+ def getAreaDeviceType(self, userID, response):
|
|
|
+ own_permission = ModelService.check_perm(userID=userID, permID=30)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+ print('区域设备类型统计:')
|
|
|
+ di_qs = Device_Info.objects.values('area', 'Type').annotate(value=Count('UID', distinct=True)).order_by()
|
|
|
+ res = {
|
|
|
+ 'quantity': 0,
|
|
|
+ 'data': []
|
|
|
+ }
|
|
|
+ data = res['data']
|
|
|
+ tmpDict = {}
|
|
|
+ tmpDict['null'] = {
|
|
|
+ 'area': '未知',
|
|
|
+ 'quantity': 0,
|
|
|
+ 'models': []
|
|
|
+ }
|
|
|
+ for di in di_qs:
|
|
|
+ area = di['area']
|
|
|
+ if area is None or area == '':
|
|
|
+ area = 'null'
|
|
|
+
|
|
|
+ if not tmpDict.__contains__(area):
|
|
|
+ tmpDict[area] = {
|
|
|
+ 'area': area,
|
|
|
+ 'quantity': 0,
|
|
|
+ 'models': []
|
|
|
+ }
|
|
|
+
|
|
|
+ item = tmpDict[area]
|
|
|
+ item['quantity'] = item['quantity'] + di['value']
|
|
|
+ res['quantity'] = res['quantity'] + item['quantity']
|
|
|
+ model = {
|
|
|
+ 'model': DEVICE_TYPE[di['Type']],
|
|
|
+ 'quantity': di['value']
|
|
|
+ }
|
|
|
+ item['models'].append(model)
|
|
|
+
|
|
|
+ for k, v in tmpDict.items():
|
|
|
+ data.append(v)
|
|
|
+ return response.json(0, res)
|
|
|
+
|
|
|
+ def getDeviceType(self, userID, response):
|
|
|
+ own_permission = ModelService.check_perm(userID=userID, permID=30)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+ # 设备类型统计
|
|
|
+ di_qs = Device_Info.objects.values('Type').annotate(quantity=Count('UID', distinct=True)).order_by()
|
|
|
+
|
|
|
+ # 设备型号统计
|
|
|
+ us_qs = UidSetModel.objects.values('deviceModel').annotate(quantity=Count('id')).order_by()
|
|
|
+
|
|
|
+ res = {
|
|
|
+ 'type_data': {
|
|
|
+ 'quantity': 0,
|
|
|
+ 'data': []
|
|
|
+ },
|
|
|
+ 'model_data': {
|
|
|
+ 'quantity': 0,
|
|
|
+ 'data': []
|
|
|
+ }
|
|
|
+ }
|
|
|
+ type_data = res['type_data']
|
|
|
+ data = type_data['data']
|
|
|
+ quantity = 0
|
|
|
+ for di in di_qs:
|
|
|
+ di['Type'] = DEVICE_TYPE[di['Type']]
|
|
|
+ quantity += di['quantity']
|
|
|
+ data.append(di)
|
|
|
+ type_data['quantity'] = quantity
|
|
|
+
|
|
|
+ model_data = res['model_data']
|
|
|
+ data = model_data['data']
|
|
|
+ quantity = 0
|
|
|
+ for us in us_qs:
|
|
|
+ data.append(us)
|
|
|
+ quantity += us['quantity']
|
|
|
+ model_data['quantity'] = quantity
|
|
|
+ return response.json(0, res)
|
|
|
+
|
|
|
+ def getAppFrequency(self, userID, request_dict, response):
|
|
|
+ own_permission = ModelService.check_perm(userID=userID, permID=30)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ # 当前的年份
|
|
|
+ current_time = int(time.time())
|
|
|
+ localtime = time.localtime(current_time)
|
|
|
+ current_year = localtime.tm_year
|
|
|
+ current_month = localtime.tm_mon
|
|
|
+
|
|
|
+ start_year = end_year = current_year
|
|
|
+ end_month = current_month
|
|
|
+ start_month = 1
|
|
|
+
|
|
|
+ result = []
|
|
|
+ if end_month != 12:
|
|
|
+ start_month = end_month + 1
|
|
|
+ start_year = current_year - 1
|
|
|
+
|
|
|
+ time_struct = [start_year, start_month, 0, 0, 0, 0, 0, 0, 0]
|
|
|
+ key_formal = '{year}{month}'
|
|
|
+ for i in range(12):
|
|
|
+ result.append({
|
|
|
+ 'date_time': key_formal.format(year=time_struct[0], month=str(time_struct[1]).zfill(2)),
|
|
|
+ 'data': None
|
|
|
+ })
|
|
|
+
|
|
|
+ month = time_struct[1] + 1
|
|
|
+ if month > 12:
|
|
|
+ time_struct[0] = time_struct[0] + 1
|
|
|
+ time_struct[1] = 1
|
|
|
+ else:
|
|
|
+ time_struct[1] = month
|
|
|
+ #
|
|
|
+ afs_qs = {}
|
|
|
+ if start_year == end_year:
|
|
|
+ afs_qs = list(AppFrequencyStatisticsModel.objects.filter(year=start_year, month__gte=start_month, month__lte=end_month).values().order_by('-year', 'month'))
|
|
|
+ else:
|
|
|
+ afs_qs = list(AppFrequencyStatisticsModel.objects.filter(year=start_year, month__gte=start_month).values().order_by('-year', 'month'))
|
|
|
+ tmps_qs = list(AppFrequencyStatisticsModel.objects.filter(year=end_year, month__lte=end_month).values().order_by('-year', 'month'))
|
|
|
+ for tmp in tmps_qs:
|
|
|
+ afs_qs.append(tmp)
|
|
|
+
|
|
|
+ tmp_dict = {}
|
|
|
+
|
|
|
+ for afs in afs_qs:
|
|
|
+ key = key_formal.format(year=afs['year'], month=str(afs['month']).zfill(2))
|
|
|
+ tmp_dict[key] = json.loads(afs['data'])
|
|
|
+
|
|
|
+ for res in result:
|
|
|
+ if tmp_dict.__contains__(res['date_time']):
|
|
|
+ res['data'] = tmp_dict[res['date_time']]
|
|
|
+ print(result)
|
|
|
+
|
|
|
+ return response.json(0, result)
|
|
|
+
|
|
|
+ def getAllAppFrequency(self, userID, response):
|
|
|
+ own_permission = ModelService.check_perm(userID=userID, permID=30)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ # 取出请求年份的统计好的数据
|
|
|
+ print('start')
|
|
|
+ time_struct = time.localtime()
|
|
|
+ current_year = time_struct.tm_year
|
|
|
+ start_year = current_year - 5 + 1
|
|
|
+ afs_qs = AppFrequencyYearStatisticsModel.objects.filter(year__gte=start_year, year__lt=current_year).order_by(
|
|
|
+ '-year')
|
|
|
+ if afs_qs.exists():
|
|
|
+ afs_qs = afs_qs.values('id', 'data', 'num', 'year')
|
|
|
+ res = []
|
|
|
+ for afs in afs_qs:
|
|
|
+ res.append({
|
|
|
+ 'year': afs['year'],
|
|
|
+ 'data': json.loads(afs['data'])
|
|
|
+ })
|
|
|
+ return response.json(0, res)
|
|
|
+ else:
|
|
|
+ return response.json(0, [])
|
|
|
+
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
|
def getUserIds(request):
|
|
@@ -299,3 +511,4 @@ def search_user_by_content(request):
|
|
|
sqlDict['count'] = count
|
|
|
return response.json(0, sqlDict)
|
|
|
return response.json(0, {'datas': [], 'count': 0})
|
|
|
+
|