|
|
|
@ -160,9 +160,9 @@ class Prediksi:
|
|
|
|
|
|
|
|
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
|
|
|
|
# Query untuk mendapatkan nilai maksimum seq
|
|
|
|
# Query untuk mendapatkan nilai maksimum seq dari data actual
|
|
|
|
get_max_seq_query = """
|
|
|
|
get_max_seq_query = """
|
|
|
|
SELECT COALESCE(MAX(seq), 0) FROM lcc_equipment_tr_data WHERE assetnum = %s
|
|
|
|
SELECT COALESCE(MAX(seq), 0) FROM lcc_equipment_tr_data WHERE assetnum = %s AND is_actual = 1
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
cursor.execute(get_max_seq_query, (equipment_id,))
|
|
|
|
cursor.execute(get_max_seq_query, (equipment_id,))
|
|
|
|
max_seq = cursor.fetchone()[0]
|
|
|
|
max_seq = cursor.fetchone()[0]
|
|
|
|
@ -187,49 +187,6 @@ class Prediksi:
|
|
|
|
%s, %s, 0, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'Sys', NOW()
|
|
|
|
%s, %s, 0, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'Sys', NOW()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
# If a token was provided, store locally so fetch_api_data can use/refresh it
|
|
|
|
|
|
|
|
# if token:
|
|
|
|
|
|
|
|
# self.access_token = token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Fetch data from external API (uses instance access_token and will try refresh on 403)
|
|
|
|
|
|
|
|
# async def fetch_api_data(assetnum: str, year: int) -> dict:
|
|
|
|
|
|
|
|
# url = self.RELIABILITY_APP_URL
|
|
|
|
|
|
|
|
# endpoint = f"{url}/main/number-of-failures/{assetnum}/{int(year)}/{int(year)}"
|
|
|
|
|
|
|
|
# async with httpx.AsyncClient() as client:
|
|
|
|
|
|
|
|
# try:
|
|
|
|
|
|
|
|
# current_token = getattr(self, "access_token", None)
|
|
|
|
|
|
|
|
# response = await client.get(
|
|
|
|
|
|
|
|
# endpoint,
|
|
|
|
|
|
|
|
# timeout=30.0,
|
|
|
|
|
|
|
|
# headers={"Authorization": f"Bearer {current_token}"} if current_token else {},
|
|
|
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
# response.raise_for_status()
|
|
|
|
|
|
|
|
# return response.json()
|
|
|
|
|
|
|
|
# except httpx.HTTPStatusError as e:
|
|
|
|
|
|
|
|
# status = getattr(e.response, "status_code", None)
|
|
|
|
|
|
|
|
# # If we get a 403, try to refresh the access token and retry once
|
|
|
|
|
|
|
|
# if status == 403:
|
|
|
|
|
|
|
|
# print("Received 403 from reliability API, attempting to refresh access token...")
|
|
|
|
|
|
|
|
# new_access = await self.refresh_access_token()
|
|
|
|
|
|
|
|
# if new_access:
|
|
|
|
|
|
|
|
# try:
|
|
|
|
|
|
|
|
# response = await client.get(
|
|
|
|
|
|
|
|
# endpoint,
|
|
|
|
|
|
|
|
# timeout=30.0,
|
|
|
|
|
|
|
|
# headers={"Authorization": f"Bearer {new_access}"},
|
|
|
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
# response.raise_for_status()
|
|
|
|
|
|
|
|
# return response.json()
|
|
|
|
|
|
|
|
# except httpx.HTTPError as e2:
|
|
|
|
|
|
|
|
# print(f"HTTP error occurred after refresh: {e2}")
|
|
|
|
|
|
|
|
# return {}
|
|
|
|
|
|
|
|
# print(f"HTTP error occurred: {e}")
|
|
|
|
|
|
|
|
# return {}
|
|
|
|
|
|
|
|
# except httpx.HTTPError as e:
|
|
|
|
|
|
|
|
# print(f"HTTP error occurred: {e}")
|
|
|
|
|
|
|
|
# return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Menyiapkan data untuk batch insert atau update
|
|
|
|
# Menyiapkan data untuk batch insert atau update
|
|
|
|
records_to_insert = []
|
|
|
|
records_to_insert = []
|
|
|
|
|
|
|
|
|
|
|
|
@ -241,6 +198,7 @@ class Prediksi:
|
|
|
|
update_query = """
|
|
|
|
update_query = """
|
|
|
|
UPDATE lcc_equipment_tr_data
|
|
|
|
UPDATE lcc_equipment_tr_data
|
|
|
|
SET
|
|
|
|
SET
|
|
|
|
|
|
|
|
seq = %s,
|
|
|
|
rc_cm_material_cost = %s,
|
|
|
|
rc_cm_material_cost = %s,
|
|
|
|
rc_cm_labor_cost = %s,
|
|
|
|
rc_cm_labor_cost = %s,
|
|
|
|
rc_pm_material_cost = %s,
|
|
|
|
rc_pm_material_cost = %s,
|
|
|
|
@ -254,7 +212,8 @@ class Prediksi:
|
|
|
|
WHERE id = %s
|
|
|
|
WHERE id = %s
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
for _, row in data.iterrows():
|
|
|
|
for idx, row in data.iterrows():
|
|
|
|
|
|
|
|
loop_seq = max_seq + idx + 1
|
|
|
|
# Check if data exists
|
|
|
|
# Check if data exists
|
|
|
|
cursor.execute(check_existence_query, (equipment_id, int(row["year"])))
|
|
|
|
cursor.execute(check_existence_query, (equipment_id, int(row["year"])))
|
|
|
|
existing_record = cursor.fetchone()
|
|
|
|
existing_record = cursor.fetchone()
|
|
|
|
@ -263,6 +222,7 @@ class Prediksi:
|
|
|
|
# Update existing record
|
|
|
|
# Update existing record
|
|
|
|
record_id = existing_record[0]
|
|
|
|
record_id = existing_record[0]
|
|
|
|
cursor.execute(update_query, (
|
|
|
|
cursor.execute(update_query, (
|
|
|
|
|
|
|
|
int(loop_seq),
|
|
|
|
float(row.get("rc_cm_material_cost", 0)) if not pd.isna(row.get("rc_cm_material_cost", 0)) else 0.0,
|
|
|
|
float(row.get("rc_cm_material_cost", 0)) if not pd.isna(row.get("rc_cm_material_cost", 0)) else 0.0,
|
|
|
|
float(row.get("rc_cm_labor_cost", 0)) if not pd.isna(row.get("rc_cm_labor_cost", 0)) else 0.0,
|
|
|
|
float(row.get("rc_cm_labor_cost", 0)) if not pd.isna(row.get("rc_cm_labor_cost", 0)) else 0.0,
|
|
|
|
float(row.get("rc_pm_material_cost", 0)) if not pd.isna(row.get("rc_pm_material_cost", 0)) else 0.0,
|
|
|
|
float(row.get("rc_pm_material_cost", 0)) if not pd.isna(row.get("rc_pm_material_cost", 0)) else 0.0,
|
|
|
|
@ -274,12 +234,11 @@ class Prediksi:
|
|
|
|
record_id
|
|
|
|
record_id
|
|
|
|
))
|
|
|
|
))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
max_seq = max_seq + 1
|
|
|
|
|
|
|
|
# Prepare for insert
|
|
|
|
# Prepare for insert
|
|
|
|
records_to_insert.append(
|
|
|
|
records_to_insert.append(
|
|
|
|
(
|
|
|
|
(
|
|
|
|
str(uuid4()), # id
|
|
|
|
str(uuid4()), # id
|
|
|
|
int(max_seq), # seq
|
|
|
|
int(loop_seq), # seq
|
|
|
|
int(row["year"]),
|
|
|
|
int(row["year"]),
|
|
|
|
equipment_id,
|
|
|
|
equipment_id,
|
|
|
|
float(row.get("rc_cm_material_cost", 0)) if not pd.isna(row.get("rc_cm_material_cost", 0)) else 0.0,
|
|
|
|
float(row.get("rc_cm_material_cost", 0)) if not pd.isna(row.get("rc_cm_material_cost", 0)) else 0.0,
|
|
|
|
@ -831,8 +790,6 @@ class Prediksi:
|
|
|
|
|
|
|
|
|
|
|
|
async def predict_equipment_data(self, assetnum, token):
|
|
|
|
async def predict_equipment_data(self, assetnum, token):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
# Update acquisition year first
|
|
|
|
|
|
|
|
acquisition_year_ref = await self.__update_equipment_acquisition_year(assetnum)
|
|
|
|
|
|
|
|
# Mengambil data dari database
|
|
|
|
# Mengambil data dari database
|
|
|
|
df = self.__fetch_data_from_db(assetnum)
|
|
|
|
df = self.__fetch_data_from_db(assetnum)
|
|
|
|
if df is None:
|
|
|
|
if df is None:
|
|
|
|
|