Added materials table to fastapi backend

This commit is contained in:
Khalim Conn-Kowlessar 2023-08-10 16:53:52 +01:00
parent 07af58eb85
commit b278e557fb
5 changed files with 67 additions and 5 deletions

2
.idea/Model.iml generated
View file

@ -7,7 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/open_uprn" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/recommendations" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="Python 3.10 (model_data)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.10 (backend)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

2
.idea/misc.xml generated
View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (model_data)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (backend)" project-jdk-type="Python SDK" />
<component name="PythonCompatibilityInspectionAdvertiser">
<option name="version" value="3" />
</component>

View file

@ -0,0 +1,51 @@
import enum
from sqlalchemy import Column, Integer, String, Float, Enum, TIMESTAMP
from sqlalchemy.orm import declarative_base
from sqlalchemy.sql import func
Base = declarative_base()
class MaterialType(enum.Enum):
suspended_floor_insulation = "suspended_floor_insulation"
solid_floor_insulation = "solid_floor_insulation"
external_wall_insulation = "external_wall_insulation"
internal_wall_insulation = "internal_wall_insulation"
class DepthUnit(enum.Enum):
mm = "mm"
class CostUnit(enum.Enum):
gbp_sq_meter = "gbp_sq_meter"
class RValueUnit(enum.Enum):
square_meter_kelvin_per_watt = "square_meter_kelvin_per_watt"
class ThermalConductivityUnit(enum.Enum):
watt_per_meter_kelvin = "watt_per_meter_kelvin"
class Material(Base):
__tablename__ = 'material'
id = Column(Integer, primary_key=True, autoincrement=True)
type = Column(Enum(MaterialType, values_callable=lambda x: [e.value for e in x]), nullable=False)
description = Column(String, nullable=False)
depths = Column(String) # You may want to use a specific JSON type depending on the database
depth_unit = Column(Enum(DepthUnit, values_callable=lambda x: [e.value for e in x]), nullable=False)
cost = Column(Float)
cost_unit = Column(Enum(CostUnit, values_callable=lambda x: [e.value for e in x]), nullable=False)
r_value_per_mm = Column(Float)
r_value_unit = Column(Enum(RValueUnit, values_callable=lambda x: [e.value for e in x]), nullable=False)
thermal_conductivity = Column(Float)
thermal_conductivity_unit = Column(
Enum(ThermalConductivityUnit, values_callable=lambda x: [e.value for e in x]),
nullable=False
)
link = Column(String)
created_at = Column(TIMESTAMP, nullable=False, server_default=func.now())

View file

@ -155,11 +155,17 @@ async def trigger_plan(body: PlanTriggerRequest):
# The materials data could be cached or local so we don't need to make
# consistent requrests to the backend for
# the same data
# TODO: It might not be the best choice to store the materials data in a database table since thi
# table probably won't be very large and won't be updated that often. It might be better to
# store this data in s3 load it into memory when the app starts up. We will test this
materials_by_type = get_materials(materials)
logger.info("Getting components and properties recommendations")
recommendations = []
for property_id, p in enumerate(input_properties):
property_recommendations = []
# For each property, classiy floor area decide
total_floor_area_group_decile = classify_decile_newvalues(
decile_boundaries=floors_decile_data["decile_boundaries"],
@ -191,7 +197,7 @@ async def trigger_plan(body: PlanTriggerRequest):
for rec in floor_recommender.recommendations:
rec["property_id"] = property_id
recommendations.extend(floor_recommender.recommendations)
property_recommendations.extend(floor_recommender.recommendations)
# Wall recommendations
# We would make this u-value query directly to the database
@ -223,7 +229,10 @@ async def trigger_plan(body: PlanTriggerRequest):
for rec in wall_recomendations.recommendations:
rec["property_id"] = property_id
recommendations.extend(wall_recomendations.recommendations)
property_recommendations.extend(wall_recomendations.recommendations)
if property_recommendations:
blah
# Once we're done, we'll store:
# 1) the property data

View file

@ -237,6 +237,7 @@ def app():
# Clean using averages
avgs = iterative_filtering(cleaning_averages, property_data)
# TODO: Should probably do a mean/median?
field_value = avgs[field].iloc[0]
if pd.isnull(field_value):
@ -281,6 +282,7 @@ def app():
rdsap_change = ending_record[RDSAP_RESPONSE] - starting_record[RDSAP_RESPONSE]
heat_demand_change = ending_record[HEAT_DEMAND_RESPONSE] - starting_record[HEAT_DEMAND_RESPONSE]
# TODO: Should this be <= 0?
if rdsap_change == 0:
# Assumption: We aren't interested in records that exhibit no change
continue