from __future__ import annotations import asyncio import logging import os import sqlite3 from contextlib import asynccontextmanager, suppress from datetime import datetime, timedelta from pathlib import Path from fastapi import FastAPI, HTTPException, Query, Request, Response, status from fastapi.responses import FileResponse, JSONResponse, StreamingResponse from fastapi.staticfiles import StaticFiles from .alerts import AlertManager, AlertSettingsUpdate, AlertSettingsView, AlertTestResult from .auth import AuthConfig, AuthorizationError, admin_token, admin_token_path, authorize_request from .backups import BackupInfo, BackupManager from .config_io import export_document, import_document, parse_document, serialize_document from .demo import DEMO_ROOT_CAUSE_NAME, DEMO_SCENARIO_NAME, build_demo_records from .discovery import DiscoveryManager from .discovery_store import DiscoveryStore from .docker_discovery import discover_containers from .docker_provider import docker_provider_mode from .events import SSE_RETRY_MS, event_bus, next_event, sse_payload from .health import TopologyHealth, status_maps, topology_health, validate_dependencies from .incidents import incident_overview, reconcile_incidents, sync_incidents from .loops import loop_registry, run_periodic from .models import ( AvailabilitySummary, BulkProbeResult, ConfigImportRequest, ConfigImportResult, DemoMutationResult, DemoState, DiscoveryObservation, DiscoveryReconcileRequest, DiscoveryResult, DiscoveryRunRequest, DiscoveryStatus, DockerDiscoveryRequest, DockerDiscoveryResult, EdgeActionRequest, EdgeMutationRequest, EventRecord, HealthResolution, HealthStatus, IncidentNoteRequest, IncidentOverview, IncidentResolveRequest, LogicalRelation, MaintenanceRequest, OverviewSummary, ProbeRepairReport, ProbeResult, ProviderStatus, ReconcileResult, RelationEvidence, SemanticTopology, Service, ServiceCreate, ServiceSnapshot, ServiceUpdate, SuggestionStatus, TopologyDiff, TopologySnapshot, TopologySuggestion, ) from .observation import ObservationManager from .probes import probe_many, probe_service from .response_cache import ( GENERATED_AT_HEADER, CachedRepresentation, RepresentationCache, etag_matches, next_freshness_boundary, representation, ) from .scheduler import scheduled_probe_loop from .semantic import build_semantic_topology from .store import Store store = Store() discovery_store = DiscoveryStore(store) discovery_manager = DiscoveryManager(store, discovery_store) def _env_int(name: str, default: int, minimum: int = 0) -> int: try: return max(minimum, int(os.getenv(name, str(default)))) except ValueError: return default def _env_float(name: str, default: float, minimum: float = 0.1) -> float: try: return max(minimum, float(os.getenv(name, str(default)))) except ValueError: return default logger = logging.getLogger("servicemap") if not logger.handlers: # Uvicorn configures only its own loggers; without a handler our INFO # lines (maintenance results, loop recovery) were never emitted. _handler = logging.StreamHandler() _handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")) logger.addHandler(_handler) logger.setLevel(logging.INFO) logger.propagate = False PROBE_INTERVAL = _env_int("SERVICEMAP_PROBE_INTERVAL", 0) PROBE_TIMEOUT = _env_float("SERVICEMAP_PROBE_TIMEOUT", 5.0) PROBE_RETRIES = _env_int("SERVICEMAP_PROBE_RETRIES", 1) PROBE_CONCURRENCY = _env_int("SERVICEMAP_PROBE_CONCURRENCY", 10, minimum=1) RETENTION_DAYS = _env_int("SERVICEMAP_RETENTION_DAYS", 30, minimum=1) EVENT_RETENTION_DAYS = _env_int("SERVICEMAP_EVENT_RETENTION_DAYS", 90, minimum=1) DISCOVERY_INTERVAL = _env_int("SERVICEMAP_DISCOVERY_INTERVAL_SECONDS", 0, minimum=0) OBSERVATION_INTERVAL = _env_int("SERVICEMAP_OBSERVATION_INTERVAL_SECONDS", 60) if OBSERVATION_INTERVAL > 0: OBSERVATION_INTERVAL = max(10, OBSERVATION_INTERVAL) # Superseded health-evidence history; current claims are never pruned. 0 disables. EVIDENCE_RETENTION_DAYS = _env_int("SERVICEMAP_EVIDENCE_RETENTION_DAYS", 7, minimum=0) MAINTENANCE_INTERVAL = _env_int("SERVICEMAP_MAINTENANCE_INTERVAL_SECONDS", 3600, minimum=60) # Discovery runs whose per-container observations are kept. DISCOVERY_OBSERVATION_RUNS = _env_int("SERVICEMAP_DISCOVERY_OBSERVATION_RUNS", 10, minimum=1) # Verified database backups in /backups; 0 hours disables the schedule. BACKUP_INTERVAL_HOURS = _env_int("SERVICEMAP_BACKUP_INTERVAL_HOURS", 24, minimum=0) BACKUP_KEEP = _env_int("SERVICEMAP_BACKUP_KEEP", 14, minimum=1) observation_manager = ObservationManager(store, interval_seconds=OBSERVATION_INTERVAL) semantic_cache = RepresentationCache(max_age_seconds=10.0) _alert_manager: AlertManager | None = None def backup_manager() -> BackupManager: return BackupManager(store, keep=BACKUP_KEEP) def alert_manager() -> AlertManager: """Bound to the current store (tests swap ``store``).""" global _alert_manager if _alert_manager is None or _alert_manager.store is not store: _alert_manager = AlertManager(store) return _alert_manager def run_maintenance() -> dict[str, int]: """Bounded retention for tables that otherwise grow with uptime.""" if store.demo_active(): return {} pruned = { "health_evidence": store.prune_health_evidence(EVIDENCE_RETENTION_DAYS), "probe_results": store.prune_probes(RETENTION_DAYS, EVENT_RETENTION_DAYS), "discovery_observations": discovery_store.prune_observations(DISCOVERY_OBSERVATION_RUNS), "topology_change_events": discovery_store.prune_change_events(EVENT_RETENTION_DAYS), } store.checkpoint() return pruned def _require_configured_discovery_targets( docker_host: str | None = None, appops_url: str | None = None ) -> None: """Request bodies cannot redirect an unauthenticated LAN deployment's inventory reads.""" configured_docker = os.getenv("SERVICEMAP_DOCKER_HOST", "unix:///var/run/docker.sock") configured_appops = os.getenv("SERVICEMAP_APPOPS_URL", "") if docker_host and docker_host != configured_docker: raise HTTPException(status_code=403, detail="Er is geen Docker-detectiedoel ingesteld") if appops_url and appops_url.rstrip("/") != configured_appops.rstrip("/"): raise HTTPException(status_code=403, detail="Er is geen AppOps-detectiedoel ingesteld") @asynccontextmanager async def lifespan(_: FastAPI): try: if AuthConfig.from_environment().mode == "token": # Create the token on first start so its location is logged # before anyone needs it. admin_token() logger.info("Editing requires the admin token in %s", admin_token_path()) except (AuthorizationError, OSError): logger.exception("ServiceMap admin token is unavailable; edits are refused") task: asyncio.Task[None] | None = None if PROBE_INTERVAL > 0: task = asyncio.create_task( scheduled_probe_loop( store, interval_seconds=PROBE_INTERVAL, timeout_seconds=PROBE_TIMEOUT, retries=PROBE_RETRIES, concurrency=PROBE_CONCURRENCY, retention_days=RETENTION_DAYS, event_retention_days=EVENT_RETENTION_DAYS, ) ) background: list[asyncio.Task[None]] = [] if DISCOVERY_INTERVAL > 0: async def discovery_cycle() -> None: result = await asyncio.to_thread(discovery_manager.run, DiscoveryRunRequest()) await asyncio.to_thread( discovery_manager.reconcile, DiscoveryReconcileRequest(run_id=result.run.id, apply_high_confidence=False), ) background.append(asyncio.create_task(run_periodic( "discovery", discovery_cycle, interval_seconds=DISCOVERY_INTERVAL, initial_delay_seconds=DISCOVERY_INTERVAL, paused=store.demo_active, ))) if OBSERVATION_INTERVAL > 0: async def observation_cycle() -> None: await asyncio.to_thread(observation_manager.refresh) background.append(asyncio.create_task(run_periodic( "observation", observation_cycle, interval_seconds=OBSERVATION_INTERVAL, paused=store.demo_active, ))) async def alert_cycle() -> None: sent = await asyncio.to_thread(alert_manager().run_cycle) if sent: logger.info("Sent %s incident notification(s)", sent) background.append(asyncio.create_task(run_periodic( "alerts", alert_cycle, interval_seconds=30, paused=store.demo_active, ))) if BACKUP_INTERVAL_HOURS > 0: async def backup_cycle() -> None: info = await asyncio.to_thread(backup_manager().create) logger.info("Backup %s written (%s bytes, verified)", info.name, info.size_bytes) background.append(asyncio.create_task(run_periodic( "backups", backup_cycle, interval_seconds=BACKUP_INTERVAL_HOURS * 3600, # First backup shortly after start, then on the schedule. initial_delay_seconds=120, paused=store.demo_active, ))) async def maintenance_window_cycle() -> None: ended = await asyncio.to_thread(store.expire_maintenance_windows) if ended: logger.info("Maintenance windows ended for %s service(s)", len(ended)) background.append(asyncio.create_task(run_periodic( "maintenance-windows", maintenance_window_cycle, interval_seconds=60, paused=store.demo_active, ))) async def maintenance_cycle() -> None: pruned = await asyncio.to_thread(run_maintenance) if any(pruned.values()): logger.info("Maintenance pruned %s", pruned) # Start shortly after boot so a long-running deployment sheds its backlog # without delaying startup, then repeat on a slow cadence. background.append(asyncio.create_task(run_periodic( "maintenance", maintenance_cycle, interval_seconds=MAINTENANCE_INTERVAL, initial_delay_seconds=30, ))) try: yield finally: if task: task.cancel() with suppress(asyncio.CancelledError): await task for background_task in background: background_task.cancel() for background_task in background: with suppress(asyncio.CancelledError): await background_task app = FastAPI(title="ServiceMap API", version="1.6.4", lifespan=lifespan) # Bounded request bodies: the largest legitimate payload is a config import. MAX_REQUEST_BYTES = 4 * 1024 * 1024 SECURITY_HEADERS = { "X-Content-Type-Options": "nosniff", "X-Frame-Options": "DENY", "Referrer-Policy": "no-referrer", "Cross-Origin-Opener-Policy": "same-origin", # The SPA ships no inline scripts or third-party assets; React Flow and # the layout use inline style attributes. "Content-Security-Policy": ( "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; " "img-src 'self' data: blob:; font-src 'self'; connect-src 'self'; " "object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'" ), } @app.middleware("http") async def authorization_boundary(request, call_next): declared = request.headers.get("content-length", "") if declared.isdigit() and int(declared) > MAX_REQUEST_BYTES: response = JSONResponse(status_code=413, content={"detail": "De aanvraag is te groot"}) else: try: authorize_request(request) except HTTPException as exc: response = JSONResponse( status_code=exc.status_code, content={"detail": exc.detail}, headers=dict(exc.headers or {}), ) else: response = await call_next(request) for name, value in SECURITY_HEADERS.items(): response.headers.setdefault(name, value) path = request.url.path if path.startswith("/assets/"): # Vite fingerprints asset names, so they never change in place. response.headers.setdefault("Cache-Control", "public, max-age=31536000, immutable") elif not path.startswith("/api/") and path not in {"/health", "/ready"}: # index.html (and other unhashed files) must be revalidated, or a # browser keeps running the previous release after a deploy. response.headers.setdefault("Cache-Control", "no-cache") return response def _snapshot(service: Service) -> ServiceSnapshot: services = [item for item in store.list_services() if item.id != service.id] health = topology_health(store, services=[*services, service]) dependents = [item.name for item in services if service.id in item.depends_on] return ServiceSnapshot( service=service, latest_probe=store.latest_probe(service.id), availability_24h=store.availability_summary(service.id, 24), dependency_status=health.dependency.get(service.id, HealthStatus.UNKNOWN), effective_status=health.effective.get(service.id, HealthStatus.UNKNOWN), direct_status=health.direct.get(service.id, HealthStatus.UNKNOWN), flapping=service.flapping, flap_count=service.flap_count, next_probe_at=store.next_probe_at(service, PROBE_INTERVAL), dependents=dependents, sources=discovery_store.sources_for_service(service.id), evidence=discovery_store.evidence_for_service(service.id), health_evidence=store.health_evidence_for_service(service.id), health_resolution=store.health_resolution(service.id), maintenance_until=store.maintenance_windows().get(service.id), ) def _name_conflict(name: str, *, exclude_id: str | None = None) -> bool: existing = store.get_service_by_name(name) return existing is not None and existing.id != exclude_id @app.get("/health") def health() -> dict[str, str]: """Liveness: the process answers. Used by the container healthcheck.""" return {"status": "ok"} @app.get("/ready") def ready() -> JSONResponse: """Readiness: the database takes writes and no background loop stalled. A loop is stalled when it has not completed a cycle for three intervals (at least three minutes); its last error is included for diagnosis. """ checks: dict[str, object] = {"loops": loop_registry.snapshot()} database_ok = True try: store.ready_check() except sqlite3.Error as exc: database_ok = False checks["database_error"] = f"{type(exc).__name__}: {exc}" checks["database"] = "ok" if database_ok else "unavailable" ok = database_ok and loop_registry.all_healthy() return JSONResponse( status_code=status.HTTP_200_OK if ok else status.HTTP_503_SERVICE_UNAVAILABLE, content={"status": "ready" if ok else "not_ready", **checks}, ) @app.get("/api/v1/meta") def meta() -> dict[str, str | int | float | bool]: try: auth_mode = AuthConfig.from_environment().mode except AuthorizationError: auth_mode = "misconfigured" return { "name": "ServiceMap", "version": "1.6.4", "stage": "v1", "scheduled_probing": PROBE_INTERVAL > 0, "probe_interval_seconds": PROBE_INTERVAL, "probe_timeout_seconds": PROBE_TIMEOUT, "probe_retries": PROBE_RETRIES, "probe_concurrency": PROBE_CONCURRENCY, "retention_days": RETENTION_DAYS, "event_retention_days": EVENT_RETENTION_DAYS, "sse": True, "discovery": True, "discovery_interval_seconds": DISCOVERY_INTERVAL, "observation_interval_seconds": OBSERVATION_INTERVAL, "docker_configured": bool(os.getenv("SERVICEMAP_DOCKER_HOST")), "docker_provider": docker_provider_mode(), "appops_configured": bool(os.getenv("SERVICEMAP_APPOPS_URL")), "auth_mode": auth_mode, # What a browser needs before it may change anything. "write_auth": {"token": "admin_token", "oidc": "oidc"}.get(auth_mode, "none"), } @app.get("/api/v1/settings/alerts", response_model=AlertSettingsView) def get_alert_settings() -> AlertSettingsView: return alert_manager().view() @app.put("/api/v1/settings/alerts", response_model=AlertSettingsView) def put_alert_settings(payload: AlertSettingsUpdate) -> AlertSettingsView: try: return alert_manager().update(payload) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc @app.post("/api/v1/settings/alerts/test", response_model=AlertTestResult) def test_alert() -> AlertTestResult: return alert_manager().send_test() @app.get("/api/v1/backups", response_model=list[BackupInfo]) def list_backups() -> list[BackupInfo]: return backup_manager().list() @app.post("/api/v1/backups", response_model=BackupInfo, status_code=status.HTTP_201_CREATED) def create_backup() -> BackupInfo: try: return backup_manager().create() except (ValueError, RuntimeError, sqlite3.Error) as exc: raise HTTPException(status_code=409, detail=str(exc)) from exc @app.get("/api/v1/backups/{name}") def download_backup(name: str) -> FileResponse: try: path = backup_manager().path_for(name) except ValueError as exc: raise HTTPException(status_code=404, detail=str(exc)) from exc return FileResponse(path, media_type="application/vnd.sqlite3", filename=name) @app.post("/api/v1/backups/{name}/restore", response_model=BackupInfo) def restore_backup(name: str) -> BackupInfo: """Restore ``name`` into the live database; returns the safety copy taken first.""" try: safety = backup_manager().restore(name) except ValueError as exc: raise HTTPException(status_code=409, detail=str(exc)) from exc except (RuntimeError, sqlite3.Error) as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc semantic_cache.clear() event_bus.publish({"event_type": "topology.changed", "message": f"Database teruggezet vanaf {name}"}) return safety @app.post("/api/v1/auth/verify") def verify_write_access() -> dict[str, bool]: """Succeeds only for a caller allowed to mutate (checked by the middleware).""" return {"ok": True} def _visible_services(services: list[Service]) -> list[Service]: return [ service for service in services if service.visibility.value == "visible" and service.lifecycle.value not in {"retired", "ignored"} ] def _service_snapshots( *, include_hidden: bool = False, include_details: bool = True ) -> tuple[list[ServiceSnapshot], TopologyHealth]: all_services = store.list_services() services = all_services if include_hidden else _visible_services(all_services) visible_ids = [service.id for service in services] latest = store.latest_probes(visible_ids) availability = store.availability_summaries(visible_ids, 24) if include_details else {} evidence_by_service = store.health_evidence_for_services([service.id for service in all_services]) # Calculate propagation over the complete canonical graph first. Hidden # infrastructure nodes may still sit between a visible service and its # dependency; filtering before this calculation would turn that impact # into a misleading unknown state. direct_all, resolutions = store.service_healths( [service.id for service in all_services], evidence_by_service=evidence_by_service ) effective_all, dependency_all = status_maps(all_services, direct_statuses=direct_all) dependent_names: dict[str, list[str]] = {} if include_details: for service in services: for dependency_id in set(service.depends_on): dependent_names.setdefault(dependency_id, []).append(service.name) windows = store.maintenance_windows() if include_details else {} sources_by_service = discovery_store.sources_for_services(visible_ids) if include_details else {} relations_by_service = discovery_store.evidence_for_services(visible_ids) if include_details else {} snapshots = [ ServiceSnapshot( service=service, latest_probe=latest.get(service.id), availability_24h=availability.get(service.id), dependency_status=dependency_all.get(service.id, service.state), effective_status=effective_all.get(service.id, service.state), direct_status=direct_all.get(service.id, service.state), flapping=service.flapping, flap_count=service.flap_count, next_probe_at=( latest[service.id].checked_at + timedelta(seconds=service.interval_seconds or PROBE_INTERVAL) if service.id in latest and (service.interval_seconds or PROBE_INTERVAL) > 0 else None ), dependents=dependent_names.get(service.id, []), sources=sources_by_service.get(service.id, []), evidence=relations_by_service.get(service.id, []), health_evidence=evidence_by_service.get(service.id, []), health_resolution=resolutions.get(service.id) or HealthResolution(), maintenance_until=windows.get(service.id), ) for service in services ] return snapshots, TopologyHealth(all_services, direct_all, effective_all, dependency_all) @app.get("/api/v1/services", response_model=list[ServiceSnapshot]) def list_services(include_hidden: bool = Query(default=False)) -> list[ServiceSnapshot]: snapshots, _ = _service_snapshots(include_hidden=include_hidden) return snapshots @app.post("/api/v1/services", response_model=Service, status_code=status.HTTP_201_CREATED) def create_service(payload: ServiceCreate) -> Service: if _name_conflict(payload.name): raise HTTPException(status_code=409, detail="Er bestaat al een service met deze naam") try: validate_dependencies(payload.depends_on, store) return store.create_service(payload) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc except sqlite3.IntegrityError as exc: raise HTTPException(status_code=409, detail="Er bestaat al een service met deze naam") from exc @app.get("/api/v1/services/{service_id}", response_model=ServiceSnapshot) def get_service(service_id: str) -> ServiceSnapshot: service = store.get_service(service_id) if service is None: raise HTTPException(status_code=404, detail="Service niet gevonden") return _snapshot(service) @app.put("/api/v1/services/{service_id}", response_model=Service) def update_service(service_id: str, payload: ServiceUpdate) -> Service: before = store.get_service(service_id) if before is None: raise HTTPException(status_code=404, detail="Service niet gevonden") if _name_conflict(payload.name, exclude_id=service_id): raise HTTPException(status_code=409, detail="Er bestaat al een service met deze naam") try: validate_dependencies(payload.depends_on, store, service_id=service_id) updated = store.update_service(service_id, payload) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc except sqlite3.IntegrityError as exc: raise HTTPException(status_code=409, detail="Er bestaat al een service met deze naam") from exc if updated is None: raise HTTPException(status_code=404, detail="Service niet gevonden") if before.source_type: discovery_manager.record_service_override(before, updated) return updated @app.delete("/api/v1/services/{service_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_service(service_id: str) -> None: if not store.delete_service(service_id): raise HTTPException(status_code=404, detail="Service niet gevonden") @app.post("/api/v1/services/{service_id}/probe", response_model=ProbeResult) async def run_probe( service_id: str, timeout_seconds: float | None = Query(default=None, ge=0.1, le=30), retries: int | None = Query(default=None, ge=0, le=5), ) -> ProbeResult: if store.demo_active(): raise HTTPException(status_code=409, detail="De demo gebruikt synthetische controlegegevens; live controles zijn gepauzeerd") service = store.get_service(service_id) if service is None: raise HTTPException(status_code=404, detail="Service niet gevonden") result = await probe_service( service, timeout_seconds=timeout_seconds, retries=retries, trigger="manual", default_timeout_seconds=PROBE_TIMEOUT, default_retries=PROBE_RETRIES, ) return store.save_probe(result) @app.post("/api/v1/probe-all", response_model=BulkProbeResult) async def run_all_probes( timeout_seconds: float | None = Query(default=None, ge=0.1, le=30), retries: int | None = Query(default=None, ge=0, le=5), concurrency: int = Query(default=PROBE_CONCURRENCY, ge=1, le=50), ) -> BulkProbeResult: if store.demo_active(): return BulkProbeResult(requested=0, completed=0, results=[]) services = [service for service in store.list_services() if service.enabled and service.monitoring_enabled] results = await probe_many( services, timeout_seconds=timeout_seconds, retries=retries, concurrency=concurrency, trigger="manual", default_timeout_seconds=PROBE_TIMEOUT, default_retries=PROBE_RETRIES, ) for result in results: store.save_probe(result) return BulkProbeResult(requested=len(services), completed=len(results), results=results) @app.get("/api/v1/services/{service_id}/history", response_model=list[ProbeResult]) def probe_history(service_id: str, limit: int = Query(default=50, ge=1, le=500)) -> list[ProbeResult]: if store.get_service(service_id) is None: raise HTTPException(status_code=404, detail="Service niet gevonden") return store.probe_history(service_id, limit) @app.get("/api/v1/services/{service_id}/availability", response_model=AvailabilitySummary) def availability(service_id: str, hours: int = Query(default=24, ge=1, le=24 * 90)) -> AvailabilitySummary: if store.get_service(service_id) is None: raise HTTPException(status_code=404, detail="Service niet gevonden") return store.availability_summary(service_id, hours) @app.get("/api/v1/services/{service_id}/evidence", response_model=HealthResolution) def service_evidence(service_id: str) -> HealthResolution: if store.get_service(service_id) is None: raise HTTPException(status_code=404, detail="Service niet gevonden") return store.health_resolution(service_id) @app.get("/api/v1/providers", response_model=list[ProviderStatus]) def providers() -> list[ProviderStatus]: return store.provider_statuses() @app.post("/api/v1/services/{service_id}/maintenance", response_model=Service) def maintenance(service_id: str, payload: MaintenanceRequest) -> Service: try: updated = store.set_maintenance(service_id, payload.enabled, payload.reason, payload.until) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc if updated is None: raise HTTPException(status_code=404, detail="Service niet gevonden") return updated @app.get("/api/v1/incidents", response_model=IncidentOverview) def incidents() -> IncidentOverview: sync_incidents(store) overview = incident_overview(store) overview.incidents = store.list_incidents(active_only=False) return overview @app.get("/api/v1/incidents/{incident_id}") def get_incident(incident_id: str) -> dict[str, object]: incident = store.get_incident(incident_id) if incident is None: raise HTTPException(status_code=404, detail="Incident niet gevonden") return {"incident": incident.model_dump(mode="json"), "timeline": [item.model_dump(mode="json") for item in store.incident_timeline(incident_id)]} @app.post("/api/v1/incidents/{incident_id}/acknowledge") def acknowledge_incident(incident_id: str) -> dict[str, object]: try: incident = store.acknowledge_incident(incident_id) except ValueError as exc: raise HTTPException(status_code=409, detail=str(exc)) from exc if incident is None: raise HTTPException(status_code=404, detail="Incident niet gevonden") return {"incident": incident.model_dump(mode="json"), "timeline": [item.model_dump(mode="json") for item in store.incident_timeline(incident_id)]} def _incident_payload(incident_id: str) -> dict[str, object]: incident = store.get_incident(incident_id) if incident is None: raise HTTPException(status_code=404, detail="Incident niet gevonden") return { "incident": incident.model_dump(mode="json"), "timeline": [item.model_dump(mode="json") for item in store.incident_timeline(incident_id)], } @app.post("/api/v1/incidents/{incident_id}/resolve") def resolve_incident(incident_id: str, payload: IncidentResolveRequest | None = None) -> dict[str, object]: if store.resolve_incident(incident_id, payload.note if payload else None) is None: raise HTTPException(status_code=404, detail="Incident niet gevonden") return _incident_payload(incident_id) @app.post("/api/v1/incidents/{incident_id}/notes") def add_incident_note(incident_id: str, payload: IncidentNoteRequest) -> dict[str, object]: if store.add_incident_note(incident_id, payload.text.strip()) is None: raise HTTPException(status_code=404, detail="Incident niet gevonden") return _incident_payload(incident_id) @app.get("/api/v1/events", response_model=list[EventRecord]) def events( limit: int = Query(default=50, ge=1, le=500), service_id: str | None = None, event_type: str | None = Query(default=None, max_length=80), before: datetime | None = None, ) -> list[EventRecord]: return store.list_events(limit=limit, service_id=service_id, event_type=event_type, before=before) @app.get("/api/v1/events/stream") async def event_stream() -> StreamingResponse: subscription = event_bus.subscribe() async def stream(): try: # ``retry`` tells EventSource how long to wait before reconnecting. yield f"retry: {SSE_RETRY_MS}\n: connected\n\n" while True: payload = await next_event(subscription) if payload is None: yield ": keepalive\n\n" else: yield sse_payload(payload) finally: event_bus.unsubscribe(subscription) return StreamingResponse( stream(), media_type="text/event-stream", headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}, ) @app.get("/api/v1/overview", response_model=OverviewSummary) def overview() -> OverviewSummary: health, overview_state = reconcile_incidents(store) services = _visible_services(health.services) counts: dict[str, int] = {} coverage: dict[str, int] = {} for service in services: status = health.effective[service.id].value counts[status] = counts.get(status, 0) + 1 state = service.monitoring_state.value coverage[state] = coverage.get(state, 0) + 1 services_by_id = {service.id: service for service in health.services} return OverviewSummary( generated_at=overview_state.generated_at, total_services=len(services), health_counts=counts, active_incidents=len(store.list_incidents(active_only=True, services=services_by_id)), likely_root_causes=[candidate.service_name for candidate in overview_state.candidates], flapping_services=[service.name for service in services if service.flapping], recent_events=store.list_events(limit=10), monitoring_coverage=coverage, ) @app.get("/api/v1/config") def export_config(format: str = Query(default="yaml", pattern="^(yaml|json)$")) -> Response: document = export_document(store) content = serialize_document(document, format) media_type = "application/json" if format == "json" else "application/yaml" extension = "json" if format == "json" else "yaml" return Response(content=content, media_type=media_type, headers={"Content-Disposition": f'attachment; filename="servicemap.{extension}"'}) @app.post("/api/v1/config/import", response_model=ConfigImportResult) def import_config(payload: ConfigImportRequest) -> ConfigImportResult: try: document = parse_document(payload.content, payload.format) return import_document(store, document, payload.mode) except (ValueError, sqlite3.IntegrityError) as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc def _demo_enabled() -> bool: return os.getenv("SERVICEMAP_DEMO_ENABLED", "").strip().lower() in {"1", "true", "yes", "on"} def _demo_state() -> DemoState: enabled = _demo_enabled() if not store.demo_active(): return DemoState(active=False, enabled=enabled) sync_incidents(store) overview_state = incident_overview(store) candidate = overview_state.candidates[0] if overview_state.candidates else None return DemoState( active=True, enabled=enabled, synthetic=True, scenario_name=DEMO_SCENARIO_NAME, service_count=len(store.list_services()), root_cause_service_id=candidate.service_id if candidate else None, root_cause_service_name=candidate.service_name if candidate else DEMO_ROOT_CAUSE_NAME, affected_service_ids=candidate.affected_service_ids if candidate else [], affected_service_names=candidate.affected_service_names if candidate else [], affected_count=len(candidate.affected_service_ids) if candidate else 0, ) def _demo_result(result: ConfigImportResult | None = None, *, restored: bool = False) -> DemoMutationResult: state = _demo_state() return DemoMutationResult( **state.model_dump(), created=result.created if result else 0, updated=result.updated if result else 0, total=result.total if result else state.service_count, restored=restored, ) @app.get("/api/v1/demo", response_model=DemoState) def demo_state() -> DemoState: return _demo_state() @app.post("/api/v1/demo", response_model=DemoMutationResult) @app.post("/api/v1/demo/reset", response_model=DemoMutationResult) def load_demo() -> DemoMutationResult: if not _demo_enabled(): raise HTTPException(status_code=409, detail="Start de demo in een aparte ServiceMap-instantie") services, probes = build_demo_records() result = store.activate_demo(services, probes) sync_incidents(store) return _demo_result(result) @app.post("/api/v1/demo/exit", response_model=DemoMutationResult) async def exit_demo() -> DemoMutationResult: restored = store.exit_demo() # Refresh live provider evidence immediately after restoring the saved # production topology. Test stores intentionally do not inherit the # process-global observer, but production always does. if restored and OBSERVATION_INTERVAL > 0 and observation_manager.store is store: await asyncio.to_thread(observation_manager.refresh) return _demo_result(restored=restored) @app.post("/api/v1/discovery/run", response_model=DiscoveryResult) def run_discovery(payload: DiscoveryRunRequest | None = None) -> DiscoveryResult: if store.demo_active(): raise HTTPException(status_code=409, detail="Sluit de demo af voor je de productie-infrastructuur detecteert") request = payload or DiscoveryRunRequest() _require_configured_discovery_targets(request.docker_host, request.appops_url) try: return discovery_manager.run(request) except ValueError as exc: raise HTTPException(status_code=409, detail=str(exc)) from exc @app.get("/api/v1/discovery/status", response_model=DiscoveryStatus) def discovery_status() -> DiscoveryStatus: return DiscoveryStatus(**discovery_manager.status()) @app.get("/api/v1/discovery/items", response_model=list[DiscoveryObservation]) def discovery_items(run_id: str | None = None) -> list[DiscoveryObservation]: selected = discovery_store.get_run(run_id) if run_id else discovery_store.last_completed() return discovery_store.observations_for_run(selected.id) if selected else [] @app.get("/api/v1/discovery/suggestions", response_model=list[TopologySuggestion]) def discovery_suggestions( status_filter: str | None = Query(default=None, alias="status"), ) -> list[TopologySuggestion]: try: suggestion_status = SuggestionStatus(status_filter) if status_filter else None except ValueError as exc: raise HTTPException(status_code=422, detail="Onbekende status van het voorstel") from exc return discovery_store.list_suggestions(status=suggestion_status) @app.post("/api/v1/discovery/suggestions/{suggestion_id}/accept", response_model=TopologySuggestion) def accept_discovery_suggestion(suggestion_id: str) -> TopologySuggestion: try: return discovery_manager.suggestion_action(suggestion_id, "confirm") except ValueError as exc: raise HTTPException(status_code=404, detail=str(exc)) from exc @app.post("/api/v1/discovery/suggestions/{suggestion_id}/reject", response_model=TopologySuggestion) def reject_discovery_suggestion(suggestion_id: str) -> TopologySuggestion: try: return discovery_manager.suggestion_action(suggestion_id, "reject") except ValueError as exc: raise HTTPException(status_code=404, detail=str(exc)) from exc @app.post("/api/v1/discovery/reconcile", response_model=ReconcileResult) def reconcile_discovery(payload: DiscoveryReconcileRequest | None = None) -> ReconcileResult: if store.demo_active(): raise HTTPException(status_code=409, detail="Sluit de demo af voor je de productie-infrastructuur verwerkt") try: return discovery_manager.reconcile(payload or DiscoveryReconcileRequest()) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc @app.post("/api/v1/discovery/probes/repair", response_model=ProbeRepairReport) def repair_discovered_probes( dry_run: bool = Query(default=True), run_id: str | None = Query(default=None), ) -> ProbeRepairReport: if store.demo_active(): raise HTTPException(status_code=409, detail="Sluit de demo af voor je controle-endpoints in productie herstelt") try: return discovery_manager.repair_probe_reachability(run_id=run_id, dry_run=dry_run) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc @app.get("/api/v1/topology/edges", response_model=list[RelationEvidence]) def topology_edges() -> list[RelationEvidence]: return discovery_store.list_evidence() @app.get("/api/v1/topology/relations", response_model=list[LogicalRelation]) def topology_relations() -> list[LogicalRelation]: """Canonical relationships: one graph edge, all supporting evidence retained.""" return discovery_store.list_logical_relations() @app.get("/api/v1/topology/snapshots", response_model=list[TopologySnapshot]) def topology_snapshots(limit: int = Query(default=30, ge=1, le=200)) -> list[TopologySnapshot]: return discovery_store.list_snapshots(limit=limit) @app.get("/api/v1/topology/diff", response_model=TopologyDiff) def topology_diff( after_snapshot_id: str = Query(...), before_snapshot_id: str | None = Query(default=None), ) -> TopologyDiff: try: return discovery_store.topology_diff( after_snapshot_id=after_snapshot_id, before_snapshot_id=before_snapshot_id, ) except ValueError as exc: raise HTTPException(status_code=404, detail=str(exc)) from exc def _semantic_representation(include_hidden: bool) -> CachedRepresentation: revision = event_bus.revision # The store identity keeps a swapped store (tests, demo restore paths) # from ever reading another database's cached topology. cache_key = (id(store), include_hidden) cached = semantic_cache.get(cache_key, revision) if cached is not None: return cached snapshots, health = _service_snapshots(include_hidden=include_hidden, include_details=False) incident_state = incident_overview(store, health=health) root_ids = {candidate.service_id for candidate in incident_state.candidates} affected_ids = { service_id for candidate in incident_state.candidates for service_id in candidate.affected_service_ids } topology = build_semantic_topology( snapshots, discovery_store.list_logical_relations(), root_service_ids=root_ids, affected_service_ids=affected_ids, ) value = representation(topology) boundary = next_freshness_boundary( [item for snapshot in snapshots for item in snapshot.health_evidence], topology.generated_at, ) semantic_cache.put( cache_key, revision, value, valid_for_seconds=( (boundary - topology.generated_at).total_seconds() if boundary is not None else None ), ) return value @app.get("/api/v1/topology/semantic", response_model=SemanticTopology) def semantic_topology( request: Request, include_hidden: bool = Query(default=False) ) -> Response: """Semantic topology with a content ETag and conditional GET support. Clients that send ``If-None-Match`` receive ``304`` without a body while the topology is unchanged; ``X-ServiceMap-Generated-At`` then states when the server last verified it. Browsers revalidate automatically. """ value = _semantic_representation(include_hidden) headers = { "ETag": value.etag, "Cache-Control": "no-cache", GENERATED_AT_HEADER: value.generated_at, } if etag_matches(request.headers.get("if-none-match"), value.etag): return Response(status_code=status.HTTP_304_NOT_MODIFIED, headers=headers) return Response(content=value.body, media_type="application/json", headers=headers) @app.post("/api/v1/topology/edges", response_model=RelationEvidence, status_code=status.HTTP_201_CREATED) def create_topology_edge(payload: EdgeMutationRequest) -> RelationEvidence: try: return discovery_manager.apply_edge(payload) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc @app.post("/api/v1/topology/edges/{evidence_id}/action", response_model=RelationEvidence) def topology_edge_action(evidence_id: str, payload: EdgeActionRequest) -> RelationEvidence: try: return discovery_manager.edge_action(evidence_id, payload.action) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc @app.post("/api/v1/docker/discover", response_model=DockerDiscoveryResult) def docker_discover(payload: DockerDiscoveryRequest) -> DockerDiscoveryResult: _require_configured_discovery_targets(payload.docker_host) try: return discover_containers( payload.docker_host, store.list_services(), appops_url=os.getenv("SERVICEMAP_APPOPS_URL"), ) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from exc @app.post("/api/v1/maintenance/prune") def prune_history(retention_days: int = Query(default=RETENTION_DAYS, ge=1, le=3650)) -> dict[str, int]: return {"deleted_probe_results": store.prune_probes(retention_days, EVENT_RETENTION_DAYS)} static_dir = Path(os.getenv("SERVICEMAP_STATIC_DIR", "/app/static")) if static_dir.is_dir(): app.mount("/", StaticFiles(directory=static_dir, html=True), name="frontend")