Skip to content

Commit

Permalink
fix: Load EER ratio even when end use is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaselhan committed Dec 5, 2024
1 parent 3219a4d commit ef9d9ad
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions backend/lcfs/web/api/fuel_code/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
)
)
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ef9d9ad

Please sign in to comment.