补充Mock全部兼容规则命中计划

This commit is contained in:
wkc
2026-03-22 16:14:38 +08:00
parent e6809c67fe
commit 6bfe7f83f2
2 changed files with 85 additions and 3 deletions

View File

@@ -48,6 +48,8 @@ PHASE2_BASELINE_RULE_CODES = [
"SUPPLIER_CONCENTRATION",
]
RULE_CONFLICT_GROUPS = []
@dataclass
class FileRecord:
@@ -179,8 +181,8 @@ class FileService:
selected_codes = set(rng.sample(rule_codes, rng.randint(min_count, max_count)))
return [rule_code for rule_code in rule_codes if rule_code in selected_codes]
def _build_rule_hit_plan(self, log_id: int) -> dict:
"""基于 logId 生成稳定的规则命中计划。"""
def _build_subset_rule_hit_plan(self, log_id: int) -> dict:
"""基于 logId 生成稳定的规则子集命中计划。"""
rng = random.Random(f"rule-plan:{log_id}")
return {
"large_transaction_hit_rules": self._pick_rule_subset(
@@ -195,6 +197,41 @@ class FileService:
),
}
def _build_all_compatible_rule_hit_plan(self) -> dict:
"""生成全部兼容规则命中计划。"""
return {
"large_transaction_hit_rules": list(LARGE_TRANSACTION_RULE_CODES),
"phase1_hit_rules": list(PHASE1_RULE_CODES),
"phase2_statement_hit_rules": list(PHASE2_STATEMENT_RULE_CODES),
"phase2_baseline_hit_rules": list(PHASE2_BASELINE_RULE_CODES),
}
def _apply_conflict_groups(self, rule_plan: dict) -> dict:
"""按显式互斥组裁剪规则计划,同组仅保留固定优先级的首个规则。"""
resolved_plan = {plan_key: list(rule_codes) for plan_key, rule_codes in rule_plan.items()}
for plan_key, rule_codes in resolved_plan.items():
filtered_codes = list(rule_codes)
for conflict_group in RULE_CONFLICT_GROUPS:
kept_rule_code = next(
(rule_code for rule_code in conflict_group if rule_code in filtered_codes),
None,
)
if kept_rule_code is None:
continue
filtered_codes = [
rule_code
for rule_code in filtered_codes
if rule_code == kept_rule_code or rule_code not in conflict_group
]
resolved_plan[plan_key] = filtered_codes
return resolved_plan
def _build_rule_hit_plan(self, log_id: int) -> dict:
"""按配置模式生成规则命中计划。"""
if settings.RULE_HIT_MODE == "all":
return self._apply_conflict_groups(self._build_all_compatible_rule_hit_plan())
return self._build_subset_rule_hit_plan(log_id)
def _create_file_record(
self,
*,