maintenance.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """Maintenance tracking models."""
  2. from datetime import datetime
  3. from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Index, String, Text, func
  4. from sqlalchemy.orm import Mapped, mapped_column, relationship
  5. from backend.app.core.database import Base
  6. class MaintenanceType(Base):
  7. """Defines a type of maintenance task with default interval."""
  8. __tablename__ = "maintenance_types"
  9. id: Mapped[int] = mapped_column(primary_key=True)
  10. name: Mapped[str] = mapped_column(String(100))
  11. description: Mapped[str | None] = mapped_column(Text)
  12. default_interval_hours: Mapped[float] = mapped_column(Float, default=100.0)
  13. # Interval type: "hours" (print hours) or "days" (calendar days)
  14. interval_type: Mapped[str] = mapped_column(String(20), default="hours")
  15. icon: Mapped[str | None] = mapped_column(String(50)) # Icon name for UI
  16. wiki_url: Mapped[str | None] = mapped_column(String(500)) # Documentation link
  17. is_system: Mapped[bool] = mapped_column(Boolean, default=False) # Pre-defined vs custom
  18. is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) # Hidden/removed type
  19. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  20. # Relationships
  21. printer_maintenance: Mapped[list["PrinterMaintenance"]] = relationship(
  22. back_populates="maintenance_type", cascade="all, delete-orphan"
  23. )
  24. class PrinterMaintenance(Base):
  25. """Tracks maintenance status for a specific printer."""
  26. __tablename__ = "printer_maintenance"
  27. # One row per printer and type: they are created on first read (FO-C14).
  28. __table_args__ = (Index("uq_printer_maintenance_printer_type", "printer_id", "maintenance_type_id", unique=True),)
  29. id: Mapped[int] = mapped_column(primary_key=True)
  30. printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"))
  31. maintenance_type_id: Mapped[int] = mapped_column(ForeignKey("maintenance_types.id", ondelete="CASCADE"))
  32. # Custom interval for this printer (overrides default if set)
  33. custom_interval_hours: Mapped[float | None] = mapped_column(Float, nullable=True)
  34. # Custom interval type for this printer (overrides default if set)
  35. custom_interval_type: Mapped[str | None] = mapped_column(String(20), nullable=True)
  36. # Tracking
  37. enabled: Mapped[bool] = mapped_column(Boolean, default=True)
  38. last_performed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  39. last_performed_hours: Mapped[float] = mapped_column(Float, default=0.0) # Hours at last reset
  40. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  41. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  42. # Relationships
  43. printer: Mapped["Printer"] = relationship(back_populates="maintenance_items")
  44. maintenance_type: Mapped["MaintenanceType"] = relationship(back_populates="printer_maintenance")
  45. history: Mapped[list["MaintenanceHistory"]] = relationship(
  46. back_populates="printer_maintenance", cascade="all, delete-orphan"
  47. )
  48. class MaintenanceHistory(Base):
  49. """Log of maintenance actions performed."""
  50. __tablename__ = "maintenance_history"
  51. id: Mapped[int] = mapped_column(primary_key=True)
  52. printer_maintenance_id: Mapped[int] = mapped_column(ForeignKey("printer_maintenance.id", ondelete="CASCADE"))
  53. performed_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  54. hours_at_maintenance: Mapped[float] = mapped_column(Float, default=0.0)
  55. notes: Mapped[str | None] = mapped_column(Text, nullable=True)
  56. # Relationships
  57. printer_maintenance: Mapped["PrinterMaintenance"] = relationship(back_populates="history")
  58. class MaintenanceLogEntry(Base):
  59. """One maintenance reset, kept as the audit log of a printer.
  60. Unlike ``maintenance_history`` (which goes with its printer task), an entry
  61. outlives the printer, the task and the user: their names are stored as
  62. they were at the reset, and the links to them are SET NULL on delete. An
  63. entry whose printer is gone is still in the fleet-wide export.
  64. """
  65. __tablename__ = "maintenance_log_entries"
  66. id: Mapped[int] = mapped_column(primary_key=True)
  67. printer_id: Mapped[int | None] = mapped_column(
  68. ForeignKey("printers.id", ondelete="SET NULL"), nullable=True, index=True
  69. )
  70. printer_name: Mapped[str] = mapped_column(String(255))
  71. maintenance_type_id: Mapped[int | None] = mapped_column(
  72. ForeignKey("maintenance_types.id", ondelete="SET NULL"), nullable=True, index=True
  73. )
  74. maintenance_type_name: Mapped[str] = mapped_column(String(100))
  75. hours_at_maintenance: Mapped[float] = mapped_column(Float, default=0.0)
  76. notes: Mapped[str | None] = mapped_column(Text, nullable=True)
  77. performed_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  78. # NULL when authentication is off.
  79. performed_by_username: Mapped[str | None] = mapped_column(String(100), nullable=True)
  80. performed_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), index=True)
  81. # The maintenance_history row written with it. That row goes with its
  82. # printer task, so the link is SET NULL: left dangling, SQLite would give
  83. # its id to the next reset, possibly another printer's, and deleting this
  84. # entry would delete that reset's history. The upgrade copies history rows
  85. # not yet here.
  86. history_id: Mapped[int | None] = mapped_column(
  87. ForeignKey("maintenance_history.id", ondelete="SET NULL"), nullable=True, index=True
  88. )
  89. # Import at end to avoid circular imports
  90. from backend.app.models.printer import Printer # noqa: E402