Files
ServiceMap-Public/backend/app/health.py
NuklearRabbit adc2c70c21 ServiceMap: update tests and backend/app (+7 areas)
Public changes compared with the previous published export:
89 files changed.

Affected areas:
- backend/app: 21 files
- dependencies: 5 files
- deployment and CI: 6 files
- documentation: 2 files
- frontend: 3 files
- frontend/src: 20 files
- project files: 1 file
- tests: 30 files
- unraid: 1 file

Changed files:
- Updated: ".env.example"
- Updated: "Dockerfile.unraid"
- Updated: "README.md"
- Updated: "backend/Dockerfile"
- Added: "backend/app/alerts.py"
- Updated: "backend/app/auth.py"
- Added: "backend/app/backups.py"
- Updated: "backend/app/config_io.py"
- Updated: "backend/app/demo.py"
- Updated: "backend/app/discovery.py"
- Updated: "backend/app/discovery_store.py"
- Updated: "backend/app/docker_discovery.py"
- Updated: "backend/app/events.py"
- Updated: "backend/app/evidence.py"
- Updated: "backend/app/health.py"
- Updated: "backend/app/incidents.py"
- Added: "backend/app/loops.py"
- Updated: "backend/app/main.py"
- Updated: "backend/app/models.py"
- Updated: "backend/app/observation.py"
- Updated: "backend/app/probes.py"
- Added: "backend/app/response_cache.py"
- Updated: "backend/app/scheduler.py"
- Updated: "backend/app/semantic.py"
- Updated: "backend/app/store.py"
- Updated: "backend/pyproject.toml"
- Updated: "backend/tests/test_api.py"
- Updated: "backend/tests/test_config_io.py"
- Updated: "backend/tests/test_demo.py"
- Updated: "backend/tests/test_discovery_v120.py"
- Updated: "backend/tests/test_health.py"
- Updated: "backend/tests/test_probes.py"
- Updated: "backend/tests/test_scheduler.py"
- Updated: "backend/tests/test_v130_probe_reachability.py"
- Updated: "backend/tests/test_v160_evidence_changes.py"
- Added: "backend/tests/test_v163_alerts.py"
- Added: "backend/tests/test_v163_backups.py"
- Added: "backend/tests/test_v163_data_integrity.py"
- Added: "backend/tests/test_v163_efficiency.py"
- Added: "backend/tests/test_v163_health_performance.py"
- Added: "backend/tests/test_v163_loops.py"
- Added: "backend/tests/test_v163_management.py"
- Added: "backend/tests/test_v163_realtime.py"
- Added: "backend/tests/test_v163_security.py"
- Added: "backend/tests/test_v163_semantic_etag.py"
- Added: "compose.demo.yaml"
- Updated: "compose.yaml"
- Added: "docker-compose.broker.yml"
- Updated: "docker-compose.yml"
- Updated: "docs/ARCHITECTURE.md"
- Added: "frontend/e2e/a11y.spec.ts"
- Updated: "frontend/e2e/demo.spec.ts"
- Updated: "frontend/index.html"
- Updated: "frontend/nginx.conf"
- Updated: "frontend/package-lock.json"
- Updated: "frontend/package.json"
- Updated: "frontend/playwright.config.ts"
- Updated: "frontend/src/App.tsx"
- Updated: "frontend/src/LivingEdge.tsx"
- Added: "frontend/src/activityView.test.ts"
- Added: "frontend/src/activityView.ts"
- Updated: "frontend/src/api.ts"
- Added: "frontend/src/apiAuth.test.ts"
- Added: "frontend/src/components/ActionsMenu.tsx"
- Added: "frontend/src/components/ConfirmDialog.test.tsx"
- Added: "frontend/src/components/ConfirmDialog.tsx"
- Added: "frontend/src/components/StatusIcon.tsx"
- Updated: "frontend/src/demo/DemoWalkthrough.test.ts"
- Updated: "frontend/src/demo/DemoWalkthrough.tsx"
- Added: "frontend/src/elk-worker-url.d.ts"
- Added: "frontend/src/labels.ts"
- Updated: "frontend/src/main.tsx"
- Added: "frontend/src/realtime/refreshPlan.test.ts"
- Added: "frontend/src/realtime/refreshPlan.ts"
- Updated: "frontend/src/realtime/useRealtimeEvents.ts"
- Added: "frontend/src/routing/urlState.test.ts"
- Added: "frontend/src/routing/urlState.ts"
- Updated: "frontend/src/styles.css"
- Added: "frontend/src/topology/GraphListView.test.tsx"
- Added: "frontend/src/topology/GraphListView.tsx"
- Updated: "frontend/src/topology/ServiceNode.tsx"
- Updated: "frontend/src/topology/TopologyCommandBar.tsx"
- Added: "frontend/src/topology/semanticGraph.test.ts"
- Updated: "frontend/src/topology/semanticGraph.ts"
- Added: "frontend/src/topologyLayout.test.ts"
- Updated: "frontend/src/topologyLayout.ts"
- Updated: "package-lock.json"
- Updated: "package.json"
- Updated: "unraid/my-servicemap.xml"

Export provenance/manifests refreshed; excluded from the change summary.

Publish curated ServiceMap source from af46f684e468dffd3a5190e28d3b572d4c26bfd7
2026-11-20 13:51:46 +01:00

157 lines
5.6 KiB
Python

from __future__ import annotations
from typing import NamedTuple
from .models import HealthStatus, Service
from .store import Store
SEVERITY = {
HealthStatus.MAINTENANCE: 0,
HealthStatus.UNMONITORED: 0,
HealthStatus.UNKNOWN: 0,
HealthStatus.HEALTHY: 1,
HealthStatus.RECOVERING: 2,
HealthStatus.DEGRADED: 3,
HealthStatus.DOWN: 4,
}
class TopologyHealth(NamedTuple):
services: list[Service]
direct: dict[str, HealthStatus]
effective: dict[str, HealthStatus]
dependency: dict[str, HealthStatus]
def topology_health(store: Store, *, services: list[Service] | None = None) -> TopologyHealth:
"""Resolve direct, dependency and effective health for the whole graph.
One bulk evidence read plus one in-memory traversal. The former
per-service recursive walk re-queried SQLite for every node on every
dependency path and dominated server CPU.
"""
services = store.list_services() if services is None else services
direct, _ = store.service_healths([service.id for service in services])
effective, dependency = status_maps(services, direct_statuses=direct)
return TopologyHealth(services, direct, effective, dependency)
def _single(service: Service, store: Store) -> TopologyHealth:
services = [item for item in store.list_services() if item.id != service.id]
return topology_health(store, services=[*services, service])
def dependency_status(service: Service, store: Store) -> HealthStatus:
return _single(service, store).dependency.get(service.id, HealthStatus.UNKNOWN)
def effective_status(service: Service, store: Store) -> HealthStatus:
return _single(service, store).effective.get(service.id, HealthStatus.UNKNOWN)
def status_maps(
services: list[Service],
*,
direct_statuses: dict[str, HealthStatus] | None = None,
) -> tuple[dict[str, HealthStatus], dict[str, HealthStatus]]:
"""Calculate the complete topology in memory for one bounded API read."""
by_id = {service.id: service for service in services}
def legacy_direct_state(service: Service) -> HealthStatus:
runtime_state = (service.runtime_state or "").casefold()
runtime_health = (service.runtime_health or "").casefold()
if not service.enabled:
return HealthStatus.UNKNOWN
if service.maintenance_enabled:
return HealthStatus.MAINTENANCE
if runtime_health == "unhealthy" or runtime_state in {"exited", "dead", "stopped"}:
return HealthStatus.DOWN
if runtime_state == "restarting":
return HealthStatus.DEGRADED
if not service.monitoring_enabled:
if runtime_health == "healthy":
return HealthStatus.HEALTHY
return HealthStatus.UNMONITORED if service.monitoring_state.value in {"unmonitored", "observed"} else HealthStatus.UNKNOWN
return service.state
direct = {
service.id: (direct_statuses.get(service.id, HealthStatus.UNKNOWN) if direct_statuses else legacy_direct_state(service))
for service in services
}
effective: dict[str, HealthStatus] = {}
dependency: dict[str, HealthStatus] = {}
def visit(service_id: str, visiting: set[str]) -> HealthStatus:
if service_id in effective:
return effective[service_id]
if service_id in visiting:
return HealthStatus.UNKNOWN
service = by_id.get(service_id)
if service is None:
return HealthStatus.UNKNOWN
next_visiting = {*visiting, service_id}
own = direct[service_id]
if not service.depends_on:
dependency[service_id] = HealthStatus.HEALTHY
effective[service_id] = own
return own
dependencies = [visit(item, next_visiting) for item in service.depends_on]
dependency_state = max(dependencies, key=lambda status: SEVERITY[status])
dependency[service_id] = dependency_state
if own == HealthStatus.MAINTENANCE or own == HealthStatus.DOWN:
result = own
elif dependency_state == HealthStatus.DOWN or (
dependency_state in {HealthStatus.DEGRADED, HealthStatus.RECOVERING}
and own in {HealthStatus.HEALTHY, HealthStatus.RECOVERING}
):
result = HealthStatus.DEGRADED
else:
result = own
effective[service_id] = result
return result
for service in services:
visit(service.id, set())
return effective, dependency
def validate_dependencies(
depends_on: list[str],
store: Store,
*,
service_id: str | None = None,
) -> None:
if len(depends_on) != len(set(depends_on)):
raise ValueError("Afhankelijkheden moeten uniek zijn")
if service_id and service_id in depends_on:
raise ValueError("Een service kan niet van zichzelf afhangen")
services = store.list_services()
known_ids = {service.id for service in services}
missing = [dependency_id for dependency_id in depends_on if dependency_id not in known_ids]
if missing:
raise ValueError(f"Onbekende afhankelijkheids-id's: {', '.join(missing)}")
if service_id is None:
return
graph = {service.id: list(service.depends_on) for service in services}
graph[service_id] = list(depends_on)
visiting: set[str] = set()
visited: set[str] = set()
def visit(node: str) -> None:
if node in visiting:
raise ValueError("De afhankelijkheidsgrafiek bevat een kring")
if node in visited:
return
visiting.add(node)
for dependency_id in graph.get(node, []):
visit(dependency_id)
visiting.remove(node)
visited.add(node)
for node in graph:
visit(node)