|
@@ -14,7 +14,7 @@ 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
|
|
|
+from Model.models import Device_Info, CountryModel, Order_Model, VodHlsModel
|
|
|
from Service.CommonService import CommonService
|
|
|
|
|
|
|
|
@@ -158,26 +158,55 @@ class DeviceDataView(View):
|
|
|
|
|
|
@classmethod
|
|
|
def device_active(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()
|
|
|
+ start_time = request_dict.get('startTime', None) # 时间戳
|
|
|
+ end_time = request_dict.get('endTime', None)
|
|
|
+ unit_time = request_dict.get('unitTime', None)
|
|
|
+ if not all([start_time, end_time, unit_time]):
|
|
|
+ return response.json(444, {'error param': 'startTime or endTime or timeUnit or order_type'})
|
|
|
+ s_time = datetime.datetime.fromtimestamp(int(start_time))
|
|
|
+ e_time = datetime.datetime.fromtimestamp(int(end_time))
|
|
|
+ time_list = CommonService.cutting_time(s_time, e_time, unit_time)
|
|
|
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)
|
|
|
+ vod_hls_model_qs = VodHlsModel.objects.filter(time__range=(start_time, end_time))
|
|
|
+ device_info = list(vod_hls_model_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 = []
|
|
|
+ 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()
|
|
|
+ uid_list = [item[key] for item in uid_qs for key in item]
|
|
|
+ rate = round(uid_qs.count() / count_all * 100, 2)
|
|
|
+ vod_dict = {
|
|
|
+ 'count': uid_qs.count(),
|
|
|
+ 'rate': rate,
|
|
|
+ '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_qs = CountryModel.objects.filter(id=country_id).values('country_name')
|
|
|
+ country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
+ country_qs = vod_hls_model_qs.filter(uid__in=uid_list).values('uid').order_by(
|
|
|
+ 'uid').distinct()
|
|
|
+ total_list = [item[key] for item in country_qs for key in item]
|
|
|
+ country_count = len(total_list)
|
|
|
+ rate = round(country_count / count_all * 100, 2)
|
|
|
+ country_dict = {
|
|
|
+ 'countryName': country_name,
|
|
|
+ 'count': item['count'],
|
|
|
+ 'rate': rate
|
|
|
+ }
|
|
|
+ region_list.append(country_dict)
|
|
|
+ res['region'] = region_list
|
|
|
|
|
|
- return response.json(0, type_list)
|
|
|
+ return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(500)
|
|
@@ -243,13 +272,15 @@ class DeviceDataView(View):
|
|
|
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(
|
|
|
+ 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(
|
|
|
+ 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)
|
|
@@ -270,7 +301,8 @@ class DeviceDataView(View):
|
|
|
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_qs = device_info_qs.filter(UID__in=part_only_list).values('UID').order_by(
|
|
|
+ 'UID').distinct().values(
|
|
|
'Type')
|
|
|
# rate = round(total / count_unique * 100, 2) # count_unique 有误,跟device_info_type_qs 总数合不上 (可以看151行)
|
|
|
test_list = [item[key] for item in type_qs for key in item]
|