from functools import cached_property
import toml
import sys
+from collections import deque
class BudgetGraphBaseError(Exception):
immediate_children: Set["Node"]
budget_excluding_subtasks: Money
budget_including_subtasks: Money
+ fixed_budget_excluding_subtasks: Money
+ fixed_budget_including_subtasks: Money
nlnet_milestone: Optional[str]
def __init__(self, graph: "BudgetGraph", bug: Bug):
self.parent_id = getattr(bug, "cf_budget_parent", None)
self.immediate_children = set()
self.budget_excluding_subtasks = Money.from_str(bug.cf_budget)
+ self.fixed_budget_excluding_subtasks = self.budget_excluding_subtasks
self.budget_including_subtasks = Money.from_str(bug.cf_total_budget)
+ self.fixed_budget_including_subtasks = self.budget_including_subtasks
self.nlnet_milestone = bug.cf_nlnet_milestone
if self.nlnet_milestone == "---":
self.nlnet_milestone = None
yield from visitor(i)
return visitor(self)
+ def children_breadth_first(self) -> Iterable["Node"]:
+ q = deque(self.immediate_children)
+ while True:
+ try:
+ node = q.popleft()
+ except IndexError:
+ return
+ q.extend(node.immediate_children)
+ yield node
+
def __eq__(self, other):
return self.bug.id == other.bug.id
f"parent={parent}, "
f"budget_excluding_subtasks={self.budget_excluding_subtasks}, "
f"budget_including_subtasks={self.budget_including_subtasks}, "
+ f"fixed_budget_excluding_subtasks={self.fixed_budget_excluding_subtasks}, "
+ f"fixed_budget_including_subtasks={self.fixed_budget_including_subtasks}, "
f"nlnet_milestone={self.nlnet_milestone!r}, "
f"immediate_children={immediate_children!r}, "
f"payees={self.payees!r}")
f" #{self.root_bug_id}")
-class BudgetGraphMoneyMismatch(BudgetGraphError):
+class BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(BudgetGraphError):
def __init__(self, bug_id: int, root_bug_id: int,
expected_budget_excluding_subtasks: Money):
super().__init__(bug_id, root_bug_id)
f" {self.expected_budget_excluding_subtasks}")
+class BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(BudgetGraphError):
+ def __init__(self, bug_id: int, root_bug_id: int,
+ expected_budget_including_subtasks: Money):
+ super().__init__(bug_id, root_bug_id)
+ self.expected_budget_including_subtasks = \
+ expected_budget_including_subtasks
+
+ def __str__(self):
+ return (f"Budget assigned to task including subtasks "
+ f"(cf_total_budget field) doesn't match calculated value: "
+ f"bug #{self.bug_id}, calculated value"
+ f" {self.expected_budget_including_subtasks}")
+
+
class BudgetGraphNegativeMoney(BudgetGraphError):
def __str__(self):
return (f"Budget assigned to task is less than zero: "
class BudgetGraphPayeesMoneyMismatch(BudgetGraphError):
- def __init__(self, bug_id: int, root_bug_id: int, payees_total: Money):
+ def __init__(self, bug_id: int, root_bug_id: int, payees_total: Money,
+ expected_payees_total: Money):
super().__init__(bug_id, root_bug_id)
self.payees_total = payees_total
+ self.expected_payees_total = expected_payees_total
def __str__(self):
- return (f"Budget assigned to task excluding subtasks "
- f"(cf_budget field) doesn't match total value "
- f"assigned to payees (cf_payees_list): "
- f"bug #{self.bug_id}, calculated total"
- f" {self.payees_total}")
+ return (f"Total budget assigned to payees (cf_payees_list) doesn't "
+ f"match expected value: bug #{self.bug_id}, calculated total "
+ f"{self.payees_total}, expected value "
+ f"{self.expected_payees_total}")
class BudgetGraphNegativePayeeMoney(BudgetGraphError):
or node.budget_excluding_subtasks != 0:
errors.append(BudgetGraphMoneyWithNoMilestone(
node.bug.id, root.bug.id))
+
if node.nlnet_milestone != root.nlnet_milestone:
errors.append(BudgetGraphMilestoneMismatch(
node.bug.id, root.bug.id))
+
if node.budget_excluding_subtasks < 0 \
or node.budget_including_subtasks < 0:
errors.append(BudgetGraphNegativeMoney(
node.bug.id, root.bug.id))
- budget = node.budget_including_subtasks
+
+ subtasks_total = Money(0)
for child in node.immediate_children:
- budget -= child.budget_including_subtasks
- if node.budget_excluding_subtasks != budget:
- errors.append(BudgetGraphMoneyMismatch(
- node.bug.id, root.bug.id, budget))
+ subtasks_total += child.fixed_budget_including_subtasks
+
payees_total = Money(0)
for payee_key, payee_value in node.payees.items():
if payee_value < 0:
errors.append(BudgetGraphNegativePayeeMoney(
node.bug.id, root.bug.id, payee_key))
payees_total += payee_value
- if node.budget_excluding_subtasks != payees_total \
- and len(node.payees) != 0:
+
+ def set_including_from_excluding_and_error():
+ node.fixed_budget_including_subtasks = \
+ node.budget_excluding_subtasks + subtasks_total
+ errors.append(
+ BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
+ node.bug.id, root.bug.id,
+ node.fixed_budget_including_subtasks))
+
+ def set_including_from_payees_and_error():
+ node.fixed_budget_including_subtasks = \
+ payees_total + subtasks_total
+ errors.append(
+ BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
+ node.bug.id, root.bug.id,
+ node.fixed_budget_including_subtasks))
+
+ def set_excluding_from_including_and_error():
+ node.fixed_budget_excluding_subtasks = \
+ node.budget_including_subtasks - subtasks_total
+ errors.append(
+ BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
+ node.bug.id, root.bug.id,
+ node.fixed_budget_excluding_subtasks))
+
+ def set_excluding_from_payees_and_error():
+ node.fixed_budget_excluding_subtasks = \
+ payees_total
+ errors.append(
+ BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
+ node.bug.id, root.bug.id,
+ node.fixed_budget_excluding_subtasks))
+
+ def set_payees_from_including_and_error():
+ fixed_payees_total = \
+ node.budget_including_subtasks - subtasks_total
errors.append(BudgetGraphPayeesMoneyMismatch(
- node.bug.id, root.bug.id, payees_total))
+ node.bug.id, root.bug.id, payees_total, fixed_payees_total))
+
+ def set_payees_from_excluding_and_error():
+ fixed_payees_total = \
+ node.budget_excluding_subtasks
+ errors.append(BudgetGraphPayeesMoneyMismatch(
+ node.bug.id, root.bug.id, payees_total, fixed_payees_total))
+
+ payees_matches_including = \
+ node.budget_including_subtasks - subtasks_total == payees_total
+ payees_matches_excluding = \
+ node.budget_excluding_subtasks == payees_total
+ including_matches_excluding = \
+ node.budget_including_subtasks - subtasks_total \
+ == node.budget_excluding_subtasks
+
+ if payees_matches_including \
+ and payees_matches_excluding \
+ and including_matches_excluding:
+ pass # no error
+ elif payees_matches_including:
+ # can't have 2 match without all 3 matching
+ assert not payees_matches_excluding
+ assert not including_matches_excluding
+ if node.budget_including_subtasks == 0 and len(node.payees) == 0:
+ set_including_from_excluding_and_error()
+ else:
+ set_excluding_from_including_and_error()
+ elif payees_matches_excluding:
+ # can't have 2 match without all 3 matching
+ assert not payees_matches_including
+ assert not including_matches_excluding
+ if node.budget_excluding_subtasks == 0 and len(node.payees) == 0:
+ if node.budget_including_subtasks == 0:
+ set_including_from_excluding_and_error()
+ else:
+ set_excluding_from_including_and_error()
+ else:
+ set_including_from_excluding_and_error()
+ elif including_matches_excluding:
+ # can't have 2 match without all 3 matching
+ assert not payees_matches_including
+ assert not payees_matches_excluding
+ if len(node.payees) == 0:
+ pass # no error -- payees is just not set
+ elif node.budget_excluding_subtasks == 0 \
+ and node.budget_including_subtasks == 0:
+ set_excluding_from_payees_and_error()
+ set_including_from_payees_and_error()
+ else:
+ set_payees_from_excluding_and_error()
+ else:
+ # nothing matches
+ if len(node.payees) == 0:
+ # payees unset -- don't need to set payees
+ if node.budget_including_subtasks == 0:
+ set_including_from_excluding_and_error()
+ else:
+ set_excluding_from_including_and_error()
+ elif node.budget_excluding_subtasks == 0 \
+ and node.budget_including_subtasks == 0:
+ set_excluding_from_payees_and_error()
+ set_including_from_payees_and_error()
+ elif node.budget_excluding_subtasks == 0:
+ set_excluding_from_including_and_error()
+ set_payees_from_including_and_error()
+ elif node.budget_including_subtasks == 0:
+ set_including_from_excluding_and_error()
+ set_payees_from_excluding_and_error()
+ else:
+ set_including_from_excluding_and_error()
+ set_payees_from_excluding_and_error()
def get_errors(self) -> List[BudgetGraphBaseError]:
errors = []
for root in roots:
try:
- self._get_node_errors(root, root, errors)
- for child in root.children():
+ for child in reversed(list(root.children_breadth_first())):
try:
self._get_node_errors(root, child, errors)
except BudgetGraphBaseError as e:
errors.append(e)
+ self._get_node_errors(root, root, errors)
except BudgetGraphBaseError as e:
errors.append(e)
return errors