浏览代码

开启统计功能

tanghongbin 5 年之前
父节点
当前提交
1a962fffba
共有 1 个文件被更改,包括 82 次插入81 次删除
  1. 82 81
      Controller/AdminManage.py

+ 82 - 81
Controller/AdminManage.py

@@ -6,7 +6,8 @@ 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
+from Model.models import Device_Info, Role, UserExModel, User_Brand, UidSetModel, AppFrequencyYearStatisticsModel, \
+    AppFrequencyStatisticsModel
 from Service.ModelService import ModelService
 from django.utils import timezone
 from Model.models import Access_Log, Device_User
@@ -343,86 +344,86 @@ class AdminManage(TemplateView):
         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 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, [])
 
 
 @require_http_methods(["GET"])