From e1868b7a56a5ec4f176e6a2547b64f256b2b8bba Mon Sep 17 00:00:00 2001 From: layman_one <359573099@qq.com> Date: Fri, 14 Apr 2023 18:36:51 +0800 Subject: [PATCH] =?UTF-8?q?4-14=E9=A6=96=E9=A1=B5=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/dvadmin/system/urls.py | 2 + backend/dvadmin/system/views/datav.py | 95 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 backend/dvadmin/system/views/datav.py diff --git a/backend/dvadmin/system/urls.py b/backend/dvadmin/system/urls.py index fab79f9..73f1d92 100644 --- a/backend/dvadmin/system/urls.py +++ b/backend/dvadmin/system/urls.py @@ -4,6 +4,7 @@ from rest_framework import routers from dvadmin.system.views.api_white_list import ApiWhiteListViewSet from dvadmin.system.views.area import AreaViewSet from dvadmin.system.views.clause import PrivacyView, TermsServiceView +from dvadmin.system.views.datav import DataVViewSet from dvadmin.system.views.dept import DeptViewSet from dvadmin.system.views.dictionary import DictionaryViewSet from dvadmin.system.views.file_list import FileViewSet @@ -40,5 +41,6 @@ urlpatterns = [ path('dept_lazy_tree/', DeptViewSet.as_view({'get': 'dept_lazy_tree'})), path('clause/privacy.html', PrivacyView.as_view()), path('clause/terms_service.html', TermsServiceView.as_view()), + path('homepage_statistics/', DataVViewSet.as_view({'get': 'homepage_statistics'})), ] urlpatterns += system_url.urls diff --git a/backend/dvadmin/system/views/datav.py b/backend/dvadmin/system/views/datav.py new file mode 100644 index 0000000..0da4a4e --- /dev/null +++ b/backend/dvadmin/system/views/datav.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Time : 2023/4/14 15:49 +# @Author : harry +import datetime +import json +import time + +from django.db.models import Count +from rest_framework.decorators import action +from rest_framework.permissions import IsAuthenticated +from rest_framework.viewsets import GenericViewSet + +from dvadmin.system.models import Users, LoginLog, FileList +from dvadmin.system.views.login_log import LoginLogSerializer +from dvadmin.utils.json_response import DetailResponse + + +def jx_timestamp(): + cur_time = datetime.datetime.now() + a = datetime.datetime.strftime(cur_time, '%Y-%m-%d %H:%M:%S') + timeStamp = int(time.mktime(time.strptime(a, "%Y-%m-%d %H:%M:%S"))) + timeArray = time.localtime(timeStamp) + otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) + return otherStyleTime + + +class DataVViewSet(GenericViewSet): + queryset = LoginLog.objects.all() + serializer_class = LoginLogSerializer + extra_filter_backends = [] + ordering_fields = ['create_datetime'] + + @action(methods=["GET"], detail=False, permission_classes=[IsAuthenticated]) + def homepage_statistics(self, request): + # Users 新增 + # LoginLog # 最后登录 + timestr = jx_timestamp().split(" ") + min_time = datetime.datetime.strptime(timestr[0] + " " + "00:00:00", "%Y-%m-%d %H:%M:%S") + max_time = datetime.datetime.strptime(timestr[0] + " " + "23:59:59", "%Y-%m-%d %H:%M:%S") + # 今日注册 + today_register = Users.objects.filter(create_datetime__gte=min_time, is_superuser=0).count() + # 今日登录 + today_login = len(set(LoginLog.objects.filter(create_datetime__gte=min_time).values_list('username'))) + # 三日新增 + Three_days_register = Users.objects.filter( + create_datetime__gte=min_time - datetime.timedelta(days=3), is_superuser=0).count() + # 七日新增 + Seven_days_register = Users.objects.filter( + create_datetime__gte=min_time - datetime.timedelta(days=7), is_superuser=0).count() + # 七日活跃 + Seven_days_login = len(set(LoginLog.objects.filter( + create_datetime__gte=min_time - datetime.timedelta(days=7)).values_list('username'))) + # 月活跃 + month_login = len(set(LoginLog.objects.filter( + create_datetime__gte=min_time - datetime.timedelta(days=30)).values_list('username'))) + # 七日用户登录数 + sum_days_login_list = [] + for i in range(7): + sum_days_login_list.append({"time": (min_time + datetime.timedelta(days=-i)).strftime("%Y-%m-%d"), + "count": len(set(LoginLog.objects.filter( + create_datetime__lte=max_time - datetime.timedelta(days=i), + create_datetime__gte=min_time - datetime.timedelta(days=i)).values_list( + 'username')))}) + + # 七日注册用户数 + sum_days_register_list = [] + for i in range(7): + sum_days_register_list.append( + {"time": (min_time + datetime.timedelta(days=-i)).strftime("%Y-%m-%d"), "count": Users.objects.filter( + create_datetime__lte=max_time - datetime.timedelta(days=i), + create_datetime__gte=min_time - datetime.timedelta(days=i), is_superuser=0).count()}) + # 用户总数 + sum_register = Users.objects.filter(is_superuser=0).count() + # FileList 附件 + today_f_l = FileList.objects.filter(create_datetime__gte=min_time).count() + sum_f_l = FileList.objects.all().count() + # 今日附件 + today_file = {'count': today_f_l, "occupy_space": 0} + # 总附件 + sum_file = {'count': sum_f_l, "occupy_space": 0} + data = { + "today_register": today_register, + "today_login": today_login, + "Three_days_register": Three_days_register, + "Seven_days_register": Seven_days_register, + "Seven_days_login": Seven_days_login, + "month_login": month_login, + "sum_days_login_list": sum_days_login_list, + "sum_days_register_list": sum_days_register_list, + "sum_register": sum_register, + "today_file": today_file, + "sum_file": sum_file, + } + return DetailResponse(data=data, msg="获取成功")