|
|
|
@ -4,11 +4,34 @@ from uuid import uuid4
|
|
|
|
from datetime import datetime
|
|
|
|
from datetime import datetime
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
|
|
from config import get_connection
|
|
|
|
from config import get_connection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def fetch_api_data(
|
|
|
|
|
|
|
|
assetnum: str, year: int, RELIABILITY_APP_URL: str, token: str
|
|
|
|
|
|
|
|
) -> dict:
|
|
|
|
|
|
|
|
url = RELIABILITY_APP_URL
|
|
|
|
|
|
|
|
# print(f"Using URL: {url}") # Add this for debugging
|
|
|
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
|
|
|
# print(
|
|
|
|
|
|
|
|
# f"{url}/main/number-of-failures/{assetnum}/{int(year)}/{int(year)}"
|
|
|
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = await client.get(
|
|
|
|
|
|
|
|
f"{url}/main/number-of-failures/{assetnum}/{int(year)}/{int(year)}",
|
|
|
|
|
|
|
|
timeout=30.0,
|
|
|
|
|
|
|
|
headers={"Authorization": f"Bearer {token}"},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
except httpx.HTTPError as e:
|
|
|
|
|
|
|
|
print(f"HTTP error occurred: {e}")
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_recursive_query(cursor, equipment_id, worktype="CM"):
|
|
|
|
def get_recursive_query(cursor, equipment_id, worktype="CM"):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Fungsi untuk menjalankan query rekursif berdasarkan equipment_id dan worktype.
|
|
|
|
Fungsi untuk menjalankan query rekursif berdasarkan equipment_id dan worktype.
|
|
|
|
@ -65,7 +88,7 @@ def get_data_tahun(cursor):
|
|
|
|
return cursor.fetchall()
|
|
|
|
return cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def query_data():
|
|
|
|
async def query_data(RELIABILITY_APP_URL: str, token: str):
|
|
|
|
connection = None
|
|
|
|
connection = None
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
# Mendapatkan koneksi dari config.py
|
|
|
|
# Mendapatkan koneksi dari config.py
|
|
|
|
@ -154,6 +177,9 @@ def query_data():
|
|
|
|
, %s, %s
|
|
|
|
, %s, %s
|
|
|
|
)
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
api_data = await fetch_api_data(
|
|
|
|
|
|
|
|
equipment_id, year, RELIABILITY_APP_URL, token
|
|
|
|
|
|
|
|
)
|
|
|
|
cursor.execute(
|
|
|
|
cursor.execute(
|
|
|
|
insert_query,
|
|
|
|
insert_query,
|
|
|
|
(
|
|
|
|
(
|
|
|
|
@ -162,7 +188,11 @@ def query_data():
|
|
|
|
year, # tahun
|
|
|
|
year, # tahun
|
|
|
|
seq, # seq
|
|
|
|
seq, # seq
|
|
|
|
1, # is_actual
|
|
|
|
1, # is_actual
|
|
|
|
1, # raw_cm_interval (minimal 1 karena minimal 1x OH)
|
|
|
|
(
|
|
|
|
|
|
|
|
api_data["data"][0]["actual_fail"]
|
|
|
|
|
|
|
|
if api_data
|
|
|
|
|
|
|
|
else 1
|
|
|
|
|
|
|
|
), # raw_cm_interval (minimal 1 karena minimal 1x OH)
|
|
|
|
0, # raw_cm_material_cost
|
|
|
|
0, # raw_cm_material_cost
|
|
|
|
0, # raw_cm_labor_time
|
|
|
|
0, # raw_cm_labor_time
|
|
|
|
0, # raw_cm_labor_human
|
|
|
|
0, # raw_cm_labor_human
|
|
|
|
@ -203,6 +233,13 @@ def query_data():
|
|
|
|
, %s, %s
|
|
|
|
, %s, %s
|
|
|
|
)
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
api_data = await fetch_api_data(
|
|
|
|
|
|
|
|
equipment_id, year, RELIABILITY_APP_URL, token
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
if api_data and "data" in api_data and api_data["data"]:
|
|
|
|
|
|
|
|
print("API data:", api_data["data"][0]["actual_fail"])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
print(f"No API data available for {equipment_id} in year {year}")
|
|
|
|
cursor.execute(
|
|
|
|
cursor.execute(
|
|
|
|
insert_query,
|
|
|
|
insert_query,
|
|
|
|
(
|
|
|
|
(
|
|
|
|
@ -212,9 +249,14 @@ def query_data():
|
|
|
|
seq, # seq
|
|
|
|
seq, # seq
|
|
|
|
1, # is_actual
|
|
|
|
1, # is_actual
|
|
|
|
(
|
|
|
|
(
|
|
|
|
recursive_row["raw_corrective_failure_interval"] + 1
|
|
|
|
api_data["data"][0]["actual_fail"]
|
|
|
|
if recursive_row
|
|
|
|
if api_data
|
|
|
|
else 1
|
|
|
|
else (
|
|
|
|
|
|
|
|
recursive_row["raw_corrective_failure_interval"]
|
|
|
|
|
|
|
|
+ 1
|
|
|
|
|
|
|
|
if recursive_row
|
|
|
|
|
|
|
|
else 1
|
|
|
|
|
|
|
|
)
|
|
|
|
), # raw_cm_interval nanti ambil dari API reliability predict
|
|
|
|
), # raw_cm_interval nanti ambil dari API reliability predict
|
|
|
|
(
|
|
|
|
(
|
|
|
|
recursive_row["raw_corrective_material_cost"]
|
|
|
|
recursive_row["raw_corrective_material_cost"]
|
|
|
|
@ -323,6 +365,7 @@ def query_data():
|
|
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
finally:
|
|
|
|
# Menutup koneksi
|
|
|
|
# Menutup koneksi
|
|
|
|
|
|
|
|
print("========Process finished and connection closed.========")
|
|
|
|
if connection or connection_wo_db:
|
|
|
|
if connection or connection_wo_db:
|
|
|
|
cursor.close()
|
|
|
|
cursor.close()
|
|
|
|
cursor_wo.close()
|
|
|
|
cursor_wo.close()
|
|
|
|
|