- Updated README.md to reflect new features and usage instructions. - Removed deprecated Dashboard.html component and integrated dashboard functionality directly into the main application. - Revised architecture documentation for clarity and added module map and request flow diagrams. - Enhanced maintenance model to include equipment association and cost tracking. - Updated requirements.txt to include new dependencies (httpx, pandas, numpy). - Improved consumption, maintenance, production, and reporting routes with better validation and response handling. - Added unit tests for maintenance and production routes, ensuring proper CRUD operations and validation. - Enhanced reporting service to calculate and return detailed summary statistics. - Redesigned Dashboard.html for improved user experience and integrated Chart.js for visualizing simulation results.
95 lines
2.6 KiB
Python
95 lines
2.6 KiB
Python
from datetime import date
|
|
from typing import List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from pydantic import BaseModel, PositiveFloat
|
|
from sqlalchemy.orm import Session
|
|
|
|
from config.database import SessionLocal
|
|
from models.maintenance import Maintenance
|
|
|
|
|
|
router = APIRouter(prefix="/api/maintenance", tags=["Maintenance"])
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
class MaintenanceBase(BaseModel):
|
|
equipment_id: int
|
|
scenario_id: int
|
|
maintenance_date: date
|
|
description: Optional[str] = None
|
|
cost: PositiveFloat
|
|
|
|
|
|
class MaintenanceCreate(MaintenanceBase):
|
|
pass
|
|
|
|
|
|
class MaintenanceUpdate(MaintenanceBase):
|
|
pass
|
|
|
|
|
|
class MaintenanceRead(MaintenanceBase):
|
|
id: int
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
def _get_maintenance_or_404(db: Session, maintenance_id: int) -> Maintenance:
|
|
maintenance = db.query(Maintenance).filter(
|
|
Maintenance.id == maintenance_id).first()
|
|
if maintenance is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Maintenance record {maintenance_id} not found",
|
|
)
|
|
return maintenance
|
|
|
|
|
|
@router.post("/", response_model=MaintenanceRead, status_code=status.HTTP_201_CREATED)
|
|
def create_maintenance(maintenance: MaintenanceCreate, db: Session = Depends(get_db)):
|
|
db_maintenance = Maintenance(**maintenance.dict())
|
|
db.add(db_maintenance)
|
|
db.commit()
|
|
db.refresh(db_maintenance)
|
|
return db_maintenance
|
|
|
|
|
|
@router.get("/", response_model=List[MaintenanceRead])
|
|
def list_maintenance(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
return db.query(Maintenance).offset(skip).limit(limit).all()
|
|
|
|
|
|
@router.get("/{maintenance_id}", response_model=MaintenanceRead)
|
|
def get_maintenance(maintenance_id: int, db: Session = Depends(get_db)):
|
|
return _get_maintenance_or_404(db, maintenance_id)
|
|
|
|
|
|
@router.put("/{maintenance_id}", response_model=MaintenanceRead)
|
|
def update_maintenance(
|
|
maintenance_id: int,
|
|
payload: MaintenanceUpdate,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
db_maintenance = _get_maintenance_or_404(db, maintenance_id)
|
|
for field, value in payload.dict().items():
|
|
setattr(db_maintenance, field, value)
|
|
db.commit()
|
|
db.refresh(db_maintenance)
|
|
return db_maintenance
|
|
|
|
|
|
@router.delete("/{maintenance_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_maintenance(maintenance_id: int, db: Session = Depends(get_db)):
|
|
db_maintenance = _get_maintenance_or_404(db, maintenance_id)
|
|
db.delete(db_maintenance)
|
|
db.commit()
|