|
@@ -7,22 +7,14 @@
|
|
|
@Software: PyCharm
|
|
|
"""
|
|
|
|
|
|
-import time
|
|
|
+import datetime
|
|
|
|
|
|
-import oss2
|
|
|
-from django.db import connection
|
|
|
-from django.db import transaction
|
|
|
-from django.db.models import Q, Count
|
|
|
+import requests
|
|
|
+from django.db.models import Count
|
|
|
from django.views.generic.base import View
|
|
|
-import datetime
|
|
|
|
|
|
-from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY, DEVICE_TYPE
|
|
|
-from Controller.DeviceConfirmRegion import Device_Region
|
|
|
-from Model.models import Device_Info, UID_Bucket, UID_Preview, UidSetModel, UidChannelSetModel, \
|
|
|
- iotdeviceInfoModel, UIDModel, Device_User, UserFamily, FamilyMember, FamilyMemberPermission, \
|
|
|
- FamilyRoomDevice, FamilyRoom, FamilyMemberJoin, GatewaySubDevice, CountryModel, DeviceTypeModel, Order_Model
|
|
|
-from Object.ResponseObject import ResponseObject
|
|
|
-from Object.TokenObject import TokenObject
|
|
|
+from Ansjer.config import DEVICE_TYPE
|
|
|
+from Model.models import Device_Info, CountryModel, Order_Model
|
|
|
from Service.CommonService import CommonService
|
|
|
|
|
|
|
|
@@ -43,21 +35,91 @@ class DeviceDataView(View):
|
|
|
token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
|
|
|
if token_code != 0:
|
|
|
return response.json(token_code)
|
|
|
- if operation == 'increase': # 查询新增用户数据
|
|
|
- return self.device_increase(request_dict, response)
|
|
|
if operation == 'type': # 统计设备类型
|
|
|
return self.type_statistics(response)
|
|
|
if operation == 'regional': # 设备地区分布
|
|
|
return self.regional_statistics(response)
|
|
|
- if operation == 'active': # 查询设备增长数据
|
|
|
- return self.device_add(request_dict, response)
|
|
|
- if operation == 'typeUid':
|
|
|
- return self.type_uid(request_dict, response)
|
|
|
+ if operation == 'addDevice': # 查询设备增长数据(数据有些许差异)
|
|
|
+ return self.add_device(request_dict, response)
|
|
|
+ if operation == 'active': # 设备活跃数据
|
|
|
+ return self.device_active(request_dict, response)
|
|
|
+ if operation == 'global/regional': # 全球设备分布
|
|
|
+ return self.global_regional(request, request_dict, response)
|
|
|
+ if operation == 'golbal/type': # 全球设备类型
|
|
|
+ return self.golbal_type(request, request_dict, response)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
|
@classmethod
|
|
|
- def type_uid(cls, request_dict, response):
|
|
|
+ def golbal_type(cls, request, request_dict, response):
|
|
|
+ """
|
|
|
+ 全球设备类型分布
|
|
|
+ @param request:请求
|
|
|
+ @param request_dict:请求参数
|
|
|
+ @param response: 响应对象
|
|
|
+ """
|
|
|
+ url_list = CommonService.get_domain_name()
|
|
|
+ try:
|
|
|
+ headers = {
|
|
|
+ 'Authorization': request.META.get('HTTP_AUTHORIZATION')
|
|
|
+ }
|
|
|
+ type_list = []
|
|
|
+ type_count = 0
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def global_regional(cls, request, request_dict, response):
|
|
|
+ """
|
|
|
+ 全球设备分布
|
|
|
+ @param request:请求
|
|
|
+ @param request_dict:请求参数
|
|
|
+ @param response:响应对象
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ url_list = CommonService.get_domain_name()
|
|
|
+ try:
|
|
|
+ headers = {
|
|
|
+ 'Authorization': request.META.get('HTTP_AUTHORIZATION')
|
|
|
+ }
|
|
|
+ device_list = []
|
|
|
+ device_count = 0
|
|
|
+ for url in url_list:
|
|
|
+ url = url + request.path.replace('global/', '')
|
|
|
+ res = requests.get(url=url, params=request_dict, headers=headers)
|
|
|
+ result = res.json()
|
|
|
+ if result['result_code'] == 0:
|
|
|
+ # 处理地区
|
|
|
+ for item in result['result']:
|
|
|
+ flag = 0
|
|
|
+ for each in device_list:
|
|
|
+ if each['name'] == item['name']:
|
|
|
+ each['total'] += int(item['total'])
|
|
|
+ device_count += int(item['total'])
|
|
|
+ flag = 1
|
|
|
+ break
|
|
|
+ if flag == 0:
|
|
|
+ device_list.append(item)
|
|
|
+ device_count += int(item['total'])
|
|
|
+ for item in device_list:
|
|
|
+ rate = round(item['total'] / device_count * 100, 2)
|
|
|
+ item['rate'] = rate
|
|
|
+ else:
|
|
|
+ return response.json(result['result_code'])
|
|
|
+ res = {
|
|
|
+ 'countries': CommonService.list_sort(device_list[:20])
|
|
|
+ }
|
|
|
+ return response.json(0, res)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, repr(e))
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def device_active(cls, request_dict, response):
|
|
|
order_type = request_dict.get('orderType', None)
|
|
|
if not order_type:
|
|
|
return response.json(444)
|
|
@@ -83,7 +145,7 @@ class DeviceDataView(View):
|
|
|
return response.json(500)
|
|
|
|
|
|
@classmethod
|
|
|
- def device_add(cls, request_dict, response):
|
|
|
+ def add_device(cls, request_dict, response):
|
|
|
"""
|
|
|
查询设备增长数据
|
|
|
@param request_dict:请求参数
|
|
@@ -95,64 +157,110 @@ class DeviceDataView(View):
|
|
|
end_time = request_dict.get('endTime', None)
|
|
|
unit_time = request_dict.get('unitTime', None)
|
|
|
order_type = request_dict.get('orderType', None)
|
|
|
- if not all([start_time, end_time, unit_time]):
|
|
|
- return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
|
|
|
+ if not all([start_time, end_time, unit_time, order_type]):
|
|
|
+ return response.json(444, {'error param': 'startTime or endTime or timeUnit or order_type'})
|
|
|
+ order_type = int(order_type)
|
|
|
start_time = datetime.datetime.fromtimestamp(int(start_time))
|
|
|
end_time = datetime.datetime.fromtimestamp(int(end_time))
|
|
|
time_list = CommonService.cutting_time(start_time, end_time, unit_time)
|
|
|
try:
|
|
|
device_info_qs = Device_Info.objects.filter(data_joined__range=(start_time, end_time))
|
|
|
+ device_count_qs = device_info_qs.count()
|
|
|
+ device_type_qs = device_info_qs.values('UID').order_by('UID').distinct()
|
|
|
info_list = []
|
|
|
- count = device_info_qs.count()
|
|
|
+
|
|
|
+ # device_info_type_qs = Device_Info.objects.values('UID').annotate(
|
|
|
+ # count=Count('UID', distinct=True)).values('Type').order_by('-count')
|
|
|
+
|
|
|
+ count_unique = device_type_qs.count()
|
|
|
+
|
|
|
+ # 统计该时间段的设备数量(去重)
|
|
|
for item in time_list:
|
|
|
- device_info_date_qs = device_info_qs.filter(data_joined__range=item)
|
|
|
- start_time = CommonService.str_to_timestamp(item[0].strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
- end_time = CommonService.str_to_timestamp(item[1].strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
+ start_time = datetime.datetime.fromtimestamp(int(item[0]))
|
|
|
+ end_time = datetime.datetime.fromtimestamp(int(item[1]))
|
|
|
+ device_type = device_info_qs.filter(data_joined__range=(start_time, end_time)).values(
|
|
|
+ 'UID').distinct()
|
|
|
info_dict = {
|
|
|
- 'count': device_info_date_qs.count(),
|
|
|
- 'startTime': start_time,
|
|
|
- 'endTime': end_time
|
|
|
+ 'count': device_type.count(),
|
|
|
+ 'startTime': item[0],
|
|
|
+ 'endTime': item[1]
|
|
|
}
|
|
|
info_list.append(info_dict)
|
|
|
- device_info_country_qs = device_info_qs.values('userID__region_country').annotate(
|
|
|
- count=Count('userID__region_country')).order_by('-count')
|
|
|
+ # 统计地区设备数量
|
|
|
+ device_info_country_qs = device_info_qs.values('userID__region_country').order_by('-count')
|
|
|
+ device_info_country_qs = device_info_qs.values('UID').annotate(
|
|
|
+ count=Count('UID', distinct=True)).values('userID__region_country').order_by('-count')
|
|
|
+ count_list = []
|
|
|
+ for device_info_country in device_info_country_qs:
|
|
|
+ region = device_info_country['userID__region_country']
|
|
|
+ count_list.append(region)
|
|
|
+ device_info_country_qs = device_info_country_qs.values('userID__region_country').distinct()
|
|
|
region_list = []
|
|
|
for item in device_info_country_qs:
|
|
|
country_id = item['userID__region_country']
|
|
|
- country_qs = CountryModel.objects.filter(id=country_id).values('country_name')
|
|
|
+ country_qs = CountryModel.objects.filter(id=country_id).values('country_name', 'id')
|
|
|
country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
- rate = round(item['count'] / count * 100, 2)
|
|
|
+ count = count_list.count(country_id)
|
|
|
+ rate = round(count / count_unique * 100, 2)
|
|
|
country_dict = {
|
|
|
'countryName': country_name,
|
|
|
- 'count': item['count'],
|
|
|
+ 'count': count,
|
|
|
'rate': rate
|
|
|
}
|
|
|
region_list.append(country_dict)
|
|
|
- device_type_qs = device_info_qs.values('Type').annotate(count=Count('Type')).order_by('-count')
|
|
|
+ # 统计设备类型数量
|
|
|
+ device_info_type_qs = device_info_qs.values('UID').annotate(
|
|
|
+ count=Count('UID', distinct=True)).values('Type').order_by('-count')
|
|
|
+ count = device_info_type_qs.count()
|
|
|
+ count_list = []
|
|
|
+ for device_info_country in device_info_type_qs:
|
|
|
+ type = device_info_country['Type']
|
|
|
+ count_list.append(type)
|
|
|
+ device_info_type_qs = device_info_type_qs.values('Type').distinct()
|
|
|
type_list = []
|
|
|
- for device_type in device_type_qs:
|
|
|
- type = device_type['type']
|
|
|
- device_type_qs = DeviceTypeModel.objects.filter(type=type).values('name')
|
|
|
- if not device_info_qs.exists():
|
|
|
- continue
|
|
|
- total = Device_Info.objects.filter(Type=type).count()
|
|
|
+ for device_type in device_info_type_qs:
|
|
|
+ type = device_type['Type']
|
|
|
+ name = DEVICE_TYPE.get(type, '未知类型')
|
|
|
+ name = name if name != 'UNKOWN' else '未知类型'
|
|
|
+ total = count_list.count(type)
|
|
|
+ rate = round(total / count_unique * 100, 2) # count_unique 有误,跟device_info_type_qs 总数合不上 (可以看151行)
|
|
|
type_dict = {
|
|
|
- 'type': device_type_qs[0]['name'],
|
|
|
- 'total': total
|
|
|
+ 'type': name,
|
|
|
+ 'count': total,
|
|
|
+ 'rate': rate
|
|
|
}
|
|
|
type_list.append(type_dict)
|
|
|
- order_model_qs = Order_Model.objects.filter(order_type=order_type).values('UID')
|
|
|
+ # 统计设备版本数量
|
|
|
+ device_info_type_qs = device_info_qs.values('UID').annotate(
|
|
|
+ count=Count('UID', distinct=True)).order_by('-count')
|
|
|
+ count = device_info_type_qs.count()
|
|
|
+ uid_list = []
|
|
|
+ for device_clound in device_info_type_qs:
|
|
|
+ uid = device_clound['UID']
|
|
|
+ uid_list.append(uid)
|
|
|
+ order_model_qs = Order_Model.objects.filter(UID__in=uid_list).values('UID').annotate(
|
|
|
+ count=Count('UID', distinct=True)).values('order_type').order_by('order_type')
|
|
|
count = order_model_qs.count()
|
|
|
- clound_list = []
|
|
|
- for device_uid in order_model_qs:
|
|
|
- UID = device_uid['UID']
|
|
|
- order_model_qs = Device_Info.objects.filter(UID=UID).values('Type')
|
|
|
- # type_qs =
|
|
|
+ order_type_list = []
|
|
|
+ for order_model in order_model_qs:
|
|
|
+ orderType = order_model['order_type']
|
|
|
+ order_type_list.append(orderType)
|
|
|
+ version_list = []
|
|
|
+ res = {}
|
|
|
+ if order_type == 0:
|
|
|
+ res['name'] = '云存'
|
|
|
+ elif order_type == 1:
|
|
|
+ res['name'] = 'AI'
|
|
|
+ elif order_type == 2:
|
|
|
+ res['name'] = '联通4G'
|
|
|
+ res['total'] = order_type_list.count(order_type)
|
|
|
+ version_list.append(res)
|
|
|
+
|
|
|
res = {
|
|
|
'info': info_list,
|
|
|
'region': region_list,
|
|
|
'type': type_list,
|
|
|
- 'cloudStorage': clound_list
|
|
|
+ 'version': version_list
|
|
|
}
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
@@ -194,7 +302,7 @@ class DeviceDataView(View):
|
|
|
res = {
|
|
|
'id': country_id,
|
|
|
'name': countryName,
|
|
|
- 'total': device_info,
|
|
|
+ 'count': device_info,
|
|
|
'rate': rate
|
|
|
}
|
|
|
device_region_list.append(res)
|
|
@@ -229,43 +337,3 @@ class DeviceDataView(View):
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(500)
|
|
|
-
|
|
|
- @classmethod
|
|
|
- def device_increase(cls, request_dict, response):
|
|
|
- """
|
|
|
- 查询用户增长数据
|
|
|
- @param request_dict:请求参数
|
|
|
- @request_dict startTime:开始时间
|
|
|
- @request_dict endTime:结束时间
|
|
|
- @param response:响应对象
|
|
|
- @return:
|
|
|
- """
|
|
|
- start_time = request_dict.get('startTime', None)
|
|
|
- end_time = request_dict.get('endTime', None)
|
|
|
- if not all([start_time, end_time]):
|
|
|
- return response.json(444, {'error param': 'startTime or endTime'})
|
|
|
- start_time = datetime.datetime.fromtimestamp(int(start_time))
|
|
|
- end_time = datetime.datetime.fromtimestamp(int(end_time))
|
|
|
- try:
|
|
|
- user_qs = Device_User.objects.filter(data_joined__range=(start_time, end_time))
|
|
|
- res = {
|
|
|
- 'total': user_qs.count(),
|
|
|
- 'region': []
|
|
|
- }
|
|
|
- if user_qs.exists():
|
|
|
- user_country_qs = user_qs.values('region_country').annotate(count=Count('region_country')).order_by(
|
|
|
- '-count')
|
|
|
- region = []
|
|
|
- for item in user_country_qs:
|
|
|
- country_id = item['region_country']
|
|
|
- country_qs = CountryModel.objects.filter(id=country_id).values('country_name')
|
|
|
- country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
- country_dict = {
|
|
|
- 'countryName': country_name,
|
|
|
- 'count': item['count']
|
|
|
- }
|
|
|
- region.append(country_dict)
|
|
|
- res['region'] = region
|
|
|
- return response.json(0, res)
|
|
|
- except Exception as e:
|
|
|
- return response.json(500, repr(e))
|