perf👌: 完善系统管理api

This commit is contained in:
HibiKier 2024-01-15 13:00:17 +08:00
parent b656f89c8e
commit 72b0b011ee
4 changed files with 51 additions and 64 deletions

View File

@ -22,5 +22,5 @@ async def _(path: Optional[str] = None) -> Result:
@router.get("/get_resources_size", dependencies=[authentication()], description="获取文件列表") @router.get("/get_resources_size", dependencies=[authentication()], description="获取文件列表")
async def _(type: Optional[str] = None) -> Result: async def _(full_path: Optional[str] = None) -> Result:
return Result.ok(await get_system_disk(type)) return Result.ok(await get_system_disk(full_path))

View File

@ -19,17 +19,3 @@ class DirFile(BaseModel):
"""文件夹或文件名称""" """文件夹或文件名称"""
parent: Optional[str] = None parent: Optional[str] = None
"""父级""" """父级"""
class SystemFolderSize(BaseModel):
"""
资源文件占比
"""
font_dir_size: float
image_dir_size: float
text_dir_size: float
record_dir_size: float
temp_dir_size: float
data_dir_size: float
log_dir_size: float
check_time: datetime

View File

@ -105,13 +105,13 @@ class SystemFolderSize(BaseModel):
资源文件占比 资源文件占比
""" """
font_dir_size: float name: str
image_dir_size: float """名称"""
text_dir_size: float size: float
record_dir_size: float """大小"""
temp_dir_size: float full_path: Optional[str]
data_dir_size: float """完整路径"""
log_dir_size: float is_dir: bool
check_time: datetime """是否为文件夹"""

View File

@ -1,7 +1,7 @@
import os import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Optional, Union from typing import Any, Dict, List, Optional, Union
import psutil import psutil
import ujson as json import ujson as json
@ -123,47 +123,48 @@ def get_system_status() -> SystemStatus:
@run_sync @run_sync
def get_system_disk( def get_system_disk(
type_: Optional[str], full_path: Optional[str],
) -> Union[SystemFolderSize, Dict[str, Union[float, datetime]]]: ) -> List[SystemFolderSize]:
""" """
说明: 说明:
获取资源文件大小等 获取资源文件大小等
""" """
if not type_: base_path = Path(full_path) if full_path else Path()
disk = SystemFolderSize( other_size = 0
font_dir_size=_get_dir_size(FONT_PATH) / 1024 / 1024, data_list = []
image_dir_size=_get_dir_size(IMAGE_PATH) / 1024 / 1024, for file in os.listdir(base_path):
text_dir_size=_get_dir_size(TEXT_PATH) / 1024 / 1024, f = base_path / file
record_dir_size=_get_dir_size(RECORD_PATH) / 1024 / 1024, if f.is_dir():
temp_dir_size=_get_dir_size(TEMP_PATH) / 1024 / 102, size = _get_dir_size(f) / 1024 / 1024
data_dir_size=_get_dir_size(DATA_PATH) / 1024 / 1024, data_list.append(SystemFolderSize(name=file, size=size, full_path=str(f), is_dir=True))
log_dir_size=_get_dir_size(LOG_PATH) / 1024 / 1024,
check_time=datetime.now().replace(microsecond=0),
)
return disk
else: else:
if type_ == "image": other_size += f.stat().st_size / 1024 / 1024
dir_path = IMAGE_PATH if other_size:
elif type_ == "font": data_list.append(SystemFolderSize(name='other_file', size=other_size, full_path=full_path, is_dir=False))
dir_path = FONT_PATH return data_list
elif type_ == "text": # else:
dir_path = TEXT_PATH # if type_ == "image":
elif type_ == "record": # dir_path = IMAGE_PATH
dir_path = RECORD_PATH # elif type_ == "font":
elif type_ == "data": # dir_path = FONT_PATH
dir_path = DATA_PATH # elif type_ == "text":
elif type_ == "temp": # dir_path = TEXT_PATH
dir_path = TEMP_PATH # elif type_ == "record":
else: # dir_path = RECORD_PATH
dir_path = LOG_PATH # elif type_ == "data":
dir_map = {} # dir_path = DATA_PATH
other_file_size = 0 # elif type_ == "temp":
for file in os.listdir(dir_path): # dir_path = TEMP_PATH
file = Path(dir_path / file) # else:
if file.is_dir(): # dir_path = LOG_PATH
dir_map[file.name] = _get_dir_size(file) / 1024 / 1024 # dir_map = {}
else: # other_file_size = 0
other_file_size += os.path.getsize(file) / 1024 / 1024 # for file in os.listdir(dir_path):
dir_map["其他文件"] = other_file_size # file = Path(dir_path / file)
dir_map["check_time"] = datetime.now().replace(microsecond=0) # if file.is_dir():
return dir_map # dir_map[file.name] = _get_dir_size(file) / 1024 / 1024
# else:
# other_file_size += os.path.getsize(file) / 1024 / 1024
# dir_map["其他文件"] = other_file_size
# dir_map["check_time"] = datetime.now().replace(microsecond=0)
# return dir_map