|
@@ -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
|
|
|
+from Model.models import Device_Info, CountryModel, UidSetModel, DeviceTypeModel, VideoPlaybackTimeModel
|
|
|
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):
|
|
@@ -327,18 +324,19 @@ class DeviceDataView(View):
|
|
|
e_time = datetime.datetime.fromtimestamp(int(end_time))
|
|
|
time_list = CommonService.cutting_time(s_time, e_time, unit_time)
|
|
|
try:
|
|
|
- vod_hls_model_qs = VodHlsModel.objects.filter(time__range=(start_time, end_time))
|
|
|
- if not vod_hls_model_qs.exists():
|
|
|
- return response.json(173)
|
|
|
- device_info = list(vod_hls_model_qs.values('uid').order_by('uid').distinct())
|
|
|
+ video_playback_time_qs = VideoPlaybackTimeModel.objects.filter(startTime__range=(start_time, end_time))
|
|
|
+ null_list = []
|
|
|
+ if not video_playback_time_qs.exists():
|
|
|
+ return response.json(0, null_list)
|
|
|
+ device_info = list(video_playback_time_qs.values('uid').order_by('uid').distinct())
|
|
|
device_info_list = [item[key] for item in device_info for key in item]
|
|
|
count_all = len(device_info_list)
|
|
|
res = {}
|
|
|
- vod_list = []
|
|
|
+ video_list = []
|
|
|
region_list = []
|
|
|
for item in time_list:
|
|
|
- vod_hls_qs = vod_hls_model_qs.filter(time__range=(item[0], item[1]))
|
|
|
- uid_qs = vod_hls_qs.values('uid').order_by('uid').distinct()
|
|
|
+ video_playback_qs = video_playback_time_qs.filter(startTime__range=(item[0], item[1]))
|
|
|
+ uid_qs = video_playback_qs.values('uid').order_by('uid').distinct()
|
|
|
uid_list = [item[key] for item in uid_qs for key in item]
|
|
|
rate = round(uid_qs.count() / count_all * 100, 2)
|
|
|
vod_dict = {
|
|
@@ -347,23 +345,22 @@ class DeviceDataView(View):
|
|
|
'startTime': item[0],
|
|
|
'endTime': item[1]
|
|
|
}
|
|
|
- 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']
|
|
|
- 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)
|
|
|
- country_dict = {
|
|
|
- 'countryName': country_name,
|
|
|
- 'count': item['count'],
|
|
|
- 'rate': rate
|
|
|
- }
|
|
|
- region_list.append(country_dict)
|
|
|
- res['region'] = region_list
|
|
|
-
|
|
|
+ video_list.append(vod_dict)
|
|
|
+ res['vodHls'] = video_list
|
|
|
+ type_country_qs = UidSetModel.objects.filter(uid__in=device_info_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(type_country['count'] / count_all * 100, 2)
|
|
|
+ country_dict = {
|
|
|
+ 'countryName': country_name,
|
|
|
+ 'count': type_country['count'],
|
|
|
+ 'rate': rate
|
|
|
+ }
|
|
|
+ region_list.append(country_dict)
|
|
|
+ res['region'] = region_list
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
@@ -381,23 +378,16 @@ 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)
|
|
|
- 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)
|
|
|
+ if not all([start_time, end_time, unit_time]):
|
|
|
+ return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
|
|
|
try:
|
|
|
- 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 = len(part_only_list)
|
|
|
- count_all = int(count_all)
|
|
|
+ uid_set_qs = UidSetModel.objects.filter(addTime__range=(start_time, end_time))
|
|
|
+ null_list = []
|
|
|
+ if not uid_set_qs.exists():
|
|
|
+ return response.json(0, null_list)
|
|
|
+ uid_qs = uid_set_qs.values('uid')
|
|
|
+ uid_list = [item[key] for item in uid_qs for key in item]
|
|
|
+ count_all = uid_set_qs.count()
|
|
|
# 统计该时间段的设备数量(已去重)
|
|
|
res = {
|
|
|
'addDevice': '',
|
|
@@ -409,92 +399,90 @@ class DeviceDataView(View):
|
|
|
region_list = []
|
|
|
type_list = []
|
|
|
version_list = []
|
|
|
+ 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)
|
|
|
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)
|
|
|
+ deivce_uid_qs = uid_set_qs.filter(addTime__range=(item[0], item[1]))
|
|
|
+ uid_qs = deivce_uid_qs.values('uid')
|
|
|
+ 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': len(part_only_list),
|
|
|
+ 'count': count_part,
|
|
|
'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,
|
|
|
- '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,
|
|
|
- '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)
|
|
|
- version_list.append(res_part)
|
|
|
- res['version'] = version_list
|
|
|
+ device_info_country_qs = uid_set_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 = uid_set_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_all * 100, 2)
|
|
|
+ country_dict = {
|
|
|
+ 'countryName': country_name,
|
|
|
+ 'count': country_count,
|
|
|
+ 'rate': rate
|
|
|
+ }
|
|
|
+ region_list.append(country_dict)
|
|
|
+ # 统计设备类型数量
|
|
|
+ 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_all - count
|
|
|
+ rate = round(count_remain / count_all * 100, 2)
|
|
|
+ type_list.append({
|
|
|
+ 'type': name,
|
|
|
+ 'count': count_remain,
|
|
|
+ 'rate': rate
|
|
|
+ })
|
|
|
+ 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_all * 100, 2)
|
|
|
+ type_list.append({
|
|
|
+ 'type': name,
|
|
|
+ 'count': type_count,
|
|
|
+ 'rate': rate
|
|
|
+ })
|
|
|
+ # 云存版本数量
|
|
|
+ cloud_qs = uid_set_qs.exclude(cloud_vod=2).values('uid')
|
|
|
+ cloud_list = [item[key] for item in cloud_qs for key in item]
|
|
|
+ device_info_type_qs = Device_Info.objects.filter(UID__in=cloud_list).distinct()
|
|
|
+ cloud_type_qs = device_info_type_qs.values('Type').annotate(
|
|
|
+ count=Count('Type', distinct=True)).order_by('-count').distinct()
|
|
|
+ type_qs = device_info_type_qs.values('Type')
|
|
|
+ test_list = [item[key] for item in type_qs for key in item]
|
|
|
+ for cloud_type in cloud_type_qs:
|
|
|
+ Type = cloud_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_count = test_list.count(Type)
|
|
|
+ rate = round(type_count / count_all * 100, 2)
|
|
|
+ version_list.append({
|
|
|
+ 'type': name,
|
|
|
+ 'count': type_count,
|
|
|
+ 'rate': rate
|
|
|
+ })
|
|
|
+ res['addDevice'] = info_list
|
|
|
+ res['region'] = CommonService.list_sort(region_list)
|
|
|
+ res['type'] = CommonService.list_sort(type_list)
|
|
|
+ res['version'] = CommonService.list_sort(version_list)
|
|
|
+
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
@@ -506,26 +494,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,
|
|
@@ -559,21 +548,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({
|
|
|
+ 'type': name,
|
|
|
+ 'count': count,
|
|
|
+ '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)
|