4-14首页统计

pull/95/head
layman_one 2023-04-14 18:36:51 +08:00
parent 03455ba9d0
commit e1868b7a56
2 changed files with 97 additions and 0 deletions

View File

@ -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

View File

@ -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="获取成功")