diff --git a/ops/bots/hub/bots.py b/ops/bots/hub/bots.py index cf16373..6bf85d2 100644 --- a/ops/bots/hub/bots.py +++ b/ops/bots/hub/bots.py @@ -245,6 +245,12 @@ class MediaBot(Bot): lines.append("Связи:") for r in rels[:10]: lines.append("• " + str(r.get("relation")) + ": " + str(r.get("n"))) + hist, _ = core_api.history(30) + growth = (hist or {}).get("growth") + if growth: + lines.append("") + lines.append("_рост за период: карточек " + str(growth.get("cards")) + + ", связей " + str(growth.get("links")) + "_") return chr(10).join(lines) def _graph_lookup(self, term): diff --git a/ops/bots/hub/core_api.py b/ops/bots/hub/core_api.py index ee56637..5ca1c85 100644 --- a/ops/bots/hub/core_api.py +++ b/ops/bots/hub/core_api.py @@ -155,3 +155,7 @@ def coworkers(query, limit=12): def suggest(q, limit=8): return _get("/suggest", {"q": q, "limit": limit}) + + +def history(days=30): + return _get("/history", {"days": days}) diff --git a/ops/media/GRAPH-HISTORY.md b/ops/media/GRAPH-HISTORY.md new file mode 100644 index 0000000..f08832b --- /dev/null +++ b/ops/media/GRAPH-HISTORY.md @@ -0,0 +1,32 @@ +# История роста графа + +Ежедневный снимок размеров графа — чтобы видеть, растёт ли медиатека. + +## Как устроено + +* таблица `graph_history(day, cards, links, persons)` создаётся при первом + обращении; +* `GET /snapshot` — сделать снимок за сегодня (upsert); +* `GET /history?days=30` — ряд снимков и прирост между первым и последним; +* cron `30 23 * * *` — снимок каждый вечер. + +## Где видно + +* панель: строка «карточек N · связей M · рост за период: карточек +X, + связей +Y»; +* бот: команда `граф` добавляет «_рост за период: …_». + +### Проверка + +Имитировал снимок недельной давности (250 карточек, 380 связей) — текущий +310/455: + +``` +история: [{13.10: 250/380/60}, {20.10: 310/455/74}] +рост: карточек 60, связей 75 +``` + +## Дополнительно: автодополнение + +В `/ask.html` поле ввода подсказывает карточки (`GET /suggest`, задержка +250 мс). Работает и по русским окончаниям: «дюн» → оба Dune. diff --git a/ops/media/core.py b/ops/media/core.py index 1d2ef20..872ca94 100644 --- a/ops/media/core.py +++ b/ops/media/core.py @@ -884,6 +884,45 @@ def do_suggest(q, limit=8): return {"query": q, "items": items} + + +def snapshot_graph(day=None): + """Снимок размеров графа за день.""" + from datetime import date + day = day or date.today().isoformat() + con = db() + cur = con.cursor(cursor_factory=psycopg2.extras.RealDictCursor) + cur.execute("""create table if not exists graph_history ( + day date primary key, cards integer, links integer, persons integer)""") + cur.execute("select count(*) as n from entities") + cards = cur.fetchone()["n"] + cur.execute("select count(*) as n from entity_links") + links = cur.fetchone()["n"] + cur.execute("select count(*) as n from entities where kind in ('актёр','режиссёр','композитор')") + persons = cur.fetchone()["n"] + cur.execute("""insert into graph_history (day, cards, links, persons) values (%s, %s, %s, %s) + on conflict (day) do update set cards = %s, links = %s, persons = %s""", + (day, cards, links, persons, cards, links, persons)) + con.commit() + con.close() + return {"day": str(day), "cards": cards, "links": links, "persons": persons} + + +def do_history(days=30): + con = db() + cur = con.cursor(cursor_factory=psycopg2.extras.RealDictCursor) + cur.execute("""select day, cards, links, persons from graph_history + order by day desc limit %s""", (days,)) + rows = [dict(r) for r in cur.fetchall()] + con.close() + rows.reverse() + growth = None + if len(rows) >= 2: + growth = {"cards": rows[-1]["cards"] - rows[0]["cards"], + "links": rows[-1]["links"] - rows[0]["links"]} + return {"items": rows, "growth": growth} + + def render_panel(): """HTML-страница: состояние системы в одном месте.""" try: @@ -927,6 +966,11 @@ def render_panel(): gstats = do_graph_stats() except Exception: gstats = {"cards": [], "relations": [], "total_cards": 0, "total_links": 0} + try: + hist = do_history(30) + growth = hist.get("growth") + except Exception: + growth = None try: cross = do_cross(limit=10) except Exception: @@ -974,6 +1018,10 @@ def render_panel(): cross_rows.append(f"
| связь | сколько |
|---|