From ef9d9adb5f2974690cd070dd8c7572a3e6c8e836 Mon Sep 17 00:00:00 2001 From: Daniel Haselhan Date: Thu, 5 Dec 2024 11:43:14 -0800 Subject: [PATCH] fix: Load EER ratio even when end use is not defined --- backend/lcfs/web/api/fuel_code/repo.py | 47 ++++++++++++++++++-------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/backend/lcfs/web/api/fuel_code/repo.py b/backend/lcfs/web/api/fuel_code/repo.py index 8eb686fea..27dd0ec01 100644 --- a/backend/lcfs/web/api/fuel_code/repo.py +++ b/backend/lcfs/web/api/fuel_code/repo.py @@ -684,7 +684,10 @@ async def get_fuel_code_by_name(self, fuel_code: str) -> FuelCode: result = await self.db.execute( select(FuelCode) .join(FuelCode.fuel_code_prefix) - .join(FuelCodeStatus, FuelCode.fuel_status_id == FuelCodeStatus.fuel_code_status_id) + .join( + FuelCodeStatus, + FuelCode.fuel_status_id == FuelCodeStatus.fuel_code_status_id, + ) .outerjoin(FuelType, FuelCode.fuel_type_id == FuelType.fuel_type_id) .options( contains_eager(FuelCode.fuel_code_prefix), @@ -693,7 +696,8 @@ async def get_fuel_code_by_name(self, fuel_code: str) -> FuelCode: ) .where( and_( - func.concat(FuelCodePrefix.prefix, FuelCode.fuel_suffix) == fuel_code, + func.concat(FuelCodePrefix.prefix, FuelCode.fuel_suffix) + == fuel_code, FuelCodeStatus.status != FuelCodeStatusEnum.Deleted, ) ) @@ -713,18 +717,35 @@ async def get_provision_of_the_act_by_name( @repo_handler async def get_energy_effectiveness_ratio( - self, fuel_type_id: int, fuel_category_id: int, end_use_type_id: int + self, fuel_type_id: int, fuel_category_id: int, end_use_type_id: Optional[int] ) -> EnergyEffectivenessRatio: + """ + Retrieves the Energy Effectiveness Ratio based on fuel type, fuel category, + and optionally the end use type. + + Args: + fuel_type_id (int): The ID of the fuel type. + fuel_category_id (int): The ID of the fuel category. + end_use_type_id (Optional[int]): The ID of the end use type (optional). - stmt = select(EnergyEffectivenessRatio).where( + Returns: + Optional[EnergyEffectivenessRatio]: The matching EnergyEffectivenessRatio record or None. + """ + conditions = [ EnergyEffectivenessRatio.fuel_type_id == fuel_type_id, EnergyEffectivenessRatio.fuel_category_id == fuel_category_id, - EnergyEffectivenessRatio.end_use_type_id == end_use_type_id, - ) + ] + + if end_use_type_id is not None: + conditions.append( + EnergyEffectivenessRatio.end_use_type_id == end_use_type_id + ) + + stmt = select(EnergyEffectivenessRatio).where(*conditions) result = await self.db.execute(stmt) - energy_density = result.scalars().first() + energy_effectiveness_ratio = result.scalars().first() - return energy_density + return energy_effectiveness_ratio @repo_handler async def get_target_carbon_intensities( @@ -785,12 +806,10 @@ async def get_standardized_fuel_data( effective_carbon_intensity = fuel_type.default_carbon_intensity # Get energy effectiveness ratio (EER) - eer = None - if fuel_type_id and fuel_category_id and end_use_id: - energy_effectiveness = await self.get_energy_effectiveness_ratio( - fuel_type_id, fuel_category_id, end_use_id - ) - eer = energy_effectiveness.ratio if energy_effectiveness else 1.0 + energy_effectiveness = await self.get_energy_effectiveness_ratio( + fuel_type_id, fuel_category_id, end_use_id + ) + eer = energy_effectiveness.ratio if energy_effectiveness else 1.0 # Fetch target carbon intensity (TCI) target_ci = None