Files
buryan-infra/ops/work/web/services/svc_engine.py
T

169 lines
6.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
"""Сервис ДВИЖОК (8102): совместимость, группы, ёмкость, порты, кабели, BOM."""
import re
import sys
sys.path.insert(0, "/root/buryan-infra/ops/work/web")
from svc_base import serve # noqa
import combine_core as cc # noqa
C = cc.Core()
def _need(q):
return [x for x in (q.get("need") or []) if x]
def groups(q, body):
hk = cc.norm((q.get("host") or [""])[0])
need_n = {cc.norm(x) for x in _need(q)}
if hk not in C.devices:
return {"error": "хост не найден"}
out, used = [], set()
for g in C.GROUP_DEFS:
items = []
if g["id"] == "infra":
for x in (C.infra.get("devices") or []):
items.append({"model": x["device"], "role": "infrastructure",
"max_qty": 4, "condition": x.get("why") or "",
"specs": x.get("specs") or {}})
out.append({"id": g["id"], "name": g["name"],
"hint": "PoE-коммутаторы и хабы", "items": items})
continue
for e in C.edges_of(hk):
t, k = e["to"], cc.norm(e["to"])
if k in used:
continue
role = C.devices.get(k, {}).get("role")
if role not in g["roles"]:
continue
if g.get("skip") and re.match(g["skip"], t, re.I):
continue
if g.get("only") and not re.match(g["only"], t, re.I):
continue
if need_n and not need_n <= {cc.norm(x) for x in C.feats.get(k, set())}:
continue
used.add(k)
a = C.attrs.get(k, {})
items.append({"model": t, "role": role, "max_qty": e.get("max_qty"),
"condition": (e.get("condition") or "").strip()[:160],
"power_w": a.get("power.consumption_w"),
"pickup_m": a.get("audio.pickup_m"),
"zoom": a.get("camera.zoom_optical") or a.get("camera.zoom_hybrid"),
"ports_vch": a.get("ports.vch")})
if g["id"] == "mount":
have = {i["model"] for i in items}
for m in C.mounts_map.get(hk, []):
if m not in have:
items.append({"model": m, "role": "mount", "max_qty": 1,
"condition": "лист Mounts & Bracket"})
if items:
out.append({"id": g["id"], "name": g["name"], "hint": "",
"items": sorted(items, key=lambda x: x["model"])})
return {"host": C.devices[hk]["name"], "groups": out,
"completeness": C.completeness(hk)}
def check(q, body):
return C.check_pair((q.get("host") or [""])[0], (q.get("device") or [""])[0])
def _items(q):
out = []
for raw in (q.get("item") or []):
if ":" in raw:
m, n = raw.rsplit(":", 1)
try:
out.append([m, int(n)])
except ValueError:
pass
else:
out.append([raw, 1])
return out
def capacity(q, body):
return C.capacity((q.get("host") or [""])[0], _items(q), q.get("infra") or [])
def port_plan(q, body):
return C.port_plan((q.get("host") or [""])[0], _items(q))
def cables(q, body):
return C.cable_schedule((q.get("host") or [""])[0], _items(q))
def bom(q, body):
hk = cc.norm((q.get("host") or [""])[0])
if hk not in C.devices:
return {"error": "хост не найден"}
host = C.devices[hk]["name"]
seats = int((q.get("seats") or ["8"])[0])
need = _need(q)
need_m = 3 if seats <= 6 else 6 if seats <= 12 else 8 if seats <= 16 else 10
bom, warn = [], []
bom.append({"role": "Хост", "model": host, "qty": 1, "why": "основа"})
def by_role(role):
return [e for e in C.edges_of(hk)
if C.devices.get(cc.norm(e["to"]), {}).get("role") == role]
ha = {cc.norm(f) for f in C.feats.get(hk, [])}
for n in need:
if cc.norm(n) not in ha:
warn.append("хост не имеет «%s»" % n)
built = C.attrs.get(hk, {}).get("audio.pickup_m")
if built and built >= need_m:
bom.append({"role": "Микрофон", "model": "встроенный " + host, "qty": 1,
"why": "захват %s м ≥ %s м" % (built, need_m)})
else:
cands = sorted(((C.attrs.get(cc.norm(e["to"]), {}).get("audio.pickup_m") or 0, e)
for e in by_role("audio")), key=lambda x: -x[0])
if cands:
p, e = cands[0]
qty = max(1, -(-need_m // max(int(p), 1))) if p else 1
bom.append({"role": "Микрофон", "model": e["to"], "qty": qty,
"why": "нужно %s м, у %s %s м" % (need_m, e["to"], p or "—")})
cams = by_role("camera")
if cams:
cams.sort(key=lambda e: -((C.attrs.get(cc.norm(e["to"]), {}).get("camera.zoom_optical") or 0)), reverse=True)
bom.append({"role": "Камера", "model": cams[0]["to"], "qty": 1,
"why": "до %s шт" % (cams[0].get("max_qty") or "—")})
for role, label in (("touch", "Панель"), ("sharing_or_hub", "Презентация")):
it = by_role(role)
if it:
bom.append({"role": label, "model": it[0]["to"], "qty": 1, "why": "совместимо"})
disp = by_role("display")
bom.append({"role": "Дисплей", "model": disp[0]["to"] if disp else "внешний ТВ",
"qty": 1, "why": "совместим" if disp else "у Yealink нет дисплея"})
mnts = C.mounts_map.get(hk) or []
if mnts:
bom.append({"role": "Кронштейн", "model": mnts[0], "qty": 1,
"why": "вариантов %d" % len(mnts)})
if any(cc.norm(x) == "dante" for x in need):
bom.append({"role": "Dante", "model": "CM50+CS10-D+AP08", "qty": 1,
"why": "нативный Dante-комплект"})
items = [[b["model"], b["qty"]] for b in bom
if not b["model"].startswith(("встроенный", "внешний"))]
cap = C.capacity(hk, items)
included = []
for b in bom:
pk = C.package(b["model"])
if pk["items"]:
included.append({"device": b["model"], "items": pk["items"], "kits": pk["kits"]})
return {"host": host, "seats": seats, "need_pickup": need_m,
"bom": bom, "warnings": warn, "capacity": cap,
"included": included,
"cables": C.cable_schedule(hk, items)}
serve({
"/groups": groups,
"/check": check,
"/capacity": capacity,
"/ports": port_plan,
"/cables": cables,
"/bom": bom,
}, int(sys.argv[1]) if len(sys.argv) > 1 else 8102, "engine")