Files
buryan-infra/ops/scripts/analog_recommend.py
T
2026-11-18 16:12:08 +03:00

91 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Прототип analog engine: аналоги только внутри domain + solution_class.
SIP не является доменом. Для VCS-бара нельзя вернуть SIP-телефон.
"""
import argparse
import json
import sys
import yaml
P = "/root/buryan-infra/ops/combine/catalog-p0-2026-11-18.yaml"
D = yaml.safe_load(open(P, encoding="utf-8"))["devices"]
def score(target, x):
s = 0
why = []
if x["domain"] == target["domain"]:
s += 20
if x["solution_class"] == target["solution_class"]:
s += 50
why.append("same solution_class")
if x.get("form_factor") == target.get("form_factor"):
s += 10
why.append("same form_factor")
tp = {p.upper() for p in target.get("protocols") or []}
xp = {p.upper() for p in x.get("protocols") or []}
if tp and xp & tp:
s += 10
why.append("protocol overlap")
if x.get("sales_scope") == "alternative":
s += 5
why.append("alternative vendor")
return s, why
def find(model):
q = model.lower().strip()
for x in D:
if x["model"].lower() == q or x.get("id", "").lower() == q:
return x
for x in D:
if q in x["model"].lower():
return x
return None
def analogs(model, limit=8):
t = find(model)
if not t:
return {"error": "not_found", "query": model}
cand = []
for x in D:
if x is t or x["vendor"] == t["vendor"]:
continue
# строгий барьер: только тот же домен и тот же класс
if x["domain"] != t["domain"] or x["solution_class"] != t["solution_class"]:
continue
s, why = score(t, x)
cand.append({"id": x["id"], "vendor": x["vendor"], "model": x["model"],
"solution_class": x["solution_class"], "sales_scope": x.get("sales_scope"),
"protocols": x.get("protocols") or [], "score": s,
"why": why})
cand.sort(key=lambda z: (-z["score"], z["vendor"], z["model"]))
return {"target": {"id": t["id"], "vendor": t["vendor"], "model": t["model"],
"domain": t["domain"], "solution_class": t["solution_class"],
"sales_scope": t.get("sales_scope"), "protocols": t.get("protocols") or []},
"alternatives": cand[:limit],
"note": "Аналоги только внутри domain+solution_class; телефония не предлагается для VCS."}
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("model")
ap.add_argument("--json", action="store_true")
a = ap.parse_args()
r = analogs(a.model)
if a.json:
print(json.dumps(r, ensure_ascii=False, indent=2))
else:
if "error" in r:
print("не найдено:", a.model)
sys.exit(2)
t = r["target"]
print("Цель: %s — %s (%s, %s, %s)" % (t["vendor"], t["model"], t["domain"], t["solution_class"], t["sales_scope"]))
print("Аналоги:")
for x in r["alternatives"]:
print("- %-10s %-32s score=%d %s" % (x["vendor"], x["model"], x["score"], ", ".join(x["why"])))
if not r["alternatives"]:
print("- нет альтернатив в базе")