mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
optimised api speed, good enough for now
This commit is contained in:
parent
c413482bf4
commit
f2fc921bc5
4 changed files with 31 additions and 20 deletions
|
|
@ -32,4 +32,4 @@ def aggregate_portfolio_recommendations(session, portfolio_id: int):
|
|||
|
||||
# Merge the updated portfolio back into the session
|
||||
session.merge(portfolio)
|
||||
session.commit()
|
||||
session.flush()
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ def create_property(session, portfolio_id: int, address: str, postcode: str) ->
|
|||
|
||||
# Merge the updated property back into the session
|
||||
session.merge(existing_property)
|
||||
session.commit()
|
||||
session.flush()
|
||||
|
||||
return existing_property.id, False
|
||||
|
||||
|
|
@ -50,7 +50,7 @@ def create_property(session, portfolio_id: int, address: str, postcode: str) ->
|
|||
# Add the new property to the session
|
||||
session.add(new_property)
|
||||
|
||||
session.commit()
|
||||
session.flush()
|
||||
|
||||
return new_property.id, True
|
||||
|
||||
|
|
@ -73,7 +73,7 @@ def create_property_targets(session, property_id: int, portfolio_id: int, epc_ta
|
|||
heat_demand=heat_demand_target
|
||||
)
|
||||
session.add(new_target)
|
||||
session.commit()
|
||||
session.flush()
|
||||
|
||||
return True
|
||||
|
||||
|
|
@ -93,9 +93,9 @@ def update_property_data(session, property_id: int, portfolio_id: int, property_
|
|||
|
||||
existing_property.updated_at = now
|
||||
|
||||
# Merge the updated property back into the session and commit
|
||||
# Merge the updated property back into the session and flush
|
||||
session.merge(existing_property)
|
||||
session.commit()
|
||||
session.flush()
|
||||
|
||||
except NoResultFound:
|
||||
raise Exception(f"Property with property_id {property_id} and portfolio_id {portfolio_id} not found")
|
||||
|
|
@ -125,6 +125,6 @@ def create_property_details_epc(session, property_details_epc: dict):
|
|||
new_property_details_epc = PropertyDetailsEpcModel(**property_details_epc)
|
||||
session.add(new_property_details_epc)
|
||||
|
||||
session.commit()
|
||||
session.flush()
|
||||
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from sqlalchemy import text
|
||||
from backend.app.db.models.recommendations import Plan, Recommendation, RecommendationMaterials
|
||||
from sqlalchemy import insert
|
||||
from backend.app.db.models.recommendations import Plan, Recommendation, RecommendationMaterials, PlanRecommendations
|
||||
|
||||
|
||||
def create_plan(session, plan):
|
||||
|
|
@ -24,7 +24,7 @@ def create_recommendation(session, recommendation):
|
|||
|
||||
new_recommendation = Recommendation(**recommendation)
|
||||
session.add(new_recommendation)
|
||||
session.commit()
|
||||
session.flush()
|
||||
|
||||
return new_recommendation.id
|
||||
|
||||
|
|
@ -44,25 +44,23 @@ def create_recommendation_material(session, recommendation_id, material_id, dept
|
|||
depth=depth
|
||||
)
|
||||
session.add(new_recommendation_material)
|
||||
session.commit()
|
||||
session.flush()
|
||||
|
||||
return new_recommendation_material.id
|
||||
|
||||
|
||||
def create_plan_recommendations(session, plan_id, recommendation_ids):
|
||||
"""
|
||||
This function will create a record for the plan_recommendation in the database if it does not exist.
|
||||
This function will create records for the plan_recommendation in the database.
|
||||
:param plan_id: ID of the plan
|
||||
:param recommendation_ids: list of recommendation IDs
|
||||
"""
|
||||
|
||||
for recommendation_id in recommendation_ids:
|
||||
session.execute(
|
||||
text(
|
||||
'INSERT INTO plan_recommendations (plan_id, recommendation_id) VALUES (:plan_id, '
|
||||
':recommendation_id)'),
|
||||
{'plan_id': plan_id, 'recommendation_id': recommendation_id}
|
||||
)
|
||||
# Prepare a list of dictionaries for bulk insert
|
||||
data = [{"plan_id": plan_id, "recommendation_id": rid} for rid in recommendation_ids]
|
||||
|
||||
# Bulk insert using SQLAlchemy's core API
|
||||
session.execute(insert(PlanRecommendations).values(data))
|
||||
|
||||
|
||||
def upload_recommendations(session, recommendations_to_upload, property_id):
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ from utils.uvalue_estimates import classify_decile_newvalues
|
|||
from backend.app.db.utils import row2dict
|
||||
from starlette.responses import Response
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.exc import IntegrityError, OperationalError
|
||||
|
||||
# database interaction functions
|
||||
from backend.app.db.functions.property_functions import (
|
||||
|
|
@ -135,7 +136,7 @@ async def trigger_plan(body: PlanTriggerRequest):
|
|||
session = Session()
|
||||
|
||||
try:
|
||||
|
||||
session.begin()
|
||||
logger.info("Getting the inputs")
|
||||
# Read in the trigger file from s3
|
||||
bucket_name = get_settings().PLAN_TRIGGER_BUCKET
|
||||
|
|
@ -373,6 +374,18 @@ async def trigger_plan(body: PlanTriggerRequest):
|
|||
|
||||
# Commit all changes at once
|
||||
session.commit()
|
||||
except IntegrityError:
|
||||
logger.error("Database integrity error occurred", exc_info=True)
|
||||
session.rollback()
|
||||
return Response(status_code=500, content="Database integrity error.")
|
||||
except OperationalError:
|
||||
logger.error("Database operational error occurred", exc_info=True)
|
||||
session.rollback()
|
||||
return Response(status_code=500, content="Database operational error.")
|
||||
except ValueError:
|
||||
logger.error("Value error - possibly due to malformed data", exc_info=True)
|
||||
session.rollback()
|
||||
return Response(status_code=400, content="Bad request: malformed data.")
|
||||
except Exception as e: # General exception handling
|
||||
logger.error(f"An error occurred: {e}")
|
||||
session.rollback()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue