From 9ced34bf166a330c2f7998d76efd4e260187c6c1 Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Thu, 13 Feb 2025 16:34:12 +0700 Subject: [PATCH] change hours to months --- src/calculation_time_constrains/service.py | 36 +++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/calculation_time_constrains/service.py b/src/calculation_time_constrains/service.py index 6f79d37..54f2c33 100644 --- a/src/calculation_time_constrains/service.py +++ b/src/calculation_time_constrains/service.py @@ -166,23 +166,29 @@ async def get_corrective_cost_time_chart( for item in data["data"] } - # Group data by month + # Initialize all months in the range with 0 monthly_data = {} - for date in date_range: - month_key = date.replace(day=1) - if month_key not in monthly_data: - monthly_data[month_key] = 0 - - if date in data_dict and data_dict[date] is not None: - monthly_data[month_key] += data_dict[date] - - - # Convert monthly data to list + current_date = start_date.replace(day=1) + while current_date <= end_date: + monthly_data[current_date] = 0 + # Move to next month + if current_date.month == 12: + current_date = datetime.datetime(current_date.year + 1, 1, 1) + else: + current_date = datetime.datetime(current_date.year, current_date.month + 1, 1) + + # Get the last day's value for each month + for date in data_dict.keys(): + month_key = datetime.datetime(date.year, date.month, 1) + if month_key in monthly_data and data_dict[date] is not None: + # Update only if the value is higher (to get the last day's value) + monthly_data[month_key] = max(monthly_data[month_key], data_dict[date]) + + # Convert to list maintaining chronological order complete_data = [] for month in sorted(monthly_data.keys()): complete_data.append(monthly_data[month]) - # Convert to numpy array monthly_failure = np.array(complete_data) @@ -270,9 +276,10 @@ async def create_param_and_data( async def get_calculation_result(db_session: DbSession, calculation_id: str): start_date = datetime.datetime(2025, 1, 1) - end_date = start_date + datetime.timedelta(days=677) + end_date = datetime.datetime(2026, 12, 31) months_num = get_months_between(start_date, end_date) + scope_calculation = await get_calculation_data_by_id( db_session=db_session, calculation_id=calculation_id ) @@ -412,7 +419,8 @@ async def create_calculation_result_service( db_session: DbSession, calculation: CalculationData, token: str ) -> CalculationTimeConstrainsRead: start_date = datetime.datetime(2025, 1, 1) - end_date = start_date + datetime.timedelta(days=677) + end_date = datetime.datetime(2026, 12, 31) + months_num = get_months_between(start_date, end_date)