|
@@ -16,11 +16,11 @@ from django.db.models import Q, Count
|
|
|
from django.views.generic.base import View
|
|
|
import datetime
|
|
|
|
|
|
-from Ansjer.config import OSS_STS_ACCESS_SECRET, OSS_STS_ACCESS_KEY
|
|
|
+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
|
|
|
+ FamilyRoomDevice, FamilyRoom, FamilyMemberJoin, GatewaySubDevice, CountryModel, DeviceTypeModel, Order_Model
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Service.CommonService import CommonService
|
|
@@ -58,9 +58,115 @@ class DeviceDataView(View):
|
|
|
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)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def type_uid(cls, request_dict, response):
|
|
|
+ order_type = request_dict.get('orderType', None)
|
|
|
+ if not order_type:
|
|
|
+ return response.json(444)
|
|
|
+ order_type = int(order_type)
|
|
|
+ order_type_qs = Order_Model.objects.filter(order_type=order_type).values('UID').order_by('UID').distinct()
|
|
|
+ try:
|
|
|
+ order_type_list = []
|
|
|
+ for order in order_type_qs:
|
|
|
+ UID = order['UID']
|
|
|
+ device_info_qs = Device_Info.objects.filter(UID=UID).values('Type').order_by('Type').distinct()
|
|
|
+ if not device_info_qs.exists():
|
|
|
+ continue
|
|
|
+ device_info_qs = device_info_qs[0]['Type']
|
|
|
+ order_type_list.append(device_info_qs)
|
|
|
+ type_list = []
|
|
|
+ for i in order_type_list:
|
|
|
+ if i not in type_list:
|
|
|
+ type_list.append(i)
|
|
|
+
|
|
|
+ return response.json(0, type_list)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def device_add(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 查询设备增长数据
|
|
|
+ @param request_dict:请求参数
|
|
|
+ @request_dict starTime:开始时间
|
|
|
+ @request_dict endTime:结束时间
|
|
|
+ @param response:响应对象
|
|
|
+ """
|
|
|
+ start_time = request_dict.get('startTime', None) # 时间戳
|
|
|
+ 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'})
|
|
|
+ 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))
|
|
|
+ info_list = []
|
|
|
+ count = device_info_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'))
|
|
|
+ info_dict = {
|
|
|
+ 'count': device_info_date_qs.count(),
|
|
|
+ 'startTime': start_time,
|
|
|
+ 'endTime': end_time
|
|
|
+ }
|
|
|
+ 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')
|
|
|
+ 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_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
+ rate = round(item['count'] / count * 100, 2)
|
|
|
+ country_dict = {
|
|
|
+ 'countryName': country_name,
|
|
|
+ 'count': item['count'],
|
|
|
+ 'rate': rate
|
|
|
+ }
|
|
|
+ region_list.append(country_dict)
|
|
|
+ device_type_qs = device_info_qs.values('Type').annotate(count=Count('Type')).order_by('-count')
|
|
|
+ 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()
|
|
|
+ type_dict = {
|
|
|
+ 'type': device_type_qs[0]['name'],
|
|
|
+ 'total': total
|
|
|
+ }
|
|
|
+ type_list.append(type_dict)
|
|
|
+ order_model_qs = Order_Model.objects.filter(order_type=order_type).values('UID')
|
|
|
+ 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 =
|
|
|
+ res = {
|
|
|
+ 'info': info_list,
|
|
|
+ 'region': region_list,
|
|
|
+ 'type': type_list,
|
|
|
+ 'cloudStorage': clound_list
|
|
|
+ }
|
|
|
+ return response.json(0, res)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, repr(e))
|
|
|
+
|
|
|
@classmethod
|
|
|
def regional_statistics(cls, response):
|
|
|
"""
|
|
@@ -69,29 +175,44 @@ class DeviceDataView(View):
|
|
|
"""
|
|
|
device_info_qs = Device_Info.objects.all()
|
|
|
count = device_info_qs.count()
|
|
|
- device_info_qs = device_info_qs.values('userID__region_country').order_by('userID__region_country').distinct()
|
|
|
+ device_info_qs = device_info_qs.values('UID').order_by('UID').distinct()
|
|
|
if not device_info_qs.exists():
|
|
|
return response.json(444)
|
|
|
try:
|
|
|
device_country_list = []
|
|
|
- for device_user in device_info_qs:
|
|
|
- country_id = device_user['userID__region_country']
|
|
|
+ for device_info in device_info_qs:
|
|
|
+ UID = device_info['UID']
|
|
|
+ if UID is '':
|
|
|
+ continue
|
|
|
+ device_country_list.append(UID)
|
|
|
+ device_region_qs = device_info_qs.filter(UID__in=device_country_list).values(
|
|
|
+ 'userID__region_country').annotate()
|
|
|
+ region_list = []
|
|
|
+ for device_region in device_region_qs:
|
|
|
+ region = device_region['userID__region_country']
|
|
|
+ region_list.append(region)
|
|
|
+ device_country_qs = device_info_qs.values('userID__region_country').order_by(
|
|
|
+ 'userID__region_country').distinct()
|
|
|
+ device_region_list = []
|
|
|
+ for device_country in device_country_qs:
|
|
|
+ country_id = device_country['userID__region_country']
|
|
|
country_qs = CountryModel.objects.filter(id=country_id).values('country_name', 'id')
|
|
|
countryName = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
- device_info = Device_Info.objects.filter(userID__region_country=country_id).values('id').count()
|
|
|
+ device_info = region_list.count(country_id)
|
|
|
rate = round(device_info / count * 100, 2)
|
|
|
res = {
|
|
|
- 'id': country_id,
|
|
|
- 'name': countryName,
|
|
|
- 'total': device_info,
|
|
|
- 'rate': rate
|
|
|
+ 'id': country_id,
|
|
|
+ 'name': countryName,
|
|
|
+ 'total': device_info,
|
|
|
+ 'rate': rate
|
|
|
}
|
|
|
- device_country_list.append(res)
|
|
|
- return response.json(0, device_country_list)
|
|
|
+ device_region_list.append(res)
|
|
|
+ return response.json(0, device_region_list)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(500)
|
|
|
|
|
|
+
|
|
|
@classmethod
|
|
|
def type_statistics(cls, response):
|
|
|
"""
|
|
@@ -99,27 +220,36 @@ class DeviceDataView(View):
|
|
|
@param response:响应对象
|
|
|
@return:
|
|
|
"""
|
|
|
- device_info_qs = Device_Info.objects.filter().values('Type').order_by('Type').distinct()
|
|
|
+ device_info_qs = Device_Info.objects.values('UID').order_by('UID').distinct()
|
|
|
if not device_info_qs.exists():
|
|
|
return response.json(444)
|
|
|
device_type_list = []
|
|
|
try:
|
|
|
for device_info in device_info_qs:
|
|
|
- type = device_info['Type']
|
|
|
- device_type_qs = DeviceTypeModel.objects.filter(type=type).values('name')
|
|
|
- if not device_type_qs.exists():
|
|
|
+ UID = device_info['UID']
|
|
|
+ device_type_list.append(UID)
|
|
|
+ device_info_type_qs = device_info_qs.filter(UID__in=device_type_list).values('Type')
|
|
|
+ device_type_qs = device_info_type_qs.values('Type').order_by('Type').distinct()
|
|
|
+ device_info_list = []
|
|
|
+ for device_info in device_type_qs:
|
|
|
+ Type = device_info['Type']
|
|
|
+ device_type_qs = DEVICE_TYPE.get(Type)
|
|
|
+ name = device_type_qs
|
|
|
+ type_qs = device_info_type_qs.filter(Type=Type)
|
|
|
+ total = type_qs.count()
|
|
|
+ if name is None:
|
|
|
continue
|
|
|
- total = Device_Info.objects.filter(Type=type).count()
|
|
|
res = {
|
|
|
- 'type': device_type_qs[0]['name'],
|
|
|
+ 'type': name,
|
|
|
'total': total
|
|
|
}
|
|
|
- device_type_list.append(res)
|
|
|
- return response.json(0, device_type_list)
|
|
|
+ device_info_list.append(res)
|
|
|
+ return response.json(0, device_info_list)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(500)
|
|
|
|
|
|
+
|
|
|
@classmethod
|
|
|
def device_increase(cls, request_dict, response):
|
|
|
"""
|