feat: implement priority_weighted_plus_percent pricing algorithm

This commit is contained in:
SmartUp Developer
2026-07-02 20:23:20 +08:00
parent d07ab62772
commit 8746068cd4
6 changed files with 43 additions and 6 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ class WebsiteGroupBinding(Base):
target_group_name: Mapped[str] = mapped_column(String(255), default="")
source_groups_json: Mapped[str] = mapped_column(Text, default="[]")
percent: Mapped[str] = mapped_column(String(32), default="0")
algorithm: Mapped[str] = mapped_column(String(64), default="max_plus_percent")
algorithm: Mapped[str] = mapped_column(String(64), default="priority_weighted_plus_percent")
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.now(timezone.utc))
updated_at: Mapped[datetime] = mapped_column(
+1 -1
View File
@@ -73,7 +73,7 @@ SENSITIVE_CREDENTIAL_KEYS = {
"aws_secret_access_key", "aws_session_token",
"service_account_json", "service_account", "private_key",
}
ALGORITHMS = {"max_plus_percent", "average_plus_percent", "min_plus_percent"}
ALGORITHMS = {"max_plus_percent", "average_plus_percent", "min_plus_percent", "priority_weighted_plus_percent"}
def _mask(cfg: dict) -> dict:
+1 -1
View File
@@ -91,7 +91,7 @@ class BindingCreate(BaseModel):
target_group_name: str = ""
source_groups: list[BindingSourceGroup] = Field(default_factory=list)
percent: float = Field(default=0, ge=0)
algorithm: str = "max_plus_percent"
algorithm: str = "priority_weighted_plus_percent"
enabled: bool = True
+21 -1
View File
@@ -65,12 +65,32 @@ def calculate_target_rate(values: list[Any], percent: Any = 0, algorithm: str =
base = min(rates)
elif algorithm == "max_plus_percent":
base = max(rates)
elif algorithm == "priority_weighted_plus_percent":
rates_sorted = sorted(rates)
N = len(rates_sorted)
if N == 1:
base = rates_sorted[0]
else:
base = Decimal("0")
remaining = Decimal("1.0")
for i in range(N):
if i == N - 1:
weight = remaining
else:
weight = remaining * Decimal("0.85")
remaining -= weight
base += rates_sorted[i] * weight
else:
raise WebsiteError(f"不支持的算法:{algorithm}")
pct = Decimal(str(percent or 0))
if pct < 0:
raise WebsiteError("百分比不能为负数")
return (base * (Decimal("1") + pct / Decimal("100"))).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
target_rate = (base * (Decimal("1") + pct / Decimal("100"))).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
if algorithm == "priority_weighted_plus_percent":
min_rate = min(rates)
if target_rate < min_rate:
target_rate = min_rate
return target_rate
def _unwrap_data(value: Any) -> Any:
+16
View File
@@ -399,3 +399,19 @@ def test_list_accounts_pagination_returns_none_when_any_page_fails():
assert len(requests_called) == 2
finally:
client.close()
def test_calculate_target_rate_priority_weighted():
# 0.18、0.25 + 50% 应得到约 0.29
assert calculate_target_rate(["0.18", "0.25"], 50, "priority_weighted_plus_percent") == Decimal("0.29")
# 单来源时等同于该来源倍率 + 百分比
assert calculate_target_rate(["0.18"], 50, "priority_weighted_plus_percent") == Decimal("0.27")
# 多来源时权重按低倍率优先,最高倍率不会主导价格
assert calculate_target_rate(["0.18", "0.25", "0.38"], 50, "priority_weighted_plus_percent") == Decimal("0.29")
# 无可用正数倍率时报错
with pytest.raises(WebsiteError, match="没有可用的正数上游倍率"):
calculate_target_rate([], 50, "priority_weighted_plus_percent")
# 旧算法结果不变
assert calculate_target_rate(["0.18", "0.25"], 50, "max_plus_percent") == Decimal("0.38")
+3 -2
View File
@@ -287,6 +287,7 @@
<el-form-item label="算法">
<div class="custom-select-wrapper">
<el-select v-model="bindingForm.algorithm" style="width:100%" class="custom-theme-select">
<el-option label="优先级期望成本 + 百分比" value="priority_weighted_plus_percent" />
<el-option label="最高倍率 + 百分比" value="max_plus_percent" />
<el-option label="平均倍率 + 百分比" value="average_plus_percent" />
<el-option label="最低倍率 + 百分比" value="min_plus_percent" />
@@ -1091,7 +1092,7 @@ const syncingImportStatus = ref(false)
const reorderingPriorities = ref(false)
const statusLabel = (s: string) => ({ healthy: '健康', unhealthy: '异常', unknown: '未知' }[s] || s)
const algorithmLabel = (s: string) => ({ max_plus_percent: '最高倍率', average_plus_percent: '平均倍率', min_plus_percent: '最低倍率' }[s] || s)
const algorithmLabel = (s: string) => ({ max_plus_percent: '最高倍率', average_plus_percent: '平均倍率', min_plus_percent: '最低倍率', priority_weighted_plus_percent: '优先级期望成本' }[s] || s)
const toUTC = (t: string) => /[Z+\-]\d*$/.test(t.trim()) ? t : t + 'Z'
const fmtTime = (t: string) => dayjs(toUTC(t)).format('MM-DD HH:mm:ss')
const sourceGroupId = (group: any) => String(group?.group_id || group?.id || group?.name || '')
@@ -1130,7 +1131,7 @@ function defaultBindingForm(): GroupBindingForm {
target_group_name: '',
source_groups: [],
percent: 0,
algorithm: 'max_plus_percent',
algorithm: 'priority_weighted_plus_percent',
enabled: true,
}
}