"""Buryan Hub Core: шина событий и граф сущностей. Один процесс, три задачи: 1) принимать события от сервисов и складывать в общую базу; 2) хранить карточки сущностей и связи между ними; 3) публиковать значимое в Matrix (личка владельцу). Запуск: python3 core.py (HTTP на порту 9120) """ import json import os import threading import urllib.request from datetime import datetime, timezone from http.server import BaseHTTPRequestHandler, HTTPServer import psycopg2 import psycopg2.extras DSN = os.environ.get("CORE_DSN", "dbname=hub_core user=hub_core password=hub_core host=127.0.0.1") PORT = int(os.environ.get("CORE_PORT", "9120")) MATRIX_HS = os.environ.get("MATRIX_HS", "https://matrix.buryans.ru") NOTIFY_TOKEN = os.environ.get("NOTIFY_TOKEN", "") NOTIFY_ROOM = os.environ.get("NOTIFY_ROOM", "") # какие события стоит показывать человеку сразу LOUD = { "media.imported", "media.requested", "media.failed", "backup.done", "backup.failed", "service.down", "service.up", "photo.imported", "book.added", "document.added", } def db(): con = psycopg2.connect(DSN, connect_timeout=10) con.autocommit = True return con def ensure_schema(): sql = open(os.path.join(os.path.dirname(__file__), "schema.sql"), encoding="utf-8").read() with db() as con, con.cursor() as cur: cur.execute(sql) def publish(text): """Короткое уведомление в личку владельцу.""" if not (NOTIFY_TOKEN and NOTIFY_ROOM): return txn = int(datetime.now().timestamp() * 1000) import urllib.parse rid = urllib.parse.quote(NOTIFY_ROOM, safe="") body = json.dumps({"msgtype": "m.text", "body": text, "format": "org.matrix.custom.html", "formatted_body": text.replace("\n", "
")}).encode() req = urllib.request.Request( f"{MATRIX_HS}/_matrix/client/v3/rooms/{rid}/send/m.room.message/{txn}", data=body, method="PUT", headers={"Authorization": f"Bearer {NOTIFY_TOKEN}", "Content-Type": "application/json"}) try: urllib.request.urlopen(req, timeout=10) except Exception: pass def human_event(ev): e = ev.get("entity") or {} title = e.get("title") or e.get("key") or "объект" t = ev.get("type", "") icons = {"media.imported": "🎬", "media.requested": "🕓", "media.failed": "⚠️", "backup.done": "💾", "backup.failed": "⚠️", "service.down": "🔴", "service.up": "✅", "photo.imported": "📷", "book.added": "📚", "document.added": "📄"} icon = icons.get(t, "•") src = ev.get("source", "?") extra = ev.get("payload") or {} line = f"{icon} **{title}** — {t} ({src})" if extra.get("detail"): line += f"\n{extra['detail']}" return line class Handler(BaseHTTPRequestHandler): def log_message(self, *a): pass def _json(self, code, obj): body = json.dumps(obj, ensure_ascii=False).encode() self.send_response(code) self.send_header("Content-Type", "application/json; charset=utf-8") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def do_GET(self): if self.path.startswith("/health"): try: with db() as con, con.cursor() as cur: cur.execute("select count(*) from events") n = cur.fetchone()[0] return self._json(200, {"ok": True, "events": n}) except Exception as e: return self._json(500, {"ok": False, "error": str(e)[:200]}) if self.path.startswith("/entities"): q = "" if "?" in self.path: import urllib.parse q = (urllib.parse.parse_qs(self.path.split("?", 1)[1]).get("q") or [""])[0] try: with db() as con, con.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur: if q: cur.execute("""select id, kind, title, status, external_ids, updated_at from entities where search_text like %s order by updated_at desc limit 20""", (f"%{q.lower()}%",)) else: cur.execute("""select id, kind, title, status, external_ids, updated_at from entities order by updated_at desc limit 20""") return self._json(200, {"items": [dict(r) for r in cur.fetchall()]}) except Exception as e: return self._json(500, {"error": str(e)[:200]}) if self.path.startswith("/events"): try: with db() as con, con.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur: cur.execute("""select id, at, source, type, entity, payload from events order by id desc limit 30""") return self._json(200, {"items": [dict(r) for r in cur.fetchall()]}) except Exception as e: return self._json(500, {"error": str(e)[:200]}) return self._json(404, {"error": "нет такого пути"}) def do_POST(self): length = int(self.headers.get("Content-Length", 0)) raw = self.rfile.read(length) if length else b"{}" try: data = json.loads(raw or b"{}") except json.JSONDecodeError: return self._json(400, {"error": "не JSON"}) if self.path.startswith("/event"): return self._event(data) if self.path.startswith("/entity"): return self._entity(data) if self.path.startswith("/link"): return self._link(data) return self._json(404, {"error": "нет такого пути"}) # ── события ── def _event(self, ev): src = (ev.get("source") or "").strip() typ = (ev.get("type") or "").strip() if not (src and typ): return self._json(400, {"error": "нужны source и type"}) now = datetime.now(timezone.utc) ent = ev.get("entity") or {} payload = ev.get("payload") or {} try: with db() as con, con.cursor() as cur: cur.execute( """insert into events (at, source, type, entity, payload, actor) values (%s, %s, %s, %s, %s, %s) returning id""", (now, src, typ, json.dumps(ent, ensure_ascii=False), json.dumps(payload, ensure_ascii=False), ev.get("actor") or "system")) eid = cur.fetchone()[0] # событие, связанное с сущностью, обновляет её статус if ent.get("kind") and ent.get("key"): cur.execute( """insert into entities (kind, key, title, status, external_ids, search_text, updated_at) values (%s, %s, %s, %s, %s, %s, %s) on conflict (kind, key) do update set status = excluded.status, title = coalesce(excluded.title, entities.title), updated_at = excluded.updated_at returning id""", (ent["kind"], ent["key"], ent.get("title") or ent["key"], typ, json.dumps({src: ent.get("key")}, ensure_ascii=False), f"{ent.get('title') or ''} {ent.get('key')}".lower(), now)) ent_id = cur.fetchone()[0] cur.execute("update events set entity_id = %s where id = %s", (ent_id, eid)) if typ in LOUD: threading.Thread(target=publish, args=(human_event(ev),), daemon=True).start() return self._json(201, {"ok": True, "id": eid}) except Exception as e: return self._json(500, {"error": str(e)[:200]}) # ── сущности ── def _entity(self, data): kind = (data.get("kind") or "").strip() key = (data.get("key") or "").strip() if not (kind and key): return self._json(400, {"error": "нужны kind и key"}) now = datetime.now(timezone.utc) title = data.get("title") or key try: with db() as con, con.cursor() as cur: cur.execute( """insert into entities (kind, key, title, status, external_ids, search_text, updated_at) values (%s, %s, %s, %s, %s, %s, %s) on conflict (kind, key) do update set title = excluded.title, status = coalesce(excluded.status, entities.status), external_ids = entities.external_ids || excluded.external_ids, search_text = excluded.search_text, updated_at = excluded.updated_at returning id""", (kind, key, title, data.get("status"), json.dumps(data.get("external_ids") or {}, ensure_ascii=False), f"{title} {key}".lower(), now)) rid = cur.fetchone()[0] return self._json(201, {"ok": True, "id": rid}) except Exception as e: return self._json(500, {"error": str(e)[:200]}) def _link(self, data): a, b = data.get("from"), data.get("to") rel = data.get("relation") or "related" if not (a and b): return self._json(400, {"error": "нужны from и to (id)"}) try: with db() as con, con.cursor() as cur: cur.execute( """insert into entity_links (from_id, to_id, relation) values (%s, %s, %s) on conflict do nothing returning id""", (a, b, rel)) row = cur.fetchone() return self._json(201, {"ok": True, "id": row[0] if row else None}) except Exception as e: return self._json(500, {"error": str(e)[:200]}) def main(): ensure_schema() srv = HTTPServer(("0.0.0.0", PORT), Handler) print(f"hub-core слушает {PORT}", flush=True) srv.serve_forever() if __name__ == "__main__": main()