"""Data model: titles from Plex, sessions from Tautulli, evidence and scores.""" from __future__ import annotations from datetime import datetime from enum import StrEnum from typing import Literal from pydantic import BaseModel, Field Kind = Literal["movie", "show"] class Library(BaseModel): id: int name: str type: str # movie | show | artist | photo class Title(BaseModel): """A movie or a whole show, as inventoried from the main Plex server.""" guid: str rating_key: int kind: Kind title: str year: int | None = None library_id: int library_name: str added_at: datetime size_bytes: int = 0 duration_ms: int = 0 rating: float | None = None audience_rating: float | None = None genres: list[str] = Field(default_factory=list) collections: list[str] = Field(default_factory=list) external_ids: dict[str, str] = Field(default_factory=dict) # tmdb / imdb / tvdb season_count: int = 0 episode_count: int = 0 plex_view_count: int = 0 # all-accounts history on the main server (prior) plex_last_viewed: datetime | None = None @property def best_rating(self) -> float | None: return self.audience_rating if self.audience_rating else self.rating @property def size_gb(self) -> float: return self.size_bytes / 1024**3 @property def hours(self) -> float: return self.duration_ms / 3_600_000 @property def gb_per_hour(self) -> float | None: return self.size_gb / self.hours if self.hours > 0 else None class Inventory(BaseModel): titles: list[Title] episode_to_show: dict[str, str] # episode guid -> show guid scanned_at: datetime class SessionClass(StrEnum): FULL = "full" ABANDONED = "abandoned" TOUCH = "touch" QC = "qc" class Session(BaseModel): """One Tautulli playback session, from any server.""" server: str user_id: int is_admin: bool started: datetime media_type: str # movie | episode guid: str view_offset_ms: int duration_ms: int added_at: datetime | None = None season: int | None = None episode: int | None = None @property def percent(self) -> float: if self.duration_ms <= 0: return 0.0 return min(100.0, 100.0 * self.view_offset_ms / self.duration_ms) class Evidence(BaseModel): """Per-title aggregate of classified sessions across servers.""" full_sessions: int = 0 full_users: int = 0 last_full: datetime | None = None abandoned_sessions: int = 0 # non-admin only abandoned_users: int = 0 touch_sessions: int = 0 qc_sessions: int = 0 furthest_season: int | None = None furthest_episode: int | None = None servers: list[str] = Field(default_factory=list) @property def watched(self) -> bool: return self.full_sessions > 0 class Signal(BaseModel): code: str points: float note: str class Breakdown(BaseModel): signals: list[Signal] = Field(default_factory=list) flags: list[str] = Field(default_factory=list) @property def raw(self) -> float: return sum(s.points for s in self.signals) @property def score(self) -> int: return int(round(max(0.0, min(100.0, self.raw)))) class Candidate(BaseModel): title: Title evidence: Evidence breakdown: Breakdown protected: str | None = None # reason, when protected decision: str | None = None # keep | reap | downgrade | protect, from the ledger @property def score(self) -> int: return 0 if self.protected else self.breakdown.score @property def why(self) -> str: """The strongest positive signal code, for the table.""" if self.protected: return "PROTECTED" pos = [s for s in self.breakdown.signals if s.points > 0] if not pos: return "-" return max(pos, key=lambda s: s.points).code class DiskUsage(BaseModel): path: str total_bytes: int free_bytes: int class ScanResult(BaseModel): candidates: list[Candidate] scanned_at: datetime servers: list[str] disk: DiskUsage | None = None def totals(self) -> dict[str, float]: return { "titles": len(self.candidates), "gb": sum(c.title.size_gb for c in self.candidates), "marked": sum(1 for c in self.candidates if c.decision == "reap"), "marked_gb": sum(c.title.size_gb for c in self.candidates if c.decision == "reap"), }