[PRISM] Add search and LLM metrics endpoints
This commit is contained in:
1 parent
d44f849d38
commit
1620b9af28
1 file changed
+54
@@ -1721,6 +1721,56 @@ def prism_models(limit=300):
|
||||
return {"ok": False, "error": str(e)[:200]}
|
||||
|
||||
|
||||
|
||||
def _prism_sql(sql):
|
||||
pw = open("/root/.pg-superuser-pass", encoding="utf-8").read().strip()
|
||||
env = dict(os.environ)
|
||||
env["PGPASSWORD"] = pw
|
||||
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())
|
||||
|
||||
|
||||
def prism_search(query, limit=50):
|
||||
try:
|
||||
q = str(query or "").strip()
|
||||
if len(q) < 2:
|
||||
return []
|
||||
qq = q.replace("'", "''")
|
||||
lim = max(1, min(int(limit), 100))
|
||||
sql = (
|
||||
"select coalesce(json_agg(row_to_json(t)),'[]'::json)::text from ("
|
||||
"select m.id,m.vendor,m.name,m.role,m.domain,m.quality_status,"
|
||||
"(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 where m.name ilike '%%%s%%' or m.vendor ilike '%%%s%%' "
|
||||
"or exists (select 1 from prism.sources s where s.model_id=m.id and (s.title ilike '%%%s%%' or s.url ilike '%%%s%%')) "
|
||||
"or exists (select 1 from prism.specs sp where sp.model_id=m.id and (sp.key ilike '%%%s%%' or coalesce(sp.value_text,'') ilike '%%%s%%')) "
|
||||
"order by m.vendor,m.name limit %d) t" % (qq, qq, qq, qq, qq, qq, lim)
|
||||
)
|
||||
return _prism_sql(sql)
|
||||
except Exception as e:
|
||||
return {"ok": False, "error": str(e)[:200]}
|
||||
|
||||
|
||||
def prism_llm_stats():
|
||||
try:
|
||||
sql = (
|
||||
"select json_build_object("
|
||||
"'runs',(select count(*) from prism.runs),"
|
||||
"'ok',(select count(*) from prism.runs where status='ok'),"
|
||||
"'errors',(select count(*) from prism.runs where status='error'),"
|
||||
"'extract_local7b',(select count(*) from prism.runs where kind='extract_local7b'),"
|
||||
"'extract_pc3b',(select count(*) from prism.runs where kind='extract_pc3b'),"
|
||||
"'last_run',(select max(finished_at)::text from prism.runs)"
|
||||
")::text"
|
||||
)
|
||||
return _prism_sql(sql)
|
||||
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()
|
||||
@@ -1747,6 +1797,10 @@ 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/search":
|
||||
return self._out(prism_search(q.get("q", [""])[0], q.get("limit", ["50"])[0]))
|
||||
if p == "/api/prism/llm":
|
||||
return self._out(prism_llm_stats())
|
||||
if p == "/api/prism/models":
|
||||
return self._out(prism_models(q.get("limit", ["300"])[0]))
|
||||
if p == "/api/prism/summary":
|
||||
|
||||
Reference in new issue
Block a user