Skip to content

Commit

Permalink
feat:新增后台dashboard接口
Browse files Browse the repository at this point in the history
  • Loading branch information
vastsa committed Nov 23, 2024
1 parent 9ba7d37 commit f0e7304
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
4 changes: 1 addition & 3 deletions apps/admin/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
# @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
from apps.admin.services import FileService, ConfigService, LocalFileService


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='本站未开启游客上传,如需上传请先登录后台')
Expand Down
32 changes: 32 additions & 0 deletions apps/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=['管理'])

Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @File : main.py
# @Software: PyCharm
import asyncio
import time

from fastapi import FastAPI

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f0e7304

Please sign in to comment.