mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
handle some plans having zero cost 🟩
This commit is contained in:
parent
678de56def
commit
ce94fb0573
3 changed files with 20 additions and 21 deletions
|
|
@ -60,6 +60,14 @@ class Plan:
|
|||
case _:
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def cost(self) -> float:
|
||||
return (
|
||||
self.record.cost_of_works
|
||||
if self.record.cost_of_works is not None
|
||||
else float("inf")
|
||||
)
|
||||
|
||||
def to_sqlalchemy(self) -> PlanPersistence:
|
||||
scenario_record = self.scenario.record
|
||||
|
||||
|
|
|
|||
|
|
@ -50,10 +50,13 @@ def process_portfolio(
|
|||
if not property_plans:
|
||||
raise ValueError(f"No plans for property {property_id}")
|
||||
|
||||
cheapest_plan = choose_cheapest_relevant_plan(
|
||||
property_plans, scenario_priority_order
|
||||
)
|
||||
logger.info(f"Successfully found cheapest plan for Property {property_id}")
|
||||
try:
|
||||
cheapest_plan = choose_cheapest_relevant_plan(
|
||||
property_plans, scenario_priority_order
|
||||
)
|
||||
except Exception:
|
||||
logger.error(f"Failed to find cheapest plan for property {property_id}")
|
||||
raise
|
||||
|
||||
updated_property_plan_models, updated_property_scenario_models = (
|
||||
_update_plan_and_scenario_objects(property_plans, cheapest_plan)
|
||||
|
|
@ -85,23 +88,11 @@ def choose_cheapest_relevant_plan(
|
|||
f"All plans must have an ID, but found a plan with no ID: {plan}"
|
||||
)
|
||||
|
||||
min_cost: float = min(
|
||||
(
|
||||
plan.record.cost_of_works
|
||||
if plan.record.cost_of_works is not None
|
||||
else float("inf")
|
||||
)
|
||||
for plan in eligible_plans
|
||||
)
|
||||
min_cost: float = min(plan.cost for plan in eligible_plans)
|
||||
|
||||
if all(p.record.cost_of_works == 0 for p in eligible_plans):
|
||||
cheapest_plans = eligible_plans
|
||||
else:
|
||||
cheapest_plans: List[Plan] = [
|
||||
plan
|
||||
for plan in eligible_plans
|
||||
if (plan.record.cost_of_works or float("inf")) == min_cost
|
||||
]
|
||||
cheapest_plans: List[Plan] = [
|
||||
plan for plan in eligible_plans if plan.cost == min_cost
|
||||
]
|
||||
|
||||
for priority_scenario_id in scenario_priority_order:
|
||||
for plan in cheapest_plans:
|
||||
|
|
|
|||
|
|
@ -127,7 +127,7 @@ def test_some_plans_zero_cost__cheapest_returned(
|
|||
created_at_datetime, False, cost_of_works=50.0, name="EPC C - Minor Works"
|
||||
)
|
||||
scenario_priority_order: List[int] = [4, 3]
|
||||
expected_default_plan_id = 2
|
||||
expected_default_plan_id = 1
|
||||
|
||||
# act
|
||||
actual_default_plan = choose_cheapest_relevant_plan(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue