feat: koleksi script
parent
6bd918f5a2
commit
4fd187237a
@ -0,0 +1,227 @@
|
|||||||
|
import psycopg2
|
||||||
|
from psycopg2.extras import DictCursor
|
||||||
|
from uuid import uuid4
|
||||||
|
from datetime import datetime
|
||||||
|
from config import get_connection
|
||||||
|
|
||||||
|
|
||||||
|
def get_recursive_query(cursor, equipment_id, worktype='CM'):
|
||||||
|
|
||||||
|
"""
|
||||||
|
Fungsi untuk menjalankan query rekursif berdasarkan equipment_id dan worktype.
|
||||||
|
worktype memiliki nilai default 'CM'.
|
||||||
|
"""
|
||||||
|
query = f"""
|
||||||
|
SELECT
|
||||||
|
ROW_NUMBER() OVER (ORDER BY tbl.assetnum, tbl.year, tbl.worktype) AS seq,
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
a.worktype,
|
||||||
|
a.assetnum,
|
||||||
|
DATE_PART('year', TO_TIMESTAMP(a.reportdate, 'YYYY-MM-DD HH24:MI:SS.US')) AS year,
|
||||||
|
COUNT(a.wonum) AS raw_corrective_failure_interval,
|
||||||
|
SUM(a.total_cost_max) AS raw_corrective_material_cost,
|
||||||
|
-- SUM(
|
||||||
|
-- (
|
||||||
|
-- (EXTRACT(HOUR FROM TO_TIMESTAMP(wo_finish, 'YYYY-MM-DD-HH24.MI.SS.US')) +
|
||||||
|
-- EXTRACT(MINUTE FROM TO_TIMESTAMP(wo_finish, 'YYYY-MM-DD-HH24.MI.SS.US')) / 60)
|
||||||
|
-- -
|
||||||
|
-- (EXTRACT(HOUR FROM TO_TIMESTAMP(wo_start, 'YYYY-MM-DD-HH24.MI.SS.US')) +
|
||||||
|
-- EXTRACT(MINUTE FROM TO_TIMESTAMP(wo_start, 'YYYY-MM-DD-HH24.MI.SS.US')) / 60)
|
||||||
|
-- )
|
||||||
|
-- *
|
||||||
|
-- ((DATE_PART('day', TO_TIMESTAMP(a.wo_finish, 'YYYY-MM-DD-HH24.MI.SS.US') -
|
||||||
|
-- TO_TIMESTAMP(a.wo_start, 'YYYY-MM-DD-HH24.MI.SS.US'))) + 1)
|
||||||
|
-- ) AS raw_corrective_labor_time_jam,
|
||||||
|
ROUND(SUM(EXTRACT(EPOCH FROM (TO_TIMESTAMP(a.actfinish, 'YYYY-MM-DD HH24:MI:SS') - TO_TIMESTAMP(a.actstart, 'YYYY-MM-DD HH24:MI:SS'))) / 3600), 2) AS raw_corrective_labor_time_jam,
|
||||||
|
SUM(a.jumlah_labor) AS raw_corrective_labor_technician
|
||||||
|
FROM
|
||||||
|
public.dl_wo_staging AS a
|
||||||
|
WHERE
|
||||||
|
a.unit = '3'
|
||||||
|
GROUP BY
|
||||||
|
a.assetnum,
|
||||||
|
DATE_PART('year', TO_TIMESTAMP(a.reportdate, 'YYYY-MM-DD HH24:MI:SS.US')),
|
||||||
|
a.worktype
|
||||||
|
) AS tbl
|
||||||
|
WHERE
|
||||||
|
tbl.worktype = '{worktype}'
|
||||||
|
AND tbl.assetnum = '{equipment_id}'
|
||||||
|
ORDER BY
|
||||||
|
tbl.assetnum,
|
||||||
|
tbl.year,
|
||||||
|
tbl.worktype;
|
||||||
|
"""
|
||||||
|
# Eksekusi query dan fetch hasil
|
||||||
|
cursor.execute(query)
|
||||||
|
return cursor.fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
def get_data_tahun(cursor):
|
||||||
|
query = f"""
|
||||||
|
select * from lcc_ms_year_data
|
||||||
|
"""
|
||||||
|
# Eksekusi query dan fetch hasil
|
||||||
|
cursor.execute(query)
|
||||||
|
return cursor.fetchall()
|
||||||
|
|
||||||
|
def query_data():
|
||||||
|
connection = None
|
||||||
|
try:
|
||||||
|
# Mendapatkan koneksi dari config.py
|
||||||
|
connection = get_connection()
|
||||||
|
if connection is None:
|
||||||
|
print("Koneksi ke database gagal.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Membuat cursor menggunakan DictCursor
|
||||||
|
cursor = connection.cursor(cursor_factory=DictCursor)
|
||||||
|
|
||||||
|
|
||||||
|
# TRUNCATE DATA
|
||||||
|
truncate_query = "TRUNCATE TABLE lcc_tr_data"
|
||||||
|
cursor.execute(truncate_query)
|
||||||
|
|
||||||
|
# Query untuk mendapatkan semua data dari tabel `lcc_ms_equipment_data`
|
||||||
|
query_main = "SELECT * FROM lcc_ms_equipment_data"
|
||||||
|
cursor.execute(query_main)
|
||||||
|
|
||||||
|
# Fetch semua hasil query
|
||||||
|
results = cursor.fetchall()
|
||||||
|
|
||||||
|
# Tahun sekarang
|
||||||
|
current_year = datetime.now().year
|
||||||
|
|
||||||
|
# Looping untuk setiap equipment_id
|
||||||
|
for row in results:
|
||||||
|
equipment_id = row['equipment_id'] # Mengambil equipment_id dari hasil query
|
||||||
|
forecasting_start_year = row['forecasting_start_year'] - 1
|
||||||
|
|
||||||
|
# CM
|
||||||
|
recursive_results = get_recursive_query(cursor, equipment_id, worktype='CM')
|
||||||
|
# PM
|
||||||
|
data_pm = get_recursive_query(cursor, equipment_id, worktype='PM')
|
||||||
|
# OH
|
||||||
|
data_oh = get_recursive_query(cursor, equipment_id, worktype='OH')
|
||||||
|
# Data Tahun
|
||||||
|
data_tahunan = get_data_tahun(cursor)
|
||||||
|
|
||||||
|
seq = 0
|
||||||
|
# Looping untuk setiap tahun
|
||||||
|
for year in range(forecasting_start_year, current_year + 1):
|
||||||
|
# Filter data berdasarkan tahun
|
||||||
|
recursive_row = next((r for r in recursive_results if r['year'] == year), None) # CM Corrective Maintenance
|
||||||
|
data_pm_row = next((r for r in data_pm if r['year'] == year), None)
|
||||||
|
data_oh_row = next((r for r in data_oh if r['year'] == year), None)
|
||||||
|
data_tahunan_row = next((r for r in data_tahunan if r['year'] == year), None)
|
||||||
|
|
||||||
|
# Cek apakah data sudah ada
|
||||||
|
check_query = """
|
||||||
|
SELECT COUNT(*) FROM lcc_tr_data
|
||||||
|
WHERE equipment_id = %s AND tahun = %s
|
||||||
|
"""
|
||||||
|
cursor.execute(check_query, (equipment_id, year))
|
||||||
|
data_exists = cursor.fetchone()[0]
|
||||||
|
|
||||||
|
|
||||||
|
if not data_exists:
|
||||||
|
# Insert data jika belum ada
|
||||||
|
if not recursive_row and not data_pm_row and not data_oh_row:
|
||||||
|
# Jika data recursive_row tidak ada
|
||||||
|
insert_query = """
|
||||||
|
INSERT INTO lcc_tr_data (
|
||||||
|
id, equipment_id, tahun, seq, is_actual,
|
||||||
|
raw_cm_interval, raw_cm_material_cost,
|
||||||
|
raw_cm_labor_time, raw_cm_labor_human
|
||||||
|
, raw_pm_interval, raw_pm_material_cost, raw_pm_labor_time, raw_pm_labor_human
|
||||||
|
, raw_oh_material_cost, raw_oh_labor_time, raw_oh_labor_human
|
||||||
|
, "raw_loss_output_MW", raw_loss_output_price
|
||||||
|
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s
|
||||||
|
, %s, %s, %s, %s
|
||||||
|
, %s, %s, %s
|
||||||
|
, %s, %s
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
cursor.execute(
|
||||||
|
insert_query,
|
||||||
|
(
|
||||||
|
str(uuid4()), # id
|
||||||
|
equipment_id, # equipment_id
|
||||||
|
year, # tahun
|
||||||
|
seq, # seq
|
||||||
|
1, # is_actual
|
||||||
|
1, # raw_cm_interval (minimal 1 karena minimal 1x OH)
|
||||||
|
0, # raw_cm_material_cost
|
||||||
|
0, # raw_cm_labor_time
|
||||||
|
0 # raw_cm_labor_human
|
||||||
|
, 1 # pm interval set default 1
|
||||||
|
,0,0,0
|
||||||
|
, 0,0,0
|
||||||
|
, (data_tahunan_row['total_lost'] if data_tahunan_row else 0)
|
||||||
|
, data_tahunan_row['rp_per_kwh'] if data_tahunan_row else 0
|
||||||
|
)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# Jika data recursive_row ada
|
||||||
|
insert_query = """
|
||||||
|
INSERT INTO lcc_tr_data (
|
||||||
|
id, equipment_id, tahun, seq, is_actual,
|
||||||
|
raw_cm_interval, raw_cm_material_cost,
|
||||||
|
raw_cm_labor_time, raw_cm_labor_human
|
||||||
|
, raw_pm_interval, raw_pm_material_cost, raw_pm_labor_time, raw_pm_labor_human
|
||||||
|
, raw_oh_material_cost, raw_oh_labor_time, raw_oh_labor_human
|
||||||
|
, "raw_loss_output_MW", raw_loss_output_price
|
||||||
|
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s
|
||||||
|
, %s, %s, %s, %s
|
||||||
|
, %s, %s, %s
|
||||||
|
, %s, %s
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
cursor.execute(
|
||||||
|
insert_query,
|
||||||
|
(
|
||||||
|
str(uuid4()), # id
|
||||||
|
equipment_id, # equipment_id
|
||||||
|
year, # tahun
|
||||||
|
seq, # seq
|
||||||
|
1, # is_actual
|
||||||
|
recursive_row['raw_corrective_failure_interval']+1 if recursive_row else 1, # raw_cm_interval
|
||||||
|
recursive_row['raw_corrective_material_cost'] if recursive_row else 0, # raw_cm_material_cost
|
||||||
|
(recursive_row['raw_corrective_labor_time_jam'] or 0) if recursive_row else 0, # raw_cm_labor_time
|
||||||
|
(max(recursive_row['raw_corrective_labor_technician'], 1) if recursive_row['raw_corrective_labor_time_jam'] else 0) if recursive_row else 0, # raw_cm_labor_human
|
||||||
|
|
||||||
|
1, # raw_pm_interval
|
||||||
|
data_pm_row['raw_corrective_material_cost'] if data_pm_row else 0, # raw_pm_material_cost
|
||||||
|
(data_pm_row['raw_corrective_labor_time_jam'] or 0) if data_pm_row else 0, # raw_pm_labor_time
|
||||||
|
(max(data_pm_row['raw_corrective_labor_technician'], 1) if data_pm_row['raw_corrective_labor_time_jam'] else 0) if data_pm_row else 0, # raw_pm_labor_human
|
||||||
|
|
||||||
|
data_oh_row['raw_corrective_material_cost'] if data_oh_row else 0, # raw_oh_material_cost
|
||||||
|
(data_oh_row['raw_corrective_labor_time_jam'] or 0) if data_oh_row else 0, # raw_oh_labor_time
|
||||||
|
(max(data_oh_row['raw_corrective_labor_technician'], 1) if data_oh_row['raw_corrective_labor_time_jam'] else 0) if data_oh_row else 0, # raw_oh_labor_human
|
||||||
|
|
||||||
|
(data_tahunan_row['total_lost'] if data_tahunan_row else 0)/(recursive_row['raw_corrective_failure_interval']+1 if recursive_row else 1), # raw_loss_output_MW
|
||||||
|
data_tahunan_row['rp_per_kwh'] if data_tahunan_row else 0
|
||||||
|
|
||||||
|
)
|
||||||
|
)
|
||||||
|
print(f"Data inserted for {equipment_id} in year {year}")
|
||||||
|
|
||||||
|
seq = seq+1
|
||||||
|
|
||||||
|
# Commit perubahan
|
||||||
|
connection.commit()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print("Error saat menjalankan query:", e)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
# Menutup koneksi
|
||||||
|
if connection:
|
||||||
|
cursor.close()
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
# Panggil fungsi
|
||||||
|
if __name__ == "__main__":
|
||||||
|
query_data()
|
||||||
Loading…
Reference in New Issue