Преглед изворни кода

改善全球设备数据统计接口

guanhailong пре 2 година
родитељ
комит
53091dd815
1 измењених фајлова са 26 додато и 41 уклоњено
  1. 26 41
      AdminController/dataSystemManagement/DeviceDataController.py

+ 26 - 41
AdminController/dataSystemManagement/DeviceDataController.py

@@ -10,10 +10,10 @@
 import datetime
 
 import requests
-from django.db.models import Q, Sum
+from django.db.models import Sum
 from django.views.generic.base import View
 
-from Model.models import CountryModel, UidSetModel, DeviceInfoSummary
+from Model.models import DeviceInfoSummary
 from Service.CommonService import CommonService
 
 
@@ -90,11 +90,6 @@ class DeviceDataView(View):
                         if flag == 0:
                             device_list.append(item)
                             device_count += item['count']
-                    for item in device_list:
-                        if device_count != 0:
-                            item['rate'] = round(item['count'] / device_count * 100, 2)
-                        else:
-                            break
                     for item in result['result']['region']:
                         flag = 0
                         for each in region_list:
@@ -106,11 +101,6 @@ class DeviceDataView(View):
                         if flag == 0:
                             region_list.append(item)
                             region_count += item['count']
-                    for item in region_list:
-                        if region_count != 0:
-                            item['rate'] = round(item['count'] / region_count * 100, 2)
-                        else:
-                            break
                     for item in result['result']['type']:
                         flag = 0
                         for each in type_list:
@@ -122,11 +112,6 @@ class DeviceDataView(View):
                         if flag == 0:
                             type_list.append(item)
                             type_count += item['count']
-                    for item in type_list:
-                        if type_count != 0:
-                            item['rate'] = round(item['count'] / type_count * 100, 2)
-                        else:
-                            break
                     for item in result['result']['version']:
                         flag = 0
                         for each in order_list:
@@ -138,13 +123,21 @@ class DeviceDataView(View):
                         if flag == 0:
                             order_list.append(item)
                             order_count += item['count']
-                    for item in order_list:
+
                         if order_count != 0:
                             item['rate'] = round(item['count'] / order_count * 100, 2)
                         else:
                             break
                 else:
-                    return response.json(result['result_code'])
+                    return response.json(result['result_code'], result['result'])
+            for item in device_list:
+                item['rate'] = round(item['count'] / device_count * 100, 2) if device_count else 0
+            for item in region_list:
+                item['rate'] = round(item['count'] / region_count * 100, 2) if region_count else 0
+            for item in type_list:
+                item['rate'] = round(item['count'] / type_count * 100, 2) if type_count else 0
+            for item in order_list:
+                item['rate'] = round(item['count'] / order_count * 100, 2) if order_count else 0
             res = {
                 'device': device_list,
                 'type': CommonService.list_sort(type_list),
@@ -189,11 +182,6 @@ class DeviceDataView(View):
                         if flag == 0:
                             type_list.append(item)
                             type_count += item['count']
-                    for item in type_list:
-                        if type_count != 0:
-                            item['rate'] = round(item['count'] / type_count * 100, 2)
-                        else:
-                            break
                     for item in result['result']['region']:
                         flag = 0
                         for each in region_list:
@@ -205,14 +193,12 @@ class DeviceDataView(View):
                         if flag == 0:
                             region_list.append(item)
                             region_count += item['count']
-                    for item in region_list:
-                        if region_count != 0:
-                            item['rate'] = round(item['count'] / region_count * 100, 2)
-                        else:
-                            break
                 else:
-                    return response.json(result['result_code'])
-
+                    return response.json(result['result_code'], result['result'])
+            for item in region_list:
+                item['rate'] = round(item['count'] / region_count * 100, 2) if region_count else 0
+            for item in type_list:
+                item['rate'] = round(item['count'] / type_count * 100, 2) if type_count else 0
             res = {
                 'device': type_list,
                 'region': CommonService.list_sort(region_list)
@@ -253,10 +239,10 @@ class DeviceDataView(View):
                         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'])
+                    return response.json(result['result_code'], result['result'])
+            for item in type_list:
+                item['rate'] = round(item['count'] / type_count * 100, 2) if type_count else 0
             res = {
                 'type': CommonService.list_sort(type_list)
             }
@@ -300,9 +286,6 @@ class DeviceDataView(View):
                         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:
@@ -314,12 +297,14 @@ class DeviceDataView(View):
                         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'])
+                    return response.json(result['result_code'], result['result'])
+            for item in device_list:
+                item['rate'] = round(item['count'] / device_count * 100, 2) if device_count else 0
+            for item in region_list:
+                item['rate'] = round(item['count'] / region_count * 100, 2) if region_count else 0
             res = {
-                'countries': CommonService.list_sort(device_list[:20]),
+                'countries': CommonService.list_sort(device_list[:30]),
                 'continent': region_list
             }
             return response.json(0, res)