127 lines
3.3 KiB
Python
127 lines
3.3 KiB
Python
"""
|
|
系统配置 API
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from app.models.base import get_db
|
|
from app.models.system_config import SystemConfig
|
|
from app.schemas.system_config import (
|
|
SystemConfigResponse,
|
|
SystemConfigUpdate,
|
|
ScheduleConfig,
|
|
LimitConfig,
|
|
ProbabilityConfig
|
|
)
|
|
from app.core.config import settings
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=List[SystemConfigResponse])
|
|
def get_system_configs(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取所有系统配置"""
|
|
configs = db.query(SystemConfig).all()
|
|
return configs
|
|
|
|
|
|
@router.get("/schedule", response_model=ScheduleConfig)
|
|
def get_schedule_config(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取调度配置"""
|
|
from app.services.scheduler_service import scheduler_service
|
|
|
|
return ScheduleConfig(
|
|
task_start_hour=settings.TASK_START_HOUR,
|
|
task_end_hour=settings.TASK_END_HOUR,
|
|
task_interval_min=settings.TASK_INTERVAL_MIN,
|
|
task_interval_max=settings.TASK_INTERVAL_MAX,
|
|
is_task_running=scheduler_service.is_running
|
|
)
|
|
|
|
|
|
@router.get("/limits", response_model=LimitConfig)
|
|
def get_limit_config(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取限额配置"""
|
|
return LimitConfig(
|
|
max_tokens_per_day=settings.MAX_TOKENS_PER_DAY,
|
|
max_comments_per_user_per_day=settings.MAX_COMMENTS_PER_USER_PER_DAY,
|
|
max_replies_per_user_per_day=settings.MAX_REPLIES_PER_USER_PER_DAY
|
|
)
|
|
|
|
|
|
@router.get("/probabilities", response_model=ProbabilityConfig)
|
|
def get_probability_config(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取概率配置"""
|
|
return ProbabilityConfig(
|
|
like_probability=settings.LIKE_PROBABILITY,
|
|
favorite_probability=settings.FAVORITE_PROBABILITY,
|
|
share_probability=settings.SHARE_PROBABILITY
|
|
)
|
|
|
|
|
|
@router.put("/schedule")
|
|
def update_schedule_config(
|
|
config: ScheduleConfig,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""更新调度配置"""
|
|
# TODO: 更新系统配置表并重新加载
|
|
return {"message": "Schedule config updated"}
|
|
|
|
|
|
@router.put("/limits")
|
|
def update_limit_config(
|
|
config: LimitConfig,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""更新限额配置"""
|
|
# TODO: 更新系统配置表
|
|
return {"message": "Limit config updated"}
|
|
|
|
|
|
@router.post("/scheduler/start")
|
|
def start_scheduler(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""启动定时任务"""
|
|
from app.services.scheduler_service import scheduler_service
|
|
|
|
scheduler_service.start()
|
|
scheduler_service.add_interaction_task()
|
|
|
|
return {"message": "Scheduler started", "running": scheduler_service.is_running}
|
|
|
|
|
|
@router.post("/scheduler/stop")
|
|
def stop_scheduler(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""停止定时任务"""
|
|
from app.services.scheduler_service import scheduler_service
|
|
|
|
scheduler_service.stop()
|
|
|
|
return {"message": "Scheduler stopped", "running": scheduler_service.is_running}
|
|
|
|
|
|
@router.get("/scheduler/status")
|
|
def get_scheduler_status(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取定时任务状态"""
|
|
from app.services.scheduler_service import scheduler_service
|
|
|
|
return {
|
|
"is_running": scheduler_service.is_running,
|
|
"jobs": [job.id for job in scheduler_service.scheduler.get_jobs()]
|
|
}
|