""" Model for terminal sessions - stores SSH terminal session metadata. """ from __future__ import annotations from datetime import datetime from typing import Optional from sqlalchemy import DateTime, Integer, String, text from sqlalchemy.orm import Mapped, mapped_column from sqlalchemy.sql import func from .database import Base class TerminalSession(Base): """ Represents an active or recent SSH terminal session. Sessions are created when a user opens a terminal to a host, and cleaned up after expiration or manual closure. """ __tablename__ = "terminal_sessions" id: Mapped[str] = mapped_column(String(64), primary_key=True) host_id: Mapped[str] = mapped_column(String, nullable=False, index=True) host_name: Mapped[str] = mapped_column(String, nullable=False) host_ip: Mapped[str] = mapped_column(String, nullable=False) user_id: Mapped[Optional[str]] = mapped_column(String, nullable=True) username: Mapped[Optional[str]] = mapped_column(String, nullable=True) # Token hash for session authentication (never store plain token) token_hash: Mapped[str] = mapped_column(String(128), nullable=False) # ttyd process management ttyd_port: Mapped[int] = mapped_column(Integer, nullable=False) ttyd_pid: Mapped[Optional[int]] = mapped_column(Integer, nullable=True) # Session mode: 'embedded' or 'popout' mode: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'embedded'")) # Session status: 'active', 'closed', 'expired', 'error' status: Mapped[str] = mapped_column(String(20), nullable=False, server_default=text("'active'")) # Timestamps created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, server_default=func.now() ) expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) closed_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True) def __repr__(self) -> str: return f""