Added in costs placeholder and storing recommendations in backend

This commit is contained in:
Khalim Conn-Kowlessar 2023-08-15 17:48:24 +01:00
parent eccf4814b9
commit 557aa108e5
6 changed files with 16 additions and 12 deletions

View file

@ -1,3 +1,4 @@
from sqlalchemy import text
from sqlalchemy.orm import sessionmaker
from backend.app.db.connection import db_engine
from backend.app.db.models.recommendations import Plan, Recommendation, RecommendationMaterials
@ -60,12 +61,13 @@ def create_plan_recommendations(plan_id, recommendation_ids):
:param plan_id: ID of the plan
:param recommendation_ids: list of recommendation IDs
"""
Session = sessionmaker(bind=db_engine)
with Session() as session:
for recommendation_id in recommendation_ids:
session.execute(
'INSERT INTO plan_recommendations (plan_id, recommendation_id) VALUES (:plan_id, :recommendation_id)',
text(
'INSERT INTO plan_recommendations (plan_id, recommendation_id) VALUES (:plan_id, '
':recommendation_id)'),
{'plan_id': plan_id, 'recommendation_id': recommendation_id}
)
session.commit()

View file

@ -36,6 +36,7 @@ class RecommendationMaterials(Base):
recommendation_id = Column(BigInteger, ForeignKey('recommendation.id'), nullable=False)
material_id = Column(BigInteger, ForeignKey(Material.id), nullable=False)
created_at = Column(TIMESTAMP, nullable=False, server_default=func.now())
depth = Column(Float, nullable=False)
class Plan(Base):

View file

@ -272,14 +272,13 @@ async def trigger_plan(body: PlanTriggerRequest):
# upload recommendations
uploaded_recommendation_ids = []
for rec in recommendations_to_upload:
# TODO: implement costs (at least a placeholder)
estimated_cost = sum([x["cost"] if x["cost"] else 0 for x in rec["parts"]])
recommendation_id = create_recommendation(
{
"property_id": p.id,
"type": rec["type"],
"description": rec["description"],
"estimated_cost": estimated_cost,
"estimated_cost": rec["cost"],
"default": True,
"starting_u_value": rec.get("starting_u_value"),
"new_u_value": rec.get("new_u_value"),

View file

@ -308,7 +308,7 @@ class FloorRecommendations(BaseUtility):
self.recommendations.append(
{
"parts": [
get_recommended_part(part, depth),
get_recommended_part(part, depth, cost_per_unit),
],
"type": "floor_insulation",
"description": self._make_floor_description(part, depth),

View file

@ -334,7 +334,7 @@ class WallRecommendations(BaseUtility):
recommendations.append(
{
"parts": [get_recommended_part(part, depth)],
"parts": [get_recommended_part(part, depth, cost_per_unit)],
"type": "wall_insulation",
"description": "Install " + self._make_description(part, depth),
"starting_u_value": u_value,
@ -396,8 +396,8 @@ class WallRecommendations(BaseUtility):
# For now, I'm adding them as separate items in the list
recommendation = {
"parts": [
get_recommended_part(ewi_part, ewi_depth),
get_recommended_part(iwi_part, iwi_depth)
get_recommended_part(ewi_part, ewi_depth, ewi_cost_per_unit),
get_recommended_part(iwi_part, iwi_depth, iwi_cost_per_unit)
],
"type": "wall_insulation",
"description": (

View file

@ -110,15 +110,17 @@ def update_lowest_selected_u_value(lowest_selected_u_value, new_u_value):
return lowest_selected_u_value
def get_recommended_part(part, selected_depth):
def get_recommended_part(part, selected_depth, selected_cost):
"""
Utility function to return a recommended part with the selected depth.
:param part:
:param selected_depth:
:param part: part to be recommended
:param selected_depth: depth of the selected part
:param selected_cost: cost of the selected depth
:return:
"""
recommended_part = deepcopy(part)
recommended_part["depths"] = [selected_depth]
recommended_part["cost"] = [selected_cost]
return recommended_part