adding try except for some db functions

This commit is contained in:
Khalim Conn-Kowlessar 2024-07-30 17:50:55 +01:00
parent cadbd4f48a
commit ccacdaac65
5 changed files with 36 additions and 28 deletions

View file

@ -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):

View file

@ -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,

View file

@ -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

View file

@ -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": "",

View file

@ -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]