|
@@ -1,20 +1,23 @@
|
|
# -*- coding: utf-8 -*-
|
|
# -*- coding: utf-8 -*-
|
|
import time
|
|
import time
|
|
|
|
+from datetime import date, timedelta, timezone as asj_timezone
|
|
|
|
|
|
|
|
+import boto3
|
|
from django.db.models import Count,Q
|
|
from django.db.models import Count,Q
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.views.generic import TemplateView
|
|
from django.views.generic import TemplateView
|
|
from django.utils.decorators import method_decorator
|
|
from django.utils.decorators import method_decorator
|
|
from django.contrib.auth.hashers import make_password # 对密码加密模块
|
|
from django.contrib.auth.hashers import make_password # 对密码加密模块
|
|
from Model.models import Device_Info, Role, UserExModel, User_Brand, UidSetModel, AppFrequencyYearStatisticsModel, \
|
|
from Model.models import Device_Info, Role, UserExModel, User_Brand, UidSetModel, AppFrequencyYearStatisticsModel, \
|
|
- AppFrequencyStatisticsModel
|
|
|
|
|
|
+ AppFrequencyStatisticsModel, EquipmentInfoExStatisticsModel, Equipment_Info
|
|
from Service.ModelService import ModelService
|
|
from Service.ModelService import ModelService
|
|
from django.utils import timezone
|
|
from django.utils import timezone
|
|
from Model.models import Access_Log, Device_User
|
|
from Model.models import Access_Log, Device_User
|
|
from django.views.decorators.http import require_http_methods
|
|
from django.views.decorators.http import require_http_methods
|
|
from Object.ResponseObject import ResponseObject
|
|
from Object.ResponseObject import ResponseObject
|
|
from Object.TokenObject import TokenObject
|
|
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
|
|
import datetime, simplejson as json
|
|
from Service.CommonService import CommonService
|
|
from Service.CommonService import CommonService
|
|
from Object.RedisObject import RedisObject
|
|
from Object.RedisObject import RedisObject
|
|
@@ -78,6 +81,12 @@ class AdminManage(TemplateView):
|
|
return self.getAppFrequency(userID, request_dict, response)
|
|
return self.getAppFrequency(userID, request_dict, response)
|
|
if operation == 'getHistoryAppFrequency':
|
|
if operation == 'getHistoryAppFrequency':
|
|
return self.getAllAppFrequency(userID, response)
|
|
return self.getAllAppFrequency(userID, 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):
|
|
def resetUserPwd(self, request_dict, userID, response):
|
|
own_permission = ModelService.check_perm(userID=userID, permID=50)
|
|
own_permission = ModelService.check_perm(userID=userID, permID=50)
|
|
@@ -434,6 +443,199 @@ class AdminManage(TemplateView):
|
|
else:
|
|
else:
|
|
return response.json(0, [])
|
|
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
|
|
|
|
+
|
|
|
|
+ gmtime = time.gmtime(int(time.time()) + 28800)
|
|
|
|
+ current_day = gmtime.tm_mday
|
|
|
|
+ time_struct = [gmtime.tm_year, gmtime.tm_mon, current_day, 0, 0, 0, 0, 0, 0]
|
|
|
|
+ current_time = int(time.mktime(tuple(time_struct))) - 28800
|
|
|
|
+
|
|
|
|
+ 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 getCompareKey(item):
|
|
|
|
+ return item['Timestamp']
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
@require_http_methods(["GET"])
|
|
def getUserIds(request):
|
|
def getUserIds(request):
|