95 lines
3.0 KiB
Python
95 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Сервис КАТАЛОГ (8101): устройства, порты, комплектация, функции, матрица."""
|
|
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 devices(q, body):
|
|
out = []
|
|
for k, v in sorted(C.devices.items(), key=lambda x: x[1]["name"]):
|
|
ins, outs, src = C.ports(v["name"])
|
|
out.append({"key": k, "name": v["name"], "role": v["role"],
|
|
"in": ins, "out": outs, "ports_source": src,
|
|
"features": sorted(C.feats.get(k, []))})
|
|
return {"devices": out, "count": len(out)}
|
|
|
|
|
|
def device(q, body):
|
|
k = cc.norm((q.get("key") or [""])[0])
|
|
if k not in C.devices:
|
|
return {"error": "not found"}
|
|
v = C.devices[k]
|
|
ins, outs, src = C.ports(v["name"])
|
|
return {"key": k, "name": v["name"], "role": v["role"],
|
|
"ports": {"in": ins, "out": outs, "source": src},
|
|
"attributes": C.attrs.get(k, {}),
|
|
"features": sorted(C.feats.get(k, [])),
|
|
"package": C.package(v["name"]),
|
|
"completeness": C.completeness(k)}
|
|
|
|
|
|
def ports(q, body):
|
|
out = []
|
|
for k, v in sorted(C.devices.items(), key=lambda x: x[1]["name"]):
|
|
ins, outs, src = C.ports(v["name"])
|
|
out.append({"device": v["name"], "role": v["role"],
|
|
"in": ins, "out": outs, "source": src,
|
|
"package": C.package(v["name"])["items"][:4]})
|
|
return {"devices": out, "count": len(out)}
|
|
|
|
|
|
def hosts(q, body):
|
|
out = []
|
|
for k, v in C.devices.items():
|
|
if v["role"] != "codec" or not C.edges.get(k):
|
|
continue
|
|
out.append({"key": k, "name": v["name"],
|
|
"accessories": len(C.edges_of(k)),
|
|
"completeness": C.completeness(k)})
|
|
return {"hosts": sorted(out, key=lambda x: x["name"])}
|
|
|
|
|
|
def matrix(q, body):
|
|
host_list = hosts(q, body)["hosts"]
|
|
accs = {}
|
|
for h in host_list:
|
|
for e in C.edges_of(h["key"]):
|
|
accs[cc.norm(e["to"])] = e["to"]
|
|
rows = []
|
|
for h in host_list:
|
|
cells = {}
|
|
for e in C.edges_of(h["key"]):
|
|
q2 = e.get("max_qty")
|
|
cells[cc.norm(e["to"])] = q2 if q2 else 1
|
|
ins, outs, src = C.ports(h["name"])
|
|
rows.append({"host": h["name"], "cells": cells,
|
|
"ports": {"in": ins, "out": outs, "source": src},
|
|
"completeness": h["completeness"]})
|
|
return {"rows": rows, "accessories": sorted(accs.values()),
|
|
"hosts": [h["name"] for h in host_list]}
|
|
|
|
|
|
def vendors(q, body):
|
|
return {"vendors": C.vendors()}
|
|
|
|
|
|
def port_objects(q, body):
|
|
name = (q.get("name") or q.get("device") or [""])[0]
|
|
return {"device": name, "ports": C.port_objects(name)}
|
|
|
|
|
|
serve({
|
|
"/devices": devices,
|
|
"/device": device,
|
|
"/ports": ports,
|
|
"/hosts": hosts,
|
|
"/matrix": matrix,
|
|
"/vendors": vendors,
|
|
"/port-objects": port_objects,
|
|
}, int(sys.argv[1]) if len(sys.argv) > 1 else 8101, "catalog")
|