Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into fix/daniel-data-fixes-1321
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaselhan authored Dec 5, 2024
2 parents f308cd4 + 39ceef0 commit bddf45d
Show file tree
Hide file tree
Showing 21 changed files with 549 additions and 325 deletions.
176 changes: 121 additions & 55 deletions backend/lcfs/db/seeders/common/seed_fuel_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,42 +327,52 @@
},
{
"end_use_type_id": 14,
"type": "Marine",
"sub_type": "General",
"type": "Aircraft",
"intended_use": true
},
{
"end_use_type_id": 15,
"type": "Marine",
"sub_type": "Operated within 51 to 75% of load range"
"type": "Compression-ignition engine- Marine, general",
"intended_use": true
},
{
"end_use_type_id": 16,
"type": "Marine",
"sub_type": "Operated within 76 to 100% of load range"
"type": "Compression-ignition engine- Marine, operated within 51 to 75% of load range",
"intended_use": true
},
{
"end_use_type_id": 17,
"type": "Marine, w/ methane slip reduction kit",
"sub_type": "General"
"type": "Compression-ignition engine- Marine, operated within 76 to 100% of load range",
"intended_use": true
},
{
"end_use_type_id": 18,
"type": "Marine, w/ methane slip reduction kit",
"sub_type": "Operated within 51 to 75% of load range"
"type": "Compression-ignition engine- Marine, with methane slip reduction kit- General",
"intended_use": true
},
{
"end_use_type_id": 19,
"type": "Marine, w/ methane slip reduction kit",
"sub_type": "Operated within 76 to 100% of load range"
"type": "Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 51 to 75% of load range",
"intended_use": true
},
{
"end_use_type_id": 20,
"type": "Unknown"
"type": "Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 76 to 100% of load range",
"intended_use": true
},
{
"end_use_type_id": 21,
"type": "Aircraft",
"type": "Compression-ignition engine- Marine, unknown whether kit is installed or average operating load range",
"intended_use": true
},
{
"end_use_type_id": 22,
"type": "Unknown engine type",
"intended_use": true
},
{
"end_use_type_id": 23,
"type": "Other (i.e. road transportation)",
"intended_use": true
}
],
Expand Down Expand Up @@ -398,60 +408,74 @@
"additional_uci_id": 1,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 14,
"intensity": 27.3
"intensity": 0
},
{
"additional_uci_id": 2,
"uom_id": 5,
"intensity": 0
},
{
"additional_uci_id": 3,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 15,
"intensity": 17.8
"intensity": 27.3
},
{
"additional_uci_id": 3,
"additional_uci_id": 4,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 16,
"intensity": 12.2
"intensity": 17.8
},
{
"additional_uci_id": 4,
"additional_uci_id": 5,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 17,
"intensity": 10.6
"intensity": 12.2
},
{
"additional_uci_id": 5,
"additional_uci_id": 6,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 18,
"intensity": 8.4
"intensity": 10.6
},
{
"additional_uci_id": 6,
"additional_uci_id": 7,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 19,
"intensity": 8.0
"intensity": 8.4
},
{
"additional_uci_id": 7,
"additional_uci_id": 8,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 20,
"intensity": 8.0
},
{
"additional_uci_id": 9,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 21,
"intensity": 27.3
},
{
"additional_uci_id": 8,
"additional_uci_id": 10,
"fuel_type_id": 7,
"uom_id": 5,
"intensity": 0
"end_use_type_id": 22,
"intensity": 27.3
},
{
"additional_uci_id": 9,
"additional_uci_id": 11,
"fuel_type_id": 7,
"uom_id": 5,
"end_use_type_id": 23,
"intensity": 0
}
],
Expand Down Expand Up @@ -548,75 +572,117 @@
"eer_id": 14,
"fuel_category_id": 2,
"fuel_type_id": 3,
"end_use_type_id": 14,
"ratio": 2.5
},
{
"eer_id": 15,
"fuel_category_id": 2,
"fuel_type_id": 3,
"end_use_type_id": 10,
"ratio": 2.8
},
{
"eer_id": 16,
"eer_id": 15,
"fuel_category_id": 2,
"fuel_type_id": 3,
"end_use_type_id": 11,
"ratio": 2.4
},
{
"eer_id": 17,
"eer_id": 16,
"fuel_category_id": 2,
"fuel_type_id": 3,
"end_use_type_id": 2,
"ratio": 1.0
},
{
"eer_id": 18,
"eer_id": 17,
"fuel_category_id": 2,
"fuel_type_id": 6,
"end_use_type_id": 3,
"ratio": 1.8
},
{
"eer_id": 19,
"eer_id": 18,
"fuel_category_id": 2,
"fuel_type_id": 6,
"end_use_type_id": 2,
"ratio": 0.9
},
{
"eer_id": 19,
"fuel_category_id": 2,
"fuel_type_id": 13,
"ratio": 0.9
},
{
"eer_id": 20,
"fuel_category_id": 3,
"fuel_type_id": 3,
"ratio": 2.5
},
{
"eer_id": 21,
"fuel_category_id": 3,
"fuel_type_id": 11,
"ratio": 1.0
},
{
"eer_id": 22,
"fuel_category_id": 2,
"fuel_type_id": 7,
"end_use_type_id": 12,
"end_use_type_id": 15,
"ratio": 1.0
},
{
"eer_id": 21,
"eer_id": 23,
"fuel_category_id": 2,
"fuel_type_id": 7,
"end_use_type_id": 2,
"ratio": 0.9
"end_use_type_id": 16,
"ratio": 1.0
},
{
"eer_id": 22,
"eer_id": 24,
"fuel_category_id": 2,
"fuel_type_id": 13,
"ratio": 0.9
"fuel_type_id": 7,
"end_use_type_id": 17,
"ratio": 1.0
},
{
"eer_id": 23,
"fuel_category_id": 3,
"fuel_type_id": 3,
"ratio": 2.5
"eer_id": 25,
"fuel_category_id": 2,
"fuel_type_id": 7,
"end_use_type_id": 18,
"ratio": 1.0
},
{
"eer_id": 24,
"fuel_category_id": 3,
"fuel_type_id": 11,
"eer_id": 26,
"fuel_category_id": 2,
"fuel_type_id": 7,
"end_use_type_id": 19,
"ratio": 1.0
},
{
"eer_id": 27,
"fuel_category_id": 2,
"fuel_type_id": 7,
"end_use_type_id": 20,
"ratio": 1.0
},
{
"eer_id": 28,
"fuel_category_id": 2,
"fuel_type_id": 7,
"end_use_type_id": 21,
"ratio": 1.0
},
{
"eer_id": 29,
"fuel_category_id": 2,
"fuel_type_id": 7,
"end_use_type_id": 22,
"ratio": 0.9
},
{
"eer_id": 30,
"fuel_category_id": 2,
"fuel_type_id": 7,
"end_use_type_id": 23,
"ratio": 0.9
}
],
"energy_densities": [
Expand Down Expand Up @@ -1026,4 +1092,4 @@
"display_order": 4
}
]
}
}
59 changes: 35 additions & 24 deletions backend/lcfs/services/redis/lifetime.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
from fastapi import FastAPI
from redis.asyncio import Redis
from redis.exceptions import RedisError

from redis.exceptions import RedisError, TimeoutError
import asyncio
from lcfs.settings import settings

logger = logging.getLogger(__name__)
Expand All @@ -14,27 +14,38 @@ async def init_redis(app: FastAPI) -> None:
:param app: current FastAPI application.
"""
try:
# Initialize Redis client directly
app.state.redis_client = Redis(
host=settings.redis_host,
port=settings.redis_port,
password=settings.redis_pass,
db=settings.redis_base or 0,
decode_responses=True,
socket_timeout=5, # Timeout for socket read/write (seconds)
socket_connect_timeout=5, # Timeout for connection establishment (seconds)
)

# Test the connection
await app.state.redis_client.ping()
logger.info("Redis client initialized and connection successful.")
except RedisError as e:
logger.error(f"Redis error during initialization: {e}")
raise
except Exception as e:
logger.error(f"Unexpected error during Redis initialization: {e}")
raise
retries = 5 # Retry logic in case Redis is unavailable initially
for i in range(retries):
try:
# Initialize Redis client
app.state.redis_client = Redis(
host=settings.redis_host,
port=settings.redis_port,
password=settings.redis_pass,
db=settings.redis_base or 0,
decode_responses=True,
max_connections=10,
socket_timeout=5,
socket_connect_timeout=5,
)

# Test the connection
await app.state.redis_client.ping()
logger.info("Redis client initialized and connection successful.")
break
except TimeoutError as e:
logger.error(f"Redis timeout during initialization attempt {i + 1}: {e}")
if i == retries - 1:
raise
await asyncio.sleep(2**i) # Exponential backoff
except RedisError as e:
logger.error(f"Redis error during initialization attempt {i + 1}: {e}")
if i == retries - 1:
raise
await asyncio.sleep(2**i) # Exponential backoff
except Exception as e:
logger.error(f"Unexpected error during Redis initialization: {e}")
raise


async def shutdown_redis(app: FastAPI) -> None:
Expand All @@ -46,7 +57,7 @@ async def shutdown_redis(app: FastAPI) -> None:
try:
if hasattr(app.state, "redis_client") and app.state.redis_client:
await app.state.redis_client.close()
logger.info("Redis client closed successfully.")
logger.info("Redis client closed successfully.")
except RedisError as e:
logger.error(f"Redis error during shutdown: {e}")
except Exception as e:
Expand Down
Loading

0 comments on commit bddf45d

Please sign in to comment.