diff --git a/backend/app/analysis/contract.py b/backend/app/analysis/contract.py index 9159875c..d8d8c61a 100644 --- a/backend/app/analysis/contract.py +++ b/backend/app/analysis/contract.py @@ -167,7 +167,8 @@ LABELS: dict[str, str] = { def settings_problem(values: Mapping[str, Any]) -> str | None: """一组分析设置(或分析卡上的覆盖)哪里不对:不认识的键、类型不对、超出范围、k_alert 不大于 k_warn、 - max_history 小于 min_history。都对返回 None。说法给人看。""" + max_history 小于 min_history、concentration 小于 main_share(一段占了「主要」那么多还够不上「集中」,判定语会说「没有哪一段 + 占到」一个比它小的数)。没写的项按缺省补齐了比。都对返回 None。说法给人看(键的叫法取 LABELS)。""" for key, value in values.items(): if key not in RANGES: return f"不认识的分析设置:{key}" @@ -184,6 +185,8 @@ def settings_problem(values: Mapping[str, Any]) -> str | None: return f"{LABELS['k_alert']}要大于{LABELS['k_warn']}" if merged["max_history"] < merged["min_history"]: return f"{LABELS['max_history']}不能小于{LABELS['min_history']}" + if merged["concentration"] < merged["main_share"]: + return f"{LABELS['concentration']}不能小于{LABELS['main_share']}" return None diff --git a/backend/app/api/settings.py b/backend/app/api/settings.py index dda1f216..eca23947 100644 --- a/backend/app/api/settings.py +++ b/backend/app/api/settings.py @@ -907,13 +907,14 @@ def _analysis_item_ok(key: str, value: Any) -> bool: def analysis_values(stored: Any) -> dict[str, Any]: """库里存的(或刚写进来的)analysis 一组 → 补齐缺省;存坏了的项按缺省。两两之间的大小对不上的(「明显超出」不大于 - 「超出常规波动」、最多用几期历史小于至少几期)那一对都按缺省:只换掉一项,可能换出另一种对不上。""" + 「超出常规波动」、最多用几期历史小于至少几期、「集中」的合计占比小于「主要来源」的占比)那一对都按缺省:只换掉一项,可能 + 换出另一种对不上。""" stored = stored if isinstance(stored, dict) else {} out = dict(analysis_contract.DEFAULTS) for key in analysis_contract.DEFAULTS: if key in stored and _analysis_item_ok(key, stored[key]): out[key] = float(stored[key]) if analysis_contract.RANGES[key][0] is float else stored[key] - for pair in (("k_warn", "k_alert"), ("min_history", "max_history")): + for pair in (("k_warn", "k_alert"), ("min_history", "max_history"), ("main_share", "concentration")): if analysis_contract.settings_problem({k: out[k] for k in pair}) is not None: out.update({k: analysis_contract.DEFAULTS[k] for k in pair}) return out diff --git a/backend/tests/test_analysis_contract.py b/backend/tests/test_analysis_contract.py index cc1ea743..b72e240a 100644 --- a/backend/tests/test_analysis_contract.py +++ b/backend/tests/test_analysis_contract.py @@ -19,6 +19,10 @@ def test_settings_problems_are_spoken_with_labels_not_keys(): assert c.settings_problem({"main_share": True}) == "「主要来源」的占比要填数字" assert c.settings_problem({"main_share": 0.95}) == "「主要来源」的占比要在 0.3 到 0.9 之间" assert c.settings_problem({"max_history": 8, "min_history": 9}) == "最多用几期历史不能小于至少几期历史" + # 「主要」要的占比比「集中」的合计还高:一段占 60% 会判成分散,判定语却说「没有哪一段占到 90%」 + assert c.settings_problem({"main_share": 0.9, "concentration": 0.5}) == "「集中」的合计占比不能小于「主要来源」的占比" + assert c.settings_problem({"main_share": 0.85}) == "「集中」的合计占比不能小于「主要来源」的占比" # 缺省的 0.8 补齐了比 + assert c.settings_problem({"main_share": 0.8}) is None and c.settings_problem({"concentration": 0.5}) is None assert c.settings_problem({"nope": 1}) == "不认识的分析设置:nope" diff --git a/backend/tests/test_analysis_settings.py b/backend/tests/test_analysis_settings.py index 0b782c5a..1561a0af 100644 --- a/backend/tests/test_analysis_settings.py +++ b/backend/tests/test_analysis_settings.py @@ -69,6 +69,7 @@ def test_the_group_is_the_contract(): broken = {"main_share": "一半", "max_calls": 3.5, "k_warn": 4, "k_alert": 3.5, "min_history": 20, "max_history": 10, "table_top_k": 5, "trend_alpha": True} assert analysis_values(broken) == {**contract.DEFAULTS, "table_top_k": 5} + assert analysis_values({"main_share": 0.9, "concentration": 0.5}) == contract.DEFAULTS # 浮点的项存成浮点:发起时的快照、结论里记的阈值一个写法 assert analysis_values({"k_warn": 3, "k_alert": 4})["k_warn"] == 3.0 assert isinstance(analysis_values({"k_warn": 3, "k_alert": 4})["k_alert"], float) @@ -81,6 +82,8 @@ def test_the_group_is_the_contract(): ({"max_calls": 2.5}, f"{contract.LABELS['max_calls']}要填整数"), ({"k_warn": 3.5}, f"{contract.LABELS['k_alert']}要大于{contract.LABELS['k_warn']}"), ({"min_history": 60}, f"{contract.LABELS['max_history']}不能小于{contract.LABELS['min_history']}"), + ({"main_share": 0.9, "concentration": 0.5}, + f"{contract.LABELS['concentration']}不能小于{contract.LABELS['main_share']}"), ]) def test_problems_are_said_with_labels(value, why): assert why in (analysis_problem(value) or "")