diff --git a/src/calculation_time_constrains/service.py b/src/calculation_time_constrains/service.py index 4aa8f3b..f53c5c6 100644 --- a/src/calculation_time_constrains/service.py +++ b/src/calculation_time_constrains/service.py @@ -138,14 +138,14 @@ async def get_corrective_cost_time_chart( raise -# async def get_corrective_cost_time_chart( -# material_cost: float, -# service_cost: float, -# location_tag: str, -# token, -# start_date: datetime.datetime, -# end_date: datetime.datetime -# ) -> Tuple[np.ndarray, np.ndarray]: +async def get_corrective_cost_time_chart( + material_cost: float, + service_cost: float, + location_tag: str, + token, + start_date: datetime.datetime, + end_date: datetime.datetime +) -> Tuple[np.ndarray, np.ndarray]: days_difference = (end_date - start_date).days today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) @@ -186,17 +186,17 @@ async def get_corrective_cost_time_chart( history_dict[month_key] += item["num_fail"] # Sort months chronologically - sorted_months = sorted(monthly_failures.keys()) + sorted_months = sorted(history_dict.keys()) + + failures = np.array([history_dict[month] for month in sorted_months]) + cum_failure = np.cumsum(failures) - # Calculate cumulative failures - running_total = 0 - for month in sorted_months: - running_total += monthly_failures[month] - history_dict[month] = running_total + for month_key in sorted_months: + monthly_failures[month_key] = int(cum_failure[sorted_months.index(month_key)]) # Update monthly_data with cumulative historical data - monthly_data.update(history_dict) + monthly_data.update(monthly_failures) except Exception as e: # print(f"Error fetching historical data: {e}") raise Exception(e) @@ -239,7 +239,29 @@ async def get_corrective_cost_time_chart( current_date = datetime.datetime(start_date.year, start_date.month, 1) while current_date <= end_date: if current_date not in monthly_data: - monthly_data[current_date] = 0 + # Initialize to check previous months + previous_month = current_date.replace(day=1) - datetime.timedelta(days=1) + # Now previous_month is the last day of the previous month + # Convert back to first day of previous month for consistency + previous_month = previous_month.replace(day=1) + + # Keep going back until we find data or run out of months to check + month_diff = (current_date.year - start_date.year) * 12 + (current_date.month - start_date.month) + max_attempts = max(1, month_diff) # Ensure at least 1 attempt + attempts = 0 + + while previous_month not in monthly_data and attempts < max_attempts: + # Move to the previous month (last day of the month before) + previous_month = previous_month.replace(day=1) - datetime.timedelta(days=1) + # Convert to first day of month + previous_month = previous_month.replace(day=1) + attempts += 1 + + # Use the found value or default to 0 if no previous month with data exists + if previous_month in monthly_data: + monthly_data[current_date] = monthly_data[previous_month] + else: + monthly_data[current_date] = 0 # Move to next month if current_date.month == 12: @@ -247,11 +269,13 @@ async def get_corrective_cost_time_chart( else: current_date = datetime.datetime(current_date.year, current_date.month + 1, 1) - # Convert to list maintaining chronological order + + # # 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)