Selaa lähdekoodia

设备数据查询(设备数据汇总表)

guanhailong 2 vuotta sitten
vanhempi
commit
414f476521
1 muutettua tiedostoa jossa 159 lisäystä ja 139 poistoa
  1. 159 139
      AdminController/dataSystemManagement/DeviceDataController.py

+ 159 - 139
AdminController/dataSystemManagement/DeviceDataController.py

@@ -10,10 +10,11 @@
 import datetime
 
 import requests
-from django.db.models import Count
+from django.db.models import Count, Q, Sum
 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, \
+    DeviceInfoSummary
 from Service.CommonService import CommonService
 
 
@@ -51,6 +52,23 @@ class DeviceDataView(View):
         if operation == 'global/addDevice':  # 全球新增设备数据
             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
     def golbal_add_device(cls, request, request_dict, response):
         """
@@ -342,41 +360,45 @@ class DeviceDataView(View):
         e_time = datetime.datetime.fromtimestamp(int(end_time))
         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)).values('uid').order_by('uid').distinct()
+            device_info_summary_qs = DeviceInfoSummary.objects.filter(
+                time__range=(start_time, end_time), query_type=1).values('country', 'count')
             null_list = []
-            if not video_playback_time_qs.exists():
+            if not device_info_summary_qs.exists():
                 return response.json(0, null_list)
-            count_all = video_playback_time_qs.count()
+            count_all = device_info_summary_qs.aggregate(total=Sum('count'))['total']
             res = {}
             video_list = []
             region_list = []
             for item in time_list:
-                video_playback_qs = video_playback_time_qs.filter(startTime__range=(item[0], item[1])).values(
-                    'uid').order_by('uid').distinct()
-                # uid_qs = video_playback_qs.values('uid').order_by('uid').distinct()
-                rate = round(video_playback_qs.count() / count_all * 100, 2)
+                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()
                 vod_dict = {
-                    'count': video_playback_qs.count(),
-                    'rate': rate,
+                    'count': count,
+                    'rate': round(count / count_all * 100, 2),
                     'startTime': item[0],
                     'endTime': item[1]
                 }
                 video_list.append(vod_dict)
             res['vodHls'] = video_list
-            type_country_qs = UidSetModel.objects.filter(uid__in=video_playback_time_qs.values('uid')).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)
+            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
             return response.json(0, res)
         except Exception as e:
@@ -398,12 +420,11 @@ class DeviceDataView(View):
         if not all([start_time, end_time, unit_time]):
             return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
         try:
-            uid_set_qs = UidSetModel.objects.filter(addTime__range=(start_time, end_time)).values('uid','tb_country')
+            uid_set_qs = DeviceInfoSummary.objects.filter(time__range=(start_time, end_time), query_type=0)
             null_list = []
             if not uid_set_qs.exists():
                 return response.json(0, null_list)
-            uid_qs = uid_set_qs.values('uid')
-            count_all = uid_set_qs.count()
+            count_all = uid_set_qs.aggregate(total=Sum('count'))['total']
             # 统计该时间段的设备数量(已去重)
             res = {
                 'addDevice': '',
@@ -419,78 +440,76 @@ class DeviceDataView(View):
             end_time = datetime.datetime.fromtimestamp(int(end_time))
             time_list = CommonService.cutting_time(start_time, end_time, unit_time)
             for item in time_list:
-                deivce_uid_qs = uid_set_qs.filter(addTime__range=(item[0], item[1])).count()
-                rate = round(deivce_uid_qs / count_all * 100, 2)
+                device_uid_qs = uid_set_qs.filter(time__range=(item[0], item[1]), query_type=0)
+                if device_uid_qs.exists():
+                    count = device_uid_qs.aggregate(total=Sum('count'))['total']
+                else:
+                    count = device_uid_qs.count()
+                rate = round(count / count_all * 100, 2)
                 info_dict = {
                     'startTime': item[0],
                     'endTime': item[1],
-                    'count': deivce_uid_qs,
+                    'count': count,
                     'rate': rate
                 }
                 info_list.append(info_dict)
             res['addDevice'] = info_list
             # 统计地区设备数量
-            device_info_country_qs = uid_set_qs.values('tb_country').annotate(
-                count=Count('tb_country')).order_by(
-                '-count')
+            device_info_country_qs = uid_set_qs.values('country')
             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 '未知区域'
-                rate = round(item['count'] / count_all * 100, 2)
-                country_dict = {
-                    'countryName': country_name,
-                    'count': item['count'],
-                    'rate': rate
-                }
-                region_list.append(country_dict)
+                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'] / count_all * 100, 2) if count_all else 0
             res['region'] = CommonService.list_sort(region_list)
             # 统计设备类型数量
-            device_info_type_qs = Device_Info.objects.filter(UID__in=uid_qs.values('uid'))
-            device_type_qs = device_info_type_qs.values('Type').annotate(
-                count=Count('Type')).order_by('-count').distinct()
-            name = '未知类型'
-            count_remain = count_all - device_info_type_qs.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_name_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
-                name = device_type_name_qs[0]['name'] if device_type_name_qs.exists() else '未知类型'
-                rate = round(device_type['count'] / count_all * 100, 2)
-                type_list.append({
-                    'type': name,
-                    'count': device_type['count'],
-                    'rate': rate
-                })
+            device_type_qs = uid_set_qs.values('device_type')
+            for item in device_type_qs:
+                country_temp_dict = eval(item['device_type'])
+                for x, y in country_temp_dict.items():
+                    flag = 0
+                    for each in type_list:
+                        if x == each['type']:
+                            each['count'] += y
+                            flag = 1
+                            break
+                    if flag == 0:
+                        type_list.append({
+                            'type': x,
+                            'count': y
+                        })
+            for item in type_list:
+                item['rate'] = round(item['count'] / count_all * 100, 2) if count_all else 0
             res['type'] = CommonService.list_sort(type_list)
             # 云存版本数量
-            uid_cloud_qs = uid_set_qs.exclude(cloud_vod=2).values('uid')
-            device_info_type_qs = Device_Info.objects.filter(UID__in=uid_cloud_qs.values('uid')).distinct()
-            cloud_type_qs = device_info_type_qs.values('Type').annotate(
-                count=Count('Type')).order_by('-count').distinct()
-            count = uid_cloud_qs.count() - device_info_type_qs.count()
-            name = '未知类型'
-            rate = round(count / count_all * 100, 2)
-            version_list.append({
-                'type': name,
-                'count': count,
-                'rate': rate
-            })
+            cloud_type_qs = uid_set_qs.values('vod_service')
             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 '未知类型'
-                rate = round(cloud_type['count'] / count_all * 100, 2)
-                version_list.append({
-                    'type': name,
-                    'count': cloud_type['count'],
-                    'rate': rate
-                })
+                country_temp_dict = eval(cloud_type['vod_service'])
+                for x, y in country_temp_dict.items():
+                    flag = 0
+                    for each in version_list:
+                        if x == each['type']:
+                            each['count'] += y
+                            flag = 1
+                            break
+                    if flag == 0:
+                        version_list.append({
+                            'type': x,
+                            'count': y
+                        })
+            for item in version_list:
+                item['rate'] = round(item['count'] / count_all * 100, 2) if count_all else 0
             res['version'] = CommonService.list_sort(version_list)
 
             return response.json(0, res)
@@ -504,42 +523,49 @@ class DeviceDataView(View):
         统计地区设备数量
         @param response:响应对象
         """
-        uid_set_qs = UidSetModel.objects.values('tb_country')
-        if not uid_set_qs.exists():
+        all_device_qs = DeviceInfoSummary.objects.all()
+        if not all_device_qs.exists():
             return response.json(173)
-        tb_country_qs = uid_set_qs.values('tb_country').annotate(
-            count=Count('tb_country')).order_by('-count')
+        all_country_qs = all_device_qs.values('count', 'country')
+        all_continent_qs = all_device_qs.values('continent')
+        country_count = all_country_qs.aggregate(total=Sum('count'))['total']
         res = {}
         try:
-            device_country_list = []
             continent_list = []
-            for device_country in tb_country_qs:
-                country_id = device_country['tb_country']
-                country_qs = CountryModel.objects.filter(id=country_id).values('country_name', 'region__name')
-                if country_qs.exists():
-                    name = country_qs[0]['country_name']
-                else:
-                    name = '未知地区'
-
-                device_country_list.append({
-                    'countryName': name,
-                    'count': device_country['count']
-                })
-                if country_qs.exists():
+            region_list = []
+            for item in all_country_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_continent_qs:
+                continent_temp_dict = eval(item['continent'])
+                for x, y in continent_temp_dict.items():
                     flag = 0
                     for each in continent_list:
-                        if country_qs[0]['region__name'] == each['continentName']:
-                            each['count'] += device_country['count']
+                        if x == each['continentName']:
+                            each['count'] += y
                             flag = 1
                             break
                     if flag == 0:
                         continent_list.append({
-                            'continentName': country_qs[0]['region__name'],
-                            'count': device_country['count']
+                            'continentName': x,
+                            'count': y
                         })
             for item in continent_list:
-                item['rate'] = round(item['count'] / uid_set_qs.count() * 100, 2)
-            res['countries'] = CommonService.list_sort(device_country_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)
             return response.json(0, res)
         except Exception as e:
@@ -553,37 +579,31 @@ class DeviceDataView(View):
         @param response:响应对象
         @return:
         """
-        uid_set_qs = UidSetModel.objects.values('uid').order_by('uid')
-        count_all = uid_set_qs.count()
-        device_info_qs = Device_Info.objects.filter(UID__in=uid_set_qs.values('uid'))
-        device_info_count = device_info_qs.count()
-        device_info_type_qs = device_info_qs.values('Type').annotate(count=Count('Type')).order_by('-count')
-        # 查询不到(类型)的设备数量
-        if not device_info_qs.exists():
-            return response.json(444)
+        all_device_qs = DeviceInfoSummary.objects.filter().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 = {}
         try:
-            device_info_list = []
-            count = count_all - device_info_count
-            name = '未知类型'
-            rate = round(count / count_all * 100, 2)
-            device_info_list.append({
-                'type': name,
-                'count': count,
-                'rate': rate
-            })
-            for device_info in device_info_type_qs:
-                Type = device_info['Type']
-                device_type_qs = DeviceTypeModel.objects.filter(type=Type).values('name')
-                name = device_type_qs[0]['name'] if device_type_qs.exists() else '未知类型'
-                count = device_info['count']
-                rate = round(count / count_all * 100, 2)
-                device_info_list.append({
-                    'type': name,
-                    'count': count,
-                    'rate': rate
-                })
-            res['type'] = device_info_list
+            device_type_list = []
+            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'] = device_type_list
             return response.json(0, res)
         except Exception as e:
             print(e)