ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
perf(api): stop /status/apps spawning a subprocess per app, and roll up the activity summary
Juan Denis139d5884fea1
diff --git a/backend/app/api/admin.py b/backend/app/api/admin.pyindex 1786f2e0..9b833870 100644--- a/backend/app/api/admin.py+++ b/backend/app/api/admin.py@@ -1,5 +1,6 @@"""Admin API endpoints for user management, settings, and audit logs."""-from datetime import datetime, timedelta+from datetime import date, datetime, timedelta+from typing import Dict, List, Optionalfrom flask import Blueprint, request, jsonifyfrom flask_jwt_extended import get_jwt_identityfrom sqlalchemy import func@@ -593,6 +594,64 @@ def get_permission_templates():# Activity Dashboard Endpoints# ============================================+def _day_key(value) -> Optional[str]:+ """Normalize whatever the DB returned for a day bucket to 'YYYY-MM-DD'.++ ``func.date()`` is the portable day-bucket expression here, but the two+ supported backends disagree on the Python type that comes back: SQLite+ returns the string 'YYYY-MM-DD', PostgreSQL returns a ``datetime.date``+ (``date(ts)`` is accepted there as function-style cast syntax). Both are+ reduced to the same string so the zero-fill below can key on it.+ """+ if value is None:+ return None+ if isinstance(value, (datetime, date)):+ return value.strftime('%Y-%m-%d')+ return str(value)[:10]+++def _daily_action_counts(window_start: datetime, days: int,+ *extra_filters) -> List[Dict]:+ """Per-day audit-log counts over ``days`` days starting at ``window_start``.++ ONE grouped query, then the gaps are filled with zeroes in Python. The+ previous shape issued a separate COUNT per day (90 sequential round-trips+ per series, 180 for the two series this endpoint returns) purely to learn+ that most of those days were empty.++ The zero-fill is what keeps the response byte-identical: the series must+ always have exactly ``days`` entries, in ascending date order, including+ days with no rows at all.+ """+ window_end = window_start + timedelta(days=days)++ # Bucket on the DB side. The range filter is applied once for the whole+ # window instead of once per day.+ day_bucket = func.date(AuditLog.created_at)+ rows = db.session.query(+ day_bucket.label('day'),+ func.count(AuditLog.id),+ ).filter(+ AuditLog.created_at >= window_start,+ AuditLog.created_at < window_end,+ *extra_filters+ ).group_by(day_bucket).all()++ counts: Dict[str, int] = {}+ for day, count in rows:+ key = _day_key(day)+ if key:+ # Accumulate rather than assign: defensive against a backend that+ # could hand back two spellings of the same calendar day.+ counts[key] = counts.get(key, 0) + (count or 0)++ series = []+ for offset in range(days):+ key = (window_start + timedelta(days=offset)).strftime('%Y-%m-%d')+ series.append({'date': key, 'count': counts.get(key, 0)})+ return series++@admin_bp.route('/activity/summary', methods=['GET'])@admin_requireddef get_activity_summary():@@ -630,47 +689,40 @@ def get_activity_summary():func.count(AuditLog.id).desc()).limit(5).all()+ # Resolve the five usernames in one query instead of one SELECT per user.+ ranked_ids = [user_id for user_id, _ in top_users_query]+ usernames = {}+ if ranked_ids:+ usernames = {+ u.id: u.username+ for u in User.query.filter(User.id.in_(ranked_ids)).all()+ }+top_users = []for user_id, count in top_users_query:- user = User.query.get(user_id)- if user:+ # Preserve the ranked order and the "skip users that no longer exist"+ # behaviour of the previous per-user lookup.+ if user_id in usernames:top_users.append({'user_id': user_id,- 'username': user.username,+ 'username': usernames[user_id],'action_count': count})- # Daily action counts for the past 90 days- daily_counts = []+ # Daily action counts for the past 90 days. The window is the 90 days+ # ENDING today (inclusive), so it starts 89 days back.days_to_fetch = 90- for i in range(days_to_fetch):- day_start = today_start - timedelta(days=(days_to_fetch - 1) - i)- day_end = day_start + timedelta(days=1)- count = db.session.query(func.count(AuditLog.id)).filter(- AuditLog.created_at >= day_start,- AuditLog.created_at < day_end- ).scalar() or 0- daily_counts.append({- 'date': day_start.strftime('%Y-%m-%d'),- 'count': count- })+ window_start = today_start - timedelta(days=days_to_fetch - 1)++ daily_counts = _daily_action_counts(window_start, days_to_fetch)# Top user activity (past 90 days)top_user_daily = []if top_users:top_user_id = top_users[0]['user_id']- for i in range(days_to_fetch):- day_start = today_start - timedelta(days=(days_to_fetch - 1) - i)- day_end = day_start + timedelta(days=1)- count = db.session.query(func.count(AuditLog.id)).filter(- AuditLog.user_id == top_user_id,- AuditLog.created_at >= day_start,- AuditLog.created_at < day_end- ).scalar() or 0- top_user_daily.append({- 'date': day_start.strftime('%Y-%m-%d'),- 'count': count- })+ top_user_daily = _daily_action_counts(+ window_start, days_to_fetch, AuditLog.user_id == top_user_id+ )return jsonify({'active_users_today': active_today,diff --git a/backend/app/api/apps.py b/backend/app/api/apps.pyindex 661e3356..751ab773 100644--- a/backend/app/api/apps.py+++ b/backend/app/api/apps.py@@ -21,6 +21,7 @@ from app.services.source_connection_service import SourceConnectionServicefrom app.services.remote_docker_service import RemoteDockerServicefrom app.services.container_registry_service import ContainerRegistryServicefrom app.services.image_update_service import ImageUpdateService+from app.services import container_status_servicefrom app.services.container_sleep_service import ContainerSleepServicefrom app.services.container_scale_service import ContainerScaleServicefrom app.services.unit_compose_service import UnitComposeService@@ -1551,6 +1552,9 @@ def start_app(app_id):app.status = 'running'db.session.commit()+ # The cached aggregate now describes the pre-start world. Drop it: a status+ # pill that survives the action that changed it reads as a broken panel.+ container_status_service.invalidate(app_id)return jsonify({'message': 'Application started',@@ -1600,6 +1604,9 @@ def apply_image_update(app_id):app.status = 'running'app.last_deployed_at = datetime.utcnow()db.session.commit()+ # Containers were recreated — their ids changed, so any cached aggregate is+ # describing containers that no longer exist.+ container_status_service.invalidate(app_id)# Refresh the update badge now that we're on the new image.ImageUpdateService.check_application(app_id)@@ -1970,6 +1977,7 @@ def stop_app(app_id):app.status = 'stopped'db.session.commit()+ container_status_service.invalidate(app_id)return jsonify({'message': 'Application stopped',@@ -2008,6 +2016,7 @@ def restart_app(app_id):app.status = 'running'db.session.commit()+ container_status_service.invalidate(app_id)return jsonify({'message': 'Application restarted',diff --git a/backend/app/services/container_status_service.py b/backend/app/services/container_status_service.pyindex 5c45d791..fd7eda77 100644--- a/backend/app/services/container_status_service.py+++ b/backend/app/services/container_status_service.py@@ -6,6 +6,13 @@ deterministic status using a fixed priority hierarchy. The aggregationunit-tested in isolation; the ``get_*_status`` helpers wire it to real Dockerdata and a short-TTL cache.+Collection is a SINGLE `docker ps` for the whole host, indexed back onto apps by+compose labels (see "Bulk collection" below) and shared between callers by a+short-TTL snapshot — not one `docker compose ps` per app plus one+`docker inspect` per container, which is what made ``/status/apps`` cost ~120+process spawns per request. Anything that changes container state must call+``invalidate()``.+Status vocabulary (the aggregated enum):running:healthy every container running, health checks (if any) passing@@ -30,6 +37,11 @@ happy path; ``exited`` is a calm, fully-stopped state; ``unknown`` is the floor."""import logging+import os+import re+import threading+import time+from typing import Any, Dict, Iterable, List, Optionallogger = logging.getLogger(__name__)@@ -59,6 +71,20 @@ STATUS_PRECEDENCE = [_CACHE_PREFIX = 'container_status'_CACHE_TTL = 8 # seconds+# TTL for the shared host-wide `docker ps` snapshot (see _get_container_index).+# Deliberately SHORTER than the 5s socket-broadcast tick so the emitter never+# reuses the previous tick's collection — the snapshot only ever collapses a+# burst of near-simultaneous callers (the HTTP route + the emitter + a page+# that renders 30 status pills), never a whole polling interval.+_SNAPSHOT_TTL = 3.0 # seconds++# The snapshot lives in module memory rather than CacheService on purpose: the+# panel runs a single worker process (see CLAUDE.md / docs/ARCHITECTURE.md), it+# must be droppable synchronously by invalidate(), and round-tripping the whole+# container list through JSON would undo part of what we just saved.+_snapshot_lock = threading.Lock()+_snapshot: Dict[str, Any] = {'expires': 0.0, 'index': None}+def _normalize_state(raw):"""Map a raw docker state/status string to one of: running, restarting,@@ -198,61 +224,351 @@ def aggregate_status(container_states):}-def _container_health(container_id):- """Best-effort health string from docker inspect, or None.+# ---- Bulk collection -------------------------------------------------------+#+# The naive shape of this module was: one `docker compose ps` subprocess per+# app, plus one `docker inspect` per running container to read its health. At+# ~30 apps x ~3 containers that is ~120 process spawns for a SINGLE /status/apps+# request, repeated on a timer by the socket emitter. Both of those costs are+# avoidable:+#+# * `docker ps` already knows about every container on the host, and compose+# stamps each one with project/service/working_dir labels — so one call can+# be indexed back onto the apps that own the containers.+# * `docker ps`'s STATUS column already carries the health-check result+# ("Up 2 minutes (unhealthy)"), which is the only thing the per-container+# `docker inspect` was being spawned for.+#+# Net: ~120 spawns -> 1 per collection pass, shared between callers by a+# short-TTL snapshot.++# 'Up 2 minutes (healthy)' / '(unhealthy)' / '(health: starting)'.+_HEALTH_SUFFIX_RE = re.compile(r'\((?:health:\s*)?(healthy|unhealthy|starting)\)', re.IGNORECASE)++# Compose derives a project name from the directory name by lowercasing and+# dropping everything outside [a-z0-9_-] (then trimming leading separators).+_PROJECT_NAME_STRIP_RE = re.compile(r'[^a-z0-9_-]')+++def _health_from_status(status: Optional[str]) -> Optional[str]:+ """Pull the health-check result out of a `docker ps` STATUS string.++ Returns None when the container has no health check, which+ ``_normalize_health`` reads as 'none' — same as a failed inspect did.+ """+ if not status:+ return None+ match = _HEALTH_SUFFIX_RE.search(str(status))+ return match.group(1).lower() if match else None+++def _norm_path(path: Optional[str]) -> Optional[str]:+ """Normalize a filesystem path for equality comparison.- Reads ``State.Health.Status`` which ``get_container_state`` doesn't expose.- Defensive: any failure → None (treated as 'no health check').+ Compose records ``project.working_dir`` as an absolute path; the panel+ stores ``root_path``. They can differ only by trailing slash, ``..`` or a+ symlink, so normalize both sides the same way before comparing."""- from app.services.docker_service import DockerService+ if not path:+ return Nonetry:- info = DockerService.get_container(container_id)- if not info:- return None- health = (info.get('State') or {}).get('Health') or {}- return health.get('Status')+ return os.path.normcase(os.path.normpath(str(path).strip()))except Exception:return None-def _gather_app_container_states(app):- """Collect per-container {name, service, state, health} dicts for an app.+def _real_path(path: Optional[str]) -> Optional[str]:+ """``_norm_path`` after symlink resolution, or None if it doesn't resolve."""+ if not path:+ return None+ try:+ resolved = os.path.normcase(os.path.realpath(str(path).strip()))+ except Exception:+ return None+ return resolved or None+++def _compose_project_name(root_path: Optional[str]) -> Optional[str]:+ """The project name compose would derive from ``root_path``'s basename."""+ if not root_path:+ return None+ base = os.path.basename(os.path.normpath(str(root_path).strip()))+ if not base:+ return None+ return _PROJECT_NAME_STRIP_RE.sub('', base.lower()).lstrip('_-') or None- Wraps the Docker CLI calls in try/except so a Docker outage degrades to an- empty list (→ 'unknown') rather than raising.++class _ContainerIndex:+ """One host-wide `docker ps` result, indexed by every key an app can match.++ An Application identifies its containers three ways, in descending order of+ trust:++ 1. its compose project directory (``root_path``) — matched against the+ ``com.docker.compose.project.working_dir`` label, which compose sets+ regardless of a custom ``COMPOSE_PROJECT_NAME``;+ 2. the compose project NAME derived from that directory — the fallback+ when the working_dir label is absent (older compose) or the project+ was brought up from a different path;+ 3. an explicit ``container_id`` for non-compose, single-container apps.++ Anything that matches nothing yields an empty list, which aggregates to+ 'unknown' — exactly what an empty `docker compose ps` produced before."""++ __slots__ = ('by_dir', 'by_project', 'by_id', 'count')++ def __init__(self, containers: Iterable[Dict[str, Any]]):+ self.by_dir: Dict[str, List[Dict[str, Any]]] = {}+ self.by_project: Dict[str, List[Dict[str, Any]]] = {}+ self.by_id: Dict[str, Dict[str, Any]] = {}+ self.count = 0++ for c in containers or []:+ self.count += 1+ for key in self._dir_keys(c):+ self.by_dir.setdefault(key, []).append(c)++ project = (c.get('project') or '').strip().lower()+ if project:+ self.by_project.setdefault(project, []).append(c)++ cid = (c.get('id') or '').strip()+ name = (c.get('name') or '').strip().lstrip('/')+ if cid:+ self.by_id[cid] = c+ # Apps often store a short id; index the 12-char prefix too.+ self.by_id.setdefault(cid[:12], c)+ if name:+ self.by_id.setdefault(name, c)++ @staticmethod+ def _dir_keys(container: Dict[str, Any]) -> List[str]:+ """Directory keys a container can be found under (normalized + real)."""+ keys = []+ candidates = [container.get('working_dir')]+ # config_files is a comma-separated list of compose file paths; their+ # directory is the project dir when working_dir wasn't stamped.+ for cf in (container.get('config_files') or '').split(','):+ cf = cf.strip()+ if cf:+ candidates.append(os.path.dirname(cf))+ for candidate in candidates:+ for key in (_norm_path(candidate), _real_path(candidate)):+ if key and key not in keys:+ keys.append(key)+ return keys++ def for_app(self, app) -> List[Dict[str, Any]]:+ """Containers belonging to ``app``, in the trust order documented above."""+ root_path = getattr(app, 'root_path', None)+ for key in (_norm_path(root_path), _real_path(root_path)):+ if key and key in self.by_dir:+ return self.by_dir[key]++ project = _compose_project_name(root_path)+ if project and project in self.by_project:+ return self.by_project[project]++ container_id = (getattr(app, 'container_id', None) or '').strip()+ if container_id and container_id in self.by_id:+ return [self.by_id[container_id]]++ return []+++def _collect_container_index() -> _ContainerIndex:+ """Run the ONE `docker ps` and index it. Never raises."""from app.services.docker_service import DockerService+ try:+ containers = DockerService.list_compose_containers() or []+ except Exception as e:+ logger.warning('Bulk container collection failed: %s', e)+ containers = []+ return _ContainerIndex(containers)+++def _get_container_index(use_cache: bool = True) -> _ContainerIndex:+ """The shared host-wide container snapshot, collected at most once per TTL.++ This is what makes the HTTP route and the socket emitter share a single+ collection pass instead of each triggering their own.+ """+ if not use_cache:+ return _collect_container_index()++ now = time.monotonic()+ with _snapshot_lock:+ if _snapshot['index'] is not None and _snapshot['expires'] > now:+ return _snapshot['index']++ # Collected outside the lock: a slow/hung docker must not serialize every+ # caller behind it. A rare duplicate collection is cheaper than a stall.+ index = _collect_container_index()+ with _snapshot_lock:+ _snapshot['index'] = index+ _snapshot['expires'] = time.monotonic() + _SNAPSHOT_TTL+ return index+++def invalidate(application_id=None) -> None:+ """Drop cached status so the next read re-collects from Docker.++ MUST be called by anything that changes container state (start / stop /+ restart / deploy). A cached status that survives an explicit user action is+ worse than a slow one: the UI would show the pre-action state and look+ broken. Passing ``application_id`` also clears that app's cached result;+ the host snapshot is always dropped because one app's compose up/down+ changes rows other apps are read from.+ """+ with _snapshot_lock:+ _snapshot['index'] = None+ _snapshot['expires'] = 0.0++ from app.services.cache_service import CacheService+ try:+ if application_id is None:+ CacheService.delete_pattern(f'{_CACHE_PREFIX}:*')+ else:+ CacheService.delete(f'{_CACHE_PREFIX}:app:{application_id}')+ except Exception: # cache is best-effort; the snapshot drop already landed+ pass+++def _states_from_index_rows(rows: Iterable[Dict[str, Any]]) -> List[Dict[str, Any]]:+ """`docker ps` rows -> the {id,name,service,state,health} shape."""states = []+ for c in rows or []:+ state = (c.get('state') or '').strip()+ if not state:+ # Very old daemons don't expose a State column in `docker ps`; the+ # STATUS text ("Up 2 minutes", "Exited (0) ...") still tells us.+ status_text = (c.get('status') or '').strip().lower()+ if status_text.startswith('up'):+ state = 'running'+ elif status_text:+ state = status_text.split()[0]+ states.append({+ 'id': c.get('id') or c.get('name'),+ 'name': (c.get('name') or '').lstrip('/') or None,+ 'service': c.get('service') or None,+ 'state': state or None,+ 'health': _health_from_status(c.get('status')),+ })+ return states+++def _gather_remote_app_container_states(app) -> List[Dict[str, Any]]:+ """Container states for an app that lives on a remote (agent) server.++ Remote hosts are NOT part of the local `docker ps` snapshot, so they go+ through ``RemoteDockerService``. Gated on the agent actually being+ connected: ``send_command`` blocks up to 30s waiting for a reply, and this+ runs inside the 5s status-broadcast loop, so an offline server must cost+ nothing rather than stall every other app's status.+ """+ from app.services.agent_registry import agent_registry+ from app.services.remote_docker_service import RemoteDockerService++ try:+ if not agent_registry.get_agent(app.server_id):+ return []+ except Exception:+ return []++ target = os.path.join(app.root_path, app.compose_file or 'docker-compose.yml') \+ if getattr(app, 'root_path', None) else None+ if not target:+ return []+try:- containers = DockerService.get_all_app_containers(app) or []+ result = RemoteDockerService.compose_ps(app.server_id, target) or {}except Exception as e:- logger.warning('Failed to list containers for app %s: %s',- getattr(app, 'id', '?'), e)- return states-- for c in containers:- cid = c.get('id') or c.get('name')- # The container list already carries a state; enrich with health only- # when the container looks like it's running (cheap inspect avoidance).- health = None- if _normalize_state(c.get('state')) == 'running' and cid:- health = _container_health(cid)+ logger.warning('Remote status for app %s failed: %s', getattr(app, 'id', '?'), e)+ return []+ if not result.get('success'):+ return []++ states = []+ for c in (result.get('data') or []):+ status = c.get('Status') or c.get('status')states.append({- 'id': cid,- 'name': c.get('name'),- 'service': c.get('service'),- 'state': c.get('state'),- 'health': health,+ 'id': c.get('ID') or c.get('id') or c.get('Name') or c.get('name'),+ 'name': c.get('Name') or c.get('name') or c.get('Names'),+ 'service': c.get('Service') or c.get('service'),+ 'state': c.get('State') or c.get('state'),+ 'health': _health_from_status(status) or (c.get('Health') or c.get('health')),})return states-def get_app_status(application_id, use_cache=True):+def _gather_app_container_states(app, index: Optional[_ContainerIndex] = None):+ """Collect per-container {name, service, state, health} dicts for an app.++ Local apps are answered from the shared host-wide snapshot (zero additional+ subprocesses); remote apps go through the agent. Wrapped in try/except so a+ Docker outage degrades to an empty list (→ 'unknown') rather than raising.+ """+ if getattr(app, 'server_id', None):+ return _gather_remote_app_container_states(app)++ if index is None:+ index = _get_container_index()++ try:+ rows = index.for_app(app)+ except Exception as e:+ logger.warning('Failed to resolve containers for app %s: %s',+ getattr(app, 'id', '?'), e)+ return []++ if rows:+ return _states_from_index_rows(rows)++ # Nothing in the snapshot. For a compose app that is the honest answer+ # (`docker compose ps` would print nothing either). But an app pinned to a+ # bare container_id that the snapshot didn't carry — e.g. a stopped+ # container, which `docker ps` hides — still deserves its old inspect path.+ container_id = (getattr(app, 'container_id', None) or '').strip()+ if container_id and not getattr(app, 'root_path', None):+ return _gather_single_container_state(container_id)+ return []+++def _gather_single_container_state(container_id: str) -> List[Dict[str, Any]]:+ """Fallback for one explicitly-identified container.++ ONE `docker inspect` — state and health both come out of the same payload,+ where the old code spent two spawns (``get_container_state`` and the health+ lookup each ran their own inspect of the same container).+ """+ from app.services.docker_service import DockerService+ try:+ info = DockerService.get_container(container_id)+ except Exception as e:+ logger.warning('Failed to read container %s state: %s', container_id, e)+ return []+ if not info:+ return []+ state = info.get('State') or {}+ return [{+ 'id': container_id,+ 'name': (info.get('Name') or container_id).lstrip('/'),+ 'service': 'main',+ 'state': state.get('Status'),+ 'health': (state.get('Health') or {}).get('Status'),+ }]+++def get_app_status(application_id, use_cache=True, index: Optional[_ContainerIndex] = None):"""Aggregated status for a single Application.- Loads the app, gathers its containers via DockerService, aggregates, and- caches the result for a short TTL. Fully defensive: a missing app or a- Docker outage returns a well-formed 'unknown' result rather than raising.+ Loads the app, gathers its containers, aggregates, and caches the result for+ a short TTL. Fully defensive: a missing app or a Docker outage returns a+ well-formed 'unknown' result rather than raising.++ ``index`` lets a bulk caller (``list_app_statuses``) hand in the host-wide+ snapshot it already collected, so N apps cost one collection pass in total.Returns:dict: aggregate_status() output plus 'app_id' and 'kind'.@@ -286,7 +602,12 @@ def get_app_status(application_id, use_cache=True):result['reasons'] = ['application not found']return result- states = _gather_app_container_states(app)+ # use_cache=False means "give me the truth right now" — it has to bypass the+ # shared snapshot too, or an 8s-stale answer would just come from 3s-stale+ # data instead of Docker.+ if index is None:+ index = _get_container_index(use_cache=use_cache)+ states = _gather_app_container_states(app, index=index)agg = aggregate_status(states)result.update(agg)result['app_id'] = application_id@@ -335,15 +656,14 @@ def get_database_status(database_id, container_id=None, use_cache=True):return resulttry:- from app.services.docker_service import DockerService- state = DockerService.get_container_state(container_id)- health = _container_health(container_id)- agg = aggregate_status([{- 'id': container_id,- 'name': container_id,- 'state': (state or {}).get('state'),- 'health': health,- }])+ # One inspect, not two: state and health live in the same payload.+ states = _gather_single_container_state(container_id)+ if not states:+ # An unresolvable container still counts as one unknown member —+ # same output the two-inspect version produced on a failed lookup.+ states = [{'id': container_id, 'name': container_id,+ 'state': None, 'health': None}]+ agg = aggregate_status(states)result.update(agg)result['database_id'] = database_idresult['kind'] = 'database'@@ -357,6 +677,11 @@ def list_app_statuses():Returns a list of {app_id, status, total, healthy} suitable for the listendpoint and the socket change-detection snapshot. Never raises.++ Cost: ONE `docker ps` for every local app on the host (shared with any other+ caller inside the snapshot TTL), plus one agent round-trip per *online*+ remote server's apps. The previous shape was one `docker compose ps` per app+ plus one `docker inspect` per running container."""summaries = []try:@@ -366,8 +691,11 @@ def list_app_statuses():logger.warning('Failed to list applications for status: %s', e)return summaries+ # Collect once, up front, and pass it down — the whole point of the round.+ index = _get_container_index()+for app in apps:- full = get_app_status(app.id)+ full = get_app_status(app.id, index=index)summaries.append({'app_id': app.id,'status': full.get('status', STATUS_UNKNOWN),diff --git a/backend/app/services/deployment_service.py b/backend/app/services/deployment_service.pyindex e9ef78ea..ae89e765 100644--- a/backend/app/services/deployment_service.py+++ b/backend/app/services/deployment_service.py@@ -196,6 +196,11 @@ class DeploymentService:db.session.commit()+ # A deploy replaces containers; the cached aggregate is now about+ # containers that no longer exist. Force the next read to re-collect.+ from app.services import container_status_service+ container_status_service.invalidate(app_id)+# Generate diff with previous deploymentcls._generate_diff(deployment)@@ -448,6 +453,10 @@ class DeploymentService:db.session.commit()+ # Same reasoning as deploy: containers were replaced.+ from app.services import container_status_service+ container_status_service.invalidate(app_id)+if log_callback:log_callback(f"Rollback successful! Now running v{target.version} code as v{version}")diff --git a/backend/app/services/docker_service.py b/backend/app/services/docker_service.pyindex d02a838a..1d7047e8 100644--- a/backend/app/services/docker_service.py+++ b/backend/app/services/docker_service.py@@ -147,6 +147,76 @@ class DockerService:logger.error(f"Failed to list containers: {e}")return []+ # One `docker ps` row, tab-delimited. Tabs are used instead of+ # `--format '{{json .}}'` because the JSON form flattens every label into a+ # single comma-joined `Labels` string — and compose's+ # `project.config_files` label is itself comma-separated, so that string+ # cannot be parsed back unambiguously. `{{.Label "k"}}` pulls each label+ # out on its own; none of these values can contain a tab.+ _PS_LABEL_FIELDS = (+ ('id', '{{.ID}}'),+ ('name', '{{.Names}}'),+ ('state', '{{.State}}'),+ ('status', '{{.Status}}'),+ ('project', '{{.Label "com.docker.compose.project"}}'),+ ('service', '{{.Label "com.docker.compose.service"}}'),+ ('working_dir', '{{.Label "com.docker.compose.project.working_dir"}}'),+ ('config_files', '{{.Label "com.docker.compose.project.config_files"}}'),+ )++ @classmethod+ def list_compose_containers(cls, all_containers=False):+ """Every container on this host in ONE `docker ps`, with compose labels.++ This exists so a caller that needs the state of N compose projects can+ pay for a single process spawn instead of one `docker compose ps` per+ project (plus one `docker inspect` per container). ``status`` carries+ the health suffix docker prints — ``Up 2 minutes (healthy)`` — which is+ the same information ``docker inspect`` would give under+ ``State.Health.Status``, so the inspect calls are not needed either.++ Args:+ all_containers: include stopped containers. Defaults to False so the+ result matches what ``docker compose ps`` (no ``--all``) would+ have listed.++ Returns:+ list[dict]: {id, name, state, status, project, service,+ working_dir, config_files}. Empty on any failure —+ callers treat that as "Docker unavailable".+ """+ keys = [k for k, _ in cls._PS_LABEL_FIELDS]+ fmt = '\t'.join(tpl for _, tpl in cls._PS_LABEL_FIELDS)+ cmd = ['docker', 'ps', '--no-trunc', '--format', fmt]+ if all_containers:+ cmd.insert(2, '-a')++ try:+ result = subprocess.run(cmd, capture_output=True, text=True)+ if result.returncode != 0:+ logger.warning('docker ps (bulk) failed: %s', result.stderr.strip())+ return []+ except Exception as e:+ logger.error(f"Failed to bulk-list containers: {e}")+ return []++ containers = []+ for line in (result.stdout or '').splitlines():+ if not line.strip():+ continue+ parts = line.split('\t')+ # Trailing empty labels can be dropped by the template engine; pad+ # rather than skip so a container with no compose labels still+ # contributes its id/name/state.+ parts += [''] * (len(keys) - len(parts))+ row = dict(zip(keys, (p.strip() for p in parts[:len(keys)])))+ # docker renders a missing label as '<no value>' in some versions.+ for k in ('project', 'service', 'working_dir', 'config_files'):+ if row.get(k) == '<no value>':+ row[k] = ''+ containers.append(row)+ return containers+@staticmethoddef get_container(container_id):"""Get detailed container information."""diff --git a/backend/tests/test_activity_summary_rollup.py b/backend/tests/test_activity_summary_rollup.pynew file mode 100644index 00000000..e692b332--- /dev/null+++ b/backend/tests/test_activity_summary_rollup.py@@ -0,0 +1,246 @@+"""Tests for /admin/activity/summary's grouped day rollup.++The endpoint used to issue one COUNT per day, twice (90 days x 2 series = 180+sequential queries) purely to discover that most days were empty. It now issues+one GROUP BY per series and fills the gaps in Python, so these tests pin both+halves: the query count, and the zero-fill that keeps the response identical.+"""++from datetime import datetime, timedelta++import pytest+from sqlalchemy import event++from app import db+from app.api.admin import _daily_action_counts, _day_key+from app.models import AuditLog, User++DAYS = 90+++def admin_and_logs(app):+ """One admin plus audit rows on exactly three known days inside the window."""+ from werkzeug.security import generate_password_hash++ with app.app_context():+ user = User(email='[email protected]', username='activityadmin',+ password_hash=generate_password_hash('x'),+ role=User.ROLE_ADMIN, is_active=True)+ db.session.add(user)+ db.session.commit()++ today_start = datetime.utcnow().replace(hour=0, minute=0, second=0,+ microsecond=0)+ # today (x3), 5 days ago (x1), 40 days ago (x2). Everything else empty.+ plan = [(0, 3), (5, 1), (40, 2)]+ for days_ago, count in plan:+ stamp = today_start - timedelta(days=days_ago) + timedelta(hours=6)+ for _ in range(count):+ db.session.add(AuditLog(action='user.login', user_id=user.id,+ created_at=stamp))+ db.session.commit()+ yield {'user_id': user.id, 'today_start': today_start, 'plan': plan}+++class _QueryCounter:+ """Counts statements that actually touch audit_logs."""++ def __init__(self, engine):+ self.engine = engine+ self.count = 0++ def _hook(self, conn, cursor, statement, params, context, executemany):+ if 'audit_logs' in statement.lower():+ self.count += 1++ def __enter__(self):+ event.listen(self.engine, 'before_cursor_execute', self._hook)+ return self++ def __exit__(self, *exc):+ event.remove(self.engine, 'before_cursor_execute', self._hook)+ return False+++# ------------------------------------------------------------- zero-fill++class TestZeroFill:+ def test_series_length_matches_the_window(self, app, admin_and_logs):+ window_start = admin_and_logs['today_start'] - timedelta(days=DAYS - 1)+ with app.app_context():+ series = _daily_action_counts(window_start, DAYS)+ assert len(series) == DAYS++ def test_empty_days_still_appear_as_zero(self, app, admin_and_logs):+ """The whole point of the Python-side fill: a day with no rows is still+ a point on the chart, not a missing entry."""+ today_start = admin_and_logs['today_start']+ window_start = today_start - timedelta(days=DAYS - 1)+ with app.app_context():+ series = _daily_action_counts(window_start, DAYS)++ by_date = {row['date']: row['count'] for row in series}+ # 3 days carry rows; the other 87 must be present with count 0.+ assert sum(1 for c in by_date.values() if c == 0) == DAYS - 3+ assert by_date[today_start.strftime('%Y-%m-%d')] == 3+ assert by_date[(today_start - timedelta(days=5)).strftime('%Y-%m-%d')] == 1+ assert by_date[(today_start - timedelta(days=40)).strftime('%Y-%m-%d')] == 2+ # A day we never wrote to is present and zero.+ assert by_date[(today_start - timedelta(days=6)).strftime('%Y-%m-%d')] == 0++ def test_dates_are_contiguous_and_ascending(self, app, admin_and_logs):+ window_start = admin_and_logs['today_start'] - timedelta(days=DAYS - 1)+ with app.app_context():+ series = _daily_action_counts(window_start, DAYS)++ dates = [row['date'] for row in series]+ assert dates == sorted(dates)+ assert dates[0] == window_start.strftime('%Y-%m-%d')+ assert dates[-1] == admin_and_logs['today_start'].strftime('%Y-%m-%d')+ assert len(set(dates)) == DAYS++ def test_row_shape_is_date_and_count_only(self, app, admin_and_logs):+ window_start = admin_and_logs['today_start'] - timedelta(days=DAYS - 1)+ with app.app_context():+ series = _daily_action_counts(window_start, DAYS)+ assert all(set(row) == {'date', 'count'} for row in series)+ assert all(isinstance(row['count'], int) for row in series)++ def test_empty_table_is_ninety_zeroes(self, app):+ window_start = datetime.utcnow().replace(+ hour=0, minute=0, second=0, microsecond=0) - timedelta(days=DAYS - 1)+ with app.app_context():+ series = _daily_action_counts(window_start, DAYS)+ assert len(series) == DAYS+ assert all(row['count'] == 0 for row in series)++ def test_rows_outside_the_window_are_excluded(self, app, admin_and_logs):+ """The range filter is applied once for the whole window; a row just+ outside it must not leak into the first bucket."""+ today_start = admin_and_logs['today_start']+ with app.app_context():+ db.session.add(AuditLog(action='user.login',+ user_id=admin_and_logs['user_id'],+ created_at=today_start - timedelta(days=200)))+ db.session.commit()+ series = _daily_action_counts(today_start - timedelta(days=DAYS - 1), DAYS)+ assert sum(row['count'] for row in series) == 6 # 3 + 1 + 2++ def test_extra_filter_narrows_the_series(self, app, admin_and_logs):+ today_start = admin_and_logs['today_start']+ with app.app_context():+ other = User(email='[email protected]', username='otheruser',+ password_hash='x', role=User.ROLE_ADMIN, is_active=True)+ db.session.add(other)+ db.session.commit()+ db.session.add(AuditLog(action='user.login', user_id=other.id,+ created_at=today_start + timedelta(hours=2)))+ db.session.commit()++ everyone = _daily_action_counts(today_start - timedelta(days=DAYS - 1), DAYS)+ just_them = _daily_action_counts(+ today_start - timedelta(days=DAYS - 1), DAYS,+ AuditLog.user_id == other.id)++ assert sum(r['count'] for r in everyone) == 7+ assert sum(r['count'] for r in just_them) == 1+ assert len(just_them) == DAYS # still zero-filled+++# ---------------------------------------------------------- query volume++class TestQueryVolume:+ def test_one_grouped_query_per_series(self, app, admin_and_logs):+ """90 sequential COUNTs -> 1 GROUP BY. This is the whole round."""+ window_start = admin_and_logs['today_start'] - timedelta(days=DAYS - 1)+ with app.app_context():+ with _QueryCounter(db.engine) as counter:+ _daily_action_counts(window_start, DAYS)+ assert counter.count == 1++ def test_endpoint_is_no_longer_linear_in_days(self, app, client, admin_and_logs):+ """End to end: the summary route did 180 audit_logs queries for its two+ 90-day series alone. Bound it well below that."""+ from flask_jwt_extended import create_access_token++ with app.app_context():+ token = create_access_token(identity=admin_and_logs['user_id'])+ with _QueryCounter(db.engine) as counter:+ response = client.get('/api/v1/admin/activity/summary',+ headers={'Authorization': f'Bearer {token}'})+ queries = counter.count++ assert response.status_code == 200+ # 4 aggregates + 2 grouped series = 6; leave headroom for the audit+ # middleware without ever tolerating a per-day loop.+ assert queries <= 12, f'{queries} audit_logs queries — per-day loop is back?'+++# --------------------------------------------------------- endpoint shape++class TestEndpointShape:+ def test_payload_keys_and_series_lengths(self, app, client, admin_and_logs):+ from flask_jwt_extended import create_access_token++ with app.app_context():+ token = create_access_token(identity=admin_and_logs['user_id'])+ response = client.get('/api/v1/admin/activity/summary',+ headers={'Authorization': f'Bearer {token}'})+ assert response.status_code == 200+ payload = response.get_json()++ assert set(payload) == {+ 'active_users_today', 'actions_this_week', 'total_users',+ 'top_users', 'daily_counts', 'top_user_daily',+ }+ assert len(payload['daily_counts']) == DAYS+ # There IS a top user here, so the second series is populated too.+ assert len(payload['top_user_daily']) == DAYS+ assert all(set(row) == {'date', 'count'} for row in payload['daily_counts'])+ assert payload['daily_counts'][-1]['count'] == 3+ assert payload['top_users'][0]['username'] == 'activityadmin'+ assert set(payload['top_users'][0]) == {'user_id', 'username', 'action_count'}++ def test_top_user_daily_is_empty_when_there_is_no_top_user(self, app, client):+ """Preserved edge case: no audit rows at all -> [] , not 90 zeroes."""+ from flask_jwt_extended import create_access_token+ from werkzeug.security import generate_password_hash++ with app.app_context():+ user = User(email='[email protected]', username='lonelyadmin',+ password_hash=generate_password_hash('x'),+ role=User.ROLE_ADMIN, is_active=True)+ db.session.add(user)+ db.session.commit()+ token = create_access_token(identity=user.id)++ response = client.get('/api/v1/admin/activity/summary',+ headers={'Authorization': f'Bearer {token}'})+ payload = response.get_json()+ assert payload['top_users'] == []+ assert payload['top_user_daily'] == []+ assert len(payload['daily_counts']) == DAYS+++# ------------------------------------------------------ dialect portability++class TestDayKeyPortability:+ """SQLite's date() returns 'YYYY-MM-DD'; PostgreSQL's returns a date object.+ Both have to reduce to the same key or the zero-fill silently misses."""++ def test_string_from_sqlite(self):+ assert _day_key('2026-08-12') == '2026-08-12'++ def test_date_object_from_postgres(self):+ from datetime import date as _date+ assert _day_key(_date(2026, 8, 12)) == '2026-08-12'++ def test_datetime_is_truncated(self):+ assert _day_key(datetime(2026, 8, 12, 13, 45)) == '2026-08-12'++ def test_string_with_time_is_truncated(self):+ assert _day_key('2026-08-12 13:45:00.000000') == '2026-08-12'++ def test_none_is_none(self):+ assert _day_key(None) is Nonediff --git a/backend/tests/test_container_status_bulk.py b/backend/tests/test_container_status_bulk.pynew file mode 100644index 00000000..035d99a3--- /dev/null+++ b/backend/tests/test_container_status_bulk.py@@ -0,0 +1,429 @@+"""Tests for the bulk (single ``docker ps``) container-status collection.++The point of this round: ``/status/apps`` used to spawn one ``docker compose ps``+per app plus one ``docker inspect`` per running container — ~120 processes for+30 apps — and the socket emitter re-paid that on a timer. These tests pin the+new contract: ONE collection pass per burst, shared between callers, dropped by+an explicit invalidate.+"""++import pytest++from app.services import container_status_service as css+from app.services.container_status_service import (+ STATUS_DEGRADED,+ STATUS_RUNNING_HEALTHY,+ STATUS_RUNNING_UNHEALTHY,+ STATUS_UNKNOWN,+)+from app.services.docker_service import DockerService++[email protected](autouse=True)+def clean_status_caches():+ """Both cache layers are process-global; isolate every test from the last."""+ from app.services import cache_service+ cache_service._memory_cache.clear()+ css.invalidate()+ yield+ cache_service._memory_cache.clear()+ css.invalidate()+++def _row(**kw):+ """A `docker ps` row in the shape list_compose_containers returns."""+ row = {'id': '', 'name': '', 'state': '', 'status': '',+ 'project': '', 'service': '', 'working_dir': '', 'config_files': ''}+ row.update(kw)+ return row+++class _FakeApp:+ """Minimal duck-typed Application for the index lookups."""++ def __init__(self, id=1, root_path=None, container_id=None,+ compose_file=None, server_id=None):+ self.id = id+ self.root_path = root_path+ self.container_id = container_id+ self.compose_file = compose_file+ self.server_id = server_id+++# ---------------------------------------------------- health off `docker ps`++class TestHealthFromStatus:+ def test_healthy_suffix(self):+ assert css._health_from_status('Up 2 minutes (healthy)') == 'healthy'++ def test_unhealthy_suffix(self):+ assert css._health_from_status('Up 3 hours (unhealthy)') == 'unhealthy'++ def test_health_starting_suffix(self):+ assert css._health_from_status('Up 4 seconds (health: starting)') == 'starting'++ def test_no_healthcheck_is_none(self):+ # No suffix at all -> None, which _normalize_health reads as 'none'.+ assert css._health_from_status('Up 5 days') is None+ assert css._health_from_status('') is None+ assert css._health_from_status(None) is None++ def test_exit_code_parens_are_not_health(self):+ # 'Exited (137) 2 minutes ago' must not be mistaken for a health value.+ assert css._health_from_status('Exited (137) 2 minutes ago') is None+ assert css._health_from_status('Restarting (1) 5 seconds ago') is None+++# ------------------------------------------------------------- the index++class TestContainerIndex:+ def test_matches_by_compose_working_dir(self):+ index = css._ContainerIndex([+ _row(id='a1', name='blog-web-1', state='running', status='Up 2 minutes',+ project='blog', service='web', working_dir='/srv/apps/blog'),+ ])+ rows = index.for_app(_FakeApp(root_path='/srv/apps/blog'))+ assert [r['id'] for r in rows] == ['a1']++ def test_working_dir_match_survives_trailing_slash(self):+ index = css._ContainerIndex([+ _row(id='a1', name='blog-web-1', state='running',+ working_dir='/srv/apps/blog/'),+ ])+ assert index.for_app(_FakeApp(root_path='/srv/apps/blog')) != []++ def test_falls_back_to_derived_project_name(self):+ """A custom COMPOSE_PROJECT_NAME still matches through working_dir; an+ older compose with no working_dir label matches through the name."""+ index = css._ContainerIndex([+ _row(id='b1', name='myblog-web-1', state='running', project='myblog'),+ ])+ # Directory 'My_Blog!' normalizes to compose's 'my_blog' — no match,+ # while a directory literally named 'myblog' does match.+ assert index.for_app(_FakeApp(root_path='/srv/apps/myblog')) != []+ assert index.for_app(_FakeApp(root_path='/srv/apps/other')) == []++ def test_matches_by_config_files_directory(self):+ index = css._ContainerIndex([+ _row(id='c1', name='api-web-1', state='running',+ config_files='/srv/apps/api/docker-compose.yml'),+ ])+ assert index.for_app(_FakeApp(root_path='/srv/apps/api')) != []++ def test_matches_bare_container_id(self):+ index = css._ContainerIndex([+ _row(id='d' * 64, name='lonely', state='running'),+ ])+ app = _FakeApp(root_path=None, container_id='d' * 64)+ assert index.for_app(app) != []+ # ...and by the short id apps often store.+ assert index.for_app(_FakeApp(container_id='d' * 12)) != []+ # ...and by name.+ assert index.for_app(_FakeApp(container_id='lonely')) != []++ def test_unmatched_app_gets_nothing(self):+ index = css._ContainerIndex([_row(id='x', working_dir='/elsewhere')])+ assert index.for_app(_FakeApp(root_path='/srv/apps/nope')) == []++ def test_one_container_is_not_double_counted(self):+ """A container is indexed under several keys; a lookup must still see it+ once, or 'total' would inflate."""+ index = css._ContainerIndex([+ _row(id='e1', name='shop-web-1', state='running', project='shop',+ working_dir='/srv/apps/shop',+ config_files='/srv/apps/shop/docker-compose.yml'),+ ])+ rows = index.for_app(_FakeApp(root_path='/srv/apps/shop'))+ assert len(rows) == 1+++class TestStatesFromIndexRows:+ def test_state_and_health_are_carried_through(self):+ states = css._states_from_index_rows([+ _row(id='1', name='/web', state='running',+ status='Up 2 minutes (unhealthy)', service='web'),+ ])+ assert states == [{'id': '1', 'name': 'web', 'service': 'web',+ 'state': 'running', 'health': 'unhealthy'}]++ def test_state_derived_from_status_when_column_missing(self):+ states = css._states_from_index_rows([+ _row(id='1', name='web', state='', status='Up 9 minutes'),+ ])+ assert states[0]['state'] == 'running'++ def test_aggregates_to_the_same_enum_as_before(self):+ rows = [+ _row(id='1', name='web', state='running', status='Up 1 minute', service='web'),+ _row(id='2', name='db', state='exited', status='Exited (0) 1 minute ago', service='db'),+ ]+ agg = css.aggregate_status(css._states_from_index_rows(rows))+ assert agg['status'] == STATUS_DEGRADED+ assert agg['total'] == 2+++# ------------------------------------------------- one pass for every app++def three_apps(app):+ """Three local compose apps sharing one Docker host."""+ from app import db+ from app.models import Application, User+ from werkzeug.security import generate_password_hash++ with app.app_context():+ owner = User.query.first()+ if owner is None:+ owner = User(email='[email protected]', username='statusprobe',+ password_hash=generate_password_hash('x'),+ role='admin', is_active=True)+ db.session.add(owner)+ db.session.commit()+ rows = []+ for name in ('alpha', 'beta', 'gamma'):+ row = Application(name=name, app_type='docker', status='running',+ user_id=owner.id, root_path=f'/srv/apps/{name}',+ compose_file='docker-compose.yml')+ db.session.add(row)+ rows.append(row)+ db.session.commit()+ yield rows+++class _Collector:+ """Counts how many times the bulk `docker ps` was actually run."""++ def __init__(self, rows):+ self.rows = rows+ self.calls = 0++ def __call__(self, all_containers=False):+ self.calls += 1+ return list(self.rows)+++def _install_collector(monkeypatch, rows):+ collector = _Collector(rows)+ monkeypatch.setattr(DockerService, 'list_compose_containers',+ staticmethod(collector))+ # Any use of these two would mean we regressed to the per-app shape.+ def _boom(*a, **kw): # pragma: no cover - only runs on regression+ raise AssertionError('per-app docker subprocess was spawned')+ monkeypatch.setattr(DockerService, 'compose_ps', staticmethod(_boom))+ monkeypatch.setattr(DockerService, 'get_container', staticmethod(_boom))+ return collector+++class TestSingleCollectionPass:+ def test_list_app_statuses_runs_docker_ps_once_for_all_apps(+ self, app, three_apps, monkeypatch):+ rows = []+ for name in ('alpha', 'beta', 'gamma'):+ rows.append(_row(id=f'{name}-web', name=f'{name}-web-1', state='running',+ status='Up 2 minutes (healthy)', project=name,+ service='web', working_dir=f'/srv/apps/{name}'))+ rows.append(_row(id=f'{name}-db', name=f'{name}-db-1', state='running',+ status='Up 2 minutes', project=name,+ service='db', working_dir=f'/srv/apps/{name}'))+ collector = _install_collector(monkeypatch, rows)++ with app.app_context():+ summaries = css.list_app_statuses()++ # 3 apps x 2 containers = 6 containers. The old shape cost 3 compose-ps+ # spawns + 6 inspects = 9; the new one costs exactly 1.+ assert collector.calls == 1+ assert len(summaries) == 3+ assert {s['status'] for s in summaries} == {STATUS_RUNNING_HEALTHY}+ assert all(s['total'] == 2 and s['healthy'] == 2 for s in summaries)++ def test_response_shape_is_unchanged(self, app, three_apps, monkeypatch):+ _install_collector(monkeypatch, [])+ with app.app_context():+ summaries = css.list_app_statuses()+ assert all(set(s) == {'app_id', 'status', 'total', 'healthy'}+ for s in summaries)++ def test_unhealthy_is_seen_without_any_inspect(self, app, three_apps, monkeypatch):+ """Health used to cost one `docker inspect` per running container. It now+ comes off the ps STATUS column — get_container is wired to explode."""+ _install_collector(monkeypatch, [+ _row(id='a1', name='alpha-web-1', state='running',+ status='Up 2 minutes (unhealthy)', project='alpha',+ service='web', working_dir='/srv/apps/alpha'),+ ])+ with app.app_context():+ result = css.get_app_status(three_apps[0].id, use_cache=False)+ assert result['status'] == STATUS_RUNNING_UNHEALTHY++ def test_app_with_no_containers_is_unknown(self, app, three_apps, monkeypatch):+ _install_collector(monkeypatch, [])+ with app.app_context():+ result = css.get_app_status(three_apps[0].id, use_cache=False)+ assert result['status'] == STATUS_UNKNOWN+ assert result['total'] == 0+ assert result['app_id'] == three_apps[0].id+ assert result['kind'] == 'app'+++class TestSnapshotSharing:+ def test_burst_of_callers_shares_one_collection(self, app, three_apps, monkeypatch):+ """The HTTP route and the socket emitter must not each trigger their own+ pass — that is what made the cost un-amortised."""+ collector = _install_collector(monkeypatch, [])+ with app.app_context():+ css.list_app_statuses() # HTTP /status/apps+ css._last_app_statuses.clear()+ css.get_changed_app_statuses() # socket emitter tick+ css.list_app_statuses()+ assert collector.calls == 1+ css._last_app_statuses.clear()++ def test_invalidate_forces_a_fresh_collection(self, app, three_apps, monkeypatch):+ """A stale status is worse than a slow one: an explicit start/stop/deploy+ must drop the snapshot, not wait out its TTL."""+ collector = _install_collector(monkeypatch, [])+ with app.app_context():+ css.list_app_statuses()+ assert collector.calls == 1+ css.invalidate()+ css.list_app_statuses()+ assert collector.calls == 2++ def test_invalidate_for_one_app_clears_its_cached_result(+ self, app, three_apps, monkeypatch):+ _install_collector(monkeypatch, [+ _row(id='a1', name='alpha-web-1', state='running',+ status='Up 1 minute', working_dir='/srv/apps/alpha'),+ ])+ app_id = three_apps[0].id+ with app.app_context():+ first = css.get_app_status(app_id)+ assert first['status'] == STATUS_RUNNING_HEALTHY++ # Same app, now stopped. Without invalidation the 8s per-app cache+ # would keep serving 'running'.+ _install_collector(monkeypatch, [])+ assert css.get_app_status(app_id)['status'] == STATUS_RUNNING_HEALTHY+ css.invalidate(app_id)+ assert css.get_app_status(app_id)['status'] == STATUS_UNKNOWN++ def test_use_cache_false_bypasses_the_snapshot_too(+ self, app, three_apps, monkeypatch):+ """'Give me the truth now' has to reach Docker, not a 3s-old snapshot."""+ collector = _install_collector(monkeypatch, [])+ with app.app_context():+ css.get_app_status(three_apps[0].id, use_cache=False)+ css.get_app_status(three_apps[0].id, use_cache=False)+ assert collector.calls == 2+++class TestRemoteAppsAreNotAnsweredFromTheLocalHost:+ def test_remote_app_never_reads_the_local_snapshot(self, app, monkeypatch):+ """An app on an agent-managed server must not inherit a local container+ that happens to share its project directory."""+ called = {}++ def _fake_remote(a):+ called['app'] = a+ return []++ monkeypatch.setattr(css, '_gather_remote_app_container_states', _fake_remote)+ index = css._ContainerIndex([+ _row(id='local1', name='alpha-web-1', state='running',+ working_dir='/srv/apps/alpha'),+ ])+ remote_app = _FakeApp(id=7, root_path='/srv/apps/alpha', server_id='srv-abc')+ assert css._gather_app_container_states(remote_app, index=index) == []+ assert called['app'] is remote_app++ def test_offline_agent_costs_nothing(self, app, monkeypatch):+ """send_command blocks for up to 30s; the 5s broadcast loop cannot pay+ that for a server that is not even connected."""+ from app.services import agent_registry as agent_registry_module+ from app.services.remote_docker_service import RemoteDockerService++ monkeypatch.setattr(agent_registry_module.agent_registry, 'get_agent',+ lambda server_id: None)++ def _boom(*a, **kw): # pragma: no cover - only runs on regression+ raise AssertionError('agent was contacted for an offline server')++ monkeypatch.setattr(RemoteDockerService, 'compose_ps', staticmethod(_boom))+ remote_app = _FakeApp(id=8, root_path='/srv/apps/alpha', server_id='srv-off')+ with app.app_context():+ assert css._gather_remote_app_container_states(remote_app) == []+++# ----------------------------------------------- the docker ps call itself++class TestListComposeContainers:+ def test_parses_tab_delimited_rows(self, monkeypatch):+ import app.services.docker_service as docker_module++ captured = {}++ class _Result:+ returncode = 0+ stdout = (+ 'abc123\tblog-web-1\trunning\tUp 2 minutes (healthy)\t'+ 'blog\tweb\t/srv/apps/blog\t/srv/apps/blog/docker-compose.yml\n'+ 'def456\tblog-db-1\trunning\tUp 2 minutes\t'+ 'blog\tdb\t/srv/apps/blog\t/srv/apps/blog/docker-compose.yml\n'+ )+ stderr = ''++ def _fake_run(cmd, **kwargs):+ captured['cmd'] = cmd+ return _Result()++ monkeypatch.setattr(docker_module.subprocess, 'run', _fake_run)+ rows = DockerService.list_compose_containers()++ assert len(rows) == 2+ assert rows[0]['project'] == 'blog'+ assert rows[0]['service'] == 'web'+ assert rows[0]['working_dir'] == '/srv/apps/blog'+ assert rows[0]['status'] == 'Up 2 minutes (healthy)'+ # One process, and no `-a`: matching `docker compose ps`'s default keeps+ # a fully-stopped project reporting exactly what it reported before.+ assert captured['cmd'][:2] == ['docker', 'ps']+ assert '-a' not in captured['cmd']++ def test_missing_labels_do_not_drop_the_row(self, monkeypatch):+ import app.services.docker_service as docker_module++ class _Result:+ returncode = 0+ # A non-compose container: trailing label columns are empty, and+ # some docker versions render them as '<no value>'.+ stdout = 'zzz\tsolo\trunning\tUp 1 hour\t<no value>\t<no value>\t\t\n'+ stderr = ''++ monkeypatch.setattr(docker_module.subprocess, 'run',+ lambda cmd, **kw: _Result())+ rows = DockerService.list_compose_containers()+ assert len(rows) == 1+ assert rows[0]['name'] == 'solo'+ assert rows[0]['project'] == ''++ def test_docker_failure_is_an_empty_list(self, monkeypatch):+ import app.services.docker_service as docker_module++ class _Result:+ returncode = 1+ stdout = ''+ stderr = 'Cannot connect to the Docker daemon'++ monkeypatch.setattr(docker_module.subprocess, 'run',+ lambda cmd, **kw: _Result())+ assert DockerService.list_compose_containers() == []++ def test_exception_is_an_empty_list(self, monkeypatch):+ import app.services.docker_service as docker_module++ def _explode(*a, **kw):+ raise OSError('docker not found')++ monkeypatch.setattr(docker_module.subprocess, 'run', _explode)+ assert DockerService.list_compose_containers() == []