From f0e7304bb20ed5230a523c76a008a28d8e1c34f9 Mon Sep 17 00:00:00 2001 From: Lan Date: Sat, 23 Nov 2024 16:43:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E5=90=8E=E5=8F=B0dashbo?= =?UTF-8?q?ard=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/admin/dependencies.py | 4 +--- apps/admin/views.py | 32 ++++++++++++++++++++++++++++++++ main.py | 2 ++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/apps/admin/dependencies.py b/apps/admin/dependencies.py index 8efcced9..6aaf0579 100644 --- a/apps/admin/dependencies.py +++ b/apps/admin/dependencies.py @@ -2,8 +2,6 @@ # @Author : Lan # @File : depends.py # @Software: PyCharm -from typing import Union - from fastapi import Header, HTTPException from fastapi.requests import Request from core.settings import settings @@ -11,7 +9,7 @@ async def admin_required(authorization: str = Header(default=None), request: Request = None): - is_admin = authorization == str(settings.admin_token) + is_admin = authorization.split(' ')[-1] if authorization else '' == str(settings.admin_token) if request.url.path.startswith('/share/'): if not settings.openUpload and not is_admin: raise HTTPException(status_code=403, detail='本站未开启游客上传,如需上传请先登录后台') diff --git a/apps/admin/views.py b/apps/admin/views.py index b562189e..37bf2641 100644 --- a/apps/admin/views.py +++ b/apps/admin/views.py @@ -2,12 +2,14 @@ # @Author : Lan # @File : views.py # @Software: PyCharm +import datetime from fastapi import APIRouter, Depends from apps.admin.services import FileService, ConfigService, LocalFileService from apps.admin.dependencies import admin_required, get_file_service, get_config_service, get_local_file_service from apps.admin.schemas import IDData, ShareItem, DeleteItem from core.response import APIResponse +from apps.base.models import FileCodes, KeyValue admin_api = APIRouter(prefix='/admin', tags=['管理']) @@ -17,6 +19,36 @@ async def login(admin: bool = Depends(admin_required)): return APIResponse() +@admin_api.get('/dashboard') +async def dashboard(admin: bool = Depends(admin_required)): + all_codes = await FileCodes.all() + all_size = str(sum([code.size for code in all_codes])) + sys_start = await KeyValue.filter(key='sys_start').first() + # 获取当前日期时间 + now = datetime.datetime.now() + today_start = now.replace(hour=0, minute=0, second=0, microsecond=0) + yesterday_start = today_start - datetime.timedelta(days=1) + yesterday_end = today_start - datetime.timedelta(microseconds=1) + # 统计昨天一整天的记录数(从昨天0点到23:59:59) + yesterday_codes = FileCodes.filter( + created_at__gte=yesterday_start, + created_at__lte=yesterday_end + ) + # 统计今天到现在的记录数(从今天0点到现在) + today_codes = FileCodes.filter( + created_at__gte=today_start + ) + return APIResponse(detail={ + 'totalFiles': len(all_codes), + 'storageUsed': all_size, + 'sysUptime': sys_start.value, + 'yesterdayCount': await yesterday_codes.count(), + 'yesterdaySize': str(sum([code.size for code in await yesterday_codes])), + 'todayCount': await today_codes.count(), + 'todaySize': str(sum([code.size for code in await today_codes])), + }) + + @admin_api.delete('/file/delete') async def file_delete( data: IDData, diff --git a/main.py b/main.py index 1d0a1e05..20a6ddd2 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ # @File : main.py # @Software: PyCharm import asyncio +import time from fastapi import FastAPI @@ -55,6 +56,7 @@ async def lifespan(app: FastAPI): async def load_config(): user_config, _ = await KeyValue.get_or_create(key='settings', defaults={'value': DEFAULT_CONFIG}) + await KeyValue.update_or_create(key='sys_start', defaults={'value': int(time.time() * 1000)}) settings.user_config = user_config.value # 更新 ip_limit 配置 ip_limit['error'].minutes = settings.errorMinute