"""Model-free Docker view helpers. Kept separate from routes.py/models.py so tests can import it directly: importing a module that re-imports plugins.docker.models AFTER the app has loaded the docker plugin raises "Table 'docker_containers' is already defined". This module touches no ORM models, so it's safe to import in either environment. """ from __future__ import annotations def dedup_by_container_id(containers: list) -> list: """Collapse containers double-reported across hosts. Swarm-aware agents on every manager list cluster-wide tasks, so the same container (identical container_id) arrives once per manager and would otherwise be counted N times. Keep the first occurrence per non-empty container_id — callers order running-first, so a running report wins over a stale stopped one. Rows without a container_id (older agents) can't be matched, so they're kept as-is.""" seen: set[str] = set() out = [] for c in containers: cid = (c.container_id or "").strip() if cid: if cid in seen: continue seen.add(cid) out.append(c) return out