1 from budget_sync
.ordered_set
import OrderedSet
2 from bugzilla
.bug
import Bug
3 from typing
import Callable
, Set
, Dict
, Iterable
, Optional
, List
, Tuple
, Union
, Any
4 from budget_sync
.util
import BugStatus
, PrettyPrinter
5 from budget_sync
.money
import Money
6 from budget_sync
.config
import Config
, Person
, Milestone
10 from collections
import deque
11 from datetime
import date
, time
, datetime
13 from functools
import cached_property
14 except ImportError: # :nocov:
15 # compatibility with python < 3.8
16 from cached_property
import cached_property
# :nocov:
19 class BudgetGraphBaseError(Exception):
23 class BudgetGraphParseError(BudgetGraphBaseError
):
24 def __init__(self
, bug_id
: int):
28 class BudgetGraphPayeesParseError(BudgetGraphParseError
):
29 def __init__(self
, bug_id
: int, msg
: str):
30 super().__init
__(bug_id
)
34 return f
"Failed to parse cf_payees_list field of " \
35 f
"bug #{self.bug_id}: {self.msg}"
38 class BudgetGraphUnknownAssignee(BudgetGraphParseError
):
39 def __init__(self
, bug_id
: int, assignee
: str):
40 super().__init
__(bug_id
)
41 self
.assignee
= assignee
44 return f
"Bug #{self.bug_id} is assigned to an unknown person: " \
48 class BudgetGraphLoopError(BudgetGraphBaseError
):
49 def __init__(self
, bug_ids
: List
[int]):
50 self
.bug_ids
= bug_ids
53 retval
= f
"Detected Loop in Budget Graph: #{self.bug_ids[-1]} -> "
54 retval
+= " -> ".join((f
"#{i}" for i
in self
.bug_ids
))
58 class _NodeSimpleReprWrapper
:
59 def __init__(self
, node
: "Node"):
63 return f
"#{self.node.bug.id}"
65 def __lt__(self
, other
):
67 return self
.node
.bug
.id < other
.node
.bug
.id
70 class PayeeState(enum
.Enum
):
71 NotYetSubmitted
= "not yet submitted"
72 Submitted
= "submitted"
76 _Date
= Union
[date
, datetime
]
79 def _parse_money_from_toml(value
: Any
) -> Money
:
80 if not isinstance(value
, (int, str)):
81 msg
= f
"monetary amount is not a string or integer " \
82 f
"(to use fractional amounts such as 123.45, write " \
83 f
"\"123.45\"): {value!r}"
88 def _parse_date_time_or_none_from_toml(value
: Any
) -> Optional
[_Date
]:
89 if value
is None or isinstance(value
, (date
, datetime
)):
91 elif isinstance(value
, time
):
92 msg
= f
"just a time of day by itself is not enough," \
93 f
" a date must be included: {str(value)}"
95 elif isinstance(value
, bool):
96 msg
= f
"invalid date: {str(value).lower()}"
98 elif isinstance(value
, (str, int, float)):
99 msg
= f
"invalid date: {value!r}"
100 raise ValueError(msg
)
102 msg
= f
"invalid date"
103 raise ValueError(msg
)
111 paid
: Optional
[_Date
],
112 submitted
: Optional
[_Date
]):
114 self
.payee_key
= payee_key
117 self
.submitted
= submitted
120 def payee(self
) -> Person
:
122 return self
.node
.graph
.config
.all_names
[self
.payee_key
]
124 msg
= f
"unknown payee name: {self.payee_key!r} is not the name " \
125 f
"or an alias of any known person"
126 raise BudgetGraphPayeesParseError(self
.node
.bug
.id, msg
) \
127 .with_traceback(sys
.exc_info()[2])
131 if self
.paid
is not None:
132 return PayeeState
.Paid
133 if self
.submitted
is not None:
134 return PayeeState
.Submitted
135 return PayeeState
.NotYetSubmitted
138 def _from_toml(node
: "Node", payee_key
: str, toml_value
: Any
) -> "Payment":
141 known_keys
= ("paid", "submitted", "amount")
142 if isinstance(toml_value
, dict):
144 amount
= toml_value
['amount']
146 msg
= f
"value for key {payee_key!r} is missing the " \
147 f
"`amount` field which is required"
148 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
) \
149 .with_traceback(sys
.exc_info()[2])
150 for k
, v
in toml_value
.items():
151 if k
in ("paid", "submitted"):
153 parsed_value
= _parse_date_time_or_none_from_toml(v
)
154 except ValueError as e
:
155 msg
= f
"failed to parse `{k}` field for" \
156 f
" key {payee_key!r}: {e}"
157 raise BudgetGraphPayeesParseError(
159 .with_traceback(sys
.exc_info()[2])
163 assert k
== "submitted"
164 submitted
= parsed_value
165 if k
not in known_keys
:
166 msg
= f
"value for key {payee_key!r} has an unknown" \
168 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
) \
169 .with_traceback(sys
.exc_info()[2])
171 paid
= _parse_date_time_or_none_from_toml(
172 toml_value
.get('paid'))
173 except ValueError as e
:
174 msg
= f
"failed to parse `paid` field for" \
175 f
" key {payee_key!r}: {e}"
176 raise BudgetGraphPayeesParseError(
178 .with_traceback(sys
.exc_info()[2])
180 submitted
= _parse_date_time_or_none_from_toml(
181 toml_value
.get('submitted'))
182 except ValueError as e
:
183 msg
= f
"failed to parse `submitted` field for" \
184 f
" key {payee_key!r}: {e}"
185 raise BudgetGraphPayeesParseError(
187 .with_traceback(sys
.exc_info()[2])
188 elif isinstance(toml_value
, (int, str, float)):
189 # float included for better error messages
192 msg
= f
"value for key {payee_key!r} is invalid -- it should " \
193 f
"either be a monetary value or a table"
194 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
)
196 amount
= _parse_money_from_toml(amount
)
197 except ValueError as e
:
198 msg
= f
"failed to parse monetary amount for key {payee_key!r}: {e}"
199 raise BudgetGraphPayeesParseError(
201 .with_traceback(sys
.exc_info()[2])
202 return Payment(node
=node
, payee_key
=payee_key
, amount
=amount
,
203 paid
=paid
, submitted
=submitted
)
207 payee
= f
"Person<{self.payee.identifier!r}>"
208 except BudgetGraphBaseError
:
209 payee
= "<unknown person>"
210 return (f
"Payment(node={_NodeSimpleReprWrapper(self.node)}, "
212 f
"payee_key={self.payee_key!r}, "
213 f
"amount={self.amount}, "
214 f
"state={self.state.name}, "
215 f
"paid={str(self.paid)}, "
216 f
"submitted={str(self.submitted)})")
220 class PaymentSummaryState(enum
.Enum
):
221 Submitted
= PayeeState
.Submitted
222 Paid
= PayeeState
.Paid
223 NotYetSubmitted
= PayeeState
.NotYetSubmitted
227 class PaymentSummary
:
228 total_submitted
: Money
229 """includes amount paid"""
231 def __init__(self
, payments
: Iterable
[Payment
]):
232 self
.payments
= tuple(payments
)
233 self
.total
= Money(0)
234 self
.total_paid
= Money(0)
235 self
.total_submitted
= Money(0)
236 self
.submitted_date
= None
237 self
.paid_date
= None
238 self
.not_submitted
= []
240 for payment
in self
.payments
:
241 if summary_state
is None:
242 summary_state
= PaymentSummaryState(payment
.state
)
243 self
.submitted_date
= payment
.submitted
244 self
.paid_date
= payment
.paid
245 elif summary_state
!= PaymentSummaryState(payment
.state
) \
246 or self
.submitted_date
!= payment
.submitted \
247 or self
.paid_date
!= payment
.paid
:
248 summary_state
= PaymentSummaryState
.Inconsistent
249 self
.paid_date
= None
250 self
.submitted_date
= None
251 self
.total
+= payment
.amount
252 if payment
.state
is PayeeState
.Submitted
:
253 self
.total_submitted
+= payment
.amount
254 elif payment
.state
is PayeeState
.Paid
:
255 self
.total_submitted
+= payment
.amount
256 self
.total_paid
+= payment
.amount
258 assert payment
.state
is PayeeState
.NotYetSubmitted
259 self
.not_submitted
.append(payment
.node
.bug
.id)
260 if summary_state
is None:
261 self
.state
= PaymentSummaryState
.NotYetSubmitted
263 self
.state
= summary_state
265 def get_not_submitted(self
):
266 return self
.not_submitted
268 def __repr__(self
) -> str:
269 return (f
"PaymentSummary(total={self.total}, "
270 f
"total_paid={self.total_paid}, "
271 f
"total_submitted={self.total_submitted}, "
272 f
"submitted_date={self.submitted_date}, "
273 f
"paid_date={self.paid_date}, "
274 f
"state={self.state}, "
275 f
"payments={self.payments})")
277 def __pretty_print__(self
, pp
: PrettyPrinter
):
278 with pp
.type_pp("PaymentSummary") as tpp
:
279 tpp
.field("total", self
.total
)
280 tpp
.field("total_submitted", self
.total_submitted
)
281 tpp
.field("submitted_date", self
.submitted_date
)
282 tpp
.field("paid_date", self
.paid_date
)
283 tpp
.field("state", self
.state
)
284 tpp
.field("payments", self
.payments
)
287 class BudgetGraphUnknownMilestone(BudgetGraphParseError
):
288 def __init__(self
, bug_id
: int, milestone_str
: str):
289 super().__init
__(bug_id
)
290 self
.milestone_str
= milestone_str
293 return f
"failed to parse cf_nlnet_milestone field of bug " \
294 f
"#{self.bug_id}: unknown milestone: {self.milestone_str!r}"
297 class BudgetGraphUnknownStatus(BudgetGraphParseError
):
298 def __init__(self
, bug_id
: int, status_str
: str):
299 super().__init
__(bug_id
)
300 self
.status_str
= status_str
303 return f
"failed to parse status field of bug " \
304 f
"#{self.bug_id}: unknown status: {self.status_str!r}"
310 parent_id
: Optional
[int]
311 immediate_children
: OrderedSet
["Node"]
312 budget_excluding_subtasks
: Money
313 budget_including_subtasks
: Money
314 fixed_budget_excluding_subtasks
: Money
315 fixed_budget_including_subtasks
: Money
316 milestone_str
: Optional
[str]
318 def __init__(self
, graph
: "BudgetGraph", bug
: Bug
):
321 self
.parent_id
= getattr(bug
, "cf_budget_parent", None)
322 self
.immediate_children
= OrderedSet()
323 self
.budget_excluding_subtasks
= Money
.from_str(bug
.cf_budget
)
324 self
.fixed_budget_excluding_subtasks
= self
.budget_excluding_subtasks
325 self
.budget_including_subtasks
= Money
.from_str(bug
.cf_total_budget
)
326 self
.fixed_budget_including_subtasks
= self
.budget_including_subtasks
327 self
.milestone_str
= bug
.cf_nlnet_milestone
328 if self
.milestone_str
== "---":
329 self
.milestone_str
= None
332 def status(self
) -> BugStatus
:
334 return BugStatus
.cast(self
.bug
.status
)
336 new_err
= BudgetGraphUnknownStatus(self
.bug
.id, self
.bug
.status
)
337 raise new_err
.with_traceback(sys
.exc_info()[2])
340 def assignee(self
) -> Person
:
342 return self
.graph
.config
.all_names
[self
.bug
.assigned_to
]
344 raise BudgetGraphUnknownAssignee(self
.bug
.id,
345 self
.bug
.assigned_to
) \
346 .with_traceback(sys
.exc_info()[2])
349 def bug_url(self
) -> str:
350 return f
"{self.graph.config.bugzilla_url_stripped}/show_bug.cgi?" \
354 def milestone(self
) -> Optional
[Milestone
]:
355 if self
.milestone_str
is None:
358 return self
.graph
.config
.milestones
[self
.milestone_str
]
360 new_err
= BudgetGraphUnknownMilestone(
361 self
.bug
.id, self
.milestone_str
)
362 raise new_err
.with_traceback(sys
.exc_info()[2])
365 def payments(self
) -> Dict
[str, Payment
]:
367 parsed
= toml
.loads(self
.bug
.cf_payees_list
)
368 except toml
.TomlDecodeError
as e
:
369 new_err
= BudgetGraphPayeesParseError(
370 self
.bug
.id, f
"TOML parse error: {e}")
371 raise new_err
.with_traceback(sys
.exc_info()[2])
373 for key
, value
in parsed
.items():
374 if not isinstance(key
, str):
375 raise BudgetGraphPayeesParseError(
376 self
.bug
.id, f
"key is not a string: {key!r}")
377 retval
[key
] = Payment
._from
_toml
(self
, key
, value
)
381 def resolved_payments(self
) -> Dict
[Person
, List
[Payment
]]:
382 retval
: Dict
[Person
, List
[Payment
]] = {}
383 for payment
in self
.payments
.values():
384 if payment
.payee
not in retval
:
385 retval
[payment
.payee
] = []
386 retval
[payment
.payee
].append(payment
)
390 def payment_summaries(self
) -> Dict
[Person
, PaymentSummary
]:
391 return {person
: PaymentSummary(payments
)
392 for person
, payments
in self
.resolved_payments
.items()}
395 def submitted_excluding_subtasks(self
) -> Money
:
397 for payment
in self
.payments
.values():
398 if payment
.submitted
is not None or payment
.paid
is not None:
399 retval
+= payment
.amount
403 def paid_excluding_subtasks(self
) -> Money
:
405 for payment
in self
.payments
.values():
406 if payment
.paid
is not None:
407 retval
+= payment
.amount
411 def submitted_including_subtasks(self
) -> Money
:
412 retval
= self
.submitted_excluding_subtasks
413 for i
in self
.immediate_children
:
414 retval
+= i
.submitted_including_subtasks
418 def paid_including_subtasks(self
) -> Money
:
419 retval
= self
.paid_excluding_subtasks
420 for i
in self
.immediate_children
:
421 retval
+= i
.paid_including_subtasks
425 def parent(self
) -> Optional
["Node"]:
426 if self
.parent_id
is not None:
427 return self
.graph
.nodes
[self
.parent_id
]
430 def parents(self
) -> Iterable
["Node"]:
432 while parent
is not None:
434 parent
= parent
.parent
436 def _raise_loop_error(self
):
438 for parent
in self
.parents():
439 bug_ids
.append(parent
.bug
.id)
442 raise BudgetGraphLoopError(bug_ids
)
445 def root(self
) -> "Node":
446 # also checks for loop errors
448 for parent
in self
.parents():
451 self
._raise
_loop
_error
()
455 def is_in_nlnet_mou(self
):
456 """returns true if this bugreport is an immediate child of a top-level
457 milestone. it does *not* return true for the top-level bug itself
458 because only the immediate children comprise the MoU.
461 if self
.parent
is not None and self
.milestone
is not None:
462 return self
.parent
.bug
.id == self
.milestone
.canonical_bug_id
463 except BudgetGraphBaseError
:
468 def closest_bug_in_mou(self
) -> Optional
["Node"]:
469 """returns the closest bug that is in a NLNet MoU, searching only in
470 this bug and parents.
472 if self
.is_in_nlnet_mou
:
474 for parent
in self
.parents():
475 if parent
.is_in_nlnet_mou
:
479 def children(self
) -> Iterable
["Node"]:
480 def visitor(node
: Node
) -> Iterable
[Node
]:
481 for i
in node
.immediate_children
:
483 yield from visitor(i
)
486 def children_breadth_first(self
) -> Iterable
["Node"]:
487 q
= deque(self
.immediate_children
)
493 q
.extend(node
.immediate_children
)
496 def __eq__(self
, other
):
497 return self
.bug
.id == other
.bug
.id
502 def __pretty_print__(self
, pp
: PrettyPrinter
):
503 with pp
.type_pp("Node") as tpp
:
504 tpp
.field("graph", ...)
505 tpp
.field("id", _NodeSimpleReprWrapper(self
))
506 tpp
.try_field("root",
507 lambda: _NodeSimpleReprWrapper(self
.root
),
508 BudgetGraphLoopError
)
509 parent
= f
"#{self.parent_id}" if self
.parent_id
is not None else None
510 tpp
.field("parent", parent
)
511 tpp
.field("budget_excluding_subtasks",
512 self
.budget_excluding_subtasks
)
513 tpp
.field("budget_including_subtasks",
514 self
.budget_including_subtasks
)
515 tpp
.field("fixed_budget_excluding_subtasks",
516 self
.fixed_budget_excluding_subtasks
)
517 tpp
.field("fixed_budget_including_subtasks",
518 self
.fixed_budget_including_subtasks
)
519 tpp
.field("milestone_str", self
.milestone_str
)
520 tpp
.field("is_in_nlnet_mou", self
.is_in_nlnet_mou
)
521 tpp
.try_field("milestone", lambda: self
.milestone
,
522 BudgetGraphBaseError
)
523 immediate_children
= [_NodeSimpleReprWrapper(i
)
524 for i
in self
.immediate_children
]
525 tpp
.field("immediate_children", immediate_children
)
526 tpp
.try_field("payments",
527 lambda: list(self
.payments
.values()),
528 BudgetGraphBaseError
)
530 status
= repr(self
.status
)
531 except BudgetGraphBaseError
:
532 status
= f
"<unknown status: {self.bug.status!r}>"
533 tpp
.field("status", status
)
535 assignee
= f
"Person<{self.assignee.identifier!r}>"
536 except BudgetGraphBaseError
:
537 assignee
= f
"<unknown assignee: {self.bug.assigned_to!r}>"
538 tpp
.field("assignee", assignee
)
539 tpp
.try_field("resolved_payments",
540 lambda: self
.resolved_payments
,
541 BudgetGraphBaseError
)
542 tpp
.try_field("payment_summaries",
543 lambda: self
.payment_summaries
,
544 BudgetGraphBaseError
)
548 root
= _NodeSimpleReprWrapper(self
.root
)
549 except BudgetGraphLoopError
:
550 root
= "<loop error>"
552 milestone
= repr(self
.milestone
)
553 except BudgetGraphBaseError
:
554 milestone
= "<unknown milestone>"
556 status
= repr(self
.status
)
557 except BudgetGraphBaseError
:
558 status
= f
"<unknown status: {self.bug.status!r}>"
560 assignee
= f
"Person<{self.assignee.identifier!r}>"
561 except BudgetGraphBaseError
:
562 assignee
= f
"<unknown assignee: {self.bug.assigned_to!r}>"
563 immediate_children
= []
564 for i
in self
.immediate_children
:
565 immediate_children
.append(_NodeSimpleReprWrapper(i
))
566 immediate_children
.sort()
567 parent
= f
"#{self.parent_id}" if self
.parent_id
is not None else None
568 payments
= list(self
.payments
.values())
569 resolved_payments
= self
.resolved_payments
570 payment_summaries
= self
.payment_summaries
571 return (f
"Node(graph=..., "
572 f
"id={_NodeSimpleReprWrapper(self)}, "
575 f
"budget_excluding_subtasks={self.budget_excluding_subtasks}, "
576 f
"budget_including_subtasks={self.budget_including_subtasks}, "
577 f
"fixed_budget_excluding_subtasks={self.fixed_budget_excluding_subtasks}, "
578 f
"fixed_budget_including_subtasks={self.fixed_budget_including_subtasks}, "
579 f
"milestone_str={self.milestone_str!r}, "
580 f
"is_in_nlnet_mou={self.is_in_nlnet_mou!r}, "
581 f
"milestone={milestone}, "
582 f
"immediate_children={immediate_children!r}, "
583 f
"payments={payments!r}, "
585 f
"assignee={assignee}, "
586 f
"resolved_payments={resolved_payments!r}, "
587 f
"payment_summaries={payment_summaries!r})")
590 class BudgetGraphError(BudgetGraphBaseError
):
591 def __init__(self
, bug_id
: int, root_bug_id
: int):
593 self
.root_bug_id
= root_bug_id
596 class BudgetGraphMoneyWithNoMilestone(BudgetGraphError
):
598 return (f
"Bug assigned money but without"
599 f
" any assigned milestone: #{self.bug_id}")
602 class BudgetGraphMilestoneMismatch(BudgetGraphError
):
604 return (f
"Bug's assigned milestone doesn't match the milestone "
605 f
"assigned to the root bug: descendant bug"
606 f
" #{self.bug_id}, root bug"
607 f
" #{self.root_bug_id}")
610 class BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(BudgetGraphError
):
611 def __init__(self
, bug_id
: int, root_bug_id
: int,
612 expected_budget_excluding_subtasks
: Money
):
613 super().__init
__(bug_id
, root_bug_id
)
614 self
.expected_budget_excluding_subtasks
= \
615 expected_budget_excluding_subtasks
618 return (f
"Budget assigned to task excluding subtasks "
619 f
"(cf_budget field) doesn't match calculated value: "
620 f
"bug #{self.bug_id}, calculated value"
621 f
" {self.expected_budget_excluding_subtasks}")
624 class BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(BudgetGraphError
):
625 def __init__(self
, bug_id
: int, root_bug_id
: int,
626 expected_budget_including_subtasks
: Money
):
627 super().__init
__(bug_id
, root_bug_id
)
628 self
.expected_budget_including_subtasks
= \
629 expected_budget_including_subtasks
632 return (f
"Budget assigned to task including subtasks "
633 f
"(cf_total_budget field) doesn't match calculated value: "
634 f
"bug #{self.bug_id}, calculated value"
635 f
" {self.expected_budget_including_subtasks}")
638 class BudgetGraphNegativeMoney(BudgetGraphError
):
640 return (f
"Budget assigned to task is less than zero: "
641 f
"bug #{self.bug_id}")
644 class BudgetGraphPayeesMoneyMismatch(BudgetGraphError
):
645 def __init__(self
, bug_id
: int, root_bug_id
: int, payees_total
: Money
,
646 expected_payees_total
: Money
):
647 super().__init
__(bug_id
, root_bug_id
)
648 self
.payees_total
= payees_total
649 self
.expected_payees_total
= expected_payees_total
652 return (f
"Total budget assigned to payees (cf_payees_list) doesn't "
653 f
"match expected value: bug #{self.bug_id}, calculated total "
654 f
"{self.payees_total}, expected value "
655 f
"{self.expected_payees_total}")
658 class BudgetGraphNegativePayeeMoney(BudgetGraphError
):
659 def __init__(self
, bug_id
: int, root_bug_id
: int, payee_key
: str):
660 super().__init
__(bug_id
, root_bug_id
)
661 self
.payee_key
= payee_key
664 return (f
"Budget assigned to payee for task is less than zero: "
665 f
"bug #{self.bug_id}, payee {self.payee_key!r}")
668 class BudgetGraphIncorrectRootForMilestone(BudgetGraphError
):
669 def __init__(self
, bug_id
: int, milestone
: str, milestone_canonical_bug_id
: int):
670 super().__init
__(bug_id
, bug_id
)
671 self
.milestone
= milestone
672 self
.milestone_canonical_bug_id
= milestone_canonical_bug_id
675 return (f
"Bug #{self.bug_id} is not the canonical root bug for "
676 f
"assigned milestone {self.milestone!r} but has no parent "
677 f
"bug set: the milestone's canonical root bug is "
678 f
"#{self.milestone_canonical_bug_id}")
682 nodes
: Dict
[int, Node
]
684 def __init__(self
, bugs
: Iterable
[Bug
], config
: Config
):
688 self
.nodes
[bug
.id] = Node(self
, bug
)
689 for node
in self
.nodes
.values():
690 if node
.parent
is None:
692 node
.parent
.immediate_children
.add(node
)
693 # useful debug prints
695 # node = self.nodes[bug.id]
696 # print ("bug added", bug.id, node, node.parent.immediate_children)
699 def roots(self
) -> OrderedSet
[Node
]:
701 for node
in self
.nodes
.values():
702 # calling .root also checks for loop errors
707 def _get_node_errors(self
, root
: Node
, node
: Node
,
708 errors
: List
[BudgetGraphBaseError
]):
709 if node
.milestone_str
is None:
710 if node
.budget_including_subtasks
!= 0 \
711 or node
.budget_excluding_subtasks
!= 0:
712 errors
.append(BudgetGraphMoneyWithNoMilestone(
713 node
.bug
.id, root
.bug
.id))
716 # check for milestone errors
718 if root
== node
and node
.milestone
is not None:
719 if node
.milestone
.canonical_bug_id
!= node
.bug
.id:
720 if node
.budget_including_subtasks
!= 0 \
721 or node
.budget_excluding_subtasks
!= 0:
722 errors
.append(BudgetGraphIncorrectRootForMilestone(
723 node
.bug
.id, node
.milestone
.identifier
,
724 node
.milestone
.canonical_bug_id
726 except BudgetGraphBaseError
as e
:
730 # check for status errors
732 except BudgetGraphBaseError
as e
:
736 # check for assignee errors
738 except BudgetGraphBaseError
as e
:
741 if node
.milestone_str
!= root
.milestone_str
:
742 errors
.append(BudgetGraphMilestoneMismatch(
743 node
.bug
.id, root
.bug
.id))
745 if node
.budget_excluding_subtasks
< 0 \
746 or node
.budget_including_subtasks
< 0:
747 errors
.append(BudgetGraphNegativeMoney(
748 node
.bug
.id, root
.bug
.id))
751 subtasks_total
= Money(0)
752 for child
in node
.immediate_children
:
753 subtasks_total
+= child
.fixed_budget_including_subtasks
754 childlist
.append(child
.bug
.id)
755 # useful debug prints
756 # print ("subtask total", node.bug.id, root.bug.id, subtasks_total,
759 payees_total
= Money(0)
760 payee_payments
: Dict
[Person
, List
[Payment
]] = {}
761 for payment
in node
.payments
.values():
762 if payment
.amount
< 0:
763 errors
.append(BudgetGraphNegativePayeeMoney(
764 node
.bug
.id, root
.bug
.id, payment
.payee_key
))
765 payees_total
+= payment
.amount
767 # check for payee errors
769 previous_payment
= payee_payments
.get(payment
.payee
)
770 if previous_payment
is not None:
771 payee_payments
[payment
.payee
].append(payment
)
773 payee_payments
[payment
.payee
] = [payment
]
774 except BudgetGraphBaseError
as e
:
777 def set_including_from_excluding_and_error():
778 node
.fixed_budget_including_subtasks
= \
779 node
.budget_excluding_subtasks
+ subtasks_total
781 BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
782 node
.bug
.id, root
.bug
.id,
783 node
.fixed_budget_including_subtasks
))
785 def set_including_from_payees_and_error():
786 node
.fixed_budget_including_subtasks
= \
787 payees_total
+ subtasks_total
789 BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
790 node
.bug
.id, root
.bug
.id,
791 node
.fixed_budget_including_subtasks
))
793 def set_excluding_from_including_and_error():
794 v
= node
.budget_including_subtasks
- subtasks_total
796 return set_including_from_excluding_and_error()
797 node
.fixed_budget_excluding_subtasks
= v
799 BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
800 node
.bug
.id, root
.bug
.id,
801 node
.fixed_budget_excluding_subtasks
))
803 def set_excluding_from_payees_and_error():
804 node
.fixed_budget_excluding_subtasks
= \
807 BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
808 node
.bug
.id, root
.bug
.id,
809 node
.fixed_budget_excluding_subtasks
))
811 def set_payees_from_including_and_error():
812 fixed_payees_total
= \
813 node
.budget_including_subtasks
- subtasks_total
814 errors
.append(BudgetGraphPayeesMoneyMismatch(
815 node
.bug
.id, root
.bug
.id, payees_total
, fixed_payees_total
))
817 def set_payees_from_excluding_and_error():
818 fixed_payees_total
= \
819 node
.budget_excluding_subtasks
820 errors
.append(BudgetGraphPayeesMoneyMismatch(
821 node
.bug
.id, root
.bug
.id, payees_total
, fixed_payees_total
))
823 payees_matches_including
= \
824 node
.budget_including_subtasks
- subtasks_total
== payees_total
825 payees_matches_excluding
= \
826 node
.budget_excluding_subtasks
== payees_total
827 including_matches_excluding
= \
828 node
.budget_including_subtasks
- subtasks_total \
829 == node
.budget_excluding_subtasks
831 if payees_matches_including \
832 and payees_matches_excluding \
833 and including_matches_excluding
:
835 elif payees_matches_including
:
836 # can't have 2 match without all 3 matching
837 assert not payees_matches_excluding
838 assert not including_matches_excluding
839 if node
.budget_including_subtasks
== 0 and len(node
.payments
) == 0:
840 set_including_from_excluding_and_error()
842 set_excluding_from_including_and_error()
843 elif payees_matches_excluding
:
844 # can't have 2 match without all 3 matching
845 assert not payees_matches_including
846 assert not including_matches_excluding
847 if node
.budget_excluding_subtasks
== 0 and len(node
.payments
) == 0:
848 if node
.budget_including_subtasks
== 0:
849 set_including_from_excluding_and_error()
851 set_excluding_from_including_and_error()
853 set_including_from_excluding_and_error()
854 elif including_matches_excluding
:
855 # can't have 2 match without all 3 matching
856 assert not payees_matches_including
857 assert not payees_matches_excluding
858 if len(node
.payments
) == 0:
859 pass # no error -- payees is just not set
860 elif node
.budget_excluding_subtasks
== 0 \
861 and node
.budget_including_subtasks
== 0:
862 set_excluding_from_payees_and_error()
863 set_including_from_payees_and_error()
865 set_payees_from_excluding_and_error()
868 if len(node
.payments
) == 0:
869 # payees unset -- don't need to set payees
870 if node
.budget_including_subtasks
== 0:
871 set_including_from_excluding_and_error()
873 set_excluding_from_including_and_error()
874 elif node
.budget_excluding_subtasks
== 0 \
875 and node
.budget_including_subtasks
== 0:
876 set_excluding_from_payees_and_error()
877 set_including_from_payees_and_error()
878 elif node
.budget_excluding_subtasks
== 0:
879 set_excluding_from_including_and_error()
880 set_payees_from_including_and_error()
881 elif node
.budget_including_subtasks
== 0:
882 set_including_from_excluding_and_error()
883 set_payees_from_excluding_and_error()
885 set_including_from_excluding_and_error()
886 set_payees_from_excluding_and_error()
888 def get_errors(self
) -> List
[BudgetGraphBaseError
]:
892 except BudgetGraphBaseError
as e
:
898 for child
in reversed(list(root
.children_breadth_first())):
900 self
._get
_node
_errors
(root
, child
, errors
)
901 except BudgetGraphBaseError
as e
:
903 self
._get
_node
_errors
(root
, root
, errors
)
904 except BudgetGraphBaseError
as e
:
909 def assigned_nodes(self
) -> Dict
[Person
, List
[Node
]]:
910 retval
: Dict
[Person
, List
[Node
]]
911 retval
= {person
: [] for person
in self
.config
.people
.values()}
912 for node
in self
.nodes
.values():
913 retval
[node
.assignee
].append(node
)
917 def assigned_nodes_for_milestones(self
) -> Dict
[Milestone
, List
[Node
]]:
918 retval
: Dict
[Milestone
, List
[Node
]]
919 retval
= {milestone
: []
920 for milestone
in self
.config
.milestones
.values()}
921 for node
in self
.nodes
.values():
922 if node
.milestone
is not None:
923 retval
[node
.milestone
].append(node
)
927 def milestone_payments(self
) -> Dict
[Milestone
, List
[Payment
]]:
928 retval
: Dict
[Milestone
, List
[Payment
]] = {
929 milestone
: [] for milestone
in self
.config
.milestones
.values()
931 for node
in self
.nodes
.values():
932 if node
.milestone
is not None:
933 retval
[node
.milestone
].extend(node
.payments
.values())
937 def payments(self
) -> Dict
[Person
, Dict
[Milestone
, List
[Payment
]]]:
938 retval
: Dict
[Person
, Dict
[Milestone
, List
[Payment
]]] = {
941 for milestone
in self
.config
.milestones
.values()
943 for person
in self
.config
.people
.values()
945 for node
in self
.nodes
.values():
946 if node
.milestone
is not None:
947 for payment
in node
.payments
.values():
948 retval
[payment
.payee
][node
.milestone
].append(payment
)
952 def milestone_people(self
) -> Dict
[Milestone
, OrderedSet
[Person
]]:
953 """get a list of people associated with each milestone
955 payments
= list(self
.payments
) # just activate the payments
957 for milestone
in self
.milestone_payments
.keys():
958 retval
[milestone
] = OrderedSet()
959 for milestone
, payments
in self
.milestone_payments
.items():
960 for payment
in payments
:
961 retval
[milestone
].add(payment
.payee
)
964 def __pretty_print__(self
, pp
: PrettyPrinter
):
965 with pp
.type_pp("BudgetGraph") as tpp
:
966 tpp
.field("nodes", self
.nodes
)
967 tpp
.try_field("roots",
968 lambda: [_NodeSimpleReprWrapper(i
)
969 for i
in self
.roots
],
970 BudgetGraphBaseError
)
971 tpp
.try_field("assigned_nodes",
974 _NodeSimpleReprWrapper(node
)
977 for person
, nodes
in self
.assigned_nodes
.items()
979 BudgetGraphBaseError
)
980 tpp
.try_field("assigned_nodes_for_milestones",
983 _NodeSimpleReprWrapper(node
)
986 for milestone
, nodes
in self
.assigned_nodes_for_milestones
.items()
988 BudgetGraphBaseError
)
989 tpp
.try_field("payments",
990 lambda: self
.payments
, BudgetGraphBaseError
)
991 tpp
.try_field("milestone_people",
992 lambda: self
.milestone_people
,
993 BudgetGraphBaseError
)
996 nodes
= [*self
.nodes
.values()]
998 def repr_or_failed(f
: Callable
[[], Any
]) -> str:
1001 except BudgetGraphBaseError
:
1005 roots
= [_NodeSimpleReprWrapper(i
) for i
in self
.roots
]
1007 roots_str
= repr(roots
)
1008 except BudgetGraphBaseError
:
1009 roots_str
= "<failed>"
1010 assigned_nodes
= repr_or_failed(lambda: {
1012 _NodeSimpleReprWrapper(node
)
1015 for person
, nodes
in self
.assigned_nodes
.items()
1017 assigned_nodes_for_milestones
= repr_or_failed(lambda: {
1019 _NodeSimpleReprWrapper(node
)
1022 for milestone
, nodes
in self
.assigned_nodes_for_milestones
.items()
1024 milestone_payments
= repr_or_failed(lambda: self
.milestone_payments
)
1025 payments
= repr_or_failed(lambda: self
.payments
)
1026 milestone_people
= repr_or_failed(lambda: self
.milestone_people
)
1027 return (f
"BudgetGraph{{nodes={nodes!r}, "
1029 f
"assigned_nodes={assigned_nodes}, "
1030 f
"assigned_nodes_for_milestones={assigned_nodes_for_milestones}, "
1031 f
"milestone_payments={milestone_payments}, "
1032 f
"payments={payments}, "
1033 f
"milestone_people={milestone_people}}}")