-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1699 from bcgov/feat/kevin-1588
Feat: idir fuel code count dashboard widget
- Loading branch information
Showing
16 changed files
with
318 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
backend/lcfs/db/migrations/versions/2025-01-14-23-47_8119d12538df.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.