瀏覽代碼

减轻api接口(设备数据模块)的数据负担

guanhailong 2 年之前
父節點
當前提交
e6605ae327
共有 1 個文件被更改,包括 8 次插入25 次删除
  1. 8 25
      AdminController/dataSystemManagement/DeviceDataController.py

+ 8 - 25
AdminController/dataSystemManagement/DeviceDataController.py

@@ -10,7 +10,7 @@
 import datetime
 import datetime
 
 
 import requests
 import requests
-from django.db.models import Count, Q
+from django.db.models import Count
 from django.views.generic.base import View
 from django.views.generic.base import View
 
 
 from Model.models import Device_Info, CountryModel, UidSetModel, DeviceTypeModel, VideoPlaybackTimeModel
 from Model.models import Device_Info, CountryModel, UidSetModel, DeviceTypeModel, VideoPlaybackTimeModel
@@ -51,23 +51,6 @@ class DeviceDataView(View):
         if operation == 'global/addDevice':  # 全球新增设备数据
         if operation == 'global/addDevice':  # 全球新增设备数据
             return self.golbal_add_device(request, request_dict, response)
             return self.golbal_add_device(request, request_dict, response)
 
 
-    #     if operation == 'ip/country':  #
-    #         return self.ip_country(response)
-    #     else:
-    #         return response.json(414)
-    #
-    # @staticmethod
-    # def ip_country(response):
-    #     uid_set_qs = UidSetModel.objects.filter(~Q(ip='') & Q(tb_country=0)).values('ip')
-    #     for uid_set in uid_set_qs:
-    #         ip = uid_set['ip']
-    #         ipInfo = CommonService.getIpIpInfo(ip, 'CN')
-    #         country_qs = CountryModel.objects.filter(country_code=ipInfo['country_code']).values('id')
-    #         if country_qs.exists():
-    #             country = country_qs[0]['id']
-    #             uid_set_qs.filter(ip=ip).update(tb_country=country)
-    #     return response.json(0)
-
     @classmethod
     @classmethod
     def golbal_add_device(cls, request, request_dict, response):
     def golbal_add_device(cls, request, request_dict, response):
         """
         """
@@ -430,13 +413,13 @@ class DeviceDataView(View):
             device_info_country_qs = uid_set_qs.values('tb_country').annotate(
             device_info_country_qs = uid_set_qs.values('tb_country').annotate(
                 count=Count('tb_country')).order_by(
                 count=Count('tb_country')).order_by(
                 '-count')
                 '-count')
+            country_qs = uid_set_qs.values('tb_country')
+            country_list = [item[key] for item in country_qs for key in item]
             for item in device_info_country_qs:
             for item in device_info_country_qs:
                 country_id = item['tb_country']
                 country_id = item['tb_country']
                 country_qs = CountryModel.objects.filter(id=country_id).values('country_name', 'id')
                 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_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)
+                country_count = country_list.count(country_id)
                 rate = round(country_count / count_all * 100, 2)
                 rate = round(country_count / count_all * 100, 2)
                 country_dict = {
                 country_dict = {
                     'countryName': country_name,
                     'countryName': country_name,
@@ -456,13 +439,13 @@ class DeviceDataView(View):
                 'count': count_remain,
                 'count': count_remain,
                 'rate': rate
                 'rate': rate
             })
             })
+            type_qs = device_info_type_qs.values('Type')
+            device_type_list = [item[key] for item in type_qs for key in item]
             for device_type in device_type_qs:
             for device_type in device_type_qs:
                 Type = device_type['Type']
                 Type = device_type['Type']
                 device_type_name_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
                 device_type_name_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
                 name = device_type_name_qs[0]['name'] if device_type_name_qs.exists() else '未知类型'
                 name = device_type_name_qs[0]['name'] if device_type_name_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)
+                type_count = device_type_list.count(Type)
                 rate = round(type_count / count_all * 100, 2)
                 rate = round(type_count / count_all * 100, 2)
                 type_list.append({
                 type_list.append({
                     'type': name,
                     'type': name,
@@ -528,7 +511,7 @@ class DeviceDataView(View):
                 else:
                 else:
                     name = '未知地区'
                     name = '未知地区'
                 count = uid_set_qs.filter(
                 count = uid_set_qs.filter(
-                    tb_country=device_country['tb_country']).values('uid').annotate(
+                    tb_country=country_id).values('uid').annotate(
                     count=Count('uid', distinct=True)).order_by('-count').count()
                     count=Count('uid', distinct=True)).order_by('-count').count()
 
 
                 device_country_list.append({
                 device_country_list.append({