|
|
|
@ -138,14 +138,14 @@ async def get_corrective_cost_time_chart(
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# async def get_corrective_cost_time_chart(
|
|
|
|
async def get_corrective_cost_time_chart(
|
|
|
|
# material_cost: float,
|
|
|
|
material_cost: float,
|
|
|
|
# service_cost: float,
|
|
|
|
service_cost: float,
|
|
|
|
# location_tag: str,
|
|
|
|
location_tag: str,
|
|
|
|
# token,
|
|
|
|
token,
|
|
|
|
# start_date: datetime.datetime,
|
|
|
|
start_date: datetime.datetime,
|
|
|
|
# end_date: datetime.datetime
|
|
|
|
end_date: datetime.datetime
|
|
|
|
# ) -> Tuple[np.ndarray, np.ndarray]:
|
|
|
|
) -> Tuple[np.ndarray, np.ndarray]:
|
|
|
|
days_difference = (end_date - start_date).days
|
|
|
|
days_difference = (end_date - start_date).days
|
|
|
|
|
|
|
|
|
|
|
|
today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
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"]
|
|
|
|
history_dict[month_key] += item["num_fail"]
|
|
|
|
|
|
|
|
|
|
|
|
# Sort months chronologically
|
|
|
|
# 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
|
|
|
|
for month_key in sorted_months:
|
|
|
|
running_total = 0
|
|
|
|
monthly_failures[month_key] = int(cum_failure[sorted_months.index(month_key)])
|
|
|
|
for month in sorted_months:
|
|
|
|
|
|
|
|
running_total += monthly_failures[month]
|
|
|
|
|
|
|
|
history_dict[month] = running_total
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Update monthly_data with cumulative historical data
|
|
|
|
# Update monthly_data with cumulative historical data
|
|
|
|
monthly_data.update(history_dict)
|
|
|
|
monthly_data.update(monthly_failures)
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
# print(f"Error fetching historical data: {e}")
|
|
|
|
# print(f"Error fetching historical data: {e}")
|
|
|
|
raise Exception(e)
|
|
|
|
raise Exception(e)
|
|
|
|
@ -239,6 +239,28 @@ async def get_corrective_cost_time_chart(
|
|
|
|
current_date = datetime.datetime(start_date.year, start_date.month, 1)
|
|
|
|
current_date = datetime.datetime(start_date.year, start_date.month, 1)
|
|
|
|
while current_date <= end_date:
|
|
|
|
while current_date <= end_date:
|
|
|
|
if current_date not in monthly_data:
|
|
|
|
if current_date not in monthly_data:
|
|
|
|
|
|
|
|
# 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
|
|
|
|
monthly_data[current_date] = 0
|
|
|
|
|
|
|
|
|
|
|
|
# Move to next month
|
|
|
|
# Move to next month
|
|
|
|
@ -247,11 +269,13 @@ async def get_corrective_cost_time_chart(
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
current_date = datetime.datetime(current_date.year, current_date.month + 1, 1)
|
|
|
|
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 = []
|
|
|
|
complete_data = []
|
|
|
|
for month in sorted(monthly_data.keys()):
|
|
|
|
for month in sorted(monthly_data.keys()):
|
|
|
|
complete_data.append(monthly_data[month])
|
|
|
|
complete_data.append(monthly_data[month])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Convert to numpy array
|
|
|
|
# Convert to numpy array
|
|
|
|
monthly_failure = np.array(complete_data)
|
|
|
|
monthly_failure = np.array(complete_data)
|
|
|
|
|
|
|
|
|
|
|
|
|