# -*- encoding: utf-8 -*- """ @File : UserDataController.py @Time : 2022/8/16 10:44 @Author : peng @Email : zhangdongming@asj6.wecom.work @Software: PyCharm """ import datetime 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 Service.CommonService import CommonService # 设备数据 class DeviceDataView(View): def get(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.GET, request, operation) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.POST, request, operation) def validation(self, request_dict, request, operation): 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 == 'type': # 统计设备类型 return self.type_statistics(response) if operation == 'regional': # 设备地区分布 return self.regional_statistics(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 == 'global/type': # 全球设备类型 return self.golbal_type(request, request_dict, response) if operation == 'global/active': # 全球设备活跃分布 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): """ 全球新增设备数据 @param request:请求 @param request_dict:请求参数 @param response: 响应对象 """ url_list = CommonService.get_domain_name() try: headers = { 'Authorization': request.META.get('HTTP_AUTHORIZATION') } device_list = [] device_count = 0 type_list = [] type_count = 0 region_list = [] region_count = 0 order_list = [] order_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']['addDevice']: flag = 0 for each in device_list: if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']: each['count'] += item['count'] device_count += item['count'] flag = 1 break if flag == 0: device_list.append(item) device_count += item['count'] for item in device_list: item['rate'] = round(item['count'] / device_count * 100, 2) for item in result['result']['region']: flag = 0 for each in region_list: if item['countryName'] == each['countryName']: each['count'] += item['count'] region_count += item['count'] flag = 1 break if flag == 0: region_list.append(item) region_count += item['count'] for item in region_list: item['rate'] = round(item['count'] / region_count * 100, 2) for item in result['result']['type']: flag = 0 for each in type_list: if item['type'] == each['type']: each['count'] += item['count'] type_count += item['count'] flag = 1 break if flag == 0: type_list.append(item) type_count += item['count'] for item in type_list: item['rate'] = round(item['count'] / type_count * 100, 2) for item in result['result']['version']: flag = 0 for each in order_list: if item['type'] == each['type']: each['count'] += item['count'] order_count += item['count'] flag = 1 break if flag == 0: order_list.append(item) order_count += item['count'] for item in order_list: item['rate'] = round(item['count'] / order_count * 100, 2) else: return response.json(result['result_code']) res = { 'device': device_list, 'type': type_list, 'region': CommonService.list_sort(region_list), 'version': order_list } return response.json(0, res) except Exception as e: print(e) return response.json(500, repr(e)) @classmethod def golbal_active(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 region_list = [] region_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']['vodHls']: flag = 0 for each in type_list: if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']: each['count'] += item['count'] type_count += item['count'] flag = 1 break if flag == 0: type_list.append(item) type_count += item['count'] for item in type_list: item['rate'] = round(item['count'] / type_count * 100, 2) for item in result['result']['region']: flag = 0 for each in region_list: if item['countryName'] == each['countryName']: each['count'] += item['count'] region_count += item['count'] flag = 1 break if flag == 0: region_list.append(item) region_count += item['count'] for item in region_list: item['rate'] = round(item['count'] / region_count * 100, 2) else: return response.json(result['result_code']) res = { 'device': type_list, 'region': CommonService.list_sort(region_list) } return response.json(0, res) except Exception as e: print(e) return response.json(500, repr(e)) @classmethod 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 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']['region']: flag = 0 for each in type_list: if item['countryName'] == each['countryName']: each['count'] += item['count'] type_count += item['count'] each['countryType'] += item['countryType'] type_count += item['countryType'] flag = 1 break if flag == 0: type_list.append(item) type_count += item['count'] for item in type_list: item['rate'] = round(item['count'] / type_count * 100, 2) else: return response.json(result['result_code']) res = { 'type': CommonService.list_sort(type_list) } return response.json(0, res) 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 region_list = [] region_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']['countries']: flag = 0 for each in device_list: if each['countryName'] == item['countryName']: each['count'] += int(item['count']) device_count += int(item['count']) flag = 1 break if flag == 0: device_list.append(item) device_count += int(item['count']) for item in device_list: rate = round(item['count'] / device_count * 100, 2) item['rate'] = rate for item in result['result']['continent']: flag = 0 for each in region_list: if each['continentName'] == item['continentName']: each['count'] += item['count'] region_count += item['count'] flag = 1 break if flag == 0: region_list.append(item) region_count += item['count'] for item in region_list: item['rate'] = round(item['count'] / region_count * 100, 2) else: return response.json(result['result_code']) res = { 'countries': CommonService.list_sort(device_list[:20]), 'continent': region_list } return response.json(0, res) except Exception as e: return response.json(500, repr(e)) @classmethod def device_active(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('timeUnit', 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: 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()) 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, res) except Exception as e: print(e) return response.json(500) @classmethod def add_device(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('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) 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) # 统计该时间段的设备数量(已去重) res = { 'addDevice': '', 'region': '', 'type': '', 'version': '', } info_list = [] region_list = [] type_list = [] version_list = [] 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, '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 return response.json(0, res) except Exception as e: print(e) return response.json(500, repr(e)) @classmethod def regional_statistics(cls, response): """ 统计地区设备数量 @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 = device_info_qs.count() if not device_country_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'] 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() device_country_list.append({ 'countryName': name, 'count': count }) if country_qs.exists(): flag = 0 for each in continent_list: if country_qs[0]['region__name'] == each['continentName']: each['count'] += count flag = 1 break if flag == 0: continent_list.append({ 'continentName': country_qs[0]['region__name'], 'count': count }) for item in continent_list: item['rate'] = round(item['count'] / count * 100, 2) res['countries'] = device_country_list res['continent'] = continent_list return response.json(0, res) except Exception as e: print(e) return response.json(500) @classmethod def type_statistics(cls, response): """ 统计设备类型 @param response:响应对象 @return: """ device_info_qs = Device_Info.objects.all().values('Type').annotate(count=Count('Type')).order_by('-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() device_info_list.append({ 'type': name, 'count': count }) device_country_qs = Device_Info.objects.all().values('userID__region_country').annotate( count=Count('userID__region_country')).order_by('-count') device_country_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', 'region__name') if not country_qs.exists(): country_name = '未知地区' else: country_name = country_qs[0]['country_name'] device_type_qs = Device_Info.objects.filter(userID__region_country=country_id).values('Type').annotate( count=Count('Type', distinct=True)).order_by('-count') country_type_list = [] for device_type in device_type_qs: type = device_type['Type'] name = DEVICE_TYPE.get(type, '未知类型') name = name if name != 'UNKOWN' else '未知类型' count = device_type_qs.filter(Type=device_type['Type']).values('UID').annotate( count=Count('UID', distinct=True)).order_by('-count').count() country_type_list.append({ 'type': name, 'count': count }) 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() device_country_list.append({ 'countryName': country_name, 'count': count, 'countryType': country_type_list }) res['type'] = device_info_list res['region'] = device_country_list return response.json(0, res) except Exception as e: print(e) return response.json(500)