# plugins/docker/models.py from __future__ import annotations import uuid from datetime import datetime, timezone from sqlalchemy import DateTime, Float, ForeignKey, Index, Integer, String, Text from sqlalchemy.orm import Mapped, mapped_column from steward.models.base import Base class DockerContainer(Base): """Latest known state per container, scoped to the host that reported it. Collection is per-host via the host agent, so container names are only unique within a host — the natural key is (host_id, name). host_id is NOT NULL: every container arrives through a host_agent ingest that resolves a Host first. Deleting a host cascades its containers away. """ __tablename__ = "docker_containers" host_id: Mapped[str] = mapped_column( String(36), ForeignKey("hosts.id", ondelete="CASCADE"), primary_key=True ) name: Mapped[str] = mapped_column(String(255), primary_key=True) container_id: Mapped[str] = mapped_column(String(64), nullable=False, default="") image: Mapped[str] = mapped_column(String(512), nullable=False, default="") status: Mapped[str] = mapped_column(String(32), nullable=False, default="unknown") # running | stopped | paused | exited | dead cpu_pct: Mapped[float | None] = mapped_column(Float, nullable=True) mem_usage_bytes: Mapped[int | None] = mapped_column(Integer, nullable=True) mem_limit_bytes: Mapped[int | None] = mapped_column(Integer, nullable=True) mem_pct: Mapped[float | None] = mapped_column(Float, nullable=True) restart_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0) ports_json: Mapped[str] = mapped_column(Text, nullable=False, default="[]") # JSON: [{"host_port": 8080, "container_port": 80, "protocol": "tcp"}] started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) scraped_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, default=lambda: datetime.now(timezone.utc), ) class DockerMetric(Base): """Time-series CPU/memory per container — one row per sample per running container, scoped to the reporting host.""" __tablename__ = "docker_metrics" id: Mapped[str] = mapped_column( String(36), primary_key=True, default=lambda: str(uuid.uuid4()) ) host_id: Mapped[str] = mapped_column( String(36), ForeignKey("hosts.id", ondelete="CASCADE"), nullable=False, index=True ) container_name: Mapped[str] = mapped_column(String(255), nullable=False, index=True) scraped_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, default=lambda: datetime.now(timezone.utc), index=True, ) cpu_pct: Mapped[float] = mapped_column(Float, nullable=False, default=0.0) mem_pct: Mapped[float] = mapped_column(Float, nullable=False, default=0.0) mem_usage_bytes: Mapped[int] = mapped_column(Integer, nullable=False, default=0) # Per-container history lookups filter on (host_id, container_name) then sort # by time — a composite index keeps the rows() sparkline queries cheap. __table_args__ = ( Index("ix_docker_metrics_host_container_time", "host_id", "container_name", "scraped_at"), )