maintenance.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Maintenance tracking schemas."""
  2. from datetime import datetime
  3. from pydantic import BaseModel, Field
  4. from backend.app.schemas.not_null import IgnoresNullForRequiredColumns
  5. # Maintenance Type schemas
  6. class MaintenanceTypeBase(BaseModel):
  7. name: str = Field(..., min_length=1, max_length=100)
  8. description: str | None = None
  9. default_interval_hours: float = Field(default=100.0, ge=1.0)
  10. # "hours" = print hours, "days" = calendar days
  11. interval_type: str = Field(default="hours", pattern="^(hours|days)$")
  12. icon: str | None = None
  13. wiki_url: str | None = None # Documentation link for custom types
  14. class MaintenanceTypeCreate(MaintenanceTypeBase):
  15. pass
  16. class MaintenanceTypeUpdate(IgnoresNullForRequiredColumns):
  17. __orm_table__ = "maintenance_types"
  18. name: str | None = None
  19. description: str | None = None
  20. default_interval_hours: float | None = Field(default=None, ge=1.0)
  21. interval_type: str | None = Field(default=None, pattern="^(hours|days)$")
  22. icon: str | None = None
  23. wiki_url: str | None = None
  24. class MaintenanceTypeResponse(MaintenanceTypeBase):
  25. id: int
  26. is_system: bool
  27. created_at: datetime
  28. class Config:
  29. from_attributes = True
  30. # Printer Maintenance schemas
  31. class PrinterMaintenanceBase(BaseModel):
  32. printer_id: int
  33. maintenance_type_id: int
  34. custom_interval_hours: float | None = None
  35. enabled: bool = True
  36. class PrinterMaintenanceUpdate(IgnoresNullForRequiredColumns):
  37. __orm_table__ = "printer_maintenance"
  38. custom_interval_hours: float | None = None
  39. custom_interval_type: str | None = Field(default=None, pattern="^(hours|days)$")
  40. enabled: bool | None = None
  41. class PrinterMaintenanceResponse(BaseModel):
  42. id: int
  43. printer_id: int
  44. maintenance_type_id: int
  45. maintenance_type: MaintenanceTypeResponse
  46. custom_interval_hours: float | None
  47. enabled: bool
  48. last_performed_at: datetime | None
  49. last_performed_hours: float
  50. created_at: datetime
  51. updated_at: datetime
  52. class Config:
  53. from_attributes = True
  54. # Maintenance History schemas
  55. class MaintenanceHistoryBase(BaseModel):
  56. notes: str | None = None
  57. class MaintenanceHistoryResponse(MaintenanceHistoryBase):
  58. id: int
  59. printer_maintenance_id: int
  60. performed_at: datetime
  61. hours_at_maintenance: float
  62. class Config:
  63. from_attributes = True
  64. # Combined status response for frontend
  65. class MaintenanceStatus(BaseModel):
  66. """Maintenance status for a printer with calculated values."""
  67. id: int
  68. printer_id: int
  69. printer_name: str
  70. printer_model: str | None # For model-specific documentation links
  71. maintenance_type_id: int
  72. maintenance_type_name: str
  73. maintenance_type_icon: str | None
  74. maintenance_type_wiki_url: str | None # Custom wiki URL for the type
  75. enabled: bool
  76. # Interval configuration
  77. interval_hours: float # custom or default (hours for print-based, days for time-based)
  78. interval_type: str # "hours" or "days"
  79. # For print-hour based maintenance
  80. current_hours: float # total print hours for printer
  81. hours_since_maintenance: float # current - last_performed
  82. hours_until_due: float # interval - hours_since (for hours type)
  83. # For time-based maintenance
  84. days_since_maintenance: float | None # days since last performed
  85. days_until_due: float | None # for days type
  86. # Status flags
  87. is_due: bool # hours_until_due <= 0 OR days_until_due <= 0
  88. is_warning: bool # within 10% of interval
  89. last_performed_at: datetime | None
  90. class PrinterMaintenanceOverview(BaseModel):
  91. """Overview of all maintenance items for a printer."""
  92. printer_id: int
  93. printer_name: str
  94. printer_model: str | None # For model-specific documentation links
  95. total_print_hours: float
  96. maintenance_items: list[MaintenanceStatus]
  97. due_count: int
  98. warning_count: int
  99. class PerformMaintenanceRequest(BaseModel):
  100. """Request to mark maintenance as performed."""
  101. notes: str | None = None