feat:新增后台dashboard接口
This commit is contained in:
@@ -2,8 +2,6 @@
|
|||||||
# @Author : Lan
|
# @Author : Lan
|
||||||
# @File : depends.py
|
# @File : depends.py
|
||||||
# @Software: PyCharm
|
# @Software: PyCharm
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
from fastapi import Header, HTTPException
|
from fastapi import Header, HTTPException
|
||||||
from fastapi.requests import Request
|
from fastapi.requests import Request
|
||||||
from core.settings import settings
|
from core.settings import settings
|
||||||
@@ -11,7 +9,7 @@ from apps.admin.services import FileService, ConfigService, LocalFileService
|
|||||||
|
|
||||||
|
|
||||||
async def admin_required(authorization: str = Header(default=None), request: Request = None):
|
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 request.url.path.startswith('/share/'):
|
||||||
if not settings.openUpload and not is_admin:
|
if not settings.openUpload and not is_admin:
|
||||||
raise HTTPException(status_code=403, detail='本站未开启游客上传,如需上传请先登录后台')
|
raise HTTPException(status_code=403, detail='本站未开启游客上传,如需上传请先登录后台')
|
||||||
|
|||||||
@@ -2,12 +2,14 @@
|
|||||||
# @Author : Lan
|
# @Author : Lan
|
||||||
# @File : views.py
|
# @File : views.py
|
||||||
# @Software: PyCharm
|
# @Software: PyCharm
|
||||||
|
import datetime
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from apps.admin.services import FileService, ConfigService, LocalFileService
|
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.dependencies import admin_required, get_file_service, get_config_service, get_local_file_service
|
||||||
from apps.admin.schemas import IDData, ShareItem, DeleteItem
|
from apps.admin.schemas import IDData, ShareItem, DeleteItem
|
||||||
from core.response import APIResponse
|
from core.response import APIResponse
|
||||||
|
from apps.base.models import FileCodes, KeyValue
|
||||||
|
|
||||||
admin_api = APIRouter(prefix='/admin', tags=['管理'])
|
admin_api = APIRouter(prefix='/admin', tags=['管理'])
|
||||||
|
|
||||||
@@ -17,6 +19,36 @@ async def login(admin: bool = Depends(admin_required)):
|
|||||||
return APIResponse()
|
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')
|
@admin_api.delete('/file/delete')
|
||||||
async def file_delete(
|
async def file_delete(
|
||||||
data: IDData,
|
data: IDData,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
# @File : main.py
|
# @File : main.py
|
||||||
# @Software: PyCharm
|
# @Software: PyCharm
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import time
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
@@ -55,6 +56,7 @@ async def lifespan(app: FastAPI):
|
|||||||
|
|
||||||
async def load_config():
|
async def load_config():
|
||||||
user_config, _ = await KeyValue.get_or_create(key='settings', defaults={'value': DEFAULT_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
|
settings.user_config = user_config.value
|
||||||
# 更新 ip_limit 配置
|
# 更新 ip_limit 配置
|
||||||
ip_limit['error'].minutes = settings.errorMinute
|
ip_limit['error'].minutes = settings.errorMinute
|
||||||
|
|||||||
Reference in New Issue
Block a user