bulk uploading recommendations - improved speed by half a second

This commit is contained in:
Khalim Conn-Kowlessar 2023-08-21 11:05:28 +01:00
parent 6d381c2b65
commit 8f7ae68b4a

View file

@ -67,37 +67,46 @@ def create_plan_recommendations(session, plan_id, recommendation_ids):
def upload_recommendations(session, recommendations_to_upload, property_id):
uploaded_recommendation_ids = []
# Prepare data for bulk insert for Recommendation
recommendations_data = [
{
"property_id": property_id,
"type": rec["type"],
"description": rec["description"],
"estimated_cost": rec["cost"],
"default": rec["default"],
"starting_u_value": rec.get("starting_u_value"),
"new_u_value": rec.get("new_u_value"),
"sap_points": rec["sap_points"]
}
for rec in recommendations_to_upload
]
for rec in recommendations_to_upload:
new_recommendation = Recommendation(
property_id=property_id,
type=rec["type"],
description=rec["description"],
estimated_cost=rec["cost"],
default=rec["default"],
starting_u_value=rec.get("starting_u_value"),
new_u_value=rec.get("new_u_value"),
sap_points=rec["sap_points"]
)
session.add(new_recommendation)
uploaded_recommendation_ids.append(new_recommendation)
session.bulk_insert_mappings(Recommendation, recommendations_data)
# Flush to get the IDs for the newly created recommendations
# To get the IDs of the newly inserted recommendations, we need to flush the session
session.flush()
# Create recommendation-materials
recommendation_materials = []
for rec, recommendation in zip(recommendations_to_upload, uploaded_recommendation_ids):
for part in rec["parts"]:
recommendation_material = RecommendationMaterials(
recommendation_id=recommendation.id,
material_id=part["id"],
depth=part["depths"][0] if part["depths"] else None,
)
recommendation_materials.append(recommendation_material)
# Map the uploaded_recommendation_ids with the original data for reference
uploaded_recommendation_ids = [rec.id for rec in session.query(Recommendation).filter(
Recommendation.property_id == property_id,
Recommendation.description.in_([rec["description"] for rec in recommendations_to_upload])
)]
session.add_all(recommendation_materials)
# Prepare data for bulk insert for RecommendationMaterials
recommendation_materials_data = [
{
"recommendation_id": recommendation_id,
"material_id": part["id"],
"depth": part["depths"][0] if part["depths"] else None,
}
for rec, recommendation_id in zip(recommendations_to_upload, uploaded_recommendation_ids)
for part in rec["parts"]
]
session.bulk_insert_mappings(RecommendationMaterials, recommendation_materials_data)
# Commit all changes
session.commit()
return [rec.id for rec in uploaded_recommendation_ids]
return uploaded_recommendation_ids