Files
buryan-infra/ops/services/graph.py

70 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""Граф зависимостей сервисов из карточек → страница Wiki Graph."""
import glob
import os
import re
CARDS = "/root/wiki/cards"
OUT = "/root/wiki/Graph.md"
GROUPS = {"Ядро": "core", "Дом": "home", "Медиа": "media", "Знания": "knowledge",
"Автоматизация": "automation", "Наблюдаемость": "obs",
"Служебные": "support", "Прочее": "other"}
def parse(path):
text = open(path, encoding="utf-8").read()
name = os.path.basename(path)[:-3]
def f(label):
m = re.search(rf"\|\s*{label}\s*\|\s*(.+?)\s*\|", text)
return m.group(1).strip() if m else ""
deps = [d.strip() for d in f("Зависимости").split(",") if d.strip() and d.strip() != "—"]
return {"name": name, "group": f("Группа"), "deps": deps,
"crit": f("Критичность")}
def main():
cards = [parse(p) for p in glob.glob(os.path.join(CARDS, "*.md"))]
names = {c["name"] for c in cards}
lines = ["# Граф зависимостей", "",
"Кто от кого зависит. Падение нижнего узла ломает верхние.", "",
"```mermaid", "graph TD"]
for g in ("Ядро", "Дом", "Медиа", "Знания", "Автоматизация", "Наблюдаемость"):
items = [c for c in cards if c["group"] == g]
if not items:
continue
lines.append(f" subgraph {GROUPS.get(g, 'g')}[{g}]")
for c in items:
mark = "((!))" if c["crit"] == "high" else ""
label = c["name"] + (" 🔴" if c["crit"] == "high" else "")
lines.append(f" {safe(c['name'])}[\"{label}\"]")
lines.append(" end")
edges = []
for c in cards:
for d in c["deps"]:
target = d if d in names else None
if not target:
cand = [n for n in names if d in n]
target = cand[0] if cand else None
if target:
edges.append(f" {safe(target)} --> {safe(c['name'])}")
lines.extend(sorted(set(edges)))
lines += ["```", "",
"## Как читать", "",
"- Стрелка A → B: B зависит от A.",
"- 🔴 — критичный сервис.",
"- Если узел упал, всё, что справа, перестанет работать.", ""]
open(OUT, "w", encoding="utf-8").write("\n".join(lines))
print(f"узлов: {len(cards)}, связей: {len(set(edges))}")
def safe(name):
return re.sub(r"[^a-zA-Z0-9_]", "_", name)
if __name__ == "__main__":
main()