Quellcode durchsuchen

添加统计接口

tanghongbin vor 5 Jahren
Ursprung
Commit
fbc24b7172

+ 31 - 0
Ansjer/config.py

@@ -126,3 +126,34 @@ FCM_CONFIG = {
     'com.ansjer.accloud_a': 'AAAAb9YP3rk:APA91bFm06w8b9OKQ0gz0iaWFuRqRIkvgAz6z7Gp3dBU_X-LNGJQd1hc1QR2W7QzBglF8SHtERA45a2lbdLRa5qv7hxfd6W_sJLBK7dA8jklsOQBvy505oUzTwMKWy4TwH-exps9KrhO',
     'com.ansjer.zccloud_ab': 'AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I',
 }
+
+DEVICE_TYPE = {
+    0: 'UNKOWN',
+    1: 'DVR',
+    2: 'NVR',
+    3: 'POENVR',
+    4: 'WIRELESSNVR',
+    5: 'POEIPC',
+    6: 'BOLTIPC',
+    7: 'IPC',
+    8: 'KAPIANJI',
+    9: 'FISHEYE',
+    10: 'C512',
+    11: 'C611',
+    12: 'C612',
+    13: 'C199',
+    14: 'C190',
+    15: 'C199_PRO',
+    10001: 'DVRPTZ'
+}
+
+APP_FREQUENT = {
+    0: 'UNKOWN',
+    1: '每天',
+    2: '三天',
+    3: '一周',
+    4: '两周',
+    5: '一个月',
+    6: '一个月以上',
+
+}

+ 1 - 0
Ansjer/urls.py

@@ -203,6 +203,7 @@ urlpatterns = [
     url(r'^date/(?P<operation>.*)$', DateController.DateConView.as_view()),
 
     url(r'^equipment/flow_test$', EquipmentManager.uid_status_test),
+    url(r'^account/appFrequency/(?P<operation>.*)$', UserController.UserAppFrequencyView.as_view()),
     # app 设备消息模板
     # 路由加参数参考
     # url(r'^(?P<path>.*)/(?P<UID>.*)/lls$', Test.Test.as_view(), name=u'gg'),

+ 127 - 2
Controller/AdminManage.py

@@ -1,16 +1,17 @@
 # -*- coding: utf-8 -*-
+from django.db.models import Count
 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
+from Model.models import Device_Info, Role, UserExModel, User_Brand, UidSetModel
 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
+from Ansjer.config import OFF_LINE_TIME_DELTA, DEVICE_TYPE
 import datetime, simplejson as json
 from Service.CommonService import CommonService
 from Object.RedisObject import RedisObject
@@ -64,6 +65,12 @@ class AdminManage(TemplateView):
             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)
 
     def resetUserPwd(self, request_dict, userID, response):
         own_permission = ModelService.check_perm(userID=userID, permID=50)
@@ -213,6 +220,124 @@ class AdminManage(TemplateView):
             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']
+            model = {
+                'model': DEVICE_TYPE[di['Type']],
+                '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:
+            di['Type'] = DEVICE_TYPE[di['Type']]
+            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)
+
 
 @require_http_methods(["GET"])
 def getUserIds(request):

+ 39 - 39
Controller/UidSetController.py

@@ -410,42 +410,42 @@ class UidSetView(View):
         else:
             return response.json(0)
 
-    def do_test(self, response):
-        # di_qs = Device_Info.objects.values('Type').annotate(c=Count('UID', distinct=True)).order_by()
-        # for di in di_qs:
-        #     print(di)
-        #
-        # # 设备总数量
-        # count = Device_Info.objects.values('UID').distinct().count()
-        # print('设备总数量:' + str(count))
-        #
-        # # 设备型号统计
-        # print('设备型号统计:')
-        # us_qs = UidSetModel.objects.values('deviceModel').annotate(c=Count('id')).order_by()
-        # for us in us_qs:
-        #     print(us)
-        #
-        # # 手机品牌类型统计
-        # print('手机品牌类型统计:')
-        # ub_qs = User_Brand.objects.values('deviceSupplier').annotate(c=Count('id')).order_by()
-        # for ub in ub_qs:
-        #     print(ub)
-        #
-        # # 手机型号统计
-        # print('手机型号统计:')
-        # ub_qs = User_Brand.objects.values('deviceSupplier', 'deviceModel').annotate(c=Count('id')).order_by()
-        # for ub in ub_qs:
-        #     print(ub)
-        #
-        # # 区域统计
-        # print('区域统计:')
-        # di_qs = Device_Info.objects.values('area').annotate(c=Count('UID', distinct=True)).order_by()
-        # for di in di_qs:
-        #     print(di)
-        #
-        # # 区域设备类型统计
-        # print('区域设备类型统计:')
-        # di_qs = Device_Info.objects.values('area', 'Type').annotate(c=Count('UID', distinct=True)).order_by()
-        # for di in di_qs:
-        #     print(di)
-        return response.json(0)
+    # def do_test(self, response):
+    #     di_qs = Device_Info.objects.values('Type').annotate(c=Count('UID', distinct=True)).order_by()
+    #     for di in di_qs:
+    #         print(di)
+    #
+    #     # 设备总数量
+    #     count = Device_Info.objects.values('UID').distinct().count()
+    #     print('设备总数量:' + str(count))
+    #
+    #     # 设备型号统计
+    #     print('设备型号统计:')
+    #     us_qs = UidSetModel.objects.values('deviceModel').annotate(c=Count('id')).order_by()
+    #     for us in us_qs:
+    #         print(us)
+    #
+    #     # 手机品牌类型统计
+    #     print('手机品牌类型统计:')
+    #     ub_qs = User_Brand.objects.values('deviceSupplier').annotate(c=Count('id')).order_by()
+    #     for ub in ub_qs:
+    #         print(ub)
+    #
+    #     # 手机型号统计
+    #     print('手机型号统计:')
+    #     ub_qs = User_Brand.objects.values('deviceSupplier', 'deviceModel').annotate(c=Count('id')).order_by()
+    #     for ub in ub_qs:
+    #         print(ub)
+    #
+    #     # 区域统计
+    #     print('区域统计:')
+    #     di_qs = Device_Info.objects.values('area').annotate(c=Count('UID', distinct=True)).order_by()
+    #     for di in di_qs:
+    #         print(di)
+    #
+    #     # 区域设备类型统计
+    #     print('区域设备类型统计:')
+    #     di_qs = Device_Info.objects.values('area', 'Type').annotate(c=Count('UID', distinct=True)).order_by()
+    #     for di in di_qs:
+    #         print(di)
+    #     return response.json(0)

+ 74 - 1
Controller/UserController.py

@@ -27,7 +27,8 @@ from ratelimit.decorators import ratelimit
 
 from Ansjer.config import AuthCode_Expire, SERVER_DOMAIN, APNS_CONFIG, JPUSH_CONFIG, FCM_CONFIG, TUTK_PUSH_DOMAIN
 from Controller.CheckUserData import DataValid, date_handler, RandomStr
-from Model.models import Device_User, Role, UidPushModel, UserOauth2Model, UserExModel, Device_Info,UidSetModel
+from Model.models import Device_User, Role, UidPushModel, UserOauth2Model, UserExModel, Device_Info, UidSetModel, \
+    UserAppFrequencyModel
 from Object.AWS.SesClassObject import SesClassObject
 from Object.AliSmsObject import AliSmsObject
 from Object.RedisObject import RedisObject
@@ -2930,8 +2931,80 @@ class Image_Code_RegisterView(TemplateView):
         return response.json(0, res)
 
 
+class UserAppFrequencyView(TemplateView):
+    @method_decorator(csrf_exempt)
+    def dispatch(self, *args, **kwargs):
+        return super(UserAppFrequencyView, self).dispatch(*args, **kwargs)
+
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        request_dict = request.POST
+        operation = kwargs.get('operation')
+        return self.validates(request_dict, operation)
+
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        request_dict = request.GET
+        operation = kwargs.get('operation')
+        return self.validates(request_dict, operation)
+
+    def validates(self, request_dict, operation):
+        token = request_dict.get('token', None)
+        response = ResponseObject()
+
+        token = TokenObject(token)
+        if token.code != 0:
+            return response.json(token.code)
 
+        if operation == 'refresh':
+            return self.do_refresh(request_dict, token.userID, response)
+        else:
+            return response.json(404)
 
+    def do_refresh(self, request_dict, userID, response):
+        type = request_dict.get('type', None)
+        month = request_dict.get('month', None)
+        if not type or not month:
+            return response.json(444, 'type')
+        else:
+            type = int(type)
+            now_time = int(time.time())
+            month = int(month)
+            uaf_qs = UserAppFrequencyModel.objects.filter(user__userID=userID)
+
+            if not uaf_qs.exists():
+                user = Device_User.objects.filter(userID=userID)[0]
+                data = {
+                    'user': user,
+                    'type': type,
+                    'data_time': month,
+                    'add_time': now_time,
+                    'update_time': now_time,
+                }
+                UserAppFrequencyModel.objects.create(**data)
+                return response.json(0)
+            else:
+                updateMonth = time.strftime('%m', time.localtime(month))
+                uaf = uaf_qs.values('id', 'type', 'data_time')[0]
+                dbMonth = time.strftime('%m', time.localtime(int(uaf['data_time'])))
+                print('update month is ' + updateMonth)
+                print('db month is ' + dbMonth)
+                if updateMonth == dbMonth:
+                    UserAppFrequencyModel.objects.filter(id=uaf['id']).update(type=type)
+                    return response.json(0)
+                elif updateMonth > dbMonth:
+                    user = Device_User.objects.filter(userID=userID)[0]
+                    data = {
+                        'user': user,
+                        'type': type,
+                        'data_time': month,
+                        'add_time': now_time,
+                        'update_time': now_time,
+                    }
+                    UserAppFrequencyModel.objects.create(**data)
+                    return response.json(0)
+                else:
+                    return response.json(444, 'month')
 
 
 

+ 15 - 0
Model/models.py

@@ -908,3 +908,18 @@ class GrantCodeModel(models.Model):
         ordering = ('-add_time',)
         verbose_name = u'授权码表'
         db_table = 'oauth_grant_code'
+
+
+class UserAppFrequencyModel(models.Model):
+    id = models.AutoField(primary_key=True)
+    user = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE, verbose_name='关联设备用户表')
+    type = models.SmallIntegerField(default=0, verbose_name='使用频率类型') # 1:每天,2:三天,3:一周,4:两周,5:一个月,6:一个月以上
+    data_time = models.IntegerField(default=0, verbose_name='数据时间')
+    add_time = models.IntegerField(default=0, verbose_name='添加时间')
+    update_time = models.IntegerField(default=0, verbose_name='更新时间')
+
+    class Meta:
+        verbose_name = '用户使用APP频率表'
+        verbose_name_plural = verbose_name
+        db_table = 'user_app_frequency'
+        ordering = ('-add_time',)

+ 1 - 0
Service/TemplateService.py

@@ -78,6 +78,7 @@ class TemplateService:
             'v3/account/register',
             'v3/account/changePwd',
             'v3/account/resetPwdByCode',
+            'account/appFrequency',
         ]
         return apiList