|
|
|
|
@ -9,6 +9,7 @@ from typing import Optional
|
|
|
|
|
|
|
|
|
|
from src.database.core import DbSession
|
|
|
|
|
from src.auth.service import CurrentUser
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_master_by_assetnum(
|
|
|
|
|
@ -63,7 +64,14 @@ async def get_master_by_assetnum(
|
|
|
|
|
)
|
|
|
|
|
min_seq = min_record[1] if min_record else None
|
|
|
|
|
|
|
|
|
|
return equipment_master_record, equipment_record, records, min_eac_value, min_seq, last_actual_year
|
|
|
|
|
return (
|
|
|
|
|
equipment_master_record,
|
|
|
|
|
equipment_record,
|
|
|
|
|
records,
|
|
|
|
|
min_eac_value,
|
|
|
|
|
min_seq,
|
|
|
|
|
last_actual_year,
|
|
|
|
|
)
|
|
|
|
|
# return result.scalars().all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -91,7 +99,9 @@ async def get_all(
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def generate_transaction(*, db_session: DbSession, data_in: EquipmentCreate):
|
|
|
|
|
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(MasterRecords)
|
|
|
|
|
@ -101,6 +111,23 @@ async def generate_transaction(*, db_session: DbSession, data_in: EquipmentCreat
|
|
|
|
|
await db_session.execute(query)
|
|
|
|
|
await db_session.commit()
|
|
|
|
|
"""Generate transaction for equipment."""
|
|
|
|
|
|
|
|
|
|
# Fetch data from external API
|
|
|
|
|
async def fetch_api_data(assetnum: str, year: int) -> dict:
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
try:
|
|
|
|
|
response = await client.get(
|
|
|
|
|
f"http://192.168.1.82:8000/reliability/main/number-of-failures/{assetnum}/{year}/{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 {}
|
|
|
|
|
|
|
|
|
|
# Initialize base transaction with default values
|
|
|
|
|
base_transaction = {
|
|
|
|
|
"assetnum": data_in.assetnum,
|
|
|
|
|
"is_actual": 0,
|
|
|
|
|
@ -142,10 +169,41 @@ async def generate_transaction(*, db_session: DbSession, data_in: EquipmentCreat
|
|
|
|
|
|
|
|
|
|
transactions = []
|
|
|
|
|
|
|
|
|
|
# Query existing records with is_actual=1
|
|
|
|
|
actual_years_query = (
|
|
|
|
|
Select(MasterRecords.tahun)
|
|
|
|
|
.filter(MasterRecords.assetnum == data_in.assetnum)
|
|
|
|
|
.filter(MasterRecords.is_actual == 1)
|
|
|
|
|
)
|
|
|
|
|
result = await db_session.execute(actual_years_query)
|
|
|
|
|
actual_years = {row[0] for row in result.all()}
|
|
|
|
|
|
|
|
|
|
for sequence, year in enumerate(
|
|
|
|
|
range(data_in.acquisition_year - 1, data_in.forecasting_target_year + 1), 0
|
|
|
|
|
):
|
|
|
|
|
# Skip if year already has actual data
|
|
|
|
|
if year in actual_years:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
transaction = base_transaction.copy()
|
|
|
|
|
# Update values from API
|
|
|
|
|
api_data = await fetch_api_data(data_in.assetnum, year)
|
|
|
|
|
|
|
|
|
|
if api_data:
|
|
|
|
|
# Get current num_fail
|
|
|
|
|
current_num_fail = api_data["data"][0]["num_fail"]
|
|
|
|
|
|
|
|
|
|
# Calculate sum of previous failures for this asset
|
|
|
|
|
previous_failures_query = (
|
|
|
|
|
Select(func.sum(MasterRecords.raw_cm_interval))
|
|
|
|
|
.filter(MasterRecords.assetnum == data_in.assetnum)
|
|
|
|
|
.filter(MasterRecords.tahun < year)
|
|
|
|
|
)
|
|
|
|
|
previous_failures_result = await db_session.execute(previous_failures_query)
|
|
|
|
|
previous_failures_sum = previous_failures_result.scalar() or 0
|
|
|
|
|
|
|
|
|
|
# Update with current minus sum of previous
|
|
|
|
|
transaction.update({"raw_cm_interval": current_num_fail - previous_failures_sum})
|
|
|
|
|
transaction.update({"tahun": int(year), "seq": int(sequence)})
|
|
|
|
|
transactions.append(MasterRecords(**transaction))
|
|
|
|
|
|
|
|
|
|
@ -156,29 +214,34 @@ async def generate_transaction(*, db_session: DbSession, data_in: EquipmentCreat
|
|
|
|
|
return len(transactions)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create(*, db_session: DbSession, equipment_in: EquipmentCreate):
|
|
|
|
|
async def create(*, db_session: DbSession, equipment_in: EquipmentCreate, token):
|
|
|
|
|
"""Creates a new document."""
|
|
|
|
|
equipment = Equipment(**equipment_in.model_dump())
|
|
|
|
|
db_session.add(equipment)
|
|
|
|
|
await db_session.commit()
|
|
|
|
|
await generate_transaction(db_session=db_session, data_in=equipment_in)
|
|
|
|
|
await generate_transaction(db_session=db_session, data_in=equipment_in, token=token)
|
|
|
|
|
return equipment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def update(
|
|
|
|
|
*, db_session: DbSession, equipment: Equipment, equipment_in: EquipmentUpdate
|
|
|
|
|
*, db_session: DbSession, equipment: Equipment, equipment_in: EquipmentUpdate, token
|
|
|
|
|
):
|
|
|
|
|
"""Updates a document."""
|
|
|
|
|
data = equipment_in.model_dump()
|
|
|
|
|
|
|
|
|
|
data = equipment_in.model_dump()
|
|
|
|
|
update_data = equipment_in.model_dump(exclude_defaults=True)
|
|
|
|
|
|
|
|
|
|
for field in data:
|
|
|
|
|
if field in update_data:
|
|
|
|
|
setattr(equipment, field, update_data[field])
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return equipment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|