Some checks failed
Tests / Backend Tests (Python) (3.10) (push) Has been cancelled
Tests / Backend Tests (Python) (3.11) (push) Has been cancelled
Tests / Backend Tests (Python) (3.12) (push) Has been cancelled
Tests / Frontend Tests (JS) (push) Has been cancelled
Tests / Integration Tests (push) Has been cancelled
Tests / All Tests Passed (push) Has been cancelled
56 lines
2.6 KiB
Python
56 lines
2.6 KiB
Python
"""Docker Alert model for Homelab Automation."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, Index
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.sql import func
|
|
|
|
from .database import Base
|
|
|
|
|
|
class DockerAlert(Base):
|
|
"""Model representing a Docker alert for container issues."""
|
|
__tablename__ = "docker_alerts"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
host_id: Mapped[str] = mapped_column(String, ForeignKey("hosts.id", ondelete="CASCADE"), nullable=False)
|
|
container_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
severity: Mapped[str] = mapped_column(String(20), nullable=False, default="warning") # warning/error/critical
|
|
state: Mapped[str] = mapped_column(String(20), nullable=False, default="open") # open/closed/acknowledged
|
|
message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
opened_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
|
closed_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
acknowledged_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
acknowledged_by: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
last_notified_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
# Relationship to host
|
|
host: Mapped["Host"] = relationship("Host", back_populates="docker_alerts")
|
|
|
|
__table_args__ = (
|
|
Index("idx_docker_alerts_state_host", "state", "host_id"),
|
|
{"sqlite_autoincrement": True},
|
|
)
|
|
|
|
def __repr__(self) -> str: # pragma: no cover
|
|
return f"<DockerAlert id={self.id} container={self.container_name} severity={self.severity}>"
|
|
|
|
def to_dict(self) -> dict:
|
|
"""Convert to dictionary for API responses."""
|
|
return {
|
|
"id": self.id,
|
|
"host_id": self.host_id,
|
|
"container_name": self.container_name,
|
|
"severity": self.severity,
|
|
"state": self.state,
|
|
"message": self.message,
|
|
"opened_at": self.opened_at.isoformat() if self.opened_at else None,
|
|
"closed_at": self.closed_at.isoformat() if self.closed_at else None,
|
|
"acknowledged_at": self.acknowledged_at.isoformat() if self.acknowledged_at else None,
|
|
"acknowledged_by": self.acknowledged_by,
|
|
"last_notified_at": self.last_notified_at.isoformat() if self.last_notified_at else None,
|
|
}
|