feat(automation): 增加重登熔断与登录预算配置
This commit is contained in:
@@ -17,7 +17,14 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..deps import require_permission
|
||||
from ..models import Account, AutomationJob, AutomationRun, LoginTask, User
|
||||
from ..models import (
|
||||
Account,
|
||||
AutomationJob,
|
||||
AutomationRun,
|
||||
LoginQuotaSetting,
|
||||
LoginTask,
|
||||
User,
|
||||
)
|
||||
from ..services.audit_service import record_audit
|
||||
from ..services.automation import (
|
||||
ensure_jobs,
|
||||
@@ -43,6 +50,39 @@ class JobUpdate(BaseModel):
|
||||
concurrency: int | None = Field(default=None, ge=1, le=16)
|
||||
|
||||
|
||||
class LoginQuotaUpdate(BaseModel):
|
||||
daily_attempt_limit: int = Field(..., ge=1, le=1_000_000)
|
||||
hourly_attempt_limit: int = Field(..., ge=1, le=100_000)
|
||||
per_account_daily_limit: int = Field(..., ge=1, le=100)
|
||||
onboarding_daily_limit: int = Field(..., ge=0, le=100_000)
|
||||
relogin_high_value_daily_limit: int = Field(..., ge=0, le=100_000)
|
||||
relogin_ordinary_daily_limit: int = Field(..., ge=0, le=100_000)
|
||||
manual_daily_limit: int = Field(..., ge=0, le=100_000)
|
||||
login_concurrency: int = Field(..., ge=1, le=10)
|
||||
failure_pause_ratio: int = Field(..., ge=0, le=100)
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
def _login_quota_out(row: LoginQuotaSetting) -> dict[str, Any]:
|
||||
return {
|
||||
"daily_attempt_limit": int(row.daily_attempt_limit),
|
||||
"hourly_attempt_limit": int(row.hourly_attempt_limit),
|
||||
"per_account_daily_limit": int(row.per_account_daily_limit),
|
||||
"onboarding_daily_limit": int(row.onboarding_daily_limit),
|
||||
"relogin_high_value_daily_limit": int(
|
||||
row.relogin_high_value_daily_limit
|
||||
),
|
||||
"relogin_ordinary_daily_limit": int(
|
||||
row.relogin_ordinary_daily_limit
|
||||
),
|
||||
"manual_daily_limit": int(row.manual_daily_limit),
|
||||
"login_concurrency": int(row.login_concurrency),
|
||||
"failure_pause_ratio": int(row.failure_pause_ratio),
|
||||
"enabled": bool(row.enabled),
|
||||
"updated_at": row.updated_at.isoformat() if row.updated_at else None,
|
||||
}
|
||||
|
||||
|
||||
def _job_out(job: AutomationJob) -> dict[str, Any]:
|
||||
return {
|
||||
"key": job.key,
|
||||
@@ -125,6 +165,58 @@ def list_jobs(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/login-quota")
|
||||
def get_login_quota(
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("automation:view")),
|
||||
):
|
||||
"""查看登录与自动重登的数据库级预算。"""
|
||||
row = db.query(LoginQuotaSetting).filter(LoginQuotaSetting.id == 1).first()
|
||||
if row is None:
|
||||
row = LoginQuotaSetting(id=1)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
return _login_quota_out(row)
|
||||
|
||||
|
||||
@router.put("/login-quota")
|
||||
def update_login_quota(
|
||||
payload: LoginQuotaUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("automation:manage")),
|
||||
):
|
||||
"""更新登录、首次登录、重登和账号检测预算。"""
|
||||
row = db.query(LoginQuotaSetting).filter(LoginQuotaSetting.id == 1).first()
|
||||
if row is None:
|
||||
row = LoginQuotaSetting(id=1)
|
||||
db.add(row)
|
||||
before = _login_quota_out(row) if row.updated_at else {}
|
||||
for field in (
|
||||
"daily_attempt_limit",
|
||||
"hourly_attempt_limit",
|
||||
"per_account_daily_limit",
|
||||
"onboarding_daily_limit",
|
||||
"relogin_high_value_daily_limit",
|
||||
"relogin_ordinary_daily_limit",
|
||||
"manual_daily_limit",
|
||||
"login_concurrency",
|
||||
"failure_pause_ratio",
|
||||
"enabled",
|
||||
):
|
||||
setattr(row, field, getattr(payload, field))
|
||||
row.updated_by = current.id
|
||||
row.updated_at = datetime.now(UTC)
|
||||
record_audit(
|
||||
db,
|
||||
current,
|
||||
action="automation:login-quota:update",
|
||||
target="login_quota_settings:1",
|
||||
detail={"before": before, "after": _login_quota_out(row)},
|
||||
)
|
||||
db.commit()
|
||||
return _login_quota_out(row)
|
||||
|
||||
|
||||
@router.patch("/jobs/{key}")
|
||||
def update_job(
|
||||
key: str,
|
||||
|
||||
Reference in New Issue
Block a user