Add anomaly and performance monitoring
This commit is contained in:
@@ -8,6 +8,7 @@ from zoneinfo import ZoneInfo
|
||||
from app.actuators.models import (
|
||||
ActuatorRecord,
|
||||
AdaptiveWeightUpdate,
|
||||
AnomalyEvent,
|
||||
AutomationConflict,
|
||||
BehaviorMode,
|
||||
BehaviorPattern,
|
||||
@@ -88,6 +89,16 @@ class BehaviorEngine:
|
||||
),
|
||||
"last_trained_at": now,
|
||||
"reason": "Noch kein geeigneter Kontext für Verhaltenslernen vorhanden.",
|
||||
"anomalies": _detect_anomalies(
|
||||
record,
|
||||
now=now,
|
||||
min_behavior_actions=self._settings.min_behavior_actions,
|
||||
stale_hours=self._settings.retrain_stale_hours,
|
||||
sample_count=0,
|
||||
trusted_actions=0,
|
||||
prediction=None,
|
||||
safety_blockers=[],
|
||||
),
|
||||
}
|
||||
),
|
||||
)
|
||||
@@ -128,6 +139,16 @@ class BehaviorEngine:
|
||||
"patterns": [],
|
||||
"last_trained_at": now,
|
||||
"reason": "Noch keine historischen Aktorhandlungen gefunden.",
|
||||
"anomalies": _detect_anomalies(
|
||||
record,
|
||||
now=now,
|
||||
min_behavior_actions=self._settings.min_behavior_actions,
|
||||
stale_hours=self._settings.retrain_stale_hours,
|
||||
sample_count=0,
|
||||
trusted_actions=0,
|
||||
prediction=None,
|
||||
safety_blockers=[],
|
||||
),
|
||||
}
|
||||
),
|
||||
)
|
||||
@@ -200,6 +221,16 @@ class BehaviorEngine:
|
||||
reason,
|
||||
),
|
||||
"active_model_version": model_version_id,
|
||||
"anomalies": _detect_anomalies(
|
||||
record,
|
||||
now=now,
|
||||
min_behavior_actions=self._settings.min_behavior_actions,
|
||||
stale_hours=self._settings.retrain_stale_hours,
|
||||
sample_count=len(patterns),
|
||||
trusted_actions=trusted_actions,
|
||||
prediction=record.behavior.prediction,
|
||||
safety_blockers=record.behavior.safety_blockers,
|
||||
),
|
||||
}
|
||||
)
|
||||
return self._save_behavior(record, behavior)
|
||||
@@ -326,6 +357,16 @@ class BehaviorEngine:
|
||||
"assumptions": _assumption_lines(record),
|
||||
"uncertainties": _uncertainty_lines(record, record.behavior.sample_count, record.behavior.high_confidence_sample_count),
|
||||
"safety_blockers": safety_blockers if prediction is not None else [],
|
||||
"anomalies": _detect_anomalies(
|
||||
record,
|
||||
now=now,
|
||||
min_behavior_actions=self._settings.min_behavior_actions,
|
||||
stale_hours=self._settings.retrain_stale_hours,
|
||||
sample_count=record.behavior.sample_count,
|
||||
trusted_actions=record.behavior.high_confidence_sample_count,
|
||||
prediction=prediction,
|
||||
safety_blockers=safety_blockers if prediction is not None else [],
|
||||
),
|
||||
"confidence_trend": (
|
||||
[*record.behavior.confidence_trend, round(prediction.confidence, 4)][-30:]
|
||||
if prediction is not None
|
||||
@@ -493,6 +534,18 @@ class BehaviorEngine:
|
||||
*record.behavior.adaptive_weight_updates,
|
||||
*adaptive_updates,
|
||||
][-50:],
|
||||
"anomalies": _detect_anomalies(
|
||||
record,
|
||||
now=now,
|
||||
min_behavior_actions=self._settings.min_behavior_actions,
|
||||
stale_hours=self._settings.retrain_stale_hours,
|
||||
sample_count=len(patterns),
|
||||
trusted_actions=record.behavior.high_confidence_sample_count,
|
||||
prediction=prediction,
|
||||
safety_blockers=record.behavior.safety_blockers,
|
||||
correct_feedback_count=correct_count,
|
||||
incorrect_feedback_count=incorrect_count,
|
||||
),
|
||||
}
|
||||
)
|
||||
record_for_save = (
|
||||
@@ -560,6 +613,20 @@ class BehaviorEngine:
|
||||
"automation_conflicts": _automation_conflicts(record, related),
|
||||
}
|
||||
)
|
||||
behavior = behavior.model_copy(
|
||||
update={
|
||||
"anomalies": _detect_anomalies(
|
||||
record.model_copy(update={"behavior": behavior}),
|
||||
now=datetime.now(timezone.utc),
|
||||
min_behavior_actions=self._settings.min_behavior_actions,
|
||||
stale_hours=self._settings.retrain_stale_hours,
|
||||
sample_count=behavior.sample_count,
|
||||
trusted_actions=behavior.high_confidence_sample_count,
|
||||
prediction=behavior.prediction,
|
||||
safety_blockers=behavior.safety_blockers,
|
||||
)
|
||||
}
|
||||
)
|
||||
return self._save_behavior(record, behavior)
|
||||
|
||||
def set_automation_enabled(
|
||||
@@ -1208,6 +1275,115 @@ def _automation_conflicts(
|
||||
return conflicts
|
||||
|
||||
|
||||
def _detect_anomalies(
|
||||
record: ActuatorRecord,
|
||||
*,
|
||||
now: datetime,
|
||||
min_behavior_actions: int,
|
||||
stale_hours: int,
|
||||
sample_count: int,
|
||||
trusted_actions: int,
|
||||
prediction: BehaviorPrediction | None,
|
||||
safety_blockers: list[str],
|
||||
correct_feedback_count: int | None = None,
|
||||
incorrect_feedback_count: int | None = None,
|
||||
) -> list[AnomalyEvent]:
|
||||
anomalies: list[AnomalyEvent] = []
|
||||
|
||||
def add(category: str, severity: str, title: str, detail: str) -> None:
|
||||
anomalies.append(
|
||||
AnomalyEvent(
|
||||
anomaly_id=f"{record.actuator_entity_id}.{category}",
|
||||
category=category,
|
||||
severity=severity,
|
||||
title=title,
|
||||
detail=detail,
|
||||
detected_at=now,
|
||||
)
|
||||
)
|
||||
|
||||
if not record.assignment.selected_context_entity_ids and not record.assignment.selected_numeric_entity_id:
|
||||
add(
|
||||
"missing_context",
|
||||
"warning",
|
||||
"Kein Kontext verbunden",
|
||||
"Der Aktor hat keine Sensor-/Kontextbasis. Entscheidungen bleiben unsicher.",
|
||||
)
|
||||
if sample_count < min_behavior_actions:
|
||||
add(
|
||||
"low_samples",
|
||||
"info",
|
||||
"Zu wenig Lernbeispiele",
|
||||
f"{sample_count} von {min_behavior_actions} benoetigten Handlungen gelernt.",
|
||||
)
|
||||
if trusted_actions < sample_count:
|
||||
add(
|
||||
"unclear_sources",
|
||||
"info",
|
||||
"Unklare Aktorhandlungen",
|
||||
"Ein Teil der gelernten Handlungen stammt nicht eindeutig von Nutzer oder Automation.",
|
||||
)
|
||||
if record.behavior.last_trained_at is not None:
|
||||
age = now - record.behavior.last_trained_at
|
||||
if age > timedelta(hours=stale_hours):
|
||||
add(
|
||||
"stale_training",
|
||||
"warning",
|
||||
"Training ist veraltet",
|
||||
f"Letztes Training liegt mehr als {stale_hours} Stunden zurueck.",
|
||||
)
|
||||
if prediction is not None and prediction.matching_patterns and prediction.confidence < record.behavior.safety.min_confidence:
|
||||
add(
|
||||
"low_confidence_prediction",
|
||||
"warning",
|
||||
"Vorhersage unter Sicherheitsgrenze",
|
||||
(
|
||||
f"Confidence {prediction.confidence:.0%} liegt unter "
|
||||
f"{record.behavior.safety.min_confidence:.0%}."
|
||||
),
|
||||
)
|
||||
if record.behavior.safety.manual_block:
|
||||
add(
|
||||
"manual_block",
|
||||
"info",
|
||||
"Manuelle Sicherheitssperre aktiv",
|
||||
"Der Aktor ist bewusst gegen automatisches Schalten gesperrt.",
|
||||
)
|
||||
if safety_blockers:
|
||||
add(
|
||||
"safety_blockers",
|
||||
"info",
|
||||
"Safety blockiert aktuelle Aktion",
|
||||
" ".join(safety_blockers)[:500],
|
||||
)
|
||||
if any(conflict.severity == "warning" for conflict in record.behavior.automation_conflicts):
|
||||
add(
|
||||
"automation_conflict",
|
||||
"critical",
|
||||
"Parallele Automation erkannt",
|
||||
"SillyHome und mindestens eine passende HA-Automation koennen parallel schalten.",
|
||||
)
|
||||
correct = (
|
||||
record.behavior.correct_feedback_count
|
||||
if correct_feedback_count is None
|
||||
else correct_feedback_count
|
||||
)
|
||||
incorrect = (
|
||||
record.behavior.incorrect_feedback_count
|
||||
if incorrect_feedback_count is None
|
||||
else incorrect_feedback_count
|
||||
)
|
||||
total = correct + incorrect
|
||||
if total >= 3 and incorrect / total >= 0.35:
|
||||
add(
|
||||
"feedback_error_rate",
|
||||
"critical",
|
||||
"Viele falsche Vorhersagen",
|
||||
f"{incorrect} von {total} Feedbacks waren negativ. Modell pruefen oder Rollback nutzen.",
|
||||
)
|
||||
return anomalies[-30:]
|
||||
|
||||
|
||||
def predict_behavior(
|
||||
patterns: list[BehaviorPattern],
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user