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
This commit is contained in:
1 parent
d7319250e8
commit
adc2c70c21
91 files changed
+9205
-1691
No files matched your search
@@ -0,0 +1,375 @@
|
||||
"""v1.6.3 regression guards for health-evidence reads and graph resolution.
|
||||
|
||||
Production profiling showed 78% of server CPU in a full ``health_evidence``
|
||||
scan, issued once per node of a recursive, un-memoised health walk. These
|
||||
tests pin the indexed query shape, equivalence of the bulk and single reads,
|
||||
the batched graph result and the bounded query count.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import random
|
||||
import sqlite3
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
from statistics import quantiles
|
||||
from time import perf_counter
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app import main as main_module
|
||||
from app.discovery_store import DiscoveryStore
|
||||
|
||||
from app.health import SEVERITY, status_maps, topology_health
|
||||
from app.incidents import incident_overview, sync_incidents
|
||||
from app.models import (
|
||||
ConfigService,
|
||||
EvidenceProvider,
|
||||
EvidenceScope,
|
||||
HealthEvidence,
|
||||
HealthStatus,
|
||||
ServiceCreate,
|
||||
ServiceKind,
|
||||
)
|
||||
from app.store import Store
|
||||
|
||||
|
||||
def _service(store: Store, name: str, depends_on: list[str] | None = None):
|
||||
return store.create_service(
|
||||
ServiceCreate(
|
||||
name=name,
|
||||
kind=ServiceKind.HTTP,
|
||||
url=f"https://{name.lower()}.test",
|
||||
depends_on=depends_on or [],
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _component(
|
||||
evidence_id: str,
|
||||
service_id: str,
|
||||
status: HealthStatus,
|
||||
*,
|
||||
at: datetime | None = None,
|
||||
) -> HealthEvidence:
|
||||
now = at or datetime.now(UTC)
|
||||
return HealthEvidence(
|
||||
id=evidence_id,
|
||||
subject_type="service",
|
||||
subject_id=service_id,
|
||||
service_id=service_id,
|
||||
provider=EvidenceProvider.DOCKER,
|
||||
provider_ref=service_id,
|
||||
status=status,
|
||||
scope=EvidenceScope.COMPONENT,
|
||||
confidence=0.95,
|
||||
observed_at=now,
|
||||
valid_until=datetime.now(UTC) + timedelta(minutes=5),
|
||||
received_at=now,
|
||||
summary="Docker healthcheck",
|
||||
)
|
||||
|
||||
|
||||
def _application(evidence_id: str, status: HealthStatus) -> HealthEvidence:
|
||||
now = datetime.now(UTC)
|
||||
return HealthEvidence(
|
||||
id=evidence_id,
|
||||
subject_type="application",
|
||||
subject_id="appops:northstar",
|
||||
provider=EvidenceProvider.APPOPS,
|
||||
provider_ref="northstar",
|
||||
status=status,
|
||||
scope=EvidenceScope.APPLICATION,
|
||||
confidence=0.9,
|
||||
observed_at=now,
|
||||
valid_until=now + timedelta(minutes=5),
|
||||
received_at=now,
|
||||
summary="AppOps aggregate",
|
||||
)
|
||||
|
||||
|
||||
def _plan(store: Store, sql: str, params: list[object]) -> list[str]:
|
||||
with sqlite3.connect(store.path) as db:
|
||||
return [row[3] for row in db.execute(f"EXPLAIN QUERY PLAN {sql}", params)]
|
||||
|
||||
|
||||
def test_evidence_reads_use_indexes_instead_of_full_scans(tmp_path: Path, monkeypatch) -> None:
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
service = _service(store, "API")
|
||||
store.record_health_evidence(_component("ev-1", service.id, HealthStatus.HEALTHY))
|
||||
|
||||
captured: list[str] = []
|
||||
original = sqlite3.Connection.execute
|
||||
|
||||
class Recording(sqlite3.Connection):
|
||||
def execute(self, sql, parameters=(), /): # type: ignore[override]
|
||||
if "health_evidence" in sql and sql.lstrip().upper().startswith(("SELECT", "WITH")):
|
||||
captured.append(sql)
|
||||
return original(self, sql, parameters)
|
||||
|
||||
real_connect = sqlite3.connect
|
||||
monkeypatch.setattr(
|
||||
"app.store.sqlite3.connect",
|
||||
lambda *args, **kwargs: real_connect(*args, factory=Recording, **kwargs),
|
||||
)
|
||||
store.health_evidence_for_service(service.id)
|
||||
store.health_evidence_for_services([service.id])
|
||||
monkeypatch.setattr("app.store.sqlite3.connect", real_connect)
|
||||
|
||||
single, bulk = captured[-2], captured[-1]
|
||||
for sql, params in (
|
||||
(single, [service.id, service.id, 100]),
|
||||
(bulk, [service.id, service.id, 100]),
|
||||
):
|
||||
details = _plan(store, sql, params)
|
||||
assert not any(detail in {"SCAN e", "SCAN health_evidence"} for detail in details), details
|
||||
|
||||
|
||||
def test_bulk_and_single_evidence_reads_return_the_same_rows(tmp_path: Path) -> None:
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
api = _service(store, "API")
|
||||
worker = _service(store, "Worker")
|
||||
base = datetime.now(UTC) - timedelta(hours=2)
|
||||
# Status flips create superseded history for the direct claim.
|
||||
for index, status in enumerate([HealthStatus.HEALTHY, HealthStatus.DOWN, HealthStatus.HEALTHY]):
|
||||
store.record_health_evidence(
|
||||
_component(f"api-{index}", api.id, status, at=base + timedelta(minutes=index))
|
||||
)
|
||||
store.record_health_evidence(_component("worker-0", worker.id, HealthStatus.DOWN, at=base))
|
||||
# One application claim linked to both components.
|
||||
store.record_health_evidence(_application("app-0", HealthStatus.HEALTHY), service_ids=[api.id, worker.id])
|
||||
|
||||
bulk = store.health_evidence_for_services([api.id, worker.id])
|
||||
for service in (api, worker):
|
||||
single = store.health_evidence_for_service(service.id)
|
||||
assert [item.id for item in bulk[service.id]] == [item.id for item in single]
|
||||
assert {item.id for item in bulk[api.id]} == {"api-0", "api-1", "api-2", "app-0"}
|
||||
assert {item.id for item in bulk[worker.id]} == {"worker-0", "app-0"}
|
||||
|
||||
limited = store.health_evidence_for_services([api.id], limit_per_service=2)
|
||||
assert [item.id for item in limited[api.id]] == [
|
||||
item.id for item in store.health_evidence_for_service(api.id, limit=2)
|
||||
]
|
||||
|
||||
|
||||
def test_prune_removes_only_old_superseded_history(tmp_path: Path) -> None:
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
api = _service(store, "API")
|
||||
old = datetime.now(UTC) - timedelta(days=30)
|
||||
store.record_health_evidence(_component("old-down", api.id, HealthStatus.DOWN, at=old))
|
||||
store.record_health_evidence(
|
||||
_component("old-up", api.id, HealthStatus.HEALTHY, at=old + timedelta(minutes=1))
|
||||
)
|
||||
store.record_health_evidence(_component("recent-down", api.id, HealthStatus.DOWN))
|
||||
store.record_health_evidence(_application("app-0", HealthStatus.HEALTHY), service_ids=[api.id])
|
||||
before = store.service_health(api.id)
|
||||
|
||||
assert store.prune_health_evidence(7) == 2
|
||||
|
||||
remaining = {item.id for item in store.health_evidence_for_service(api.id)}
|
||||
assert remaining == {"recent-down", "app-0"}
|
||||
assert store.service_health(api.id) == before == HealthStatus.DOWN
|
||||
with sqlite3.connect(store.path) as db:
|
||||
orphans = db.execute(
|
||||
"SELECT COUNT(*) FROM health_evidence_service_links "
|
||||
"WHERE evidence_id NOT IN (SELECT id FROM health_evidence)"
|
||||
).fetchone()[0]
|
||||
assert orphans == 0
|
||||
assert store.prune_health_evidence(0) == 0
|
||||
|
||||
|
||||
def test_batched_discovery_context_matches_per_service_reads(tmp_path: Path) -> None:
|
||||
from app.discovery_store import DiscoveryStore
|
||||
from app.models import DiscoveryConfidence, DiscoveryLifecycle, RelationEvidence
|
||||
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
discovery = DiscoveryStore(store)
|
||||
api = _service(store, "API")
|
||||
database = _service(store, "Database")
|
||||
lonely = _service(store, "Lonely")
|
||||
for index, service in enumerate((api, database)):
|
||||
source = discovery.save_source(
|
||||
source_type="docker",
|
||||
source_id=f"container-{index}",
|
||||
source_name=service.name,
|
||||
lifecycle=DiscoveryLifecycle.ACTIVE,
|
||||
metadata={},
|
||||
)
|
||||
discovery.attach_source(service.id, source.id)
|
||||
now = datetime.now(UTC)
|
||||
discovery.save_evidence(
|
||||
RelationEvidence(
|
||||
id="rel-1",
|
||||
source_service_id=api.id,
|
||||
target_service_id=database.id,
|
||||
evidence_type="compose",
|
||||
confidence=DiscoveryConfidence.HIGH,
|
||||
explanation="depends_on",
|
||||
first_seen_at=now,
|
||||
last_seen_at=now,
|
||||
)
|
||||
)
|
||||
|
||||
ids = [api.id, database.id, lonely.id]
|
||||
sources = discovery.sources_for_services(ids)
|
||||
relations = discovery.evidence_for_services(ids)
|
||||
for service_id in ids:
|
||||
assert sources[service_id] == discovery.sources_for_service(service_id)
|
||||
assert relations[service_id] == discovery.evidence_for_service(service_id)
|
||||
assert [item.id for item in relations[database.id]] == ["rel-1"]
|
||||
assert relations[lonely.id] == [] and sources[lonely.id] == []
|
||||
|
||||
|
||||
def _reference_effective(
|
||||
graph: dict[str, list[str]], direct: dict[str, HealthStatus]
|
||||
) -> dict[str, HealthStatus]:
|
||||
"""The pre-v1.6.3 recursive per-path algorithm, kept as an oracle."""
|
||||
|
||||
def dependency(node: str, visiting: set[str]) -> HealthStatus:
|
||||
if not graph[node]:
|
||||
return HealthStatus.HEALTHY
|
||||
states = [
|
||||
effective(item, visiting) if item in graph else HealthStatus.UNKNOWN
|
||||
for item in graph[node]
|
||||
]
|
||||
return max(states, key=lambda status: SEVERITY[status])
|
||||
|
||||
def effective(node: str, visiting: set[str]) -> HealthStatus:
|
||||
if node in visiting:
|
||||
return HealthStatus.UNKNOWN
|
||||
own = direct[node]
|
||||
if own == HealthStatus.MAINTENANCE:
|
||||
return own
|
||||
dep = dependency(node, {*visiting, node})
|
||||
if own == HealthStatus.DOWN:
|
||||
return own
|
||||
if dep == HealthStatus.DOWN:
|
||||
return HealthStatus.DEGRADED
|
||||
if dep in {HealthStatus.DEGRADED, HealthStatus.RECOVERING} and own in {
|
||||
HealthStatus.HEALTHY,
|
||||
HealthStatus.RECOVERING,
|
||||
}:
|
||||
return HealthStatus.DEGRADED
|
||||
return own
|
||||
|
||||
return {node: effective(node, set()) for node in graph}
|
||||
|
||||
|
||||
def test_batched_graph_matches_recursive_reference_on_random_dags() -> None:
|
||||
rng = random.Random(163)
|
||||
statuses = list(HealthStatus)
|
||||
for _ in range(200):
|
||||
size = rng.randint(1, 14)
|
||||
nodes = [f"n{index}" for index in range(size)]
|
||||
# Acyclic by construction: a node only depends on lower indexes.
|
||||
graph = {
|
||||
node: rng.sample(nodes[:index], k=rng.randint(0, min(3, index)))
|
||||
for index, node in enumerate(nodes)
|
||||
}
|
||||
if rng.random() < 0.2:
|
||||
graph[nodes[-1]].append("missing")
|
||||
direct = {node: rng.choice(statuses) for node in nodes}
|
||||
services = [_fake_service(node, graph[node]) for node in nodes]
|
||||
effective, _ = status_maps(services, direct_statuses=direct)
|
||||
assert effective == _reference_effective(graph, direct), (graph, direct)
|
||||
|
||||
|
||||
def _fake_service(identifier: str, depends_on: list[str]):
|
||||
from app.models import Service
|
||||
|
||||
now = datetime.now(UTC)
|
||||
return Service(
|
||||
id=identifier,
|
||||
name=identifier,
|
||||
kind=ServiceKind.HTTP,
|
||||
url="https://example.test",
|
||||
depends_on=depends_on,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
|
||||
def test_incident_resolution_uses_a_bounded_number_of_queries(tmp_path: Path, monkeypatch) -> None:
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
# A deep chain plus a fan-in reproduces the shape that made the old walk
|
||||
# re-read every shared dependency once per path.
|
||||
previous: list[str] = []
|
||||
for index in range(40):
|
||||
service = _service(store, f"S{index:02d}", depends_on=previous[-2:])
|
||||
status = HealthStatus.DOWN if index == 0 else HealthStatus.HEALTHY
|
||||
store.record_health_evidence(_component(f"ev-{index}", service.id, status))
|
||||
previous.append(service.id)
|
||||
|
||||
connections = 0
|
||||
original = store._connect
|
||||
|
||||
def counting_connect():
|
||||
nonlocal connections
|
||||
connections += 1
|
||||
return original()
|
||||
|
||||
monkeypatch.setattr(store, "_connect", counting_connect)
|
||||
overview = incident_overview(store)
|
||||
assert connections <= 4, connections
|
||||
assert [candidate.service_id for candidate in overview.candidates] == [previous[0]]
|
||||
assert len(overview.candidates[0].affected_service_ids) == 39
|
||||
|
||||
health = topology_health(store)
|
||||
assert health.effective[previous[0]] == HealthStatus.DOWN
|
||||
assert health.effective[previous[-1]] == HealthStatus.DEGRADED
|
||||
|
||||
connections = 0
|
||||
sync_incidents(store)
|
||||
# One graph resolution plus incident journal writes; never one per node.
|
||||
assert connections < 20, connections
|
||||
|
||||
|
||||
def test_topology_request_budget_at_300_services(tmp_path: Path, monkeypatch) -> None:
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
definitions = [
|
||||
ConfigService(
|
||||
name=f"Service {index}",
|
||||
kind=ServiceKind.HTTP,
|
||||
url=f"https://service-{index}.test",
|
||||
domain="Benchmark",
|
||||
application=f"Application {index // 10}",
|
||||
depends_on=[f"Service {index - 1}"] if index % 10 else [],
|
||||
)
|
||||
for index in range(300)
|
||||
]
|
||||
assert store.import_config_services(definitions, "merge").created == 300
|
||||
for service in store.list_services():
|
||||
store.record_health_evidence(_component(f"ev-{service.id}", service.id, HealthStatus.HEALTHY))
|
||||
monkeypatch.setattr(main_module, "store", store)
|
||||
monkeypatch.setattr(main_module, "discovery_store", DiscoveryStore(store))
|
||||
|
||||
connections = 0
|
||||
original = store._connect
|
||||
|
||||
def counting_connect():
|
||||
nonlocal connections
|
||||
connections += 1
|
||||
return original()
|
||||
|
||||
monkeypatch.setattr(store, "_connect", counting_connect)
|
||||
client = TestClient(main_module.app)
|
||||
budget_ms = os.environ.get("SERVICEMAP_API_P95_BUDGET_MS")
|
||||
|
||||
for endpoint in ("/api/v1/topology/semantic", "/api/v1/overview"):
|
||||
connections = 0
|
||||
assert client.get(endpoint).status_code == 200
|
||||
assert connections <= 30, (endpoint, connections)
|
||||
elapsed_ms = []
|
||||
for _ in range(20):
|
||||
if endpoint.endswith("/semantic"):
|
||||
# A provider or topology change invalidates this representation in production.
|
||||
main_module.semantic_cache.clear()
|
||||
started = perf_counter()
|
||||
response = client.get(endpoint)
|
||||
elapsed_ms.append((perf_counter() - started) * 1000)
|
||||
assert response.status_code == 200
|
||||
p95_ms = quantiles(elapsed_ms, n=20, method="inclusive")[-1]
|
||||
print(f"{endpoint} recompute p95: {p95_ms:.1f} ms")
|
||||
if budget_ms is not None:
|
||||
assert p95_ms <= float(budget_ms), (endpoint, p95_ms, budget_ms)
|
||||
Reference in new issue
Block a user