|
@@ -65,14 +65,36 @@ class DeviceDataView(View):
|
|
|
}
|
|
|
type_list = []
|
|
|
type_count = 0
|
|
|
- return response.json(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):
|
|
|
"""
|
|
@@ -89,26 +111,41 @@ class DeviceDataView(View):
|
|
|
}
|
|
|
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']:
|
|
|
+ for item in result['result']['countries']:
|
|
|
flag = 0
|
|
|
for each in device_list:
|
|
|
if each['name'] == item['name']:
|
|
|
- each['total'] += int(item['total'])
|
|
|
- device_count += int(item['total'])
|
|
|
+ each['count'] += int(item['count'])
|
|
|
+ device_count += int(item['count'])
|
|
|
flag = 1
|
|
|
break
|
|
|
if flag == 0:
|
|
|
device_list.append(item)
|
|
|
- device_count += int(item['total'])
|
|
|
+ device_count += int(item['count'])
|
|
|
for item in device_list:
|
|
|
- rate = round(item['total'] / device_count * 100, 2)
|
|
|
+ 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 = {
|
|
@@ -168,39 +205,32 @@ class DeviceDataView(View):
|
|
|
device_count_qs = device_info_qs.count()
|
|
|
device_type_qs = device_info_qs.values('UID').order_by('UID').distinct()
|
|
|
info_list = []
|
|
|
-
|
|
|
- # device_info_type_qs = Device_Info.objects.values('UID').annotate(
|
|
|
- # count=Count('UID', distinct=True)).values('Type').order_by('-count')
|
|
|
-
|
|
|
count_unique = device_type_qs.count()
|
|
|
-
|
|
|
# 统计该时间段的设备数量(去重)
|
|
|
- for item in time_list:
|
|
|
+ for item in device_info_qs:
|
|
|
start_time = datetime.datetime.fromtimestamp(int(item[0]))
|
|
|
end_time = datetime.datetime.fromtimestamp(int(item[1]))
|
|
|
device_type = device_info_qs.filter(data_joined__range=(start_time, end_time)).values(
|
|
|
- 'UID').distinct()
|
|
|
+ 'UID')
|
|
|
info_dict = {
|
|
|
'count': device_type.count(),
|
|
|
'startTime': item[0],
|
|
|
'endTime': item[1]
|
|
|
}
|
|
|
+ for device_type in device_type_qs:
|
|
|
+ uid = device_type['UID']
|
|
|
+
|
|
|
info_list.append(info_dict)
|
|
|
# 统计地区设备数量
|
|
|
- device_info_country_qs = device_info_qs.values('userID__region_country').order_by('-count')
|
|
|
- device_info_country_qs = device_info_qs.values('UID').annotate(
|
|
|
- count=Count('UID', distinct=True)).values('userID__region_country').order_by('-count')
|
|
|
- count_list = []
|
|
|
- for device_info_country in device_info_country_qs:
|
|
|
- region = device_info_country['userID__region_country']
|
|
|
- count_list.append(region)
|
|
|
- device_info_country_qs = device_info_country_qs.values('userID__region_country').distinct()
|
|
|
+ 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', 'id')
|
|
|
country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
- count = count_list.count(country_id)
|
|
|
+ count = device_info_qs.filter(userID__region_country=item['userID__region_country']).values(
|
|
|
+ 'UID').annotate(count=Count('UID', distinct=True)).order_by('-count').count()
|
|
|
rate = round(count / count_unique * 100, 2)
|
|
|
country_dict = {
|
|
|
'countryName': country_name,
|
|
@@ -209,20 +239,21 @@ class DeviceDataView(View):
|
|
|
}
|
|
|
region_list.append(country_dict)
|
|
|
# 统计设备类型数量
|
|
|
- device_info_type_qs = device_info_qs.values('UID').annotate(
|
|
|
- count=Count('UID', distinct=True)).values('Type').order_by('-count')
|
|
|
+ device_info_type_qs = device_info_qs.values('Type').annotate(
|
|
|
+ count=Count('Type', distinct=True)).order_by('-count')
|
|
|
count = device_info_type_qs.count()
|
|
|
- count_list = []
|
|
|
- for device_info_country in device_info_type_qs:
|
|
|
- type = device_info_country['Type']
|
|
|
- count_list.append(type)
|
|
|
+ # count_list = []
|
|
|
+ # for device_info_country in device_info_type_qs:
|
|
|
+ # type = device_info_country['Type']
|
|
|
+ # count_list.append(type)
|
|
|
device_info_type_qs = device_info_type_qs.values('Type').distinct()
|
|
|
type_list = []
|
|
|
for device_type in device_info_type_qs:
|
|
|
type = device_type['Type']
|
|
|
name = DEVICE_TYPE.get(type, '未知类型')
|
|
|
name = name if name != 'UNKOWN' else '未知类型'
|
|
|
- total = count_list.count(type)
|
|
|
+ total = device_info_qs.filter(Type=device_type['Type']).values('UID').annotate(
|
|
|
+ count=Count('UID', distinct=True)).order_by('-count').count()
|
|
|
rate = round(total / count_unique * 100, 2) # count_unique 有误,跟device_info_type_qs 总数合不上 (可以看151行)
|
|
|
type_dict = {
|
|
|
'type': name,
|
|
@@ -272,41 +303,48 @@ class DeviceDataView(View):
|
|
|
统计地区设备数量
|
|
|
@param response:响应对象
|
|
|
"""
|
|
|
- device_info_qs = Device_Info.objects.all()
|
|
|
- device_info_qs = device_info_qs.values('UID').order_by('UID').distinct()
|
|
|
+ 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_info_qs.exists():
|
|
|
+ if not device_country_qs.exists():
|
|
|
return response.json(444)
|
|
|
+ res = {}
|
|
|
try:
|
|
|
device_country_list = []
|
|
|
- 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 = []
|
|
|
+ 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', 'id')
|
|
|
- countryName = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
|
|
|
- device_info = region_list.count(country_id)
|
|
|
- rate = round(device_info / count * 100, 2)
|
|
|
- res = {
|
|
|
- 'id': country_id,
|
|
|
- 'name': countryName,
|
|
|
- 'count': device_info,
|
|
|
- 'rate': rate
|
|
|
- }
|
|
|
- device_region_list.append(res)
|
|
|
- return response.json(0, device_region_list)
|
|
|
+ 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)
|
|
@@ -321,6 +359,7 @@ class DeviceDataView(View):
|
|
|
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:
|
|
@@ -331,9 +370,43 @@ class DeviceDataView(View):
|
|
|
count=Count('UID', distinct=True)).order_by('-count').count()
|
|
|
device_info_list.append({
|
|
|
'type': name,
|
|
|
- 'total': count
|
|
|
+ '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
|
|
|
})
|
|
|
- return response.json(0, device_info_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)
|