fix: delay binding.percent update until remote sync succeeds and update UI labels to 自动保底盈利

This commit is contained in:
SmartUp Developer
2026-07-02 21:27:52 +08:00
parent 0e77de31ae
commit fdbcca4d95
3 changed files with 57 additions and 7 deletions
+9 -5
View File
@@ -113,6 +113,7 @@ def _log(
message: str,
old_rate: Any = None,
new_rate: Any = None,
effective_percent: str = None,
) -> WebsiteSyncLog:
row = WebsiteSyncLog(
website_id=website.id,
@@ -120,7 +121,7 @@ def _log(
target_group_id=binding.target_group_id,
target_group_name=binding.target_group_name,
algorithm=binding.algorithm,
percent=binding.percent,
percent=effective_percent if effective_percent is not None else binding.percent,
source_rates_json=json.dumps(source_rates, ensure_ascii=False),
old_rate=fixed_decimal_string(old_rate, 2) if old_rate not in (None, "") else None,
new_rate=fixed_decimal_string(new_rate, 2) if new_rate not in (None, "") else None,
@@ -168,6 +169,7 @@ def sync_binding(db: Session, binding: WebsiteGroupBinding, write: bool = True)
})
try:
target_rate = calculate_target_rate([item.get("rate") for item in source_rates], binding.percent, binding.algorithm)
auto_percent_str = None
if binding.algorithm == "priority_weighted_plus_percent":
rates = [rate for rate in (parse_positive_decimal(item.get("rate")) for item in source_rates) if rate is not None]
if rates:
@@ -179,10 +181,10 @@ def sync_binding(db: Session, binding: WebsiteGroupBinding, write: bool = True)
w1 = Decimal("0.85")
w_others = Decimal("0.15") / Decimal(N - 1)
base = rates_sorted[0] * w1 + sum(rates_sorted[1:], Decimal("0")) * w_others
if base > 0:
effective_percent = (target_rate / base - Decimal("1")) * Decimal("100")
binding.percent = fixed_decimal_string(effective_percent.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP), 2)
auto_percent_str = fixed_decimal_string(effective_percent.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP), 2)
except Exception as exc:
return _log(db, binding, website, source_rates, "failed", str(exc))
@@ -198,15 +200,17 @@ def sync_binding(db: Session, binding: WebsiteGroupBinding, write: bool = True)
binding.target_group_id,
target_rate,
)
if auto_percent_str is not None:
binding.percent = auto_percent_str
website.last_status = "healthy"
website.last_error = None
except Exception as exc:
website.last_status = "unhealthy"
website.last_error = str(exc)
db.commit()
return _log(db, binding, website, source_rates, "failed", f"写回失败:{exc}", old_rate, target_rate)
return _log(db, binding, website, source_rates, "failed", f"写回失败:{exc}", old_rate, target_rate, auto_percent_str)
db.commit()
log = _log(db, binding, website, source_rates, "success", "同步成功", old_rate, target_rate)
log = _log(db, binding, website, source_rates, "success", "同步成功", old_rate, target_rate, auto_percent_str)
old_rate_str = fixed_decimal_string(old_rate, 2) if old_rate not in (None, "") else None
new_rate_str = fixed_decimal_string(target_rate, 2)
if old_rate_str != new_rate_str: