Skip to content

Commit

Permalink
Merge pull request #1767 from bcgov/fix/daniel-fix-line2-calcs-1738
Browse files Browse the repository at this point in the history
fix: Move renewable flag to DB
  • Loading branch information
dhaselhan authored Jan 22, 2025
2 parents 2387a23 + e9a268e commit 375f1d7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Add renewable to Fuel Types
Revision ID: 0576833c005d
Revises: ec826b9226df
Create Date: 2025-01-22 00:04:37.043142
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "0576833c005d"
down_revision = "ec826b9226df"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"fuel_type",
sa.Column(
"renewable",
sa.Boolean(),
server_default=sa.text("FALSE"),
nullable=False,
comment="Indicates whether the fuel should be categorized as renewable",
),
)

# Update specific fuel types to set renewable = TRUE
op.execute(
"""
UPDATE fuel_type
SET renewable = TRUE
WHERE fuel_type IN (
'Renewable gasoline',
'Ethanol',
'Renewable naphtha',
'Biodiesel',
'HDRD',
'Other diesel fuel',
'Alternative jet fuel',
'Other'
)
"""
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("fuel_type", "renewable")
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion backend/lcfs/db/models/compliance/FuelSupply.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ class FuelSupply(BaseModel, Auditable, Versioning):
end_use_type = relationship("EndUseType")

def __repr__(self):
return f"<FuelSupply(id={self.fuel_supply_id}>"
return f"<FuelSupply(id={self.fuel_supply_id}, fuel_type_id={self.fuel_type_id}, quantity={self.quantity})>"
6 changes: 6 additions & 0 deletions backend/lcfs/db/models/fuel/FuelType.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class FuelType(BaseModel, Auditable, DisplayOrder):
default=False,
comment="Indicates whether the fuel is fossil-derived for other uses",
)
renewable = Column(
Boolean,
server_default=text("FALSE"),
nullable=False,
comment="Indicates whether the fuel should be categorized as renewable",
)
provision_1_id = Column(
Integer,
ForeignKey("provision_of_the_act.provision_of_the_act_id"),
Expand Down
12 changes: 0 additions & 12 deletions backend/lcfs/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,3 @@ class FILE_MEDIA_TYPE(Enum):
}

default_ci = {"Gasoline": 93.67, "Diesel": 100.21, "Jet fuel": 88.83}


RENEWABLE_FUEL_TYPES = [
"Renewable gasoline",
"Ethanol",
"Renewable naphtha",
"Biodiesel",
"HDRD",
"Other diesel",
"Alternative jet fuel",
"Other",
]
17 changes: 5 additions & 12 deletions backend/lcfs/web/api/compliance_report/summary_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
from lcfs.web.exception.exceptions import DataNotFoundException
from lcfs.web.utils.calculations import calculate_compliance_units

from lcfs.utils.constants import RENEWABLE_FUEL_TYPES

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -243,12 +241,6 @@ async def update_compliance_report_summary(

return summary_data

def filter_renewable_fuel_supplies(self, fuel_supplies: List[FuelSupply]) -> List[FuelSupply]:
return [
supply for supply in fuel_supplies
if supply.fuel_type and supply.fuel_type.fuel_type in RENEWABLE_FUEL_TYPES
]

@service_handler
async def calculate_compliance_report_summary(
self, report_id: int
Expand Down Expand Up @@ -338,16 +330,17 @@ async def calculate_compliance_report_summary(
fossil_quantities = await self.calculate_fuel_quantities(
compliance_report.compliance_report_id,
effective_fuel_supplies,
fossil_derived=True
fossil_derived=True,
)
# line 2
filtered_renewable_fuel_supplies = self.filter_renewable_fuel_supplies(
effective_fuel_supplies)
filtered_renewable_fuel_supplies = [
fs for fs in effective_fuel_supplies if fs.fuel_type.renewable
]

renewable_quantities = await self.calculate_fuel_quantities(
compliance_report.compliance_report_id,
filtered_renewable_fuel_supplies,
fossil_derived=False
fossil_derived=False,
)

renewable_fuel_target_summary = self.calculate_renewable_fuel_target_summary(
Expand Down

0 comments on commit 375f1d7

Please sign in to comment.