add chache for last known good in sqlite..

This commit is contained in:
larssand
2026-06-30 10:30:59 +02:00
parent 50463aa4d6
commit 2aaa2e28f3
6 changed files with 91 additions and 2 deletions

View File

@@ -12,7 +12,7 @@ from .event_context import build_event_context
from .feedback import FeedbackStore
from .graylog_mcp import GraylogMcpClient
from .graylog_source import GraylogStreamSource
from .history import HistoryStore
from .history import HistoryStore, StatusSnapshotStore
from .incidents import IncidentStore, build_incidents
from .data_quality import assess_data_quality
from .llm import ollama_dashboard_assessment, ollama_profile_advice
@@ -107,6 +107,7 @@ def build_status(
config_path: str | None = None,
history_path: str | None = None,
incident_path: str | None = None,
status_cache_path: str | None = None,
) -> dict[str, object]:
config_store = ConfigStore(config_path) if config_path else None
config_exists = bool(config_store and config_store.path.exists())
@@ -342,9 +343,27 @@ def build_status(
history = HistoryStore(history_path)
history.record(status)
status["history"] = history.recent()
if status_cache_path and not (isinstance(mcp_status, dict) and mcp_status.get("status") == "error"):
StatusSnapshotStore(status_cache_path).save("last_good", status)
return status
def cached_status_with_error(cache_path: str, error_status: dict[str, object]) -> dict[str, object] | None:
cached = StatusSnapshotStore(cache_path).load("last_good")
if not cached:
return None
cached["generated_at"] = int(time.time())
cached["stale"] = True
cached["stale_reason"] = "live_mcp_error"
capabilities = cached.setdefault("capabilities", {})
if isinstance(capabilities, dict):
capabilities["graylog_mcp"] = error_status
cached.setdefault("status_cache", {})
if isinstance(cached["status_cache"], dict):
cached["status_cache"].update({"served_from_cache": True, "reason": "live_mcp_error"})
return cached
def add_llm_assessment(status: dict[str, object], *, previous: str | None = None, model: str | None = None, timeout: int | None = None) -> None:
try:
status["llm_assessment"] = {
@@ -385,6 +404,7 @@ def monitor_loop(
baseline_path: str | None = None,
config_path: str | None = None,
history_path: str | None = None,
status_cache_path: str | None = None,
) -> None:
print(f"Monitoring {log_path}")
print(f"Writing status to {output}")
@@ -397,7 +417,13 @@ def monitor_loop(
status = build_status(
log_path, policy_path=policy_path, anomaly_limit=anomaly_limit,
baseline_path=baseline_path, config_path=config_path, history_path=history_path,
status_cache_path=status_cache_path,
)
mcp = status.get("capabilities", {}).get("graylog_mcp", {}) if isinstance(status.get("capabilities"), dict) else {}
if status_cache_path and isinstance(mcp, dict) and mcp.get("status") == "error":
cached = cached_status_with_error(status_cache_path, mcp)
if cached:
status = cached
if effective_llm:
now = int(time.time())
if now - last_llm_at >= llm_interval: