Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into fix/prashanth-transactions-comments…
Browse files Browse the repository at this point in the history
…-fix-1702
  • Loading branch information
prv-proton authored Jan 17, 2025
2 parents d175ec2 + 3af0a64 commit 305e969
Show file tree
Hide file tree
Showing 50 changed files with 1,995 additions and 238 deletions.
1 change: 1 addition & 0 deletions backend/lcfs/db/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"mv_org_compliance_report_count",
"transaction_status_view",
"mv_compliance_report_count",
"mv_fuel_code_count",
]


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""mv for fuel code count
Revision ID: 8119d12538df
Revises: d25e7c47659e
Create Date: 2025-01-14 23:47:28.504150
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "8119d12538df"
down_revision = "fe03799b4018"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.execute(
"""
CREATE MATERIALIZED VIEW mv_fuel_code_count AS
SELECT
CASE fuel_status_id
WHEN 1 THEN 'Draft'
END as status,
COUNT(*) as count
FROM fuel_code
WHERE fuel_status_id = 1
GROUP BY fuel_status_id;
"""
)

op.execute(
"""
CREATE UNIQUE INDEX mv_fuel_code_count_idx
ON mv_fuel_code_count (status);
"""
)

op.execute(
"""
CREATE OR REPLACE FUNCTION refresh_mv_fuel_code_count()
RETURNS TRIGGER AS $$
BEGIN
REFRESH MATERIALIZED VIEW CONCURRENTLY mv_fuel_code_count;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
"""
)

op.execute(
"""
CREATE TRIGGER refresh_mv_fuel_code_count_after_change
AFTER INSERT OR UPDATE OR DELETE ON fuel_code
FOR EACH STATEMENT EXECUTE FUNCTION refresh_mv_fuel_code_count();
"""
)

# Refresh the materialized view to include existing fuel codes
op.execute(
"REFRESH MATERIALIZED VIEW CONCURRENTLY mv_fuel_code_count;"
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.execute(
"DROP TRIGGER IF EXISTS refresh_mv_fuel_code_count_after_change ON fuel_code;")
op.execute("DROP FUNCTION IF EXISTS refresh_mv_fuel_code_count();")
op.execute("DROP MATERIALIZED VIEW IF EXISTS mv_fuel_code_count;")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""add truck and marine transport mode
Revision ID: 5bc0ef48739a
Revises: f78e53370ed2
Create Date: 2025-01-15 22:48:43.582069
"""

import sqlalchemy as sa
from alembic import op
from datetime import datetime

# revision identifiers, used by Alembic.
revision = "5bc0ef48739a"
down_revision = "8119d12538df"
branch_labels = None
depends_on = None


def upgrade() -> None:
current_time = datetime.now()

# Insert Truck and Marine transport modes
op.execute(
"""
INSERT INTO transport_mode (transport_mode, create_date, update_date, create_user, update_user)
VALUES
('Truck', '{}', '{}', 'no_user', 'no_user'),
('Marine', '{}', '{}', 'no_user', 'no_user')
""".format(
current_time, current_time, current_time, current_time
)
)


def downgrade() -> None:
# Remove Truck and Marine transport modes
op.execute(
"""
DELETE FROM transport_mode
WHERE transport_mode IN ('Truck', 'Marine')
"""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""add marine end use
Revision ID: 998929392c8b
Revises: 5bc0ef48739a
Create Date: 2025-01-07 19:35:00.064999
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "998929392c8b"
down_revision = "5bc0ef48739a"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.execute(
"""
INSERT INTO end_use_type (end_use_type_id, type, intended_use)
VALUES (25, 'Marine', TRUE)
ON CONFLICT (end_use_type_id) DO NOTHING;
"""
)
# Energy Effectiveness Ratios
op.execute(
"""
INSERT INTO energy_effectiveness_ratio (
eer_id, fuel_category_id, fuel_type_id, end_use_type_id, ratio, effective_status
)
VALUES (44, 2, 3, 25, 2.5, TRUE)
ON CONFLICT (eer_id) DO NOTHING;
"""
)


def downgrade() -> None:
op.execute(
"""
DELETE FROM energy_effectiveness_ratio
WHERE eer_id = 44;
"""
)
op.execute(
"""
DELETE FROM end_use_type
WHERE end_use_type_id = 25;
"""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Add TFRS Summary Columns
Revision ID: 98d79870df6b
Revises: 998929392c8b
Create Date: 2025-01-16 20:01:34.038941
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "98d79870df6b"
down_revision = "998929392c8b"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"compliance_report_summary",
sa.Column("credits_offset_a", sa.Integer(), nullable=True),
)
op.add_column(
"compliance_report_summary",
sa.Column("credits_offset_b", sa.Integer(), nullable=True),
)
op.add_column(
"compliance_report_summary",
sa.Column("credits_offset_c", sa.Integer(), nullable=True),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("compliance_report_summary", "credits_offset_c")
op.drop_column("compliance_report_summary", "credits_offset_b")
op.drop_column("compliance_report_summary", "credits_offset_a")
# ### end Alembic commands ###
5 changes: 5 additions & 0 deletions backend/lcfs/db/models/compliance/ComplianceReportSummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ class ComplianceReportSummary(BaseModel, Auditable):
line_21_non_compliance_penalty_payable = Column(Float, nullable=False, default=0)
total_non_compliance_penalty_payable = Column(Float, nullable=False, default=0)

# Legacy TFRS Columns
credits_offset_a = Column(Integer)
credits_offset_b = Column(Integer)
credits_offset_c = Column(Integer)

compliance_report = relationship("ComplianceReport", back_populates="summary")

def __repr__(self):
Expand Down
20 changes: 20 additions & 0 deletions backend/lcfs/db/models/fuel/FuelCodeCountView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from sqlalchemy import Column, Integer, String
from lcfs.db.base import BaseModel


class FuelCodeCountView(BaseModel):
__tablename__ = "mv_fuel_code_count"
__table_args__ = {
"extend_existing": True,
"comment": "Materialized view for counting fuel code by status",
}

status = Column(
String,
primary_key=True,
comment="Status name (e.g. draft, approved, deleted)"
)
count = Column(
Integer,
comment="Count of fuel code for this status"
)
8 changes: 8 additions & 0 deletions backend/lcfs/db/seeders/dev/fuel_code_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ def create_fuel_entry(
effective_date,
expiration_date,
fuel_type_id,
fuel_status_id=2,
):
return {
**base_fuel_data, # Extend with the base fields
"fuel_status_id": fuel_status_id,
"fuel_suffix": fuel_suffix,
"company": company,
"carbon_intensity": carbon_intensity,
Expand All @@ -49,6 +51,7 @@ def create_fuel_entry(
async def seed_fuel_codes(session):
fuel_codes_to_seed = [
create_fuel_entry(
fuel_status_id=1,
fuel_suffix="102.5",
company="Neste Oil Singapore",
carbon_intensity=37.21,
Expand All @@ -57,6 +60,7 @@ async def seed_fuel_codes(session):
fuel_type_id=5,
),
create_fuel_entry(
fuel_status_id=1,
fuel_suffix="124.4",
company="Ag Processing Inc.",
carbon_intensity=3.62,
Expand All @@ -65,6 +69,7 @@ async def seed_fuel_codes(session):
fuel_type_id=1,
),
create_fuel_entry(
fuel_status_id=1,
fuel_suffix="125.4",
company="Archer Daniels Midland",
carbon_intensity=-2.14,
Expand All @@ -73,6 +78,7 @@ async def seed_fuel_codes(session):
fuel_type_id=1,
),
create_fuel_entry(
fuel_status_id=3,
fuel_suffix="138.5",
company="ADM Agri-Industries Company",
carbon_intensity=4.26,
Expand All @@ -81,6 +87,7 @@ async def seed_fuel_codes(session):
fuel_type_id=1,
),
create_fuel_entry(
fuel_status_id=3,
fuel_suffix="143.4",
company="Green Plains Otter Tail LLC",
carbon_intensity=44.06,
Expand All @@ -89,6 +96,7 @@ async def seed_fuel_codes(session):
fuel_type_id=4,
),
create_fuel_entry(
fuel_status_id=3,
fuel_suffix="251.2",
company="Incobrasa Industries, Ltd.",
carbon_intensity=0.35,
Expand Down
45 changes: 45 additions & 0 deletions backend/lcfs/tests/fuel_export/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime
import pytest
from unittest.mock import AsyncMock, MagicMock
from lcfs.web.api.compliance_report.schema import CompliancePeriodSchema, ComplianceReportHistorySchema, ComplianceReportOrganizationSchema, ComplianceReportStatusSchema, ComplianceReportUserSchema, SummarySchema
from lcfs.web.api.fuel_export.repo import FuelExportRepository
from lcfs.web.api.fuel_code.repo import FuelCodeRepository
from lcfs.web.api.fuel_export.services import FuelExportServices
Expand Down Expand Up @@ -45,6 +47,49 @@ def mock_compliance_report_repo():
repo = AsyncMock(spec=ComplianceReportRepository)
return repo

@pytest.fixture
def compliance_period_schema():
return CompliancePeriodSchema(
compliance_period_id=1,
description="2024",
effective_date=datetime(2024, 1, 1),
expiration_date=datetime(2024, 3, 31),
display_order=1,
)

@pytest.fixture
def compliance_report_organization_schema():
return ComplianceReportOrganizationSchema(
organization_id=1, name="Acme Corporation"
)

@pytest.fixture
def summary_schema():
return SummarySchema(summary_id=1, is_locked=False)

@pytest.fixture
def compliance_report_status_schema():
return ComplianceReportStatusSchema(compliance_report_status_id=1, status="Draft")

@pytest.fixture
def compliance_report_user_schema(compliance_report_organization_schema):
return ComplianceReportUserSchema(
first_name="John",
last_name="Doe",
organization=compliance_report_organization_schema,
)

@pytest.fixture
def compliance_report_history_schema(
compliance_report_status_schema, compliance_report_user_schema
):
return ComplianceReportHistorySchema(
compliance_report_history_id=1,
compliance_report_id=1,
status=compliance_report_status_schema,
user_profile=compliance_report_user_schema,
create_date=datetime(2024, 4, 1, 12, 0, 0),
)

@pytest.fixture
def mock_fuel_code_repo():
Expand Down
Loading

0 comments on commit 305e969

Please sign in to comment.