فهرست منبع

首页数据新增用户地区、所有用户地区

peng 3 سال پیش
والد
کامیت
6e30418424
1فایلهای تغییر یافته به همراه64 افزوده شده و 5 حذف شده
  1. 64 5
      AdminController/dataSystemManagement/HomeDataController.py

+ 64 - 5
AdminController/dataSystemManagement/HomeDataController.py

@@ -10,12 +10,12 @@ import datetime
 import openpyxl
 import requests
 
-from django.db.models import Sum
+from django.db.models import Sum, Count
 from django.http import HttpResponse
 from django.utils.encoding import escape_uri_path
 from django.views.generic.base import View
 
-from Model.models import VideoPlaybackTimeModel, Device_User, Device_Info, Order_Model
+from Model.models import VideoPlaybackTimeModel, Device_User, Device_Info, Order_Model, CountryModel
 from Service.CommonService import CommonService
 
 
@@ -61,9 +61,21 @@ class HomeDataView(View):
         end_time_stamp = CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S'))
         start_time_stamp = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
         try:
-            user_increase_count = Device_User.objects.filter(data_joined__range=(start_time, end_time)).count()
+            user_increase_qs = Device_User.objects.filter(data_joined__range=(start_time, end_time))
+            user_increase_list = user_increase_qs.values('region_country').annotate(
+                count=Count('region_country')).order_by('-count')
+            for item in user_increase_list:
+                country_qs = CountryModel.objects.filter(id=item['region_country']).values('country_name')
+                item['countryName'] = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
+            user_increase_count = user_increase_qs.count()
             user_active_count = Device_User.objects.filter(last_login__range=(start_time, end_time)).count()
-            user_all_count = Device_User.objects.filter(data_joined__lte=end_time).count()
+            user_all_qs = Device_User.objects.filter(data_joined__lte=end_time)
+            user_all_list = user_all_qs.values('region_country').annotate(
+                count=Count('region_country')).order_by('-count')
+            for item in user_all_list:
+                country_qs = CountryModel.objects.filter(id=item['region_country']).values('country_name')
+                item['countryName'] = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
+            user_all_count = user_all_qs.count()
             device_increase_count = Device_Info.objects.filter(data_joined__range=(start_time, end_time)).values(
                 'UID').distinct().count()
             device_active_count = VideoPlaybackTimeModel.objects.filter(
@@ -94,6 +106,8 @@ class HomeDataView(View):
                 'vodOrderAllTotal': round(vod_order_all_total, 2) if vod_order_all_total else 0,
                 'aiOrderAllTotal': round(ai_order_all_total, 2) if ai_order_all_total else 0,
                 'unicomOrderAllTotal': round(unicom_order_all_total, 2) if unicom_order_all_total else 0,
+                'userIncreaseRegion': list(user_increase_list),
+                'userAllRegion': list(user_all_list)
             }
             return response.json(0, res)
         except Exception as e:
@@ -161,6 +175,12 @@ class HomeDataView(View):
             vod_order_all_total = 0
             ai_order_all_total = 0
             unicom_order_all_total = 0
+            user_increase_temp_list = []
+            user_increase_list = []
+            user_increase_other_dict = {'count': 0, 'countryName': '其他', 'rate': ''}
+            user_all_temp_list = []
+            user_all_list = []
+            user_all_other_dict = {'count': 0, 'countryName': '其他', 'rate': ''}
             for url in url_list:
                 url = url + request.path.replace('global/', '')
                 res = requests.get(url=url, params=request_dict, headers=headers)
@@ -179,6 +199,44 @@ class HomeDataView(View):
                 vod_order_all_total += result['result']['vodOrderAllTotal']
                 ai_order_all_total += result['result']['aiOrderAllTotal']
                 unicom_order_all_total += result['result']['unicomOrderAllTotal']
+                for item in result['result']['userIncreaseRegion']:
+                    flag = 0
+                    for each in user_increase_temp_list:
+                        if item['countryName'] == each['countryName']:
+                            each['count'] += item['count']
+                            flag = 1
+                            break
+                    if flag == 0:
+                        user_increase_temp_list.append(item)
+
+                for item in result['result']['userAllRegion']:
+                    flag = 0
+                    for each in user_all_temp_list:
+                        if item['countryName'] == each['countryName']:
+                            each['count'] += item['count']
+                            flag = 1
+                            break
+                    if flag == 0:
+                        user_all_temp_list.append(item)
+            for item in user_increase_temp_list:
+                rate = round(item['count'] / user_increase_count * 100, 2)
+                if rate >= 10:
+                    item['rate'] = rate
+                    user_increase_list.append(item)
+                else:
+                    user_increase_other_dict['count'] += item['count']
+            user_increase_other_dict['rate'] = round(user_increase_other_dict['count'] / user_increase_count * 100, 2)
+            user_increase_list.append(user_increase_other_dict)
+
+            for item in user_all_temp_list:
+                rate = round(item['count'] / user_all_count * 100, 2)
+                if rate >= 10:
+                    item['rate'] = rate
+                    user_all_list.append(item)
+                else:
+                    user_all_other_dict['count'] += item['count']
+            user_all_other_dict['rate'] = round(user_all_other_dict['count'] / user_increase_count * 100, 2)
+            user_all_list.append(user_all_other_dict)
             res = {
                 'userIncreaseCount': user_increase_count,
                 'userActiveCount': user_active_count,
@@ -194,6 +252,8 @@ class HomeDataView(View):
                 'vodOrderAllTotal': vod_order_all_total,
                 'aiOrderAllTotal': ai_order_all_total,
                 'unicomOrderAllTotal': unicom_order_all_total,
+                'userIncreaseRegion': user_increase_list,
+                'userAllRegion': user_all_list
             }
             return response.json(0, res)
         except Exception as e:
@@ -230,7 +290,6 @@ class HomeDataView(View):
         except Exception as e:
             return response.json(500, repr(e))
 
-
     @classmethod
     def export_data(cls, request_dict, response):
         """