fix: normalize datetime objects to naive format in router and refactor forecast message construction

main
Cizz22 3 months ago
parent 216e42206e
commit a7072efdaa

@ -510,7 +510,7 @@ async def get_forecast_eaf(
from .schema import SimulationInput from .schema import SimulationInput
# 1. Get current time # 1. Get current time
now = datetime.now() now = datetime.now().replace(tzinfo=None)
# 2. Determine Last Overhaul Date from Master Data # 2. Determine Last Overhaul Date from Master Data
oh_record = await _get_or_create_master_data(db_session, "Last Overhaul Date", 0) oh_record = await _get_or_create_master_data(db_session, "Last Overhaul Date", 0)
@ -522,15 +522,16 @@ async def get_forecast_eaf(
oh_date_str = oh_record.value_string oh_date_str = oh_record.value_string
try: try:
oh_date = datetime.fromisoformat(oh_date_str) # Ensure oh_date is naive
oh_date = datetime.fromisoformat(oh_date_str).replace(tzinfo=None)
except Exception: except Exception:
oh_date = datetime(now.year, 1, 1) oh_date = datetime(now.year, 1, 1).replace(tzinfo=None)
# 3. Calculate Age in Hours (Offset) # 3. Calculate Age in Hours (Offset)
# Using the helper from utils.py # Using the helper from utils.py
offset_hours = hours_between(oh_date, now) offset_hours = hours_between(oh_date, now)
#start date is oh + offset # start date is oh + offset
start_date = oh_date + timedelta(hours=offset_hours) start_date = oh_date + timedelta(hours=offset_hours)
# 4. Define Forecast Window # 4. Define Forecast Window
@ -573,6 +574,12 @@ async def get_forecast_eaf(
total_ut = plant_result.get("totalUpTime", 0) total_ut = plant_result.get("totalUpTime", 0)
efor = (total_dt / (total_dt + total_ut)) * 100 if (total_dt + total_ut) > 0 else 0 efor = (total_dt / (total_dt + total_ut)) * 100 if (total_dt + total_ut) > 0 else 0
forecast_msg = (
f"Forecast generated successfully, "
f"start date: {start_date.strftime('%Y-%m-%d %H:%M:%S')}, "
f"end_date: {(start_date + timedelta(hours=duration)).strftime('%Y-%m-%d %H:%M:%S')}"
)
return StandardResponse( return StandardResponse(
data={ data={
"range": time_range, "range": time_range,
@ -582,7 +589,7 @@ async def get_forecast_eaf(
"sim_duration_hours": duration, "sim_duration_hours": duration,
"reference_oh_date": oh_date_str "reference_oh_date": oh_date_str
}, },
message=f"Forecast generated successfully, start date: {start_date.strftime('%Y-%m-%d %H:%M:%S')}, end_date: {(start_date + timedelta(hours=duration)).strftime('%Y-%m-%d %H:%M:%S')}" message=forecast_msg
) )
except Exception as e: except Exception as e:
import logging import logging

Loading…
Cancel
Save