Forráskód Böngészése

完善代码格式(设备数据模块)

guanhailong 3 éve
szülő
commit
4e5cc54b7c

+ 93 - 97
AdminController/dataSystemManagement/DeviceDataController.py

@@ -325,8 +325,9 @@ class DeviceDataView(View):
         time_list = CommonService.cutting_time(s_time, e_time, unit_time)
         try:
             video_playback_time_qs = VideoPlaybackTimeModel.objects.filter(startTime__range=(start_time, end_time))
+            null_list = []
             if not video_playback_time_qs.exists():
-                return response.json(173)
+                return response.json(0, null_list)
             device_info = list(video_playback_time_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)
@@ -345,20 +346,20 @@ class DeviceDataView(View):
                     'endTime': item[1]
                 }
                 video_list.append(vod_dict)
-                type_country_qs = UidSetModel.objects.filter(uid__in=uid_list).values(
-                    'tb_country').annotate(count=Count('tb_country')).order_by('-count')
-                for type_country in type_country_qs:
-                    country_id = type_country['tb_country']
-                    country_name_qs = CountryModel.objects.filter(id=country_id).values('country_name')
-                    country_name = country_name_qs[0]['country_name'] if country_name_qs.exists() else '未知区域'
-                    rate = round(type_country['count'] / count_all * 100, 2)
-                    country_dict = {
-                        'countryName': country_name,
-                        'count': type_country['count'],
-                        'rate': rate
-                    }
-                    region_list.append(country_dict)
             res['vodHls'] = video_list
+            type_country_qs = UidSetModel.objects.filter(uid__in=device_info_list).values(
+                'tb_country').annotate(count=Count('tb_country')).order_by('-count')
+            for type_country in type_country_qs:
+                country_id = type_country['tb_country']
+                country_name_qs = CountryModel.objects.filter(id=country_id).values('country_name')
+                country_name = country_name_qs[0]['country_name'] if country_name_qs.exists() else '未知区域'
+                rate = round(type_country['count'] / count_all * 100, 2)
+                country_dict = {
+                    'countryName': country_name,
+                    'count': type_country['count'],
+                    'rate': rate
+                }
+                region_list.append(country_dict)
             res['region'] = region_list
             return response.json(0, res)
         except Exception as e:
@@ -381,6 +382,11 @@ class DeviceDataView(View):
             return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
         try:
             uid_set_qs = UidSetModel.objects.filter(addTime__range=(start_time, end_time))
+            null_list = []
+            if not uid_set_qs.exists():
+                return response.json(0, null_list)
+            uid_qs = uid_set_qs.values('uid')
+            uid_list = [item[key] for item in uid_qs for key in item]
             count_all = uid_set_qs.count()
             # 统计该时间段的设备数量(已去重)
             res = {
@@ -399,89 +405,79 @@ class DeviceDataView(View):
             for item in time_list:
                 deivce_uid_qs = uid_set_qs.filter(addTime__range=(item[0], item[1]))
                 uid_qs = deivce_uid_qs.values('uid')
-                if uid_qs.exists():
-                    uid_list = [item[key] for item in uid_qs for key in item]
-                    count_part = deivce_uid_qs.count()
-                    rate = round(count_part / count_all * 100, 2)
-                    info_dict = {
-                        'startTime': item[0],
-                        'endTime': item[1],
-                        'count': count_part,
-                        'rate': rate
-                    }
-                    info_list.append(info_dict)
-                    # 统计地区设备数量
-                    device_info_country_qs = deivce_uid_qs.values('tb_country').annotate(
-                        count=Count('tb_country')).order_by(
-                        '-count')
-                    for item in device_info_country_qs:
-                        country_id = item['tb_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 = deivce_uid_qs.values('tb_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)
-                    # 统计设备类型数量
-                    device_info_type_qs = Device_Info.objects.filter(UID__in=uid_list)
-                    device_type_qs = device_info_type_qs.values('Type').annotate(
-                        count=Count('Type', distinct=True)).order_by('-count').distinct()
-                    count = device_info_type_qs.count()
-                    name = '未知类型'
-                    count_remain = count_part - count
-                    rate = round(count_remain / count_part * 100, 2)
-                    type_list.append({
-                        'type': name,
-                        'count': count_remain,
-                        'rate': rate
-                    })
-                    for device_type in device_type_qs:
-                        Type = device_type['Type']
-                        device_type_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
-                        name = device_type_qs[0]['name'] if device_type_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)
-                        rate = round(type_count / count_part * 100, 2)
-                        type_list.append({
-                            'type': name,
-                            'count': type_count,
-                            'rate': rate
-                        })
-                    # 云存版本数量
-                    cloud_qs = deivce_uid_qs.exclude(cloud_vod=2).values('uid')
-                    cloud_list = [item[key] for item in cloud_qs for key in item]
-                    device_info_type_qs = Device_Info.objects.filter(UID__in=cloud_list).distinct()
-                    cloud_type_qs = device_info_type_qs.values('Type').annotate(
-                        count=Count('Type', distinct=True)).order_by('-count').distinct()
-                    type_qs = device_info_type_qs.values('Type')
-                    test_list = [item[key] for item in type_qs for key in item]
-                    for cloud_type in cloud_type_qs:
-                        Type = cloud_type['Type']
-                        device_type_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
-                        name = device_type_qs[0]['name'] if device_type_qs.exists() else '未知类型'
-                        type_count = test_list.count(Type)
-                        rate = round(type_count / count_part * 100, 2)
-                        version_list.append({
-                            'type': name,
-                            'count': type_count,
-                            'rate': rate
-                        })
-                else:
-                    null_dict = {
-                        'startTime': item[0],
-                        'endTime': item[1],
-                        'count': 0,
-                        'rate': 0.0
-                    }
-                    info_list.append(null_dict)
-                    res['addDevice'] = info_list
+                uid_list = [item[key] for item in uid_qs for key in item]
+                count_part = deivce_uid_qs.count()
+                rate = round(count_part / count_all * 100, 2)
+                info_dict = {
+                    'startTime': item[0],
+                    'endTime': item[1],
+                    'count': count_part,
+                    'rate': rate
+                }
+                info_list.append(info_dict)
+                # 统计地区设备数量
+            device_info_country_qs = uid_set_qs.values('tb_country').annotate(
+                count=Count('tb_country')).order_by(
+                '-count')
+            for item in device_info_country_qs:
+                country_id = item['tb_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 = 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)
+                rate = round(country_count / count_all * 100, 2)
+                country_dict = {
+                    'countryName': country_name,
+                    'count': country_count,
+                    'rate': rate
+                }
+                region_list.append(country_dict)
+            # 统计设备类型数量
+            device_info_type_qs = Device_Info.objects.filter(UID__in=uid_list)
+            device_type_qs = device_info_type_qs.values('Type').annotate(
+                count=Count('Type', distinct=True)).order_by('-count').distinct()
+            count = device_info_type_qs.count()
+            name = '未知类型'
+            count_remain = count_all - count
+            rate = round(count_remain / count_all * 100, 2)
+            type_list.append({
+                'type': name,
+                'count': count_remain,
+                'rate': rate
+            })
+            for device_type in device_type_qs:
+                Type = device_type['Type']
+                device_type_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
+                name = device_type_qs[0]['name'] if device_type_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)
+                rate = round(type_count / count_all * 100, 2)
+                type_list.append({
+                    'type': name,
+                    'count': type_count,
+                    'rate': rate
+                })
+            # 云存版本数量
+            cloud_qs = uid_set_qs.exclude(cloud_vod=2).values('uid')
+            cloud_list = [item[key] for item in cloud_qs for key in item]
+            device_info_type_qs = Device_Info.objects.filter(UID__in=cloud_list).distinct()
+            cloud_type_qs = device_info_type_qs.values('Type').annotate(
+                count=Count('Type', distinct=True)).order_by('-count').distinct()
+            type_qs = device_info_type_qs.values('Type')
+            test_list = [item[key] for item in type_qs for key in item]
+            for cloud_type in cloud_type_qs:
+                Type = cloud_type['Type']
+                device_type_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
+                name = device_type_qs[0]['name'] if device_type_qs.exists() else '未知类型'
+                type_count = test_list.count(Type)
+                rate = round(type_count / count_all * 100, 2)
+                version_list.append({
+                    'type': name,
+                    'count': type_count,
+                    'rate': rate
+                })
             res['addDevice'] = info_list
             res['region'] = CommonService.list_sort(region_list)
             res['type'] = CommonService.list_sort(type_list)