From ccacdaac65865bdff15a0225a05f845ade8130a1 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 30 Jul 2024 17:50:55 +0100 Subject: [PATCH] adding try except for some db functions --- .../db/functions/recommendations_functions.py | 46 +++++++++++-------- backend/app/plan/router.py | 5 +- backend/app/plan/schemas.py | 1 + etl/xml_survey_extraction/app.py | 10 ++-- .../optimiser/optimiser_functions.py | 2 +- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/backend/app/db/functions/recommendations_functions.py b/backend/app/db/functions/recommendations_functions.py index cfb3d570..c7765039 100644 --- a/backend/app/db/functions/recommendations_functions.py +++ b/backend/app/db/functions/recommendations_functions.py @@ -1,5 +1,6 @@ from sqlalchemy import insert, delete from sqlalchemy.orm import Session +from sqlalchemy.exc import SQLAlchemyError from backend.app.db.models.recommendations import ( Plan, Recommendation, RecommendationMaterials, PlanRecommendations, Scenario ) @@ -14,12 +15,15 @@ def create_plan(session: Session, plan): :param session: The database session :param plan: dictionary of data representing a plan to be created """ - - new_plan = Plan(**plan) - session.add(new_plan) - session.flush() - - return new_plan.id + try: + new_plan = Plan(**plan) + session.add(new_plan) + session.flush() + session.commit() + return new_plan.id + except SQLAlchemyError as e: + session.rollback() + raise e def create_scenario(session: Session, scenario): @@ -28,12 +32,15 @@ def create_scenario(session: Session, scenario): :param session: The database session :param scenario: dictionary of data representing a scenario to be created """ - - new_scenario = Scenario(**scenario) - session.add(new_scenario) - session.flush() - - return new_scenario.id + try: + new_scenario = Scenario(**scenario) + session.add(new_scenario) + session.flush() + session.commit() + return new_scenario + except SQLAlchemyError as e: + session.rollback() + raise e def create_recommendation(session: Session, recommendation): @@ -42,12 +49,15 @@ def create_recommendation(session: Session, recommendation): :param session: The database session :param recommendation: dictionary of data representing a recommendation to be created """ - - new_recommendation = Recommendation(**recommendation) - session.add(new_recommendation) - session.flush() - - return new_recommendation.id + try: + new_recommendation = Recommendation(**recommendation) + session.add(new_recommendation) + session.flush() + session.commit() + return new_recommendation.id + except SQLAlchemyError as e: + session.rollback() + raise e def create_recommendation_material(session: Session, recommendation_id, material_id, depth): diff --git a/backend/app/plan/router.py b/backend/app/plan/router.py index 1340bae3..4d73778e 100644 --- a/backend/app/plan/router.py +++ b/backend/app/plan/router.py @@ -297,9 +297,6 @@ async def trigger_plan(body: PlanTriggerRequest): session = sessionmaker(bind=db_engine)() created_at = datetime.now().isoformat() - # TODO: We should store the trigger file path in the database with the plan so we can track the file that - # triggered the plan - # TODO: if the measure is already installed, it should actually be the very first phase try: @@ -412,7 +409,7 @@ async def trigger_plan(body: PlanTriggerRequest): return Response(status_code=204) # If we have any work to do, we create a new scenario - scenario = create_scenario( + engine_scenario = create_scenario( session=session, scenario={ "name": body.scenario_name, diff --git a/backend/app/plan/schemas.py b/backend/app/plan/schemas.py index 263115af..b1e3a43a 100644 --- a/backend/app/plan/schemas.py +++ b/backend/app/plan/schemas.py @@ -4,6 +4,7 @@ from typing import Optional class PlanTriggerRequest(BaseModel): budget: Optional[float] = None + # This can only have a fixed set of values goal: str housing_type: str goal_value: str diff --git a/etl/xml_survey_extraction/app.py b/etl/xml_survey_extraction/app.py index aeaf8abe..ed2d20b6 100644 --- a/etl/xml_survey_extraction/app.py +++ b/etl/xml_survey_extraction/app.py @@ -21,7 +21,7 @@ SCENARIOS = { { "portfolio_id": str(86), "housing_type": "Private", - "goal": "Increase EPC", + "goal": "Increasing EPC", "goal_value": "A", "trigger_file_path": "", "already_installed_file_path": "", @@ -36,7 +36,7 @@ SCENARIOS = { { "portfolio_id": str(86), "housing_type": "Private", - "goal": "Increase EPC", + "goal": "Increasing EPC", "goal_value": "A", "trigger_file_path": "", "already_installed_file_path": "", @@ -51,7 +51,7 @@ SCENARIOS = { { "portfolio_id": str(86), "housing_type": "Private", - "goal": "Increase EPC", + "goal": "Increasing EPC", "goal_value": "A", "trigger_file_path": "", "already_installed_file_path": "", @@ -72,7 +72,7 @@ SCENARIOS = { { "portfolio_id": str(87), "housing_type": "Private", - "goal": "Increase EPC", + "goal": "Increasing EPC", "goal_value": "A", "trigger_file_path": "", "already_installed_file_path": "", @@ -87,7 +87,7 @@ SCENARIOS = { { "portfolio_id": str(87), "housing_type": "Private", - "goal": "Increase EPC", + "goal": "Increasing EPC", "goal_value": "A", "trigger_file_path": "", "already_installed_file_path": "", diff --git a/recommendations/optimiser/optimiser_functions.py b/recommendations/optimiser/optimiser_functions.py index 083a7c25..c1123e3d 100644 --- a/recommendations/optimiser/optimiser_functions.py +++ b/recommendations/optimiser/optimiser_functions.py @@ -9,7 +9,7 @@ def prepare_input_measures(property_recommendations, goal): """ goal_map = { - "Increase EPC": "sap_points" + "Increasing EPC": "sap_points" } goal_key = goal_map[goal]