do batch update per portfolio not per property

This commit is contained in:
Daniel Roth 2026-02-19 12:44:41 +00:00
parent 9d12eef0e5
commit 63bef436d0
2 changed files with 28 additions and 19 deletions

View file

@ -10,7 +10,7 @@ payload = {
"body": json.dumps(
{
"portfolio_id": 556,
"scenarios_to_consider": [],
"scenarios_to_consider": [1039, 1041],
"scenarios_priority_order": [],
}
)

View file

@ -1,5 +1,5 @@
from collections import defaultdict
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Tuple
from backend.app.db.functions.recommendations_functions import (
bulk_update_plans,
@ -40,6 +40,9 @@ def process_portfolio(
plans_by_property: Dict[int, List[Plan]] = _group_plans_by_property(plans)
updated_plan_models: List[PlanModel] = []
updated_scenario_models: List[ScenarioModel] = []
for property_id, property_plans in plans_by_property.items():
if not property_plans:
@ -49,9 +52,16 @@ def process_portfolio(
property_plans, scenario_priority_order
)
_update_default_flags(
property_plans, cheapest_plan
) # TODO: we have already unset existing default(s), so this method can probably be a bit simpler now
updated_property_plan_models, updated_property_scenario_models = (
_update_plan_and_scenario_objects(property_plans, cheapest_plan)
)
updated_plan_models.extend(updated_property_plan_models)
updated_scenario_models.extend(updated_property_scenario_models)
if len(updated_plan_models) > 0:
bulk_update_plans(updated_plan_models, updated_scenario_models)
logger.info("Successfully updated Plan default values in database")
def choose_cheapest_relevant_plan(
@ -106,7 +116,9 @@ def _unset_defaults_for_scenarios_not_being_considered(
if id not in scenarios_to_consider:
scenarios_to_unset_default.append(id)
logger.info(f"Scenarios to unset defaults: {scenarios_to_unset_default}")
logger.info(
f"Unsetting {scenarios_to_unset_default} as default scenario(s) as not included in provided list of scenarios to consider"
)
if len(scenarios_to_unset_default) > 0:
plans_to_unset_default: List[int] = get_plan_ids_by_scenario_ids(
@ -164,7 +176,9 @@ def _group_plans_by_property(plans: List[Plan]) -> Dict[int, List[Plan]]:
return grouped
def _update_default_flags(plans: List[Plan], cheapest_plan: Plan) -> None:
def _update_plan_and_scenario_objects(
plans: List[Plan], cheapest_plan: Plan
) -> Tuple[List[PlanModel], List[ScenarioModel]]:
plans_to_update: List[Plan] = []
for plan in plans:
@ -176,17 +190,12 @@ def _update_default_flags(plans: List[Plan], cheapest_plan: Plan) -> None:
plan.set_default(should_be_default)
plans_to_update.append(plan)
if plans_to_update:
plan_models: List[PlanModel] = []
scenario_models: List[ScenarioModel] = []
plan_models: List[PlanModel] = []
scenario_models: List[ScenarioModel] = []
for plan in plans_to_update:
plan_model, scenario_model = plan.to_sqlalchemy()
plan_models.append(plan_model)
scenario_models.append(scenario_model)
for plan in plans_to_update:
plan_model, scenario_model = plan.to_sqlalchemy()
plan_models.append(plan_model)
scenario_models.append(scenario_model)
bulk_update_plans(plan_models, scenario_models)
logger.info("Successfully updated Plan default values")
else:
logger.info("All plan default values already correct. Not updating")
return (plan_models, scenario_models)