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
+273
-52
@@ -52,6 +52,16 @@ def _iso(value: datetime | None) -> str | None:
|
||||
return value.isoformat() if value is not None else None
|
||||
|
||||
|
||||
# Per-observation bookkeeping that changes on every probe. It is stored, but a
|
||||
# change in only these keys is not a material evidence change and must not
|
||||
# publish an SSE wakeup (that turned every probe into a full UI refresh).
|
||||
VOLATILE_EVIDENCE_METADATA = frozenset({"probe_id", "latency_ms", "trigger"})
|
||||
|
||||
|
||||
def _stable_metadata(metadata: dict[str, object]) -> dict[str, object]:
|
||||
return {key: value for key, value in metadata.items() if key not in VOLATILE_EVIDENCE_METADATA}
|
||||
|
||||
|
||||
def _bool(value: object, default: bool = False) -> bool:
|
||||
if value is None:
|
||||
return default
|
||||
@@ -70,14 +80,24 @@ class Store:
|
||||
connection.row_factory = sqlite3.Row
|
||||
connection.execute("PRAGMA foreign_keys = ON")
|
||||
connection.execute("PRAGMA busy_timeout = 5000")
|
||||
# Per connection, not persistent: without this every commit ran at
|
||||
# the FULL default (an fsync per write). NORMAL is durable under WAL.
|
||||
connection.execute("PRAGMA synchronous = NORMAL")
|
||||
return connection
|
||||
|
||||
def _init_schema(self) -> None:
|
||||
with self._connect() as db:
|
||||
db.execute("PRAGMA journal_mode = WAL")
|
||||
db.execute("PRAGMA synchronous = NORMAL")
|
||||
# Bound the WAL file after checkpoints (64 MiB).
|
||||
db.execute("PRAGMA journal_size_limit = 67108864")
|
||||
db.executescript(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS maintenance_windows (
|
||||
service_id TEXT PRIMARY KEY,
|
||||
ends_at TEXT NOT NULL,
|
||||
FOREIGN KEY(service_id) REFERENCES services(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS services (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
@@ -770,7 +790,7 @@ class Store:
|
||||
with self._connect() as db:
|
||||
self._upsert_service(db, updated)
|
||||
self._insert_event_db(db, "topology.changed", service_id, None, None, None, f"Service {updated.name} updated", {})
|
||||
event_bus.publish({"event_type": "topology.changed", "service_id": service_id, "message": "Service updated"})
|
||||
event_bus.publish({"event_type": "topology.changed", "service_id": service_id, "message": "Service bijgewerkt"})
|
||||
return updated
|
||||
|
||||
def delete_service(self, service_id: str) -> bool:
|
||||
@@ -787,14 +807,24 @@ class Store:
|
||||
"UPDATE services SET depends_on = ?, updated_at = ? WHERE id = ?",
|
||||
(json.dumps([value for value in dependencies if value != service_id]), _now().isoformat(), item["id"]),
|
||||
)
|
||||
db.execute("UPDATE incidents SET affected_service_ids = replace(affected_service_ids, ?, '') WHERE status != 'resolved'", (service_id,))
|
||||
# Remove the id as a JSON element. A textual replace() left an
|
||||
# empty-string entry behind (["a","b"] -> ["","b"]).
|
||||
for incident in db.execute(
|
||||
"SELECT id, affected_service_ids FROM incidents WHERE status != 'resolved'"
|
||||
).fetchall():
|
||||
affected = self._json_list(incident["affected_service_ids"])
|
||||
if service_id in affected:
|
||||
db.execute(
|
||||
"UPDATE incidents SET affected_service_ids = ? WHERE id = ?",
|
||||
(json.dumps([value for value in affected if value != service_id]), incident["id"]),
|
||||
)
|
||||
self._insert_event_db(db, "topology.changed", service_id, None, None, None, f"Service {row['name']} deleted", {})
|
||||
event_bus.publish({"event_type": "topology.changed", "service_id": service_id, "message": "Service deleted"})
|
||||
event_bus.publish({"event_type": "topology.changed", "service_id": service_id, "message": "Service verwijderd"})
|
||||
return True
|
||||
|
||||
def import_config_services(self, definitions: list[ConfigService], mode: str) -> ConfigImportResult:
|
||||
if mode not in {"merge", "replace"}:
|
||||
raise ValueError("Import mode must be merge or replace")
|
||||
raise ValueError("Importmodus moet merge of replace zijn")
|
||||
|
||||
with self._connect() as db:
|
||||
db.execute("BEGIN IMMEDIATE")
|
||||
@@ -813,7 +843,7 @@ class Store:
|
||||
missing = [item for item in definition.depends_on if item.casefold() not in ids_by_name]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
f"Service {definition.name} references unknown dependencies: {', '.join(missing)}"
|
||||
f"Service {definition.name} verwijst naar onbekende afhankelijkheden: {', '.join(missing)}"
|
||||
)
|
||||
|
||||
created = 0
|
||||
@@ -822,8 +852,16 @@ class Store:
|
||||
for definition in definitions:
|
||||
key = definition.name.casefold()
|
||||
current = existing.get(key)
|
||||
payload = definition.model_dump()
|
||||
payload["depends_on"] = [ids_by_name[item.casefold()] for item in definition.depends_on]
|
||||
if current is None:
|
||||
payload = definition.model_dump()
|
||||
payload["depends_on"] = [ids_by_name[item.casefold()] for item in definition.depends_on]
|
||||
else:
|
||||
# Merge: only fields the document states change. Rebuilding
|
||||
# from model_dump() reset every omitted field to its default.
|
||||
payload = current.model_dump(include=set(ServiceCreate.model_fields))
|
||||
payload.update(definition.model_dump(exclude_unset=True, exclude={"depends_on"}))
|
||||
if "depends_on" in definition.model_fields_set:
|
||||
payload["depends_on"] = [ids_by_name[item.casefold()] for item in definition.depends_on]
|
||||
if current is None:
|
||||
service = Service(id=ids_by_name[key], created_at=now, updated_at=now, **payload)
|
||||
created += 1
|
||||
@@ -845,8 +883,8 @@ class Store:
|
||||
self._insert_service(db, service)
|
||||
else:
|
||||
self._upsert_service(db, service)
|
||||
self._insert_event_db(db, "topology.changed", None, None, None, None, "Configuration imported", {"mode": mode})
|
||||
event_bus.publish({"event_type": "topology.changed", "message": "Configuration imported"})
|
||||
self._insert_event_db(db, "topology.changed", None, None, None, None, "Configuratie geïmporteerd", {"mode": mode})
|
||||
event_bus.publish({"event_type": "topology.changed", "message": "Configuratie geïmporteerd"})
|
||||
return ConfigImportResult(created=created, updated=updated, total=len(definitions))
|
||||
|
||||
def replace_all_services(self, definitions: list[ServiceCreate]) -> list[Service]:
|
||||
@@ -916,12 +954,29 @@ class Store:
|
||||
("status", evidence.status.value),
|
||||
("confidence", evidence.confidence),
|
||||
("summary", evidence.summary),
|
||||
("metadata", json.dumps(evidence.metadata, sort_keys=True)),
|
||||
)
|
||||
)
|
||||
)
|
||||
) or _stable_metadata(self._json_object(prior["metadata"])) != _stable_metadata(evidence.metadata)
|
||||
if prior is not None and prior["status"] == evidence.status.value:
|
||||
saved = evidence.model_copy(update={"id": prior["id"], "active": True, "superseded_by": None})
|
||||
ttl = evidence.valid_until - evidence.observed_at
|
||||
prior_valid_until = datetime.fromisoformat(prior["valid_until"])
|
||||
if (
|
||||
not materially_changed
|
||||
and prior["service_id"] == evidence.service_id
|
||||
and prior_valid_until - _now() > ttl / 2
|
||||
):
|
||||
# Unchanged and comfortably fresh: skip the timestamp-only
|
||||
# rewrite. It is renewed once less than half the TTL is
|
||||
# left, so the claim never goes stale, with about half
|
||||
# the writes of renewing on every observation cycle.
|
||||
saved = self._row_to_health_evidence(prior)
|
||||
for service_id in links:
|
||||
db.execute(
|
||||
"INSERT OR IGNORE INTO health_evidence_service_links (evidence_id, service_id) VALUES (?, ?)",
|
||||
(saved.id, service_id),
|
||||
)
|
||||
return saved
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE health_evidence SET service_id = ?, confidence = ?, observed_at = ?, valid_until = ?,
|
||||
@@ -980,18 +1035,24 @@ class Store:
|
||||
"service_id": evidence.service_id,
|
||||
"provider": evidence.provider.value,
|
||||
"status": evidence.status.value,
|
||||
"message": "Health evidence changed",
|
||||
"message": "Gezondheidsbewijs gewijzigd",
|
||||
}
|
||||
)
|
||||
return saved
|
||||
|
||||
def health_evidence_for_service(self, service_id: str, *, limit: int = 100) -> list[HealthEvidence]:
|
||||
# A direct claim and a linked claim are matched by two independently
|
||||
# indexed lookups. An ``OR`` across the LEFT JOIN cannot use either
|
||||
# index and forced a full ``health_evidence`` scan on every call.
|
||||
with self._connect() as db:
|
||||
rows = db.execute(
|
||||
"""
|
||||
SELECT DISTINCT e.* FROM health_evidence e
|
||||
LEFT JOIN health_evidence_service_links link ON link.evidence_id = e.id
|
||||
WHERE e.service_id = ? OR link.service_id = ?
|
||||
SELECT e.* FROM health_evidence e
|
||||
WHERE e.id IN (
|
||||
SELECT id FROM health_evidence WHERE service_id = ?
|
||||
UNION
|
||||
SELECT evidence_id FROM health_evidence_service_links WHERE service_id = ?
|
||||
)
|
||||
ORDER BY e.observed_at DESC, e.rowid DESC LIMIT ?
|
||||
""",
|
||||
(service_id, service_id, limit),
|
||||
@@ -1015,27 +1076,41 @@ class Store:
|
||||
return result
|
||||
placeholders = ", ".join("?" for _ in ids)
|
||||
with self._connect() as db:
|
||||
# Both match paths use their own index; the per-service limit is
|
||||
# applied in SQL so superseded history is not materialised.
|
||||
rows = db.execute(
|
||||
f"""
|
||||
SELECT e.*, e.service_id AS direct_service_id, link.service_id AS linked_service_id
|
||||
FROM health_evidence e
|
||||
LEFT JOIN health_evidence_service_links link ON link.evidence_id = e.id
|
||||
WHERE e.service_id IN ({placeholders}) OR link.service_id IN ({placeholders})
|
||||
ORDER BY e.observed_at DESC, e.rowid DESC
|
||||
WITH matched(evidence_id, matched_service_id) AS (
|
||||
SELECT id, service_id FROM health_evidence WHERE service_id IN ({placeholders})
|
||||
UNION
|
||||
SELECT evidence_id, service_id FROM health_evidence_service_links
|
||||
WHERE service_id IN ({placeholders})
|
||||
),
|
||||
ranked AS (
|
||||
SELECT e.*, m.matched_service_id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY m.matched_service_id
|
||||
ORDER BY e.observed_at DESC, e.rowid DESC
|
||||
) AS rank_in_service
|
||||
FROM matched m
|
||||
JOIN health_evidence e ON e.id = m.evidence_id
|
||||
)
|
||||
SELECT * FROM ranked
|
||||
WHERE rank_in_service <= ?
|
||||
ORDER BY matched_service_id, rank_in_service
|
||||
""",
|
||||
[*ids, *ids],
|
||||
[*ids, *ids, limit_per_service],
|
||||
).fetchall()
|
||||
seen: dict[str, set[str]] = {service_id: set() for service_id in ids}
|
||||
parsed: dict[str, HealthEvidence] = {}
|
||||
for row in rows:
|
||||
evidence = self._row_to_health_evidence(row)
|
||||
for service_id in {row["direct_service_id"], row["linked_service_id"]}:
|
||||
if (
|
||||
service_id in result
|
||||
and evidence.id not in seen[service_id]
|
||||
and len(result[service_id]) < limit_per_service
|
||||
):
|
||||
result[service_id].append(evidence)
|
||||
seen[service_id].add(evidence.id)
|
||||
service_id = row["matched_service_id"]
|
||||
if service_id not in result:
|
||||
continue
|
||||
evidence = parsed.get(row["id"])
|
||||
if evidence is None:
|
||||
evidence = self._row_to_health_evidence(row)
|
||||
parsed[row["id"]] = evidence
|
||||
result[service_id].append(evidence)
|
||||
return result
|
||||
|
||||
def application_evidence_links(self, provider: EvidenceProvider) -> dict[str, set[str]]:
|
||||
@@ -1098,7 +1173,7 @@ class Store:
|
||||
"event_type": "provider.status_changed",
|
||||
"provider": value.provider.value,
|
||||
"status": value.status.value,
|
||||
"message": "Provider availability changed",
|
||||
"message": "Beschikbaarheid van de bron gewijzigd",
|
||||
}
|
||||
)
|
||||
return value
|
||||
@@ -1205,10 +1280,37 @@ class Store:
|
||||
status_by_id[service_id] = self._legacy_health_from_row(row)
|
||||
return status_by_id, resolutions
|
||||
|
||||
def set_maintenance(self, service_id: str, enabled: bool, reason: str | None = None) -> Service | None:
|
||||
def maintenance_windows(self) -> dict[str, datetime]:
|
||||
with self._connect() as db:
|
||||
rows = db.execute("SELECT service_id, ends_at FROM maintenance_windows").fetchall()
|
||||
return {row["service_id"]: datetime.fromisoformat(row["ends_at"]) for row in rows}
|
||||
|
||||
def expire_maintenance_windows(self, now: datetime | None = None) -> list[str]:
|
||||
"""End maintenance for every window whose end time has passed."""
|
||||
|
||||
now = now or _now()
|
||||
with self._connect() as db:
|
||||
rows = db.execute(
|
||||
"SELECT service_id FROM maintenance_windows WHERE ends_at <= ?", (now.isoformat(),)
|
||||
).fetchall()
|
||||
ended: list[str] = []
|
||||
for row in rows:
|
||||
if self.set_maintenance(row["service_id"], False, "Maintenance window ended") is not None:
|
||||
ended.append(row["service_id"])
|
||||
return ended
|
||||
|
||||
def set_maintenance(
|
||||
self,
|
||||
service_id: str,
|
||||
enabled: bool,
|
||||
reason: str | None = None,
|
||||
until: datetime | None = None,
|
||||
) -> Service | None:
|
||||
service = self.get_service(service_id)
|
||||
if service is None:
|
||||
return None
|
||||
if enabled and until is not None and until <= _now():
|
||||
raise ValueError("Het einde van het onderhoud moet in de toekomst liggen")
|
||||
updated = service.model_copy(
|
||||
update={
|
||||
"maintenance_enabled": enabled,
|
||||
@@ -1221,6 +1323,14 @@ class Store:
|
||||
)
|
||||
with self._connect() as db:
|
||||
self._upsert_service(db, updated)
|
||||
if enabled and until is not None:
|
||||
db.execute(
|
||||
"INSERT INTO maintenance_windows (service_id, ends_at) VALUES (?, ?) "
|
||||
"ON CONFLICT(service_id) DO UPDATE SET ends_at = excluded.ends_at",
|
||||
(service_id, until.isoformat()),
|
||||
)
|
||||
else:
|
||||
db.execute("DELETE FROM maintenance_windows WHERE service_id = ?", (service_id,))
|
||||
self._insert_event_db(
|
||||
db,
|
||||
"service.maintenance_changed",
|
||||
@@ -1228,15 +1338,18 @@ class Store:
|
||||
None,
|
||||
service.state.value,
|
||||
HealthStatus.MAINTENANCE.value if enabled else HealthStatus.UNKNOWN.value,
|
||||
f"Maintenance {'enabled' if enabled else 'disabled'} for {service.name}",
|
||||
{"reason": reason} if reason else {},
|
||||
f"Onderhoud {'gestart' if enabled else 'beëindigd'} voor {service.name}",
|
||||
{
|
||||
**({"reason": reason} if reason else {}),
|
||||
**({"until": until.isoformat()} if enabled and until else {}),
|
||||
},
|
||||
)
|
||||
event_bus.publish(
|
||||
{
|
||||
"event_type": "service.maintenance_changed",
|
||||
"service_id": service_id,
|
||||
"status": HealthStatus.MAINTENANCE.value if enabled else HealthStatus.UNKNOWN.value,
|
||||
"message": f"Maintenance {'enabled' if enabled else 'disabled'}",
|
||||
"message": f"Onderhoud {'gestart' if enabled else 'beëindigd'}",
|
||||
}
|
||||
)
|
||||
from .incidents import sync_incidents
|
||||
@@ -1330,11 +1443,11 @@ class Store:
|
||||
None,
|
||||
service.state.value,
|
||||
status.value,
|
||||
f"Docker runtime observation updated for {service.name}",
|
||||
f"Docker-runtimewaarneming bijgewerkt voor {service.name}",
|
||||
{"runtime_state": runtime_state, "runtime_health": runtime_health, "monitoring_state": state_kind.value},
|
||||
)
|
||||
if service.state != status:
|
||||
event_bus.publish({"event_type": "service.runtime_observed", "service_id": service_id, "status": status.value, "message": "Docker runtime observation updated"})
|
||||
event_bus.publish({"event_type": "service.runtime_observed", "service_id": service_id, "status": status.value, "message": "Docker-runtimewaarneming bijgewerkt"})
|
||||
observed_at = _now()
|
||||
self.record_health_evidence(
|
||||
HealthEvidence(
|
||||
@@ -1350,7 +1463,7 @@ class Store:
|
||||
observed_at=observed_at,
|
||||
valid_until=observed_at + timedelta(seconds=max(60, int(service.interval_seconds or 60) * 2)),
|
||||
received_at=observed_at,
|
||||
summary="Docker runtime observation" if provider == EvidenceProvider.DOCKER else "Runtime observation",
|
||||
summary="Docker-runtimewaarneming" if provider == EvidenceProvider.DOCKER else "Runtimewaarneming",
|
||||
metadata={"runtime_state": runtime_state, "runtime_health": runtime_health},
|
||||
)
|
||||
)
|
||||
@@ -1389,7 +1502,7 @@ class Store:
|
||||
"service_id": probe.service_id,
|
||||
"from_status": previous.value,
|
||||
"to_status": next_state.value,
|
||||
"message": f"Service changed from {previous.value} to {next_state.value}",
|
||||
"message": f"Service gewijzigd van {previous.value} to {next_state.value}",
|
||||
"metadata": {"trigger": probe.trigger, "probe_id": probe.id},
|
||||
}
|
||||
self._insert_event_db(
|
||||
@@ -1420,11 +1533,11 @@ class Store:
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
f"Flapping detected: {flap_count} state changes in 30 minutes",
|
||||
f"Wisselvalligheid vastgesteld: {flap_count} statuswijzigingen in 30 minuten",
|
||||
{"state_changes": flap_count, "window_minutes": 30},
|
||||
created_at=now,
|
||||
)
|
||||
events.append({"event_type": "flapping.detected", "service_id": probe.service_id, "message": "Flapping detected"})
|
||||
events.append({"event_type": "flapping.detected", "service_id": probe.service_id, "message": "Wisselvalligheid vastgesteld"})
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE services
|
||||
@@ -1449,7 +1562,7 @@ class Store:
|
||||
"service_id": probe.service_id,
|
||||
"status": probe.status.value,
|
||||
"trigger": probe.trigger,
|
||||
"message": "Probe completed",
|
||||
"message": "Controle voltooid",
|
||||
}
|
||||
)
|
||||
for event in events:
|
||||
@@ -1618,6 +1731,52 @@ class Store:
|
||||
db.execute("DELETE FROM events WHERE created_at < ? AND event_type NOT LIKE 'incident.%'", (event_cutoff.isoformat(),))
|
||||
return cursor.rowcount
|
||||
|
||||
def checkpoint(self) -> None:
|
||||
"""Fold the WAL back into the database and refresh planner statistics.
|
||||
|
||||
Without a TRUNCATE checkpoint the WAL file keeps its high-water size.
|
||||
"""
|
||||
|
||||
with self._connect() as db:
|
||||
db.execute("PRAGMA wal_checkpoint(TRUNCATE)")
|
||||
db.execute("PRAGMA optimize")
|
||||
|
||||
def ready_check(self) -> None:
|
||||
"""Raise if the database cannot take a write lock right now."""
|
||||
|
||||
connection = self._connect()
|
||||
try:
|
||||
connection.execute("BEGIN IMMEDIATE")
|
||||
connection.execute("ROLLBACK")
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def prune_health_evidence(self, retention_days: int) -> int:
|
||||
"""Delete superseded evidence history older than the retention window.
|
||||
|
||||
Only inactive rows are removed. A row becomes inactive exclusively
|
||||
when a newer claim for the same provider/subject supersedes it, so
|
||||
every subject keeps its current (active) claim and health resolution
|
||||
is unaffected. Without this, every status flip leaves a permanent row
|
||||
and evidence reads grow linearly with uptime.
|
||||
"""
|
||||
|
||||
if retention_days <= 0:
|
||||
return 0
|
||||
cutoff = _now() - timedelta(days=retention_days)
|
||||
with self._connect() as db:
|
||||
cursor = db.execute(
|
||||
"DELETE FROM health_evidence WHERE active = 0 AND superseded_by IS NOT NULL AND received_at < ?",
|
||||
(cutoff.isoformat(),),
|
||||
)
|
||||
# Links cascade through the foreign key; also remove any orphan
|
||||
# links left by databases that predate enforced foreign keys.
|
||||
db.execute(
|
||||
"DELETE FROM health_evidence_service_links "
|
||||
"WHERE evidence_id NOT IN (SELECT id FROM health_evidence)"
|
||||
)
|
||||
return cursor.rowcount
|
||||
|
||||
def _insert_event_db(
|
||||
self,
|
||||
db: sqlite3.Connection,
|
||||
@@ -1669,12 +1828,37 @@ class Store:
|
||||
event_bus.publish(result.model_dump(mode="json"))
|
||||
return result
|
||||
|
||||
def list_events(self, limit: int = 50, service_id: str | None = None) -> list[EventRecord]:
|
||||
with self._connect() as db:
|
||||
if service_id:
|
||||
rows = db.execute("SELECT * FROM events WHERE service_id = ? ORDER BY created_at DESC LIMIT ?", (service_id, limit)).fetchall()
|
||||
def list_events(
|
||||
self,
|
||||
limit: int = 50,
|
||||
service_id: str | None = None,
|
||||
event_type: str | None = None,
|
||||
before: datetime | None = None,
|
||||
) -> list[EventRecord]:
|
||||
"""Newest first. ``event_type`` ending in ``.`` matches a family
|
||||
(``incident.``); ``before`` pages to older events."""
|
||||
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
if service_id:
|
||||
clauses.append("service_id = ?")
|
||||
params.append(service_id)
|
||||
if event_type:
|
||||
if event_type.endswith("."):
|
||||
# Exact prefix match without LIKE wildcards.
|
||||
clauses.append("substr(event_type, 1, ?) = ?")
|
||||
params.extend([len(event_type), event_type])
|
||||
else:
|
||||
rows = db.execute("SELECT * FROM events ORDER BY created_at DESC LIMIT ?", (limit,)).fetchall()
|
||||
clauses.append("event_type = ?")
|
||||
params.append(event_type)
|
||||
if before is not None:
|
||||
clauses.append("created_at < ?")
|
||||
params.append(before.isoformat())
|
||||
where = f"WHERE {' AND '.join(clauses)}" if clauses else ""
|
||||
with self._connect() as db:
|
||||
rows = db.execute(
|
||||
f"SELECT * FROM events {where} ORDER BY created_at DESC LIMIT ?", [*params, limit]
|
||||
).fetchall()
|
||||
return [self._row_to_event(row) for row in rows]
|
||||
|
||||
def demo_active(self) -> bool:
|
||||
@@ -1928,7 +2112,7 @@ class Store:
|
||||
""",
|
||||
(snapshot_services, snapshot_probes, snapshot_events, snapshot_incidents, snapshot_discovery, snapshot_health_evidence, json.dumps([service.id for service in services]), started_at),
|
||||
)
|
||||
event_bus.publish({"event_type": "topology.changed", "message": "Synthetic demo loaded"})
|
||||
event_bus.publish({"event_type": "topology.changed", "message": "Synthetische demo geladen"})
|
||||
return ConfigImportResult(created=len(services), updated=0, total=len(services))
|
||||
|
||||
def exit_demo(self) -> bool:
|
||||
@@ -1947,7 +2131,7 @@ class Store:
|
||||
session["snapshot_health_evidence"] or "{}",
|
||||
)
|
||||
db.execute("DELETE FROM demo_sessions WHERE id = 1")
|
||||
event_bus.publish({"event_type": "topology.changed", "message": "Synthetic demo exited"})
|
||||
event_bus.publish({"event_type": "topology.changed", "message": "Synthetische demo afgesloten"})
|
||||
return True
|
||||
|
||||
def list_incidents(self, active_only: bool = False, limit: int = 100) -> list[IncidentRecord]:
|
||||
@@ -1999,14 +2183,51 @@ class Store:
|
||||
event_bus.publish(payload)
|
||||
return IncidentEvent(id=event_id, incident_id=incident_id, event_type=event_type, message=message, created_at=created_at, service_id=service_id)
|
||||
|
||||
def resolve_incident(self, incident_id: str, note: str | None = None) -> IncidentRecord | None:
|
||||
"""Close an incident by hand (e.g. a stale one).
|
||||
|
||||
If its root is still down, the next incident sync opens a new one.
|
||||
"""
|
||||
|
||||
incident = self.get_incident(incident_id)
|
||||
if incident is None:
|
||||
return None
|
||||
if incident.status == IncidentStatus.RESOLVED:
|
||||
return incident
|
||||
now = _now()
|
||||
updated = incident.model_copy(
|
||||
update={"status": IncidentStatus.RESOLVED, "resolved_at": now, "latest_update_at": now}
|
||||
)
|
||||
self.upsert_incident(updated)
|
||||
self.append_incident_event(
|
||||
incident_id,
|
||||
"incident.resolved",
|
||||
f"Incident handmatig opgelost{f': {note}' if note else ''}",
|
||||
incident.root_service_id,
|
||||
)
|
||||
return updated
|
||||
|
||||
def add_incident_note(self, incident_id: str, text: str) -> IncidentRecord | None:
|
||||
incident = self.get_incident(incident_id)
|
||||
if incident is None:
|
||||
return None
|
||||
self.append_incident_event(incident_id, "incident.note", text, incident.root_service_id)
|
||||
updated = incident.model_copy(update={"latest_update_at": _now()})
|
||||
self.upsert_incident(updated)
|
||||
return updated
|
||||
|
||||
def acknowledge_incident(self, incident_id: str) -> IncidentRecord | None:
|
||||
incident = self.get_incident(incident_id)
|
||||
if incident is None:
|
||||
return None
|
||||
if incident.status == IncidentStatus.RESOLVED:
|
||||
raise ValueError("Resolved incidents cannot be acknowledged")
|
||||
raise ValueError("Opgeloste incidenten kunnen niet bevestigd worden")
|
||||
if incident.status == IncidentStatus.ACKNOWLEDGED:
|
||||
# Idempotent: a repeated acknowledge keeps the original time and
|
||||
# adds no duplicate timeline entry.
|
||||
return incident
|
||||
now = _now()
|
||||
updated = incident.model_copy(update={"status": IncidentStatus.ACKNOWLEDGED, "acknowledged_at": now, "latest_update_at": now})
|
||||
self.upsert_incident(updated)
|
||||
self.append_incident_event(incident_id, "incident.acknowledged", "Incident acknowledged", incident.root_service_id)
|
||||
self.append_incident_event(incident_id, "incident.acknowledged", "Incident bevestigd", incident.root_service_id)
|
||||
return updated
|
||||
Reference in new issue
Block a user