feat(automation): 按成功率灰度扩缩自动重登
This commit is contained in:
@@ -344,3 +344,63 @@ def test_account_platform_status_builds_seven_day_trends(db):
|
||||
assert login_today["failed"] == 1
|
||||
assert login_today["success_rate"] == 50.0
|
||||
assert lifecycle_today["ck_invalid"] == 1
|
||||
|
||||
|
||||
def test_account_platform_status_alerts_on_persistently_low_trends(db):
|
||||
account = _account("platform-trend-alerts")
|
||||
db.add(account)
|
||||
db.flush()
|
||||
now = datetime.now(UTC)
|
||||
db.add_all(
|
||||
[
|
||||
AccountSyncRun(
|
||||
account_id=account.id,
|
||||
domain="storehome",
|
||||
started_at=now,
|
||||
finished_at=now,
|
||||
status="failed",
|
||||
duration_ms=100,
|
||||
)
|
||||
for _ in range(20)
|
||||
]
|
||||
)
|
||||
db.add_all(
|
||||
[
|
||||
LoginAttempt(
|
||||
account_id=account.id,
|
||||
category="relogin_ordinary",
|
||||
trigger_source="automation",
|
||||
started_at=now,
|
||||
finished_at=now,
|
||||
status="failed",
|
||||
failure_kind="network",
|
||||
quota_day=now.date(),
|
||||
)
|
||||
for _ in range(10)
|
||||
]
|
||||
)
|
||||
db.add_all(
|
||||
[
|
||||
AccountEvent(
|
||||
account_id=account.id,
|
||||
event_type="relogin_failed",
|
||||
category="auth",
|
||||
occurred_at=now,
|
||||
title="重登失败",
|
||||
source="pytest",
|
||||
actor_type="system",
|
||||
source_ref=f"pytest-relogin-alert:{account.id}:{index}",
|
||||
)
|
||||
for index in range(5)
|
||||
]
|
||||
)
|
||||
db.commit()
|
||||
|
||||
status = account_platform_status(db, now=now)
|
||||
codes = {alert["code"] for alert in status["alerts"]}
|
||||
|
||||
assert {
|
||||
"query_success_rate_low",
|
||||
"login_success_rate_low",
|
||||
"relogin_failure_trend_high",
|
||||
} <= codes
|
||||
|
||||
@@ -23,6 +23,7 @@ from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AutomationJob,
|
||||
LoginAttempt,
|
||||
LoginQuotaSetting,
|
||||
LoginTask,
|
||||
User,
|
||||
@@ -230,6 +231,100 @@ class TestFailureCircuit(_Base):
|
||||
assert paused is False
|
||||
|
||||
|
||||
class TestAdaptiveBatch(_Base):
|
||||
def _attempts(
|
||||
self,
|
||||
account: Account,
|
||||
*,
|
||||
success: int,
|
||||
failed: int,
|
||||
failure_kind: str = "network",
|
||||
):
|
||||
now = datetime.now(UTC)
|
||||
rows = [
|
||||
LoginAttempt(
|
||||
account_id=account.id,
|
||||
category="relogin_ordinary",
|
||||
trigger_source="automation",
|
||||
started_at=now,
|
||||
finished_at=now,
|
||||
status="success",
|
||||
quota_day=now.date(),
|
||||
)
|
||||
for _ in range(success)
|
||||
]
|
||||
rows.extend(
|
||||
LoginAttempt(
|
||||
account_id=account.id,
|
||||
category="relogin_ordinary",
|
||||
trigger_source="automation",
|
||||
started_at=now,
|
||||
finished_at=now,
|
||||
status="failed",
|
||||
failure_kind=failure_kind,
|
||||
quota_day=now.date(),
|
||||
)
|
||||
for _ in range(failed)
|
||||
)
|
||||
self.session.add_all(rows)
|
||||
self.session.commit()
|
||||
|
||||
def test_cold_start_uses_initial_batch_not_configured_ceiling(self):
|
||||
job = self.job()
|
||||
job.batch_size = 50
|
||||
job.config = {}
|
||||
self.session.commit()
|
||||
|
||||
decision = ck_relogin.resolve_adaptive_batch_size(
|
||||
self.session,
|
||||
job,
|
||||
now=datetime.now(UTC),
|
||||
pending=100,
|
||||
)
|
||||
|
||||
assert decision.batch_size == 10
|
||||
assert decision.action == "cold_start"
|
||||
|
||||
def test_high_recent_success_grows_by_at_most_twenty_five_percent(self):
|
||||
account = self.make_account("adaptive-success")
|
||||
self._attempts(account, success=9, failed=1)
|
||||
job = self.job()
|
||||
job.batch_size = 50
|
||||
job.config = {"adaptive_batch_size": 10}
|
||||
self.session.commit()
|
||||
|
||||
decision = ck_relogin.resolve_adaptive_batch_size(
|
||||
self.session,
|
||||
job,
|
||||
now=datetime.now(UTC),
|
||||
pending=100,
|
||||
)
|
||||
|
||||
assert decision.success_rate == 90.0
|
||||
assert decision.batch_size == 13
|
||||
assert decision.action == "grow"
|
||||
assert job.config["adaptive_batch_size"] == 13
|
||||
|
||||
def test_low_recent_success_halves_batch(self):
|
||||
account = self.make_account("adaptive-shrink")
|
||||
self._attempts(account, success=2, failed=8)
|
||||
job = self.job()
|
||||
job.batch_size = 50
|
||||
job.config = {"adaptive_batch_size": 20}
|
||||
self.session.commit()
|
||||
|
||||
decision = ck_relogin.resolve_adaptive_batch_size(
|
||||
self.session,
|
||||
job,
|
||||
now=datetime.now(UTC),
|
||||
pending=100,
|
||||
)
|
||||
|
||||
assert decision.success_rate == 20.0
|
||||
assert decision.batch_size == 10
|
||||
assert decision.action == "shrink"
|
||||
|
||||
|
||||
class TestBackoff(_Base):
|
||||
def test_ladder_is_escalating(self):
|
||||
now = datetime(2026, 10, 2, 12, 0, tzinfo=UTC)
|
||||
|
||||
Reference in New Issue
Block a user