From e7ab60c0aa0342e615c3b81f1d5c541297a389ea Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Mon, 13 Oct 2025 12:47:44 +0700 Subject: [PATCH] fix --- src/calculation_time_constrains/router.py | 4 ++-- src/calculation_time_constrains/service.py | 24 +++++++++++++--------- src/calculation_time_constrains/utils.py | 15 ++++---------- src/sparepart/service.py | 2 +- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/calculation_time_constrains/router.py b/src/calculation_time_constrains/router.py index 8fcf7e8..02ac112 100644 --- a/src/calculation_time_constrains/router.py +++ b/src/calculation_time_constrains/router.py @@ -85,12 +85,12 @@ async def get_calculation_parameters( @router.get( "/{calculation_id}", response_model=StandardResponse[CalculationTimeConstrainsRead] ) -async def get_calculation_results(db_session: DbSession, calculation_id, collector_db_session:CollectorDbSession): +async def get_calculation_results(db_session: DbSession, calculation_id, token:Token): if calculation_id == 'default': calculation_id = DEFAULT_TC_ID results = await get_calculation_result( - db_session=db_session, calculation_id=calculation_id, collector_db_session=collector_db_session + db_session=db_session, calculation_id=calculation_id, token=token ) return StandardResponse( diff --git a/src/calculation_time_constrains/service.py b/src/calculation_time_constrains/service.py index f1dba5f..88a6fab 100644 --- a/src/calculation_time_constrains/service.py +++ b/src/calculation_time_constrains/service.py @@ -68,7 +68,7 @@ class OptimumCostModelWithSpareparts: self.planned_oh_months = self._get_months_between(last_oh_date, next_oh_date) # Set analysis time window (default: 1.5x planned interval) - self.time_window_months = time_window_months or int(self.planned_oh_months * 1.3) + self.time_window_months = time_window_months or int(self.planned_oh_months * 1.2) # Pre-calculate date range for API calls self.date_range = self._generate_date_range() @@ -350,10 +350,8 @@ class OptimumCostModelWithSpareparts: for imp in importance_results["calc_result"] } - loss_production_permonth = { - imp['aeros_node']['node_name']: (imp['ideal_production'] - imp['production']) / 60 - for imp in importance_results["calc_result"] - } + loss_production = importance_results["plant_result"]['total_downtime'] * 660 + except Exception as e: self.logger.error(f"Failed to get simulation results: {e}") equipment_birnbaum = {} @@ -375,14 +373,13 @@ class OptimumCostModelWithSpareparts: plant_capacity_loss_money = [metrics['derated_mwh'] * COST_PER_MWH for metrics in plant_monthly_metrics.values()] cumulative_loss_money = np.cumsum(plant_capacity_loss_money) + for equipment in equipments: location_tag = equipment.location_tag contribution_factor = equipment_birnbaum.get(location_tag, 0.0) ecs = ecs_tags.get(location_tag, None) - loss_production = loss_production_permonth.get(location_tag, 0) * 960000 - # try: # # Get failure predictions @@ -433,6 +430,8 @@ class OptimumCostModelWithSpareparts: individual_results, equipments, equipment_birnbaum, simulation_id ) + + # Phase 3: Generate final results and database objects fleet_results = [] total_corrective_costs = np.zeros(max_interval) + cumulative_loss_money[0:max_interval] @@ -763,7 +762,7 @@ async def create_param_and_data( return calculationData -async def get_calculation_result(db_session: DbSession, calculation_id: str, collector_db_session): +async def get_calculation_result(db_session: DbSession, calculation_id: str, token): """ Get calculation results with improved error handling, performance, and sparepart details """ @@ -825,7 +824,12 @@ async def get_calculation_result(db_session: DbSession, calculation_id: str, col # seeing the implementation of the code, it is not possible to determine exactly what it is # doing. It could be used to store monthly metrics for a plant, calculate metrics, or perform # some other operation related to plant data. - # plant_monthly_metrics = await plant_simulation_metrics(simulation_id=scope_calculation.rbd_simulation_id, location_tag="plant", use_location_tag=0, token=token, last_oh_date=prev_oh_scope.end_date, max_interval=scope_calculation.max_interval) + plant_monthly_metrics = await plant_simulation_metrics(simulation_id=scope_calculation.rbd_simulation_id, location_tag="plant", use_location_tag=0, token=token, last_oh_date=prev_oh_scope.end_date, max_interval=scope_calculation.max_interval) + + + loss_production_per_month = np.arange(0, scope_calculation.max_interval) + k = 4 + loss_exp = (plant_monthly_metrics['total_downtime'] * 660 * 500_000) * (np.exp(k * (loss_production_per_month / scope_calculation.max_interval)) - 1) / (np.exp(k) - 1) # REFERENCE_CAPACITY = 630 # or 550 # COST_PER_MWH = 1_000_000 # rupiah @@ -839,7 +843,7 @@ async def get_calculation_result(db_session: DbSession, calculation_id: str, col for month_index in range(data_num): month_result = { "overhaul_cost": 0.0, - "corrective_cost": 0.0, + "corrective_cost": float(loss_exp[month_index]), "procurement_cost": 0.0, "num_failures": 0.0, "day": month_index + 1, diff --git a/src/calculation_time_constrains/utils.py b/src/calculation_time_constrains/utils.py index c2be133..76e0ea5 100644 --- a/src/calculation_time_constrains/utils.py +++ b/src/calculation_time_constrains/utils.py @@ -107,11 +107,11 @@ import pandas as pd async def plant_simulation_metrics(simulation_id: str, location_tag: str, max_interval, token, last_oh_date, use_location_tag: int = 1): """Get failure predictions for equipment from simulation service""" - plot_result_url = f"{RBD_SERVICE_API}/aeros/simulation/result/plot/{simulation_id}/{location_tag}?use_location_tag={use_location_tag}" + calc_result_url = f"{RBD_SERVICE_API}/aeros/simulation/result/calc/{simulation_id}/{location_tag}" try: response = requests.get( - plot_result_url, + calc_result_url, headers={ "Content-Type": "application/json", "Authorization": f"Bearer {token}", @@ -119,19 +119,12 @@ async def plant_simulation_metrics(simulation_id: str, location_tag: str, max_in timeout=30 ) response.raise_for_status() - prediction_data = response.json() + prediction_data = response.json()['data'] except (requests.RequestException, ValueError) as e: raise Exception(str(e)) - plot_data = prediction_data.get('data', {}).get('timestamp_outs') if prediction_data.get("data") else None - if not plot_data: - raise Exception(str("no data")) - - time_series = create_time_series_data(plot_data, (max_interval * 24 * 31)) - monthly_data = analyze_monthly_metrics(time_series, last_oh_date) - - return monthly_data + return prediction_data def analyze_monthly_metrics(timestamp_outs, start_date, max_flow_rate: float = 550): if not timestamp_outs: diff --git a/src/sparepart/service.py b/src/sparepart/service.py index a4e96d5..da448a2 100644 --- a/src/sparepart/service.py +++ b/src/sparepart/service.py @@ -792,7 +792,7 @@ async def load_sparepart_data_from_db(scope, prev_oh_scope, db_session, analysis # prev_oh_scope = await get_prev_oh(db_session=db_session, overhaul_session=scope) analysis_start_date = prev_oh_scope.end_date - analysis_window_months = int(((scope.start_date - prev_oh_scope.end_date).days / 30) * 1.3) if not analysis_window_months else analysis_window_months + analysis_window_months = int(((scope.start_date - prev_oh_scope.end_date).days / 30) * 1.2) if not analysis_window_months else analysis_window_months sparepart_manager = SparepartManager(analysis_start_date, analysis_window_months) start_date = prev_oh_scope.end_date