Memory: Report-Stats, Ablauf-Warnung, Cleanup-Skript
This commit is contained in:
parent
2c362b1009
commit
a1618b37fe
2 changed files with 63 additions and 0 deletions
31
homelab-ai-bot/memory_cleanup.py
Normal file
31
homelab-ai-bot/memory_cleanup.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Bereinigt abgelaufene temporaere Memory-Items. Laeuft als Cronjob."""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.dirname(__file__))
|
||||||
|
|
||||||
|
logging.basicConfig(format="%(asctime)s [memory-cleanup] %(message)s", level=logging.INFO)
|
||||||
|
log = logging.getLogger("memory-cleanup")
|
||||||
|
|
||||||
|
import memory_client
|
||||||
|
|
||||||
|
|
||||||
|
def cleanup():
|
||||||
|
items = memory_client.get_active_memory()
|
||||||
|
now_ts = int(time.time())
|
||||||
|
archived = 0
|
||||||
|
for item in items:
|
||||||
|
exp = item.get("expires_at")
|
||||||
|
if exp and exp < now_ts:
|
||||||
|
memory_client._patch(f"/memory/{item['id']}", {"status": "archived"})
|
||||||
|
log.info("Archiviert: [%s] %s (abgelaufen)", item["id"], item["content"][:60])
|
||||||
|
archived += 1
|
||||||
|
log.info("Cleanup fertig: %d archiviert, %d aktiv", archived, len(items) - archived)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
cleanup()
|
||||||
|
|
@ -130,6 +130,20 @@ def check_all() -> list[str]:
|
||||||
for r in restarts:
|
for r in restarts:
|
||||||
alerts.append(f"🔄 Service-Neustart: {r['service']} auf {r['host']} ({r['count']}x in 35 Min)")
|
alerts.append(f"🔄 Service-Neustart: {r['service']} auf {r['host']} ({r['count']}x in 35 Min)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
import memory_client
|
||||||
|
import time as _time
|
||||||
|
now_ts = int(_time.time())
|
||||||
|
mem_items = memory_client.get_active_memory()
|
||||||
|
for item in mem_items:
|
||||||
|
exp = item.get("expires_at")
|
||||||
|
if exp and 0 < exp - now_ts < 86400:
|
||||||
|
from datetime import datetime as _dt
|
||||||
|
exp_str = _dt.fromtimestamp(exp).strftime("%d.%m. %H:%M")
|
||||||
|
alerts.append(f"⏰ Memory läuft ab ({exp_str}): {item['content'][:80]}")
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mail_client.init(cfg)
|
mail_client.init(cfg)
|
||||||
important = mail_client.get_important_mails(hours=1)
|
important = mail_client.get_important_mails(hours=1)
|
||||||
|
|
@ -187,6 +201,24 @@ def format_report() -> str:
|
||||||
else:
|
else:
|
||||||
lines.append("Stille Hosts: keine")
|
lines.append("Stille Hosts: keine")
|
||||||
|
|
||||||
|
try:
|
||||||
|
import memory_client
|
||||||
|
mem_items = memory_client.get_active_memory()
|
||||||
|
perm = [i for i in mem_items if i.get("memory_type") != "temporary"]
|
||||||
|
temp = [i for i in mem_items if i.get("memory_type") == "temporary"]
|
||||||
|
candidates = memory_client.get_candidates()
|
||||||
|
mem_line = f"Memory: {len(perm)} dauerhaft, {len(temp)} temporär"
|
||||||
|
import time as _time
|
||||||
|
now_ts = int(_time.time())
|
||||||
|
soon = [i for i in temp if i.get("expires_at") and i["expires_at"] - now_ts < 86400]
|
||||||
|
if soon:
|
||||||
|
mem_line += f", {len(soon)} laufen in 24h ab"
|
||||||
|
if candidates:
|
||||||
|
mem_line += f", {len(candidates)} Kandidaten offen"
|
||||||
|
lines.append(mem_line)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
alerts = check_all()
|
alerts = check_all()
|
||||||
if alerts:
|
if alerts:
|
||||||
lines.append(f"\n⚠️ {len(alerts)} aktive Alarme:")
|
lines.append(f"\n⚠️ {len(alerts)} aktive Alarme:")
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue