guanhailong 2 жил өмнө
parent
commit
490841bd25

+ 121 - 4
AdminController/ServeManagementController.py

@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
+import datetime
 import hashlib
 import json
 import time
@@ -14,13 +15,12 @@ from boto3.session import Session
 from django.http import JsonResponse, HttpResponseRedirect, HttpResponse, StreamingHttpResponse
 from django.views.generic.base import View
 from Model.models import Device_Info, Role, MenuModel, VodBucketModel, CDKcontextModel, Store_Meal, Order_Model, \
-    UID_Bucket, ExperienceContextModel, Lang, Device_User, CloudLogModel, UidSetModel, Unused_Uid_Meal
+    UID_Bucket, ExperienceContextModel, Lang, Device_User, CloudLogModel, UidSetModel, Unused_Uid_Meal, VodHlsModel
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
 from Object.UidTokenObject import UidTokenObject
 from Service.CommonService import CommonService
-from django.db.models import Q, F
-from time import strftime
+from django.db.models import F, Sum, Count
 
 
 class serveManagement(View):
@@ -90,6 +90,8 @@ class serveManagement(View):
                 return self.do_experience_reset(request_dict, userID, response)
             elif operation == 'getCloudUserList':  # 获取云存用户信息
                 return self.getCloudUserList(request_dict, response)
+            elif operation == 'deviceAttritionAlert':  # 流失预警
+                return self.deviceAttritionAlert(request_dict, response)
             else:
                 return response.json(404)
 
@@ -1165,4 +1167,119 @@ class serveManagement(View):
             return response.json(0, {'list': list_data})
         except Exception as e:
             print(e)
-            return response.json(500, repr(e))
+            return response.json(500, repr(e))
+
+    @classmethod
+    def deviceAttritionAlert(cls, request_dict, response):
+        """
+        流失预警界面
+        @param request_dict:
+        @param response:
+        """
+        pageNo = request_dict.get('pageNo', None)
+        pageSize = request_dict.get('pageSize', None)
+        use_status = request_dict.get('use_status', None)
+        if not all([pageNo, pageSize]):
+            return response.json(444)
+        page = int(pageNo)
+        line = int(pageSize)
+        nowTime = int(time.time())
+        attrition_list = []
+        r = 0
+        uid_buncket_qs = UID_Bucket.objects.filter(status=0).values('use_status', 'uid', 'endTime', 'addTime',
+                                                                    'use_status').annotate(count=Count('uid')).order_by(
+            '-addTime')
+        if use_status:
+            uid_buncket_qs = uid_buncket_qs.filter(use_status=use_status)
+        count = uid_buncket_qs.count()
+        uid_buncket_qs = uid_buncket_qs[(page - 1) * line:page * line]
+        try:
+            for uid_buncket in uid_buncket_qs:
+                day = 0
+                endTime = uid_buncket['endTime']
+                addTime = uid_buncket['addTime']
+                start_time = addTime
+                start_time = datetime.datetime.fromtimestamp(int(start_time))
+                r += 1
+                if r > 1:
+                    nowTime = str(nowTime)
+                    time_tuple = time.strptime(nowTime, ('%Y-%m-%d %H:%M:%S'))
+                    nowTime = time.mktime(time_tuple)  # 把格式化好的时间转换成时间戳
+                nowTime = datetime.datetime.fromtimestamp(int(nowTime))
+                time_list = CommonService.cutting_time(start_time, nowTime, 'day')
+                vod_hls_qs = VodHlsModel.objects.filter(uid=uid_buncket['uid']).values('uid')
+                #  获取自云存开通起没有上传数据天数
+                for date in time_list:
+                    vod_hls_qs = vod_hls_qs.filter(time__gte=date[0], time__lt=date[1])
+                    if not vod_hls_qs.exists():
+                        day += 1
+                        if day > 29:
+                            break
+                day = day
+                level = ''
+                use_status = uid_buncket['use_status']
+                if not use_status == 1:
+                    level = '八号预警'
+                else:
+                    # 统计设备目前未上传天数
+                    vod_hls_qs = VodHlsModel.objects.filter(uid=uid_buncket['uid']).values('uid')
+                    if 15 <= day < 25:
+                        if vod_hls_qs.exists():
+                            level = '取消预警'
+                        else:
+                            level = '一号预警'
+                    if day >= 25:
+                        if vod_hls_qs.exists():
+                            level = '取消预警'
+                        else:
+                            startTime = uid_buncket['addTime']  # 开始时间
+                            nowTime = str(nowTime)
+                            time_tuple = time.strptime(nowTime, ('%Y-%m-%d %H:%M:%S'))
+                            nowTime = time.mktime(time_tuple)  # 把格式化好的时间转换成时间戳
+                            startTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(startTime))  # 开始时间
+                            nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(nowTime))  # 结束时间
+                            startTime = datetime.datetime.strptime(startTime, '%Y-%m-%d %H:%M:%S')  # 开始时间
+                            nowTime = datetime.datetime.strptime(nowTime, '%Y-%m-%d %H:%M:%S')  # 结束时间
+                            day = (nowTime - startTime).days
+                            level = '二号预警'
+                device_user = Device_User.objects.filter(device_info__UID=uid_buncket['uid']).values('username',
+                                                                                                     'userID')
+                username = device_user[0]['username'] if device_user.exists() else ''
+                userID = device_user[0]['userID'] if device_user.exists() else ''
+                order_qs = Order_Model.objects.filter(order_type=0, userID=userID).values('UID').annotate(
+                    count=Count('UID'))
+                #  用户设备购买云存数量
+                if not order_qs.exists():
+                    device_count = 0
+                else:
+                    uid_count = order_qs.count()
+                    if uid_count == 1:
+                        device_count = 'N/A'
+                    else:
+                        device_count = uid_count
+                order_uid_list = [order[uid] for order in order_qs for uid in order]
+                vod_hls_qs = VodHlsModel.objects.filter(uid__in=order_uid_list).values('uid')
+                #  套餐使用期间是否上传过数据
+                if vod_hls_qs.exists():
+                    other = '有'
+                else:
+                    other = '无'
+
+                data = {
+                    'userID': userID,
+                    'uid': uid_buncket['uid'],
+                    'endTime': CommonService.timestamp_to_str(endTime),
+                    'addTime': CommonService.timestamp_to_str(addTime),
+                    'status': uid_buncket['use_status'],
+                    'level': level,
+                    'day': day,
+                    'other': other,
+                    'username': username,
+                    'count': device_count,
+                }
+                attrition_list.append(data)
+            test_list = [list for list in attrition_list if list['day'] > 14]  # 输出15天及以上的数据
+            return response.json(0, {'test_list': test_list, 'total': count})
+        except Exception as e:
+            return response.json(500, repr(e))
+