fix NoneType math

main
MrWaradana 2 months ago
parent 9285169bb4
commit 8444ca06ce

@ -15,6 +15,16 @@ from src.auth.service import CurrentUser
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _safe_float(x: object) -> float:
"""Safely convert `x` to float, returning 0.0 for None or invalid values."""
try:
if x is None:
return 0.0
return float(x)
except Exception:
return 0.0
async def get( async def get(
*, db_session: DbSession, transaction_data_id: str *, db_session: DbSession, transaction_data_id: str
) -> Optional[PlantTransactionData]: ) -> Optional[PlantTransactionData]:
@ -67,12 +77,12 @@ async def get_charts(
for idx, item in enumerate(chart_data): for idx, item in enumerate(chart_data):
total_cost = ( total_cost = (
float(item.chart_capex_annualized) _safe_float(item.chart_capex_annualized)
+ float(item.chart_oem_annualized) + _safe_float(item.chart_oem_annualized)
+ float(item.chart_fuel_cost_annualized) + _safe_float(item.chart_fuel_cost_annualized)
+ float(item.cost_disposal_cost) + _safe_float(item.cost_disposal_cost)
) )
revenue = float(item.chart_revenue_annualized) revenue = _safe_float(item.chart_revenue_annualized)
if previous_total_cost is not None and previous_revenue is not None: if previous_total_cost is not None and previous_revenue is not None:
prev_diff = previous_total_cost - previous_revenue prev_diff = previous_total_cost - previous_revenue

Loading…
Cancel
Save