You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6 KiB
Python

import asyncio
import time
# clean absolute imports
from src.modules.equipment.insert_actual_data import query_data
from src.modules.equipment.Prediksi import Prediksi, main as predict_run
from src.modules.equipment.Eac import Eac, main as eac_run
def format_execution_time(execution_time):
if execution_time >= 3600:
hours = int(execution_time // 3600)
minutes = int((execution_time % 3600) // 60)
seconds = execution_time % 60
return f"{hours}h {minutes}m {seconds:.2f}s."
elif execution_time >= 60:
minutes = int(execution_time // 60)
seconds = execution_time % 60
return f"{minutes}m {seconds:.2f}s."
else:
return f"{execution_time:.2f} seconds."
# Alternative calling function to just predict and calculate eac without inserting actual data
async def simulate():
start_time = time.time()
print("Starting simulation (predict + eac)...")
try:
prediction_result = await predict_run()
if prediction_result is False:
print("Prediction step failed or was skipped. Skipping EAC run.")
return
except Exception as e:
print(f"Error in predict_run: {str(e)}")
return
try:
result = eac_run()
if asyncio.iscoroutine(result):
result = await result
print("EAC run completed.")
except Exception as e:
print(f"Error in eac_run: {str(e)}")
return
end_time = time.time()
message = f"Simulation finished in {format_execution_time(end_time - start_time)}"
print(message)
return message
# Panggil fungsi
async def main():
start_time = time.time()
try:
await query_data()
except Exception as e:
print(f"Error in query_data: {str(e)}")
return
try:
prediction_result = await predict_run()
if prediction_result is False:
print("Prediction step failed or was skipped. Skipping EAC run.")
return
except Exception as e:
print(f"Error in predict_run: {str(e)}")
return
try:
result = eac_run()
if asyncio.iscoroutine(result):
result = await result
print("EAC run completed.")
except Exception as e:
print(f"Error in eac_run: {str(e)}")
return
end_time = time.time()
message = f"Script calculation finished in {format_execution_time(end_time - start_time)}"
print(message)
return message
if __name__ == "__main__":
import sys
# Use 'simulate' argument to run without query_data
if len(sys.argv) > 1 and sys.argv[1] == "simulate":
asyncio.run(simulate())
else:
asyncio.run(main())