[PRISM] Add read-only prism_* models API
This commit is contained in:
1 parent
f243e356eb
commit
8b634401b9
1 file changed
+26
@@ -1697,6 +1697,30 @@ def prism_summary():
|
||||
return {"ok": False, "error": str(e)[:200]}
|
||||
|
||||
|
||||
|
||||
def prism_models(limit=300):
|
||||
try:
|
||||
pw = open("/root/.pg-superuser-pass", encoding="utf-8").read().strip()
|
||||
env = dict(os.environ)
|
||||
env["PGPASSWORD"] = pw
|
||||
lim = max(1, min(int(limit), 500))
|
||||
sql = (
|
||||
"select coalesce(json_agg(row_to_json(t)),'[]'::json)::text from ("
|
||||
"select m.id,m.vendor,m.name,m.role,m.domain,m.status,m.quality_status,"
|
||||
"(m.quality->>'is_product')::boolean as is_product,"
|
||||
"(select count(*) from prism.specs sp where sp.model_id=m.id) as specs,"
|
||||
"(select count(*) from prism.evidence ev where ev.model_id=m.id) as evidence "
|
||||
"from prism.models m order by m.vendor,m.name limit %d) t" % lim
|
||||
)
|
||||
out = subprocess.check_output(
|
||||
["/usr/bin/psql", "-w", "-h", "192.168.1.151", "-U", "postgres",
|
||||
"-d", "rag", "-t", "-A", "-c", sql],
|
||||
env=env, text=True, timeout=15)
|
||||
return json.loads(out.strip())
|
||||
except Exception as e:
|
||||
return {"ok": False, "error": str(e)[:200]}
|
||||
|
||||
|
||||
class H(BaseHTTPRequestHandler):
|
||||
def _out(self, obj, code=200, ctype="application/json; charset=utf-8"):
|
||||
body = obj if isinstance(obj, bytes) else json.dumps(obj, ensure_ascii=False).encode()
|
||||
@@ -1723,6 +1747,8 @@ class H(BaseHTTPRequestHandler):
|
||||
return self._out(open(f, encoding="utf-8").read().encode(), ctype="text/html; charset=utf-8")
|
||||
if p in ("/health", "/api/health"):
|
||||
return self._out({"ok": True, "service": "combine-web", "devices": len(D.devices)})
|
||||
if p == "/api/prism/models":
|
||||
return self._out(prism_models(q.get("limit", ["300"])[0]))
|
||||
if p == "/api/prism/summary":
|
||||
return self._out(prism_summary())
|
||||
if p == "/api/stats":
|
||||
|
||||
Reference in new issue
Block a user