Przeglądaj źródła

后台数据:查询新增设备接口

guanhailong 3 lat temu
rodzic
commit
b3165473ff

+ 98 - 102
AdminController/dataSystemManagement/DeviceDataController.py

@@ -203,117 +203,112 @@ class DeviceDataView(View):
         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_count_qs = device_info_qs.count()
-            device_type_qs = device_info_qs.values('UID').order_by('UID').distinct()
+            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 = []
-            count_unique = device_type_qs.count()
-            # 统计该时间段的设备数量(去重)
+            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 = {
+                    'count': len(part_only_list),
                     'startTime': item[0],
-                    'endTime': item[1]
+                    'endTime': item[1],
+                    'rate': rate
                 }
-                count = 0
-                for device_type in device_type_qs:
-                    uid = device_type['UID']
-                    device_test_qs = Device_Info.objects.filter(data_joined__lt=start_time, UID=uid).values()
-                    if device_test_qs.exists():
-                        continue
-                    else:
-                        count += 1
-                        device_only_qs = device_info_qs.filter(UID=uid).values()
-                        device_only = device_only_qs[0]
-                        # res = {
-                        #     'country': '',
-                        #     'count': ''
-                        # }
-                        #
-                        # for country in device_only:
-
-
-                info_dict['count'] = count
                 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
+                        # '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')
+                    # 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]
+                    type_count = test_list.count(type)
+                    rate = round(type_count / count_part * 100, 2)
+                    type_dict = {
+                        'type': name,
+                        'count': type_count,
+                        'rate': rate
+                        # '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))
+                uid_qs = order_model_qs.values('UID').order_by('UID').distinct()
+                order_model_qs = order_model_qs.values('order_type').order_by('order_type')
+                # count = order_model_qs.count()
+                order_type_list = []
+                for order_model in order_model_qs:
+                    orderType = order_model['order_type']
+                    order_type_list.append(orderType)
+                res_part = {}
+                if order_type == 0:
+                    res_part['name'] = '云存'
+                elif order_type == 1:
+                    res_part['name'] = 'AI'
+                elif order_type == 2:
+                    res_part['name'] = '联通4G'
+                res_part['total'] = order_type_list.count(order_type)
+                res_part['rate'] = round(res_part['total'] / count_part * 100, 2)
+                version_list.append(res_part)
+                res['version'] = version_list
 
-
-
-
-        #         info_list.append(info_dict)
-        #     # 统计地区设备数量
-        #     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 = 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,
-        #             'count': count,
-        #             'rate': rate
-        #         }
-        #         region_list.append(country_dict)
-        #     # 统计设备类型数量
-        #     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)
-        #     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 = 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,
-        #             'count': total,
-        #             'rate': rate
-        #         }
-        #         type_list.append(type_dict)
-        #     # 统计设备版本数量
-        #     device_info_type_qs = device_info_qs.values('UID').annotate(
-        #         count=Count('UID', distinct=True)).order_by('-count')
-        #     count = device_info_type_qs.count()
-        #     uid_list = []
-        #     for device_clound in device_info_type_qs:
-        #         uid = device_clound['UID']
-        #         uid_list.append(uid)
-        #     order_model_qs = Order_Model.objects.filter(UID__in=uid_list).values('UID').annotate(
-        #         count=Count('UID', distinct=True)).values('order_type').order_by('order_type')
-        #     count = order_model_qs.count()
-        #     order_type_list = []
-        #     for order_model in order_model_qs:
-        #         orderType = order_model['order_type']
-        #         order_type_list.append(orderType)
-        #     version_list = []
-        #     res = {}
-        #     if order_type == 0:
-        #         res['name'] = '云存'
-        #     elif order_type == 1:
-        #         res['name'] = 'AI'
-        #     elif order_type == 2:
-        #         res['name'] = '联通4G'
-        #     res['total'] = order_type_list.count(order_type)
-        #     version_list.append(res)
-        #
-        #     res = {
-        #         'info': info_list,
-        #         'region': region_list,
-        #         'type': type_list,
-        #         'version': version_list
-        #     }
-            return response.json(0, info_list)
+            return response.json(0, res)
         except Exception as e:
+            print(e)
             return response.json(500, repr(e))
 
     @classmethod
@@ -402,7 +397,8 @@ class DeviceDataView(View):
                     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')
+                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']