File size: 1,114 Bytes
07c72fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def parse_int_from_formula(text: str) -> int:
    # Best-effort: look for pattern "A x B x C = N" and return N if present.
    # In production, prefer explicit numeric fields for each factor.
    import re
    m = re.search(r"=\s*([\d,]+)", text)
    if not m:
        return None
    return int(m.group(1).replace(",", ""))

def assert_valid(data: dict):
    cap = data.get("capacity", {})
    total_clients = cap.get("total_clients")
    formula = cap.get("formula_used", "")
    parsed_total = parse_int_from_formula(formula)
    if parsed_total is not None and total_clients is not None and parsed_total != total_clients:
        raise ValueError(f"MathValidator: capacity total {total_clients} disagrees with formula {parsed_total}")

    # Cost: total_for_n should equal total_per_client * N (if N is stated in formula or benchmark string).
    cost = data.get("cost", {})
    tpc = cost.get("total_per_client")
    tfn = cost.get("total_for_n")
    if None not in (tpc, tfn) and tpc >= 0 and tfn >= 0:
        # can't infer N safely; trust validators upstream. This check is minimal by design.
        pass