Files
second-brain/cron_tasks/daily_summary.py
Otto 8783bb2db5 fix: performance optimizations for large dataset
- Add generated columns and indexes for correctness and metadata fields
- Optimize get_all() with keyset pagination
- Add get_pending_for_review() for targeted queries
- Update cron tasks to use optimized queries instead of full table scans
- This fixes timeouts in review_brain and verify_pending_external (300s timeout)

Fixes #35: Second-Brain in Takt bringen, Dedup, Pendings, Graph und Performance
2026-06-04 12:25:11 +02:00

92 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Tägliche Zusammenfassung der Second Brain Aktivitäten.
Erstellt ein Engramm mit Highlights des Vortags.
"""
from __future__ import annotations
import json
import sqlite3
import sys
from datetime import datetime, timezone, timedelta
from pathlib import Path
BRAIN_DIR = Path("/root/.openclaw/workspace/second-brain")
DB_PATH = BRAIN_DIR / "data" / "brain.sqlite"
def run():
now = datetime.now(timezone.utc)
yesterday = now - timedelta(days=1)
date_str = yesterday.strftime("%Y-%m-%d")
conn = sqlite3.connect(str(DB_PATH), timeout=60)
conn.execute("PRAGMA busy_timeout=60000")
conn.execute("PRAGMA journal_mode=WAL")
conn.row_factory = sqlite3.Row
c = conn.cursor()
# Engramme von gestern (created_at innerhalb des Tages)
c.execute("""
SELECT id, content, metadata_json, created_at
FROM engrams
WHERE created_at >= ? AND created_at < ?
""", (yesterday.isoformat(), now.isoformat()))
rows = c.fetchall()
total_yesterday = len(rows)
sources = {}
tags = {}
for r in rows:
meta = json.loads(r["metadata_json"] or "{}")
src = meta.get("source", "unknown")
sources[src] = sources.get(src, 0) + 1
for t in meta.get("tags", []):
tags[t] = tags.get(t, 0) + 1
conn.close()
# Zusammenfassung bauen
top_sources = sorted(sources.items(), key=lambda x: x[1], reverse=True)[:5]
top_tags = sorted(tags.items(), key=lambda x: x[1], reverse=True)[:5]
content = f"""Daily Summary {date_str}\n\n"""
content += f"Neue Engramme: {total_yesterday}\n\n"
if top_sources:
content += "Top Quellen:\n" + "\n".join(f"- {src}: {cnt}" for src, cnt in top_sources) + "\n\n"
if top_tags:
content += "Top Tags:\n" + "\n".join(f"- {tag}: {cnt}" for tag, cnt in top_tags) + "\n\n"
content += f"Generiert am {now.isoformat()}"
# Engramm speichern
sys.path.insert(0, str(BRAIN_DIR))
from src.store import EngramStore
from src.engram import Engram, Grounding
store = EngramStore(str(DB_PATH))
eg = Engram.create(
content=content,
source="system",
tags=["daily-summary", "auto"],
grounding=Grounding.ASSUMPTION,
)
eg.metadata.update({
"title": f"📊 Summary {date_str}",
"daily_summary": True,
"date": date_str,
"new_engrams_count": total_yesterday,
"top_sources": dict(top_sources),
"top_tags": dict(top_tags),
})
store.save(eg)
print(json.dumps({
"success": True,
"date": date_str,
"engram_id": str(eg.id),
"new_engrams": total_yesterday,
}, indent=2, ensure_ascii=False))
if __name__ == "__main__":
run()