Browse Source

重构设备数据查询接口

guanhailong 2 năm trước cách đây
mục cha
commit
719abf1378
1 tập tin đã thay đổi với 78 bổ sung83 xóa
  1. 78 83
      AdminController/dataSystemManagement/DeviceDataController.py

+ 78 - 83
AdminController/dataSystemManagement/DeviceDataController.py

@@ -162,9 +162,9 @@ class DeviceDataView(View):
                     return response.json(result['result_code'])
             res = {
                 'device': device_list,
-                'type': type_list,
+                'type': CommonService.list_sort(type_list),
                 'region': CommonService.list_sort(region_list),
-                'version': order_list
+                'version': CommonService.list_sort(order_list)
             }
             return response.json(0, res)
         except Exception as e:
@@ -360,20 +360,22 @@ class DeviceDataView(View):
         time_list = CommonService.cutting_time(s_time, e_time, unit_time)
         try:
             device_info_summary_qs = DeviceInfoSummary.objects.filter(
-                time__range=(start_time, end_time), query_type=1).values('country', 'count')
-            res = {}
-            if not device_info_summary_qs.exists():
-                return response.json(0, res)
+                time__gte=start_time, time__lt=end_time, query_type=1).values('country', 'count')
             count_all = device_info_summary_qs.aggregate(total=Sum('count'))['total']
-
             video_list = []
             region_list = []
+            region_dict = {}
+            for item in device_info_summary_qs:
+                region_temp_dict = eval(item['country'])
+                for country, count in region_temp_dict.items():
+                    if country in region_dict:
+                        region_dict[country] += count
+                    else:
+                        region_dict[country] = count
             for item in time_list:
-                deivce_type_qs = device_info_summary_qs.filter(time__range=(item[0], item[1])).values('count')
-                if deivce_type_qs.exists():
-                    count = deivce_type_qs.aggregate(total=Sum('count'))['total']
-                else:
-                    count = deivce_type_qs.count()
+                deivce_type_qs = device_info_summary_qs.filter(time__gte=item[0], time__lt=item[1]).values('count')
+                count = deivce_type_qs.aggregate(total=Sum('count'))['total']
+                count = count if count else 0
                 vod_dict = {
                     'count': count,
                     'rate': round(count / count_all * 100, 2),
@@ -381,24 +383,18 @@ class DeviceDataView(View):
                     'endTime': item[1]
                 }
                 video_list.append(vod_dict)
-            res['vodHls'] = video_list
-            for type_country in device_info_summary_qs:
-                country_temp_dict = eval(type_country['country'])
-                for k, v in country_temp_dict.items():
-                    flag = 0
-                    for each in region_list:
-                        if k == each['countryName']:
-                            each['count'] += v
-                            flag = 1
-                            break
-                    if flag == 0:
-                        region_list.append({
-                            'countryName': k,
-                            'count': v
-                        })
-            for item in region_list:
-                item['rate'] = round(item['count'] / count_all * 100, 2) if count_all else 0
-            res['region'] = region_list
+            for country, count in region_dict.items():
+                rate = round(count / count_all * 100, 2) if count_all else 0
+                region_list.append({
+                    'countryName': country,
+                    'count': count,
+                    'rate': rate
+                })
+            res = {
+                'vodHls': video_list,
+                'region': CommonService.list_sort(region_list)
+
+            }
             return response.json(0, res)
         except Exception as e:
             print(e)
@@ -492,9 +488,9 @@ class DeviceDataView(View):
                 })
             res = {
                 'addDevice': info_list,
-                'region': region_list,
-                'type': type_list,
-                'version': vod_list
+                'region': CommonService.list_sort(region_list),
+                'type': CommonService.list_sort(type_list),
+                'version': CommonService.list_sort(vod_list)
             }
             return response.json(0, res)
         except Exception as e:
@@ -508,46 +504,45 @@ class DeviceDataView(View):
         @param response:响应对象
         """
         all_device_qs = DeviceInfoSummary.objects.filter(query_type=0).values('continent', 'count', 'country')
-
         country_count = all_device_qs.aggregate(total=Sum('count'))['total']
-        res = {}
         try:
             continent_list = []
-            region_list = []
+            country_list = []
+            continent_dict = {}
+            country_dict = {}
             for item in all_device_qs:
                 country_temp_dict = eval(item['country'])
-                for x, y in country_temp_dict.items():
-                    flag = 0
-                    for each in region_list:
-                        if x == each['countryName']:
-                            each['count'] += y
-                            flag = 1
-                            break
-                    if flag == 0:
-                        region_list.append({
-                            'countryName': x,
-                            'count': y
-                        })
-            for item in region_list:
-                item['rate'] = round(item['count'] / country_count * 100, 2) if country_count else 0
-            for item in all_device_qs:
                 continent_temp_dict = eval(item['continent'])
+                for x, y in country_temp_dict.items():
+                    if x in country_dict:
+                        country_dict[x] += y
+                    else:
+                        country_dict[x] = y
                 for x, y in continent_temp_dict.items():
-                    flag = 0
-                    for each in continent_list:
-                        if x == each['continentName']:
-                            each['count'] += y
-                            flag = 1
-                            break
-                    if flag == 0:
-                        continent_list.append({
-                            'continentName': x,
-                            'count': y
-                        })
-            for item in continent_list:
-                item['rate'] = round(item['count'] / country_count * 100, 2) if country_count else 0
-            res['countries'] = CommonService.list_sort(region_list)
-            res['continent'] = CommonService.list_sort(continent_list)
+                    if x in continent_dict:
+                        continent_dict[x] += y
+                    else:
+                        continent_dict[x] = y
+            # 地区设备量前30
+            for country, count in country_dict.items():
+                rate = round(count / country_count * 100, 2) if country_count else 0
+                country_list.append({
+                    'countryName': country,
+                    'count': count,
+                    'rate': rate
+                })
+            for continent, count in continent_dict.items():
+                rate = round(count / country_count * 100, 2) if country_count else 0
+                continent_list.append({
+                    'countryName': continent,
+                    'count': count,
+                    'rate': rate
+                })
+            res = {
+                'countries': CommonService.list_sort(country_list),
+                'continent': CommonService.list_sort(continent_list)
+            }
+
             return response.json(0, res)
         except Exception as e:
             print(e)
@@ -563,27 +558,27 @@ class DeviceDataView(View):
         all_device_qs = DeviceInfoSummary.objects.filter(query_type=0).values('device_type', 'count')
         if not all_device_qs.exists():
             return response.json(173)
-        country_count = all_device_qs.aggregate(total=Sum('count'))['total']
-        res = {}
+        count_all = all_device_qs.aggregate(total=Sum('count'))['total']
         try:
             device_type_list = []
+            device_type_dict = {}
             for item in all_device_qs:
                 country_temp_dict = eval(item['device_type'])
-                for t, c in country_temp_dict.items():
-                    flag = 0
-                    for each in device_type_list:
-                        if t == each['type']:
-                            each['count'] += c
-                            flag = 1
-                            break
-                    if flag == 0:
-                        device_type_list.append({
-                            'type': t,
-                            'count': c
-                        })
-            for item in device_type_list:
-                item['rate'] = round(item['count'] / country_count * 100, 2) if country_count else 0
-            res['type'] = CommonService.list_sort(device_type_list)
+                for k, v in country_temp_dict.items():
+                    if k in device_type_dict:
+                        device_type_dict[k] += v
+                    else:
+                        device_type_dict[k] = v
+            for x, y in device_type_dict.items():
+                rate = round(y / count_all * 100, 2) if count_all else 0
+                device_type_list.append({
+                    'type': x,
+                    'count': y,
+                    'rate': rate
+                })
+            res = {
+                'type': CommonService.list_sort(device_type_list)
+            }
             return response.json(0, res)
         except Exception as e:
             print(e)