# -*- coding: utf-8 -*- import time from datetime import timedelta, timezone as asj_timezone import boto3 import xlwt from django.db.models import Count, Q from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt 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, 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, 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 ''' http://192.168.136.40:8077/adminManage/manage?operation=getAllDeviceArea&token=debug http://192.168.136.40:8077/adminManage/manage?operation=getAllUserName&token=debug http://192.168.136.40:8077/adminManage/manage?operation=getAllUID&token=debug http://127.0.0.1:8000/adminManage/manage?operation=getAllOnLine&token=stest http://127.0.0.1:8000/adminManage/manage?operation=getOnLine&token=stest ''' class AdminManage(TemplateView): @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): return super(AdminManage, self).dispatch(*args, **kwargs) def get(self, request, *args, **kwargs): request.encoding = 'utf-8' return self.validation(request_dict=request.GET) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' return self.validation(request_dict=request.POST) def validation(self, request_dict, *args, **kwargs): response = ResponseObject() token = request_dict.get('token', None) tko = TokenObject(token) response.lang = tko.lang if tko.code != 0: return response.json(tko.code) userID = tko.userID operation = request_dict.get('operation', None) if userID is None or operation is None: return response.json(444, 'operation') if operation == 'resetUserPwd': return self.resetUserPwd(request_dict, userID, response) if operation == 'getAllOnLine': return self.getAllOnLine(userID, response) if operation == 'getOnLine': return self.getRedisOnline(userID, response) # return self.getOnLine(userID, response) if operation == 'getAllUserName': return self.getAllUserName(userID, response) if operation == 'getStatisAccess': return self.getStatisAccess(userID, request_dict, response) if operation == 'getAllUID': return self.getAllUID(userID, response) if operation == 'getAllDeviceArea': return self.getAllDeviceArea(userID, response) if operation == 'getUserBrand': return self.getUserBrand(userID, response) if operation == 'getAreaDeviceType': return self.getAreaDeviceType(userID, response) if operation == 'getDeviceType': return self.getDeviceType(userID, response) if operation == 'getAppFrequency': return self.getAppFrequency(userID, request_dict, response) if operation == 'getHistoryAppFrequency': 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) if operation == 'downloadSubscribeEmail': return self.download_subscribe_email(userID, request_dict, response) def resetUserPwd(self, request_dict, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=50) if not own_permission: return response.json(404) duserID = request_dict.get('duserID', None) userPwd = request_dict.get('userPwd', '123456') password_version = request_dict.get('pwdVersion', 'V1') if not duserID: return response.json(444, 'duserID') UserValid = Device_User.objects.filter(userID=duserID) if UserValid: if password_version == 'V1': userPwd = make_password(userPwd) is_update = UserValid.update(password=userPwd) if is_update: return response.json(0) else: return response.json(177) else: return response.json(104) def getAllUserName(self, userID, response): # 权限固定为30 own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) username_list = Device_User.objects.all().values_list('username', flat=True) if username_list: return response.json(0, {'username_list': list(username_list)}) else: return response.json(0) # 获取全部用户的在线个数 def getAllOnLine(self, userID, response): # 权限固定为30 own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) allonline = Device_User.objects.all().values('online') # 两个变量,分别是在线,离线 onlinenum = 0 noonlinenum = 0 for q in allonline: if q['online'] == True: onlinenum += 1 else: noonlinenum += 1 print("在线人数:") print(onlinenum) return response.json(0, {"onlinenum": onlinenum, "no_onlinenum": noonlinenum}) # 获取全部用户的在线人数 def getOnLine(self, userID, response): # 权限固定为30 own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is True: online_list = Device_User.objects.all().values('online', 'last_login') # 两个变量,分别是在线,离线 onlinenum = 0 noonlinenum = 0 for q in online_list: # print(q['last_login'] ) # 最后访问时间加5分种 dl_time = q['last_login'] + datetime.timedelta(minutes=OFF_LINE_TIME_DELTA) # print(dl_time) # 当前时间 now_time = timezone.localtime(timezone.now()) # print(now_time) # 如果当前时间大于最后访问的时间 if now_time < dl_time: onlinenum += 1 else: noonlinenum += 1 print("在线人") print(onlinenum) return response.json(0, {"onlinenum": onlinenum, "no_onlinenum": noonlinenum}) else: return response.json(404) # 获取全部用户的在线人数 def getRedisOnline(self, userID, response): # 权限固定为30 own_perm = ModelService.check_perm(userID, 30) if own_perm: count = int(Device_User.objects.count()) redisObj = RedisObject() onlines = int(redisObj.get_size()) print(onlines) return response.json(0, {"onlinenum": onlines, "no_onlinenum": count - onlines}) else: return response.json(404) # 获取所有设备地区 def getAllDeviceArea(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is True: qs = Device_Info.objects.all().values('area', 'UID') uid_area_dict = {} for q in qs: if q['UID'] and q['area']: uid_area_dict[q['UID']] = q['area'] if len(uid_area_dict): area_dict = {} for k, v in uid_area_dict.items(): if v in area_dict: area_dict[v] += 1 else: area_dict[v] = 1 return response.json(0, {'area': area_dict}) else: return response.json(0) else: return response.json(404) ''' 统计一天访问量 http://192.168.136.45:8077/adminManage/manage?token=test&operation=getStatisAccess×tamp=1528773308 ''' def getStatisAccess(self, userID, request_dict, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) time_stamp = int(request_dict.get('timestamp', None)) times = datetime.datetime.fromtimestamp(time_stamp) time_dict = CommonService.getTimeDict(times) res = {} for k, v in time_dict.items(): start_date = time_dict[k] end_date = time_dict[k] + datetime.timedelta(hours=1) count = Access_Log.objects.filter(time__range=(start_date, end_date)).count() if count: res[k] = count else: res[k] = 0 return response.json(0, {'count': res}) def getAllUID(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) uid_list = Device_Info.objects.all().values_list('UID', flat=True) res = {} if uid_list: res = {'count': uid_list.count(), 'uid_list': list(uid_list)} return response.json(0, res) def getUserBrand(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) # 手机型号统计 print('手机型号统计:') ub_qs = User_Brand.objects.values('deviceSupplier', 'deviceModel').annotate(value=Count('id')).order_by() res = { 'value': 0, 'data': [] } data = res['data'] tmpDict = {} for ub in ub_qs: deviceSupplier = ub['deviceSupplier'] if not tmpDict.__contains__(deviceSupplier): tmpDict[deviceSupplier] = { 'name': deviceSupplier, 'value': 0, 'children': [] } item = tmpDict[deviceSupplier] item['value'] = item['value'] + ub['value'] res['value'] = res['value'] + item['value'] model = { 'name': ub['deviceModel'], 'value': ub['value'] } item['children'].append(model) for k, v in tmpDict.items(): data.append(v) print(res) return response.json(0, res) def getAreaDeviceType(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) print('区域设备类型统计:') di_qs = Device_Info.objects.values('area', 'Type').annotate(value=Count('UID', distinct=True)).order_by() res = { 'quantity': 0, 'data': [] } data = res['data'] tmpDict = {} tmpDict['null'] = { 'area': '未知', 'quantity': 0, 'models': [] } for di in di_qs: area = di['area'] if area is None or area == '': area = 'null' if not tmpDict.__contains__(area): tmpDict[area] = { 'area': area, 'quantity': 0, 'models': [] } item = tmpDict[area] item['quantity'] = item['quantity'] + di['value'] res['quantity'] = res['quantity'] + item['quantity'] device_model = None if DEVICE_TYPE.__contains__(di['Type']): device_model = DEVICE_TYPE[di['Type']] else: device_model = DEVICE_TYPE[0] model = { 'model': device_model, 'quantity': di['value'] } item['models'].append(model) for k, v in tmpDict.items(): data.append(v) return response.json(0, res) def getDeviceType(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) # 设备类型统计 di_qs = Device_Info.objects.values('Type').annotate(quantity=Count('UID', distinct=True)).order_by() # 设备型号统计 us_qs = UidSetModel.objects.values('deviceModel').annotate(quantity=Count('id')).order_by() res = { 'type_data': { 'quantity': 0, 'data': [] }, 'model_data': { 'quantity': 0, 'data': [] } } type_data = res['type_data'] data = type_data['data'] quantity = 0 for di in di_qs: if DEVICE_TYPE.__contains__(di['Type']): device_model = DEVICE_TYPE[di['Type']] else: device_model = DEVICE_TYPE[0] di['Type'] = device_model quantity += di['quantity'] data.append(di) type_data['quantity'] = quantity model_data = res['model_data'] data = model_data['data'] quantity = 0 for us in us_qs: data.append(us) quantity += us['quantity'] model_data['quantity'] = quantity return response.json(0, res) def getAppFrequency(self, userID, request_dict, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) # 当前的年份 current_time = int(time.time()) localtime = time.localtime(current_time) current_year = localtime.tm_year current_month = localtime.tm_mon start_year = end_year = current_year end_month = current_month start_month = 1 result = [] if end_month != 12: start_month = end_month + 1 start_year = current_year - 1 time_struct = [start_year, start_month, 0, 0, 0, 0, 0, 0, 0] key_formal = '{year}{month}' for i in range(12): result.append({ 'date_time': key_formal.format(year=time_struct[0], month=str(time_struct[1]).zfill(2)), 'data': None }) month = time_struct[1] + 1 if month > 12: time_struct[0] = time_struct[0] + 1 time_struct[1] = 1 else: time_struct[1] = month # afs_qs = {} if start_year == end_year: afs_qs = list(AppFrequencyStatisticsModel.objects.filter(year=start_year, month__gte=start_month, month__lte=end_month).values().order_by('-year', 'month')) else: afs_qs = list(AppFrequencyStatisticsModel.objects.filter(year=start_year, month__gte=start_month).values().order_by('-year', 'month')) tmps_qs = list(AppFrequencyStatisticsModel.objects.filter(year=end_year, month__lte=end_month).values().order_by('-year', 'month')) for tmp in tmps_qs: afs_qs.append(tmp) tmp_dict = {} for afs in afs_qs: key = key_formal.format(year=afs['year'], month=str(afs['month']).zfill(2)) tmp_dict[key] = json.loads(afs['data']) for res in result: if tmp_dict.__contains__(res['date_time']): res['data'] = tmp_dict[res['date_time']] print(result) return response.json(0, result) def getAllAppFrequency(self, userID, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) # 取出请求年份的统计好的数据 print('start') time_struct = time.localtime() current_year = time_struct.tm_year start_year = current_year - 5 + 1 afs_qs = AppFrequencyYearStatisticsModel.objects.filter(year__gte=start_year, year__lt=current_year).order_by( '-year') if afs_qs.exists(): afs_qs = afs_qs.values('id', 'data', 'num', 'year') res = [] for afs in afs_qs: res.append({ 'year': afs['year'], 'data': json.loads(afs['data']) }) return response.json(0, res) 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 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 download_subscribe_email(self, userID, request_dict, response): own_permission = ModelService.check_perm(userID=userID, permID=30) if own_permission is not True: return response.json(404) user_qs = Device_User.objects.filter(subscribe_email=1).values('userEmail') print(user_qs) response = HttpResponse(content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=Email' + time.strftime('-%Y-%m-%d-%H-%M-%S', time.localtime()) + '.xls' workbook = xlwt.Workbook(encoding='utf-8') sheet1 = workbook.add_sheet('Email Address') num = 1 sheet1.write(0, 0, 'Email Address') for user in user_qs: email = user['userEmail'] if email == '': continue sheet1.write(num, 0, email) num += 1 workbook.save(response) return response def getCompareKey(item): return item['Timestamp'] @require_http_methods(["GET"]) def getUserIds(request): token = request.GET.get('token', None) response = ResponseObject() tko = TokenObject(token) response.lang = tko.lang if tko.code != 0: return response.json(tko.code) userID = tko.userID own_perm = ModelService.check_perm(userID, 30) if own_perm is not True: return response.json(404) dn = Device_User.objects.all().values('userID', 'username') return response.json(0, {"datas": list(dn)}) @csrf_exempt def search_user_by_content(request): if request.method == 'GET': request_dict = request.GET if request.method == 'POST': request_dict = request.POST token = request_dict.get('token', None) page = request_dict.get('page', None) line = request_dict.get('line', None) content = request_dict.get('content', None) rstime = request_dict.get('rstime', None) retime = request_dict.get('retime', None) response = ResponseObject() if page is not None and line is not None: page = int(page) line = int(line) else: return response.json(10, 'page,line is none') tko = TokenObject(token) response.lang = tko.lang if tko.code != 0: return response.json(tko.code) userID = tko.userID own_perm = ModelService.check_perm(userID=userID, permID=20) if own_perm is not True: return response.json(404) try: content = json.loads(content) search_kwargs = CommonService.get_kwargs(data=content) queryset = Device_User.objects.filter(**search_kwargs) except Exception as e: return response.json(444, repr(e)) if rstime is not None and rstime != '' and retime is not None and retime != '': startt = datetime.datetime.fromtimestamp(int(rstime)) rstime = startt.strftime("%Y-%m-%d %H:%M:%S.%f") endt = datetime.datetime.fromtimestamp(int(retime)) retime = endt.strftime("%Y-%m-%d %H:%M:%S.%f") queryset = queryset.filter(data_joined__range=(rstime, retime)) elif rstime is not None and rstime != '': startt = datetime.datetime.fromtimestamp(int(rstime)) rstime = startt.strftime("%Y-%m-%d %H:%M:%S.%f") queryset = queryset.filter(data_joined__gte=rstime) elif retime is not None and retime != '': endt = datetime.datetime.fromtimestamp(int(retime)) retime = endt.strftime("%Y-%m-%d %H:%M:%S.%f") queryset = queryset.filter(data_joined__lte=retime) if queryset.exists(): count = queryset.count() res = queryset[(page - 1) * line:page * line] sqlDict = CommonService.qs_to_dict(res) for k, v in enumerate(sqlDict["datas"]): if len(v['fields']['role']) > 0: role_query_set = Role.objects.get(rid=v['fields']['role'][0]) sqlDict["datas"][k]['fields']['role'].append(role_query_set.roleName) for val in res: if v['pk'] == val.userID: if sqlDict["datas"][k]['fields']['online'] is True: dl_time = val.last_login + datetime.timedelta(minutes=5) now_time = timezone.localtime(timezone.now()) if now_time > dl_time: sqlDict["datas"][k]['fields']['online'] = False ue = UserExModel.objects.filter(userID=v['pk']) if ue.exists(): sqlDict["datas"][k]['fields']['appBundleId'] = ue[0].appBundleId else: sqlDict["datas"][k]['fields']['appBundleId'] = '' sqlDict['count'] = count return response.json(0, sqlDict) return response.json(0, {'datas': [], 'count': 0})