From 5764175e321d8cb08477858a7afbeb1c467d73d3 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Fri, 9 Aug 2024 22:37:08 +0100 Subject: [PATCH] fixed property_non_invasive_recommendations: --- backend/app/plan/router.py | 55 ++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/backend/app/plan/router.py b/backend/app/plan/router.py index dd4fa9a2..3fbc2492 100644 --- a/backend/app/plan/router.py +++ b/backend/app/plan/router.py @@ -311,6 +311,29 @@ def get_on_site_data(body: PlanTriggerRequest): return patches, already_installed, non_invasive_recommendations +def extract_propert_on_site_recommendations(config, already_installed, non_invasive_recommendations, uprn): + property_already_installed = next(( + x for x in already_installed if + (x["address"] == config["address"]) and (x["postcode"] == config["postcode"]) + ), {}) + + # Because we have some non-invasive recommendations that match on address and postcode, but not UPRN + # we need to check existence of uprn + has_uprn = "non_invasive_recommendations" in non_invasive_recommendations[0] + if has_uprn: + property_non_invasive_recommendations = next(( + x for x in non_invasive_recommendations if + (x["uprn"] == uprn) + ), {}) + else: + property_non_invasive_recommendations = next(( + x for x in non_invasive_recommendations if + (x["address"] == config["address"]) and (x["postcode"] == config["postcode"]) + ), {}) + + return property_already_installed, property_non_invasive_recommendations + + router = APIRouter( prefix="/plan", tags=["plan"], @@ -394,15 +417,9 @@ async def trigger_plan(body: PlanTriggerRequest): cleaning_data=cleaning_data ) - property_already_installed = next(( - x for x in already_installed if - (x["address"] == config["address"]) and (x["postcode"] == config["postcode"]) - ), {}) - - property_non_invasive_recommendations = next(( - x for x in non_invasive_recommendations if - (x["uprn"] == config["uprn"]) - ), {}) + property_already_installed, property_non_invasive_recommendations = extract_propert_on_site_recommendations( + config, already_installed, non_invasive_recommendations, uprn + ) input_properties.append( Property( @@ -573,7 +590,6 @@ async def trigger_plan(body: PlanTriggerRequest): energy_consumption ) p.set_solar_panel_configuration(unit_solar_panel_configuration) - if individual_units: # Model the solar potential at the property level for unit in individual_units: @@ -887,7 +903,7 @@ async def trigger_plan(body: PlanTriggerRequest): # TODO: After optimising, if there are any cheap, quick win measures (e.g. insulate water tank with hot water # cylinder jacket), we should add these to the recommendations as default - for p in input_properties.keys(): + for p in input_properties: if not recommendations[p.id]: continue input_measures = prepare_input_measures(recommendations[p.id], body.goal) @@ -942,18 +958,11 @@ async def trigger_plan(body: PlanTriggerRequest): ] recommendations[p.id] = final_recommendations - raise Exception("Finish me!!") - # # TODO: Given the default recommendations, calcualte a total kwh and cost saving for the property!!! - # default_savings = [ - # { - # "kwh_savings": rec["kwh_savings"], - # "energy_cost_savings": rec["energy_cost_savings"] - # } for recs in property_recommendations for rec in recs if rec["default"] - # ] - - # 1) the property data - # 2) the property details (epc) - # 3) the recommendations + # With that complete, we now total the kwh and cost savings for the property + # total_kwh_savings = sum([rec["kwh_savings"] for rec in final_recommendations if rec["default"]]) + # total_energy_cost_savings = sum( + # [rec["energy_cost_savings"] for rec in final_recommendations if rec["default"]] + # ) logger.info("Uploading recommendations to the database") # If we have any work to do, we create a new scenario