init: 初始化 dpb 桃育种系统代码库

前后端 + 后端 FastAPI 全量源码、部署脚本与文档。
This commit is contained in:
34047007@qq.com
2026-08-06 00:17:49 +08:00
commit b95053c52c
1469 changed files with 322298 additions and 0 deletions
@@ -0,0 +1 @@
@@ -0,0 +1,17 @@
from fastapi import APIRouter, Security
from fastapi.responses import JSONResponse
from app.api.v1.module_monitor.server.schema import ServerMonitorSchema
from app.common.response import ResponseSchema, SuccessResponse
from app.core.dependencies import AuthPermission
from app.core.router_class import OperationLogRoute
from .service import ServerService
ServerRouter = APIRouter(route_class=OperationLogRoute, prefix="/server", tags=["服务器监控"])
@ServerRouter.get("/info", summary="查询服务器监控信息", response_model=ResponseSchema[ServerMonitorSchema], dependencies=[Security(AuthPermission(["module_monitor:server:query"]))])
async def get_monitor_server_info_controller() -> JSONResponse:
result_dict = await ServerService.get_server_monitor_info()
return SuccessResponse(data=result_dict, msg="获取服务器监控信息成功")
@@ -0,0 +1,65 @@
from pydantic import BaseModel, Field
class CpuInfoSchema(BaseModel):
"""CPU信息模型"""
cpu_num: int = Field(description="CPU核心数")
used: float = Field(ge=0, le=100, description="CPU用户使用率(%)")
sys: float = Field(ge=0, le=100, description="CPU系统使用率(%)")
free: float = Field(ge=0, le=100, description="CPU空闲率(%)")
class MemoryInfoSchema(BaseModel):
"""内存信息模型"""
total: str = Field(description="内存总量")
used: str = Field(description="已用内存")
free: str = Field(description="剩余内存")
usage: float = Field(ge=0, le=100, description="使用率(%)")
class SysInfoSchema(BaseModel):
"""系统信息模型"""
computer_ip: str = Field(description="服务器IP")
computer_name: str = Field(description="服务器名称")
os_arch: str = Field(description="系统架构")
os_name: str = Field(description="操作系统")
user_dir: str = Field(description="项目路径")
class PyInfoSchema(BaseModel):
"""Python运行信息模型"""
name: str = Field(description="Python名称")
version: str = Field(description="Python版本")
start_time: str = Field(description="启动时间")
run_time: str = Field(description="运行时长")
home: str = Field(description="安装路径")
memory_used: str = Field(description="内存占用")
memory_usage: float = Field(ge=0, le=100, description="内存使用率(%)")
memory_total: str = Field(description="总内存")
memory_free: str = Field(description="剩余内存")
class DiskInfoSchema(BaseModel):
"""磁盘信息模型"""
dir_name: str = Field(description="磁盘路径")
sys_type_name: str = Field(description="文件系统类型")
type_name: str = Field(description="磁盘类型")
total: str = Field(description="总容量")
used: str = Field(description="已用容量")
free: str = Field(description="可用容量")
usage: float = Field(ge=0, le=100, description="使用率(%)")
class ServerMonitorSchema(BaseModel):
"""服务器监控信息模型"""
cpu: CpuInfoSchema = Field(description="CPU信息")
mem: MemoryInfoSchema = Field(description="内存信息")
py: PyInfoSchema = Field(description="Python运行信息")
sys: SysInfoSchema = Field(description="系统信息")
disks: list[DiskInfoSchema] = Field(default_factory=list, description="磁盘信息")
@@ -0,0 +1,116 @@
import platform
import socket
import time
from pathlib import Path
import psutil
from app.utils.common_util import bytes2human
from .schema import (
CpuInfoSchema,
DiskInfoSchema,
MemoryInfoSchema,
PyInfoSchema,
ServerMonitorSchema,
SysInfoSchema,
)
class ServerService:
"""服务监控模块服务层"""
@staticmethod
async def get_server_monitor_info() -> ServerMonitorSchema:
return ServerMonitorSchema(
cpu=ServerService._get_cpu_info(),
mem=ServerService._get_memory_info(),
sys=ServerService._get_system_info(),
py=ServerService._get_python_info(),
disks=ServerService._get_disk_info(),
)
@staticmethod
def _get_cpu_info() -> CpuInfoSchema:
cpu_times = psutil.cpu_times_percent()
cpu_num = psutil.cpu_count(logical=True)
if not cpu_num:
cpu_num = 1
return CpuInfoSchema(
cpu_num=cpu_num,
used=cpu_times.user,
sys=cpu_times.system,
free=cpu_times.idle,
)
@staticmethod
def _get_memory_info() -> MemoryInfoSchema:
memory = psutil.virtual_memory()
return MemoryInfoSchema(
total=bytes2human(memory.total),
used=bytes2human(memory.used),
free=bytes2human(memory.free),
usage=memory.percent,
)
@staticmethod
def _get_system_info() -> SysInfoSchema:
hostname = socket.gethostname()
return SysInfoSchema(
computer_ip=socket.gethostbyname(hostname),
computer_name=platform.node(),
os_arch=platform.machine(),
os_name=platform.platform(),
user_dir=str(Path.cwd()),
)
@staticmethod
def _get_python_info() -> PyInfoSchema:
current_process = psutil.Process()
memory = psutil.virtual_memory()
process_memory = current_process.memory_info()
start_time = current_process.create_time()
run_time = ServerService._calculate_run_time(start_time)
return PyInfoSchema(
name=current_process.name(),
version=platform.python_version(),
start_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_time)),
run_time=run_time,
home=str(Path(current_process.exe())),
memory_total=bytes2human(memory.available),
memory_used=bytes2human(process_memory.rss),
memory_free=bytes2human(memory.available - process_memory.rss),
memory_usage=round((process_memory.rss / memory.available) * 100, 2),
)
@staticmethod
def _get_disk_info() -> list[DiskInfoSchema]:
disk_info = []
for partition in psutil.disk_partitions():
try:
usage = psutil.disk_usage(partition.mountpoint)
mount_point = str(Path(partition.mountpoint))
disk_info.append(
DiskInfoSchema(
dir_name=mount_point,
sys_type_name=partition.fstype,
type_name=f"本地固定磁盘({mount_point}",
total=bytes2human(usage.total),
used=bytes2human(usage.used),
free=bytes2human(usage.free),
usage=usage.percent,
),
)
except (PermissionError, FileNotFoundError):
continue
return disk_info
@staticmethod
def _calculate_run_time(start_time: float) -> str:
difference = time.time() - start_time
days = int(difference // (24 * 60 * 60))
hours = int((difference % (24 * 60 * 60)) // (60 * 60))
minutes = int((difference % (60 * 60)) // 60)
return f"{days}{hours}小时{minutes}分钟"