mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-09-30 11:12:35 +02:00
79 lines
3.2 KiB
Python
79 lines
3.2 KiB
Python
"""Maintenance tracking models."""
|
|
|
|
from datetime import datetime
|
|
from sqlalchemy import String, Boolean, DateTime, Integer, Float, ForeignKey, Text, func
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from backend.app.core.database import Base
|
|
|
|
|
|
class MaintenanceType(Base):
|
|
"""Defines a type of maintenance task with default interval."""
|
|
__tablename__ = "maintenance_types"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(100))
|
|
description: Mapped[str | None] = mapped_column(Text)
|
|
default_interval_hours: Mapped[float] = mapped_column(Float, default=100.0)
|
|
icon: Mapped[str | None] = mapped_column(String(50)) # Icon name for UI
|
|
is_system: Mapped[bool] = mapped_column(Boolean, default=False) # Pre-defined vs custom
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime, server_default=func.now()
|
|
)
|
|
|
|
# Relationships
|
|
printer_maintenance: Mapped[list["PrinterMaintenance"]] = relationship(
|
|
back_populates="maintenance_type", cascade="all, delete-orphan"
|
|
)
|
|
|
|
|
|
class PrinterMaintenance(Base):
|
|
"""Tracks maintenance status for a specific printer."""
|
|
__tablename__ = "printer_maintenance"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"))
|
|
maintenance_type_id: Mapped[int] = mapped_column(ForeignKey("maintenance_types.id", ondelete="CASCADE"))
|
|
|
|
# Custom interval for this printer (overrides default if set)
|
|
custom_interval_hours: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
|
|
# Tracking
|
|
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
last_performed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
last_performed_hours: Mapped[float] = mapped_column(Float, default=0.0) # Hours at last reset
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime, server_default=func.now()
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, server_default=func.now(), onupdate=func.now()
|
|
)
|
|
|
|
# Relationships
|
|
printer: Mapped["Printer"] = relationship(back_populates="maintenance_items")
|
|
maintenance_type: Mapped["MaintenanceType"] = relationship(back_populates="printer_maintenance")
|
|
history: Mapped[list["MaintenanceHistory"]] = relationship(
|
|
back_populates="printer_maintenance", cascade="all, delete-orphan"
|
|
)
|
|
|
|
|
|
class MaintenanceHistory(Base):
|
|
"""Log of maintenance actions performed."""
|
|
__tablename__ = "maintenance_history"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
printer_maintenance_id: Mapped[int] = mapped_column(
|
|
ForeignKey("printer_maintenance.id", ondelete="CASCADE")
|
|
)
|
|
performed_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
|
hours_at_maintenance: Mapped[float] = mapped_column(Float, default=0.0)
|
|
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
|
|
# Relationships
|
|
printer_maintenance: Mapped["PrinterMaintenance"] = relationship(back_populates="history")
|
|
|
|
|
|
# Import at end to avoid circular imports
|
|
from backend.app.models.printer import Printer # noqa: E402
|