diff --git a/src/equipment/__pycache__/router.cpython-311.pyc b/src/equipment/__pycache__/router.cpython-311.pyc index d38b233..bd93b86 100644 Binary files a/src/equipment/__pycache__/router.cpython-311.pyc and b/src/equipment/__pycache__/router.cpython-311.pyc differ diff --git a/src/equipment/__pycache__/schema.cpython-311.pyc b/src/equipment/__pycache__/schema.cpython-311.pyc index 0d1857c..e6d58d5 100644 Binary files a/src/equipment/__pycache__/schema.cpython-311.pyc and b/src/equipment/__pycache__/schema.cpython-311.pyc differ diff --git a/src/equipment/__pycache__/service.cpython-311.pyc b/src/equipment/__pycache__/service.cpython-311.pyc index 568b31f..6e7deaa 100644 Binary files a/src/equipment/__pycache__/service.cpython-311.pyc and b/src/equipment/__pycache__/service.cpython-311.pyc differ diff --git a/src/modules/equipment/__pycache__/Eac.cpython-311.pyc b/src/modules/equipment/__pycache__/Eac.cpython-311.pyc index 4f302a9..3053e28 100644 Binary files a/src/modules/equipment/__pycache__/Eac.cpython-311.pyc and b/src/modules/equipment/__pycache__/Eac.cpython-311.pyc differ diff --git a/src/modules/equipment/__pycache__/run.cpython-311.pyc b/src/modules/equipment/__pycache__/run.cpython-311.pyc index 7c85b9b..3d6de87 100644 Binary files a/src/modules/equipment/__pycache__/run.cpython-311.pyc and b/src/modules/equipment/__pycache__/run.cpython-311.pyc differ diff --git a/src/plant_transaction_data/__pycache__/router.cpython-311.pyc b/src/plant_transaction_data/__pycache__/router.cpython-311.pyc index ebc4b91..d33f9c0 100644 Binary files a/src/plant_transaction_data/__pycache__/router.cpython-311.pyc and b/src/plant_transaction_data/__pycache__/router.cpython-311.pyc differ diff --git a/src/plant_transaction_data/__pycache__/service.cpython-311.pyc b/src/plant_transaction_data/__pycache__/service.cpython-311.pyc index cbae0a6..37f669d 100644 Binary files a/src/plant_transaction_data/__pycache__/service.cpython-311.pyc and b/src/plant_transaction_data/__pycache__/service.cpython-311.pyc differ diff --git a/src/plant_transaction_data/router.py b/src/plant_transaction_data/router.py index dd85df6..be7c870 100644 --- a/src/plant_transaction_data/router.py +++ b/src/plant_transaction_data/router.py @@ -40,16 +40,15 @@ async def get_transaction_datas( message="Data retrieved successfully", ) - @router.get("/charts", response_model=StandardResponse[PlantChartData]) async def get_chart_data(db_session: DbSession, common: CommonParameters): chart_data, bep_year, bep_total_lcc = await get_charts( db_session=db_session, common=common ) - if not chart_data or not bep_year: + if not chart_data: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, - detail="A data with this id does not exist.", + detail="No chart data found.", ) return StandardResponse( data={ @@ -60,7 +59,6 @@ async def get_chart_data(db_session: DbSession, common: CommonParameters): message="Data retrieved successfully", ) - @router.get( "/{transaction_data_id}", response_model=StandardResponse[PlantTransactionDataRead] ) diff --git a/src/plant_transaction_data/service.py b/src/plant_transaction_data/service.py index c5cded4..26dc898 100644 --- a/src/plant_transaction_data/service.py +++ b/src/plant_transaction_data/service.py @@ -60,9 +60,9 @@ async def get_charts( chart_data = results.scalars().all() bep_year = None - previous_year = 0 - previous_total_cost = 0 - previous_revenue = 0 + previous_year = None + previous_total_cost = None + previous_revenue = None bep_total_lcc = 0 for idx, item in enumerate(chart_data): @@ -73,16 +73,36 @@ async def get_charts( ) revenue = item.chart_revenue_annualized - if previous_total_cost and previous_revenue: - if (previous_total_cost > previous_revenue and total_cost < revenue) or ( - previous_total_cost < previous_revenue and total_cost > revenue - ): - if total_cost < revenue: - bep_total_lcc = previous_total_cost - bep_year = previous_year + if previous_total_cost is not None and previous_revenue is not None: + prev_diff = previous_total_cost - previous_revenue + curr_diff = total_cost - revenue + + # If signs differ there's a crossing between previous and current point + if prev_diff == 0: + bep_year = previous_year + bep_total_lcc = previous_total_cost + break + + if prev_diff * curr_diff < 0: + # Interpolate linearly between the two years to estimate BEP year + denom = ( (total_cost - previous_total_cost) - (revenue - previous_revenue) ) + if denom != 0: + t = (previous_revenue - previous_total_cost) / denom + # clamp t to [0,1] + t = max(0.0, min(1.0, t)) + try: + bep_year = previous_year + t * (item.tahun - previous_year) + except Exception: + bep_year = previous_year + bep_total_lcc = previous_total_cost + t * (total_cost - previous_total_cost) else: - bep_total_lcc = total_cost - bep_year = item.tahun + # fallback if interpolation is not possible + if total_cost < revenue: + bep_total_lcc = previous_total_cost + bep_year = previous_year + else: + bep_total_lcc = total_cost + bep_year = item.tahun break previous_total_cost = total_cost