|
@@ -13,8 +13,7 @@ import requests
|
|
|
from django.db.models import Count
|
|
|
from django.views.generic.base import View
|
|
|
|
|
|
-from Ansjer.config import DEVICE_TYPE
|
|
|
-from Model.models import Device_Info, CountryModel, Order_Model, VodHlsModel, UidSetModel
|
|
|
+from Model.models import Device_Info, CountryModel, VodHlsModel, UidSetModel, DeviceTypeModel
|
|
|
from Service.CommonService import CommonService
|
|
|
|
|
|
|
|
@@ -51,8 +50,6 @@ class DeviceDataView(View):
|
|
|
return self.golbal_active(request, request_dict, response)
|
|
|
if operation == 'global/addDevice': # 全球新增设备数据
|
|
|
return self.golbal_add_device(request, request_dict, response)
|
|
|
- else:
|
|
|
- return response.json(414)
|
|
|
|
|
|
@classmethod
|
|
|
def golbal_add_device(cls, request, request_dict, response):
|
|
@@ -349,16 +346,16 @@ class DeviceDataView(View):
|
|
|
}
|
|
|
vod_list.append(vod_dict)
|
|
|
res['vodHls'] = vod_list
|
|
|
- type_country_qs = Device_Info.objects.filter(UID__in=uid_list).values(
|
|
|
- 'userID__region_country').annotate(count=Count('userID__region_country')).order_by('-count')
|
|
|
- for item in type_country_qs:
|
|
|
- country_id = item['userID__region_country']
|
|
|
+ type_country_qs = UidSetModel.objects.filter(uid__in=uid_list).values(
|
|
|
+ 'tb_country').annotate(count=Count('tb_country')).order_by('-count')
|
|
|
+ for type_country in type_country_qs:
|
|
|
+ country_id = type_country['tb_country']
|
|
|
country_name_qs = CountryModel.objects.filter(id=country_id).values('country_name')
|
|
|
country_name = country_name_qs[0]['country_name'] if country_name_qs.exists() else '未知区域'
|
|
|
- rate = round(item['count'] / count_all * 100, 2)
|
|
|
+ rate = round(type_country['count'] / count_all * 100, 2)
|
|
|
country_dict = {
|
|
|
'countryName': country_name,
|
|
|
- 'count': item['count'],
|
|
|
+ 'count': type_country['count'],
|
|
|
'rate': rate
|
|
|
}
|
|
|
region_list.append(country_dict)
|
|
@@ -381,21 +378,11 @@ class DeviceDataView(View):
|
|
|
start_time = request_dict.get('startTime', None) # 时间戳
|
|
|
end_time = request_dict.get('endTime', None)
|
|
|
unit_time = request_dict.get('timeUnit', None)
|
|
|
- order_type = request_dict.get('orderType', None)
|
|
|
- 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)
|
|
|
-
|
|
|
+ if not all([start_time, end_time, unit_time]):
|
|
|
+ return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
|
|
|
try:
|
|
|
- device_info_qs = UidSetModel.objects.filter(addTime__range=(start_time, end_time))
|
|
|
- # device_info_qs = Device_Info.objects.filter(data_joined__range=(start_time, end_time))
|
|
|
- # device_test_qs = Device_Info.objects.filter(data_joined__lt=start_time)
|
|
|
- # device_test = list(device_test_qs.values('UID').order_by('UID').distinct())
|
|
|
- # device_info = list(device_info_qs.values('UID').order_by('UID').distinct())
|
|
|
- # test_list = [item[key] for item in device_test for key in item]
|
|
|
- # device_info_list = [item[key] for item in device_info for key in item]
|
|
|
- # part_only_list = list(set(device_info_list) - set(test_list))
|
|
|
- count_all = device_info_qs.count()
|
|
|
+ uid_set_qs = UidSetModel.objects.filter(addTime__range=(start_time, end_time))
|
|
|
+ count_all = uid_set_qs.count()
|
|
|
# 统计该时间段的设备数量(已去重)
|
|
|
res = {
|
|
|
'addDevice': '',
|
|
@@ -411,91 +398,82 @@ class DeviceDataView(View):
|
|
|
end_time = datetime.datetime.fromtimestamp(int(end_time))
|
|
|
time_list = CommonService.cutting_time(start_time, end_time, unit_time)
|
|
|
for item in time_list:
|
|
|
- start_time = datetime.datetime.fromtimestamp(int(item[0]))
|
|
|
- end_time = datetime.datetime.fromtimestamp(int(item[1]))
|
|
|
- device_qs = device_info_qs.filter(data_joined__range=(start_time, end_time))
|
|
|
- device_test_qs = Device_Info.objects.filter(data_joined__lt=start_time)
|
|
|
- device_test = list(device_test_qs.values('UID').order_by('UID').distinct())
|
|
|
- device_info = list(device_qs.values('UID').order_by('UID').distinct())
|
|
|
- test_list = [item[key] for item in device_test for key in item]
|
|
|
- device_info_list = [item[key] for item in device_info for key in item]
|
|
|
- part_only_list = list(set(device_info_list) - set(test_list))
|
|
|
- count_part = len(part_only_list)
|
|
|
- rate = round(count_part / count_all * 100, 2)
|
|
|
- info_dict = {
|
|
|
- 'startTime': item[0],
|
|
|
- 'endTime': item[1],
|
|
|
- 'count': len(part_only_list),
|
|
|
- 'rate': rate
|
|
|
- }
|
|
|
- info_list.append(info_dict)
|
|
|
- res['addDevice'] = info_list
|
|
|
- # 统计地区设备数量
|
|
|
- device_info_country_qs = device_info_qs.filter(UID__in=part_only_list).values(
|
|
|
- 'userID__region_country').annotate(
|
|
|
- count=Count('userID__region_country')).order_by('-count')
|
|
|
- for item in device_info_country_qs:
|
|
|
- country_id = item['userID__region_country']
|
|
|
- country_qs = CountryModel.objects.filter(id=country_id).values('country_name', 'id')
|
|
|
- country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
- country_qs = device_info_qs.filter(UID__in=part_only_list).values('UID').order_by(
|
|
|
- 'UID').distinct().values(
|
|
|
- 'userID__region_country')
|
|
|
- total_list = [item[key] for item in country_qs for key in item]
|
|
|
- country_count = total_list.count(country_id)
|
|
|
- rate = round(country_count / count_part * 100, 2)
|
|
|
- country_dict = {
|
|
|
- 'countryName': country_name,
|
|
|
- 'count': country_count,
|
|
|
+ deivce_uid_qs = uid_set_qs.filter(addTime__range=(item[0], item[1]))
|
|
|
+ uid_qs = deivce_uid_qs.values('uid')
|
|
|
+ if uid_qs.exists():
|
|
|
+ uid_list = [item[key] for item in uid_qs for key in item]
|
|
|
+ count_part = deivce_uid_qs.count()
|
|
|
+ rate = round(count_part / count_all * 100, 2)
|
|
|
+ info_dict = {
|
|
|
+ 'startTime': item[0],
|
|
|
+ 'endTime': item[1],
|
|
|
+ 'count': count_part,
|
|
|
'rate': rate
|
|
|
}
|
|
|
- region_list.append(country_dict)
|
|
|
- res['region'] = CommonService.list_sort(region_list)
|
|
|
- # 统计设备类型数量
|
|
|
- device_info_type_qs = device_info_qs.filter(UID__in=part_only_list).values('Type').annotate(
|
|
|
- count=Count('Type', distinct=True)).order_by('-count').distinct()
|
|
|
- count = device_info_type_qs.count()
|
|
|
- for device_type in device_info_type_qs:
|
|
|
- type = device_type['Type']
|
|
|
- name = DEVICE_TYPE.get(type, '未知类型')
|
|
|
- name = name if name != 'UNKOWN' else '未知类型'
|
|
|
- type_qs = device_info_qs.filter(UID__in=part_only_list).values('UID').order_by(
|
|
|
- 'UID').distinct().values(
|
|
|
- 'Type')
|
|
|
- test_list = [item[key] for item in type_qs for key in item]
|
|
|
- type_count = test_list.count(type)
|
|
|
- rate = round(type_count / count_part * 100, 2)
|
|
|
- type_dict = {
|
|
|
- 'type': name,
|
|
|
- 'count': type_count,
|
|
|
+ info_list.append(info_dict)
|
|
|
+ res['addDevice'] = info_list
|
|
|
+ # 统计地区设备数量
|
|
|
+ device_info_country_qs = deivce_uid_qs.values('tb_country').annotate(
|
|
|
+ count=Count('tb_country')).order_by(
|
|
|
+ '-count')
|
|
|
+ for item in device_info_country_qs:
|
|
|
+ country_id = item['tb_country']
|
|
|
+ country_qs = CountryModel.objects.filter(id=country_id).values('country_name', 'id')
|
|
|
+ country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
+ country_qs = deivce_uid_qs.values('tb_country')
|
|
|
+ total_list = [item[key] for item in country_qs for key in item]
|
|
|
+ country_count = total_list.count(country_id)
|
|
|
+ rate = round(country_count / count_part * 100, 2)
|
|
|
+ country_dict = {
|
|
|
+ 'countryName': country_name,
|
|
|
+ 'count': country_count,
|
|
|
+ 'rate': rate
|
|
|
+ }
|
|
|
+ region_list.append(country_dict)
|
|
|
+ res['region'] = CommonService.list_sort(region_list)
|
|
|
+ # 统计设备类型数量
|
|
|
+ device_info_type_qs = Device_Info.objects.filter(UID__in=uid_list)
|
|
|
+ device_type_qs = device_info_type_qs.values('Type').annotate(
|
|
|
+ count=Count('Type', distinct=True)).order_by('-count').distinct()
|
|
|
+ count = device_info_type_qs.count()
|
|
|
+ name = '未知类型'
|
|
|
+ count_remain = count_part - count
|
|
|
+ rate = round(count_remain / count_part * 100, 2)
|
|
|
+ type_list.append({
|
|
|
+ 'name': name,
|
|
|
+ 'count': count_remain,
|
|
|
'rate': rate
|
|
|
- }
|
|
|
- type_list.append(type_dict)
|
|
|
- res['type'] = CommonService.list_sort(type_list)
|
|
|
- # 统计设备版本数量
|
|
|
- order_model_qs = Order_Model.objects.filter(UID__in=part_only_list).values('UID').annotate(
|
|
|
- count=Count('UID', distinct=True))
|
|
|
- order_model_qs = order_model_qs.filter(order_type=order_type).values('order_type', 'UID').order_by(
|
|
|
- 'UID')
|
|
|
- order_type_list = []
|
|
|
- for order_model in order_model_qs:
|
|
|
- orderType = order_model['UID']
|
|
|
- order_type_list.append(orderType)
|
|
|
- res_part = {}
|
|
|
- device_info_type_qs = device_info_qs.filter(UID__in=order_type_list).values('Type').annotate(
|
|
|
- count=Count('Type', distinct=True)).order_by('-count').distinct()
|
|
|
- for device_type in device_info_type_qs:
|
|
|
- type = device_type['Type']
|
|
|
- name = DEVICE_TYPE.get(type, '未知类型')
|
|
|
- res_part['type'] = name if name != 'UNKOWN' else '未知类型'
|
|
|
- type_qs = device_info_qs.filter(UID__in=order_type_list).values('UID').order_by(
|
|
|
- 'UID').distinct().values(
|
|
|
- 'Type')
|
|
|
- test_list = [item[key] for item in type_qs for key in item]
|
|
|
- res_part['count'] = test_list.count(type)
|
|
|
- res_part['rate'] = round(res_part['count'] / count_part * 100, 2)
|
|
|
+ })
|
|
|
+ for device_type in device_type_qs:
|
|
|
+ Type = device_type['Type']
|
|
|
+ device_type_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
|
|
|
+ name = device_type_qs[0]['name'] if device_type_qs.exists() else '未知类型'
|
|
|
+ type_qs = device_info_type_qs.values('Type')
|
|
|
+ test_list = [item[key] for item in type_qs for key in item]
|
|
|
+ type_count = test_list.count(Type)
|
|
|
+ rate = round(type_count / count_part * 100, 2)
|
|
|
+ type_list.append({
|
|
|
+ 'name': name,
|
|
|
+ 'count': type_count,
|
|
|
+ 'rate': rate
|
|
|
+ })
|
|
|
+ res['type'] = CommonService.list_sort(type_list)
|
|
|
+ # 云存版本数量
|
|
|
+ res_part = {}
|
|
|
+ cloud_qs = deivce_uid_qs.exclude(cloud_vod=2).values('cloud_vod').count()
|
|
|
+ res_part['count'] = cloud_qs
|
|
|
+ res_part['rate'] = round(cloud_qs / count_part * 100, 2)
|
|
|
version_list.append(res_part)
|
|
|
res['version'] = version_list
|
|
|
+ else:
|
|
|
+ info_dict = {
|
|
|
+ 'startTime': item[0],
|
|
|
+ 'endTime': item[1],
|
|
|
+ 'count': 0,
|
|
|
+ 'rate': 0.0
|
|
|
+ }
|
|
|
+ info_list.append(info_dict)
|
|
|
+ res['addDevice'] = info_list
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
@@ -507,26 +485,27 @@ class DeviceDataView(View):
|
|
|
统计地区设备数量
|
|
|
@param response:响应对象
|
|
|
"""
|
|
|
- device_country_qs = Device_Info.objects.all().values('userID__region_country').annotate(
|
|
|
- count=Count('userID__region_country')).order_by('-count')
|
|
|
- device_info_qs = Device_Info.objects.values('UID').order_by('UID').distinct()
|
|
|
- count_all = device_info_qs.count()
|
|
|
- if not device_country_qs.exists():
|
|
|
+ uid_set_qs = UidSetModel.objects.all()
|
|
|
+ tb_country_qs = uid_set_qs.values('tb_country').annotate(
|
|
|
+ count=Count('tb_country')).order_by('-count')
|
|
|
+ uid_set = uid_set_qs.values('uid')
|
|
|
+ count_all = uid_set.count()
|
|
|
+ if not uid_set_qs.exists():
|
|
|
return response.json(444)
|
|
|
res = {}
|
|
|
try:
|
|
|
device_country_list = []
|
|
|
continent_list = []
|
|
|
- for device_country in device_country_qs:
|
|
|
- country_id = device_country['userID__region_country']
|
|
|
+ for device_country in tb_country_qs:
|
|
|
+ country_id = device_country['tb_country']
|
|
|
country_qs = CountryModel.objects.filter(id=country_id).values('country_name', 'region__name')
|
|
|
if not country_qs.exists():
|
|
|
name = '未知地区'
|
|
|
else:
|
|
|
name = country_qs[0]['country_name']
|
|
|
- count = Device_Info.objects.filter(
|
|
|
- userID__region_country=device_country['userID__region_country']).values('UID').annotate(
|
|
|
- count=Count('UID', distinct=True)).order_by('-count').count()
|
|
|
+ count = uid_set_qs.filter(
|
|
|
+ tb_country=device_country['tb_country']).values('uid').annotate(
|
|
|
+ count=Count('uid', distinct=True)).order_by('-count').count()
|
|
|
|
|
|
device_country_list.append({
|
|
|
'countryName': name,
|
|
@@ -560,21 +539,36 @@ class DeviceDataView(View):
|
|
|
@param response:响应对象
|
|
|
@return:
|
|
|
"""
|
|
|
- device_info_qs = Device_Info.objects.all().values('Type').annotate(count=Count('Type')).order_by('-count')
|
|
|
+ uid_set_qs = UidSetModel.objects.values('uid').order_by('uid')
|
|
|
+ uid_list = [item[key] for item in uid_set_qs for key in item]
|
|
|
+ device_info_qs = Device_Info.objects.filter(UID__in=uid_list)
|
|
|
+ device_type_qs = device_info_qs.values('Type').annotate(count=Count('Type')).order_by('-count')
|
|
|
+ count_all = len(uid_list)
|
|
|
+ device_info_count = device_info_qs.count()
|
|
|
+ # 查询不到(类型)的设备数量
|
|
|
if not device_info_qs.exists():
|
|
|
return response.json(444)
|
|
|
res = {}
|
|
|
try:
|
|
|
device_info_list = []
|
|
|
- for device_info in device_info_qs:
|
|
|
- type = device_info['Type']
|
|
|
- name = DEVICE_TYPE.get(type, '未知类型')
|
|
|
- name = name if name != 'UNKOWN' else '未知类型'
|
|
|
- count = Device_Info.objects.filter(Type=device_info['Type']).values('UID').annotate(
|
|
|
- count=Count('UID', distinct=True)).order_by('-count').count()
|
|
|
+ count = count_all - device_info_count
|
|
|
+ name = '未知类型'
|
|
|
+ rate = round(count / count_all * 100, 2)
|
|
|
+ device_info_list.append({
|
|
|
+ 'count': count,
|
|
|
+ 'name': name,
|
|
|
+ 'rate': rate
|
|
|
+ })
|
|
|
+ for device_info in device_type_qs:
|
|
|
+ Type = device_info['Type']
|
|
|
+ device_type_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
|
|
|
+ name = device_type_qs[0]['name'] if device_type_qs.exists() else '未知类型'
|
|
|
+ count = device_info['count']
|
|
|
+ rate = round(count / count_all * 100, 2)
|
|
|
device_info_list.append({
|
|
|
'type': name,
|
|
|
- 'count': count
|
|
|
+ 'count': count,
|
|
|
+ 'rate': rate
|
|
|
})
|
|
|
res['type'] = device_info_list
|
|
|
return response.json(0, res)
|