|
|
|
|
@ -15,6 +15,8 @@ from src.config import RELIABILITY_APP_URL
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
|
|
from src.modules.equipment.run import main
|
|
|
|
|
from src.modules.equipment.Prediksi import main as predict_main
|
|
|
|
|
from src.modules.equipment.Eac import main as eac_main
|
|
|
|
|
import datetime
|
|
|
|
|
import math
|
|
|
|
|
from sqlalchemy import text
|
|
|
|
|
@ -285,15 +287,17 @@ async def generate_transaction(
|
|
|
|
|
*, db_session: DbSession, data_in: EquipmentCreate, token
|
|
|
|
|
):
|
|
|
|
|
# Delete all existing master records for this asset number and prediction data
|
|
|
|
|
query = (
|
|
|
|
|
Delete(EquipmentTransactionRecords)
|
|
|
|
|
.where(EquipmentTransactionRecords.assetnum == data_in.assetnum)
|
|
|
|
|
.where(EquipmentTransactionRecords.is_actual == 0)
|
|
|
|
|
)
|
|
|
|
|
await db_session.execute(query)
|
|
|
|
|
await db_session.commit()
|
|
|
|
|
# query = (
|
|
|
|
|
# Delete(EquipmentTransactionRecords)
|
|
|
|
|
# .where(EquipmentTransactionRecords.assetnum == data_in.assetnum)
|
|
|
|
|
# .where(EquipmentTransactionRecords.is_actual == 0)
|
|
|
|
|
# )
|
|
|
|
|
# await db_session.execute(query)
|
|
|
|
|
# await db_session.commit()
|
|
|
|
|
"""Generate transaction for equipment."""
|
|
|
|
|
prediction = await main(data_in.assetnum, token, RELIABILITY_APP_URL)
|
|
|
|
|
# prediction = await main(data_in.assetnum, token, RELIABILITY_APP_URL)
|
|
|
|
|
prediction = await predict_main(assetnum=data_in.assetnum, token=token)
|
|
|
|
|
eac = eac_main(assetnum=data_in.assetnum)
|
|
|
|
|
|
|
|
|
|
# # Fetch data from external API
|
|
|
|
|
# async def fetch_api_data(assetnum: str, year: int) -> dict:
|
|
|
|
|
@ -396,7 +400,7 @@ async def generate_transaction(
|
|
|
|
|
|
|
|
|
|
# # Return the number of transactions created
|
|
|
|
|
# return len(transactions)
|
|
|
|
|
return prediction
|
|
|
|
|
return prediction, eac
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create(*, db_session: DbSession, equipment_in: EquipmentCreate, token):
|
|
|
|
|
@ -411,7 +415,10 @@ async def create(*, db_session: DbSession, equipment_in: EquipmentCreate, token)
|
|
|
|
|
async def update(
|
|
|
|
|
*, db_session: DbSession, equipment: Equipment, equipment_in: EquipmentUpdate, token
|
|
|
|
|
):
|
|
|
|
|
"""Updates a document."""
|
|
|
|
|
"""Updates a document and re-simulates transaction for the asset."""
|
|
|
|
|
|
|
|
|
|
# capture original assetnum (optional use)
|
|
|
|
|
old_assetnum = equipment.assetnum
|
|
|
|
|
|
|
|
|
|
data = equipment_in.model_dump()
|
|
|
|
|
update_data = equipment_in.model_dump(exclude_defaults=True)
|
|
|
|
|
@ -421,11 +428,17 @@ async def update(
|
|
|
|
|
|
|
|
|
|
await db_session.commit()
|
|
|
|
|
|
|
|
|
|
updated_data = vars(equipment)
|
|
|
|
|
# equipment_create = EquipmentCreate(**updated_data)
|
|
|
|
|
# await generate_transaction(
|
|
|
|
|
# db_session=db_session, data_in=equipment_create, token=token
|
|
|
|
|
# )
|
|
|
|
|
# prepare a clean dict of attributes for return / recreate input model
|
|
|
|
|
updated_data = {k: v for k, v in vars(equipment).items() if not k.startswith("_")}
|
|
|
|
|
|
|
|
|
|
# Re-run generate_transaction for this equipment's assetnum.
|
|
|
|
|
# Build an EquipmentCreate from the updated SQLAlchemy object and call the generator.
|
|
|
|
|
try:
|
|
|
|
|
equipment_create = EquipmentCreate(**updated_data)
|
|
|
|
|
await generate_transaction(db_session=db_session, data_in=equipment_create, token=token)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# don't break the update if resimulation fails — log/print for visibility
|
|
|
|
|
print(f"Resimulation failed for assetnum {updated_data.get('assetnum')}: {e}")
|
|
|
|
|
|
|
|
|
|
return updated_data
|
|
|
|
|
|
|
|
|
|
|