Kaynağa Gözat

Merge remote-tracking branch '99/dev'

# Conflicts:
#	Controller/AdminManage.py
#	Model/models.py
lang 4 yıl önce
ebeveyn
işleme
0d4f3ce9fe

+ 1 - 0
Ansjer/config.py

@@ -32,6 +32,7 @@ SES_COMPANY_EMAIL = 'user_server@nsst.com'
 AWS_SES_ACCESS_ID = 'AKIAJKPU23EU5QWHFPKQ'
 AWS_SES_ACCESS_SECRET = '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'
 AWS_SES_ACCESS_REGION = 'us-east-1'
+AWS_SES_ACCESS_REGION_WEST = 'us-west-1'
 AWS_BUCKET = 'ansjertest'
 # 设定离线时间为5分钟
 OFF_LINE_TIME_DELTA = 5

+ 27 - 0
Ansjer/formal_statistics_push_day_task.py

@@ -0,0 +1,27 @@
+import json
+
+import requests
+
+
+base_url = 'http://dvema.com'
+username='13800138001'
+password='ansjer123456'
+
+login_url = base_url + 'account/login'
+
+data = {
+    'userName': username,
+    'userPwd': password
+}
+res = requests.post(url=login_url, data=data)
+res = res.json()
+print(res)
+
+if res['result_code'] == 0:
+    statistics_url = base_url + 'statistcs/pushDay'
+    data = {
+        'token': res['result']['access_token']
+    }
+    res = requests.post(url=statistics_url, data=data)
+    print(res.json())
+

+ 27 - 0
Ansjer/test_statistics_push_day_task.py

@@ -0,0 +1,27 @@
+import json
+
+import requests
+
+
+# url = 'http://test.dvema.com'
+base_url = 'http://test.dvema.com/'
+username='13800138001'
+password='ansjer999999'
+login_url = base_url + 'account/login'
+
+data = {
+    'userName': username,
+    'userPwd': password
+}
+res = requests.post(url=login_url, data=data)
+res = res.json()
+print(res)
+
+if res['result_code'] == 0:
+    statistics_url = base_url + 'statistcs/pushDay'
+    data = {
+        'token': res['result']['access_token']
+    }
+    res = requests.post(url=statistics_url, data=data)
+    print(res.json())
+

+ 2 - 0
Ansjer/urls.py

@@ -221,6 +221,8 @@ urlpatterns = [
     url(r'^v2/userbrand/(?P<operation>.*)$', UserBrandControllerV2.UserBrandV2.as_view()),
     url(r'^statistcs/appFrequencyMonth$', StatisticsController.statistcsAppFrequency),
     url(r'^statistcs/appFrequencyYear$', StatisticsController.statistcsAppFrequencyYear),
+    url(r'^statistcs/pushDay$', StatisticsController.statistcsPushDay),
+    url(r'^statistcs/pushMonth$', StatisticsController.statistcsPushMonth),
 
     #统计alexa连接数
     url(r'^alexa/(?P<operation>.*)$', Alexa.AlexaConnectNum.as_view()),

+ 205 - 2
Controller/AdminManage.py

@@ -1,6 +1,8 @@
 # -*- coding: utf-8 -*-
 import time
+from datetime import date, timedelta, timezone as asj_timezone
 
+import boto3
 import xlwt
 from django.db.models import Count,Q
 from django.http import HttpResponse
@@ -9,14 +11,15 @@ from django.views.generic import TemplateView
 from django.utils.decorators import method_decorator
 from django.contrib.auth.hashers import make_password  # 对密码加密模块
 from Model.models import Device_Info, Role, UserExModel, User_Brand, UidSetModel, AppFrequencyYearStatisticsModel, \
-    AppFrequencyStatisticsModel
+    AppFrequencyStatisticsModel, EquipmentInfoExStatisticsModel, Equipment_Info
 from Service.ModelService import ModelService
 from django.utils import timezone
 from Model.models import Access_Log, Device_User
 from django.views.decorators.http import require_http_methods
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
-from Ansjer.config import OFF_LINE_TIME_DELTA, DEVICE_TYPE
+from Ansjer.config import OFF_LINE_TIME_DELTA, DEVICE_TYPE, AWS_SES_ACCESS_ID, AWS_SES_ACCESS_SECRET, \
+    AWS_SES_ACCESS_REGION_WEST
 import datetime, simplejson as json
 from Service.CommonService import CommonService
 from Object.RedisObject import RedisObject
@@ -82,6 +85,12 @@ class AdminManage(TemplateView):
             return self.getAllAppFrequency(userID, response)
         if operation == 'downloadSubscribeEmail':
             return self.download_subscribe_email(userID, request_dict, response)
+        if operation == 'getPushStatistics':
+            return self.query_push_by_level(userID, request_dict, response)
+        if operation == 'getPushFailures':
+            return self.query_failures_push(userID, request_dict, response)
+        if operation == 'getPushServerCPUUsage':
+            return self.query_push_server_cpu_usage(userID, request_dict, response)
 
     def resetUserPwd(self, request_dict, userID, response):
         own_permission = ModelService.check_perm(userID=userID, permID=50)
@@ -438,6 +447,198 @@ class AdminManage(TemplateView):
         else:
             return response.json(0, [])
 
+    def query_push_by_level(self, userID, request_dict, response):
+        own_permission = ModelService.check_perm(userID=userID, permID=30)
+        if own_permission is not True:
+            return response.json(404)
+
+        level = request_dict.get('level', None)
+        print(level)
+        if level is None:
+            return response.json(444)
+
+        level = int(level)
+        if level < 0:
+            return response.json(444)
+        elif level < 3:
+            return self.query_push_day_statistics(level, response)
+        else:
+            return response.json(404)
+
+    def query_push_day_statistics(self, level, response):
+        start_time = 0
+        end_time = 0
+        # localtime = time.localtime()
+        # current_day = localtime.tm_mday
+        #
+        # time_struct = [localtime.tm_year, localtime.tm_mon, current_day, 0, 0, 0, 0, 0, 0]
+        # current_time = int(time.mktime(tuple(time_struct)))
+        current_time = int(time.time())
+        count = int(current_time / (24 * 3600))
+        current_time = count * 24 * 3600 - 8 * 3600
+        count = 0
+        if level == 0: # 七天
+            start_time = current_time - 6 * 24 * 3600
+            count = 6
+        elif level == 1:
+            start_time = current_time - 13 * 24 * 3600
+            count = 13
+        elif level == 2:
+            start_time = current_time - 29 * 24 * 3600
+            count = 29
+
+        end_time = current_time
+
+        eqx_qs = EquipmentInfoExStatisticsModel.objects.filter(statistics_date__gte=start_time, statistics_date__lte=end_time, date_type=0, push_type=-1).values()
+        data = []
+
+        for i in range(count + 1):
+            data.append({
+                'date_time': int(start_time),
+                'data': None
+            })
+            start_time += (24 * 60 * 60)
+
+        tmp_dict = {}
+        successes = 0
+        failures = 0
+        for eqx in eqx_qs:
+            successes += eqx['number_of_successes']
+            failures += eqx['number_of_failures']
+            tmp_dict[eqx['statistics_date']] = {
+                'number_of_successes': eqx['number_of_successes'],
+                'number_of_failures': eqx['number_of_failures'],
+                'total': (eqx['number_of_successes'] + eqx['number_of_failures'])
+            }
+
+        # 取出当前的推送数据
+        start_time = current_time
+        current_time = int(time.time())
+        eq_qs = Equipment_Info.objects.filter(addTime__gte=start_time, addTime__lte=current_time)
+        print(eq_qs.values())
+        # 0:APNS推送,1:谷歌推送,2:极光推送
+        tmp = {
+            'number_of_successes': 0,
+            'number_of_failures': 0,
+            'total': 0
+        }
+        for eq in eq_qs:
+            if eq.push_server_status == 200:
+                tmp['number_of_successes'] += 1
+                successes += 1
+            else:
+                tmp['number_of_failures'] += 1
+                failures += 1
+            tmp['total'] += 1
+
+        tmp_dict[current_time] = tmp
+
+        for item in data:
+            if tmp_dict.__contains__(item['date_time']):
+                item['data'] = tmp_dict[item['date_time']]
+
+        success_rate = round(successes / (successes + failures), 2)
+        arrival_rate = success_rate
+
+        res = {
+            'data': data,
+            'successes': successes,
+            'failures': failures,
+            'success_rate': success_rate,
+            'arrival_rate': arrival_rate
+        }
+
+        return response.json(0, res)
+
+    # def query_push_month_statistics(self, level, response):
+    #     start_time = 0
+    #     end_time = 0
+    #     localtime = time.localtime()
+    #     current_month = localtime.tm_mon
+    #
+    #     time_struct = [localtime.tm_year, current_month, 1, 0, 0, 0, 0, 0, 0]
+    #     current_time = int(time.mktime(tuple(time_struct)))
+    #     current_time = current_time - time.timezone - 8 * 60 * 60
+    #     count = 0
+    #     start_month = 0
+    #     if level == 3:  # 6个月
+    #         start_month = current_month - 5
+    #         count = 5
+    #     elif level == 4:
+    #         start_month = current_month - 11
+    #         count = 10
+    #
+    #
+    #     return
+
+    def query_failures_push(self, userID, request_dict, response):
+        own_permission = ModelService.check_perm(userID=userID, permID=30)
+        if own_permission is not True:
+            return response.json(404)
+
+        start_time = request_dict.get('start_time', None)
+        end_time = request_dict.get('end_time', None)
+        page = request_dict.get('page', None)
+        line = request_dict.get('line', None)
+
+        if not start_time or not end_time or not page or not line:
+            return response.json(444)
+
+        start_time = int(start_time)
+        end_time = int(end_time)
+        page = int(page)
+        line = int(line)
+        if page <= 0 or line < 0:
+            return response.json(444, 'page, line')
+
+        eq_qs = Equipment_Info.objects.filter(addTime__gte=start_time, addTime__lte=end_time)\
+            .filter(~Q(push_server_status=200)).values('id', 'devUid', 'devNickName', 'Channel', 'alarm', 'eventType', 'eventTime', 'receiveTime', 'push_server_status', 'userID__username')
+        if eq_qs.exists():
+            count = eq_qs.count()
+            eq_qs = eq_qs[(page-1)*line: page * line]
+            return response.json(0, {'count': count, 'data': list(eq_qs)})
+        else:
+            return response.json(0, {'count': 0, 'data': []})
+
+    def query_push_server_cpu_usage(self, userID, request_dict, response):
+        own_permission = ModelService.check_perm(userID=userID, permID=30)
+        if own_permission is not True:
+            return response.json(404)
+
+        start_time = request_dict.get('start_time', None)
+        end_time = request_dict.get('end_time', None)
+        tz = request_dict.get('tz', None)
+
+        if start_time is None or end_time is None or tz is None:
+            return response.json(444)
+
+        date = datetime.datetime(2020, 9, 15)
+        start_time = date.fromtimestamp((int(start_time)))
+        end_time = date.fromtimestamp(int(end_time))
+        tz = int(tz)
+
+        cloudwatch = boto3.client('cloudwatch', region_name=AWS_SES_ACCESS_REGION_WEST, aws_access_key_id=AWS_SES_ACCESS_ID,
+                                  aws_secret_access_key=AWS_SES_ACCESS_SECRET)
+        try:
+            result = cloudwatch.get_metric_statistics(Namespace='AWS/EC2', MetricName='CPUUtilization',
+                                                      StartTime=start_time,
+                                                      EndTime=end_time, Period=60,
+                                                      Statistics=['Average'],
+                                                      Dimensions=[
+                                                          {'Name': 'InstanceId', 'Value': 'i-0596e00c9af077027'}])
+            datas = result['Datapoints']
+            datas.sort(key=getCompareKey)
+            result = []
+            for data in datas:
+                tmp = data
+                utcdt = data['Timestamp']
+                time1 = str(utcdt.astimezone(asj_timezone(timedelta(hours=int(-tz)))))
+                tmp['Timestamp'] = time1[0:time1.find('+')]
+                result.append(tmp)
+            return response.json(0, result)
+        except Exception as e:
+            print(repr(e))
+            return response.json(10, 'AWS Server Error')
     def download_subscribe_email(self, userID, request_dict, response):
         own_permission = ModelService.check_perm(userID=userID, permID=30)
         if own_permission is not True:
@@ -461,6 +662,8 @@ class AdminManage(TemplateView):
         workbook.save(response)
         return response
 
+def getCompareKey(item):
+    return item['Timestamp']
 
 @require_http_methods(["GET"])
 def getUserIds(request):

+ 122 - 1
Controller/StatisticsController.py

@@ -7,7 +7,7 @@ from django.db.models import Count
 from django.views.decorators.csrf import csrf_exempt
 
 from Model.models import UserAppFrequencyModel, AppFrequencyStatisticsModel, Device_User, \
-    AppFrequencyYearStatisticsModel
+    AppFrequencyYearStatisticsModel, Equipment_Info, EquipmentInfoExStatisticsModel
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
 
@@ -40,13 +40,17 @@ def statistcsAppFrequency(request):
 
     time_struct = [current_year, 1, 1, 0, 0, 0, 0, 0, 0]
 
+    distance_time = 8 * 60 * 60
+
     # 该月的开始时间
     time_struct[1] = current_month
     start_time = time.mktime(tuple(time_struct))
+    start_time = start_time - time.timezone - distance_time
 
     # 该月的结束时间
     time_struct[1] = current_month + 1
     end_time = time.mktime(tuple(time_struct))
+    end_time = end_time - time.timezone - distance_time
 
     # 统计该月的数据
     uaf_qs = UserAppFrequencyModel.objects.filter(data_time__gte=start_time, data_time__lt=end_time) \
@@ -135,3 +139,120 @@ def statistcsAppFrequencyYear(request):
         }
         AppFrequencyYearStatisticsModel.objects.create(**data)
     return response.json(0)
+
+
+@csrf_exempt
+def statistcsPushDay(request):
+    request.encoding = 'utf-8'
+    response = ResponseObject()
+    if request.method == 'POST':
+        request_dict = request.POST
+    elif request.method == 'GET':
+        request_dict = request.GET
+    else:
+        return response.json(444)
+
+    token = TokenObject(request_dict.get('token', None))
+    if token.code != 0:
+        return response.json(token.code)
+
+    current_time = int(time.time())
+    count = int(current_time / (24 * 3600))
+    end_time = count * 24 * 3600 - 8 * 3600
+    start_time = end_time - 24 * 3600
+    print(start_time)
+    print(end_time)
+
+    eq_qs = Equipment_Info.objects.filter(addTime__gte=start_time, addTime__lte=end_time)
+    print(eq_qs.values())
+    # 0:APNS推送,1:谷歌推送,2:极光推送
+    now_time = int(time.time())
+    jpush = EquipmentInfoExStatisticsModel(push_type=2, statistics_date=start_time, add_time=now_time, date_type=0)
+    apns = EquipmentInfoExStatisticsModel(push_type=0, statistics_date=start_time, add_time=now_time, date_type=0)
+    gcm = EquipmentInfoExStatisticsModel(push_type=1, statistics_date=start_time, add_time=now_time, date_type=0)
+    total = EquipmentInfoExStatisticsModel(push_type=-1, statistics_date=start_time, add_time=now_time, date_type=0)
+    for eq in eq_qs:
+        if eq.push_type == 0:
+            do_sum(eq, apns)
+        elif eq.push_type == 1:
+            do_sum(eq, gcm)
+        elif eq.push_type == 2:
+            do_sum(eq, jpush)
+        do_sum(eq, total)
+    datas = []
+    datas.append(apns)
+    datas.append(gcm)
+    datas.append(jpush)
+    datas.append(total)
+
+    EquipmentInfoExStatisticsModel.objects.bulk_create(datas)
+    return response.json(0)
+
+
+@csrf_exempt
+def statistcsPushMonth(request):
+    request.encoding = 'utf-8'
+    response = ResponseObject()
+    if request.method == 'POST':
+        request_dict = request.POST
+    elif request.method == 'GET':
+        request_dict = request.GET
+    else:
+        return response.json(444)
+
+    token = TokenObject(request_dict.get('token', None))
+    if token.code != 0:
+        return response.json(token.code)
+
+    localtime = time.localtime()
+    current_month = localtime.tm_mon
+
+    distance_time = 8 * 60 * 60
+
+    time_struct = [localtime.tm_year, localtime.tm_mon, 1, 0, 0, 0, 0, 0, 0]
+    start_time = int(time.mktime(tuple(time_struct)))
+    start_time = start_time - time.timezone - distance_time
+
+    time_struct[1] = current_month + 1
+    end_time = int(time.mktime(tuple(time_struct)))
+    end_time = end_time - time.timezone - distance_time
+
+    eq_qs = EquipmentInfoExStatisticsModel.objects.filter(statistics_date__gte=start_time, statistics_date__lte=end_time, date_type=0)
+    # 0:APNS推送,1:谷歌推送,2:极光推送
+    now_time = int(time.time())
+    jpush = EquipmentInfoExStatisticsModel(push_type=2, statistics_date=start_time, add_time=now_time, date_type=1)
+    apns = EquipmentInfoExStatisticsModel(push_type=0, statistics_date=start_time, add_time=now_time, date_type=1)
+    gcm = EquipmentInfoExStatisticsModel(push_type=1, statistics_date=start_time, add_time=now_time, date_type=1)
+    total = EquipmentInfoExStatisticsModel(push_type=-1, statistics_date=start_time, add_time=now_time, date_type=1)
+    for eq in eq_qs:
+        if eq.push_type == 0:
+            do_sum_two(eq, apns)
+        elif eq.push_type == 1:
+            do_sum_two(eq, gcm)
+        elif eq.push_type == 2:
+            do_sum_two(eq, jpush)
+        do_sum_two(eq, total)
+    datas = []
+    datas.append(apns)
+    datas.append(gcm)
+    datas.append(jpush)
+    datas.append(total)
+
+    EquipmentInfoExStatisticsModel.objects.bulk_create(datas)
+    return response.json(0)
+
+
+def do_sum(eq: Equipment_Info, target: EquipmentInfoExStatisticsModel):
+    if eq.push_server_status == 200:
+        target.number_of_successes += 1
+    else:
+        target.number_of_failures += 1
+
+    if eq.push_device_status == 1:
+        target.number_of_arrival += 1
+
+
+def do_sum_two(eq: EquipmentInfoExStatisticsModel, target: EquipmentInfoExStatisticsModel):
+    target.number_of_successes += eq.number_of_successes
+    target.number_of_failures += eq.number_of_failures
+    target.number_of_arrival += eq.number_of_arrival

+ 3 - 2
Controller/UserController.py

@@ -3412,12 +3412,13 @@ class AppleAuthLogin(View):
 
         response = ResponseObject(lang)
         identity_token = CommonService.decode_data(identity_token)
+        # print(identity_token)
         if identity_token:
             key_url = 'https://appleid.apple.com/auth/keys'
             key_response = requests.get(key_url).json()
-            print(key_response)
+            # print(key_response)
             head = jwt.get_unverified_header(identity_token)
-            print(head)
+            # print(head)
             token_key = head['kid']
             key_object = None
             alg = None

+ 1 - 1
Model/models.py

@@ -303,7 +303,7 @@ class Access_Log(models.Model):
 
 class Equipment_Info(models.Model):
     id = models.AutoField(primary_key=True, verbose_name=u'自增标记ID')
-    devUid = models.CharField(default='', db_index=True, blank=True, max_length=32, verbose_name=u'设备ID')
+    devUid = models.CharField(default='', blank=True, max_length=32, verbose_name=u'设备ID')
     devNickName = models.CharField(blank=True, max_length=32, default='', verbose_name=u'设备昵称')
     Channel = models.IntegerField(default=1, blank=True, verbose_name=u'设备通道')
     eventType = models.IntegerField(default=0, blank=True, verbose_name=u'事件类型')