#!/usr/bin/env python3 """Nightly Audit 360 refresh: collect new run, extend graph, preserve history, publish.""" import json import subprocess import sys import urllib.request from pathlib import Path import psycopg2 PASS = Path("/root/.pg-superuser-pass").read_text().strip() BASE = Path("/root/buryan-infra/ops/audit") conn = psycopg2.connect(host="192.168.1.151", dbname="rag", user="postgres", password=PASS, client_encoding="UTF8") cur = conn.cursor() def run(args): p = subprocess.run(args, capture_output=True, text=True, timeout=300) print(p.stdout[-1000:], p.stderr[-500:], flush=True) if p.returncode != 0: raise RuntimeError("command failed: %s" % " ".join(args)) cur.execute("select max(r.id) from audit.runs r where exists(select 1 from audit.capabilities c where c.run_id=r.id)") prev = cur.fetchone()[0] run([sys.executable, str(BASE / "audit360_collect.py")]) cur.execute("select max(id) from audit.runs") new = cur.fetchone()[0] print("refresh run", prev, "->", new, flush=True) run([sys.executable, str(BASE / "audit360_graph.py"), str(new)]) run([sys.executable, str(BASE / "audit360_api_graph.py"), str(new)]) # Copy capability/opportunity matrix and preserve finding ownership/history by title. cur.execute("""insert into audit.capabilities(run_id,key,title,services,maturity,automation,leverage,criticality,evidence,gaps) select %s,key,title,services,maturity,automation,leverage,criticality,evidence,gaps from audit.capabilities where run_id=%s on conflict(run_id,key) do nothing""", (new, prev)) cur.execute("""insert into audit.opportunities(run_id,title,capability,impact,effort,risk,hypothesis,first_step,evidence,source) select %s,title,capability,impact,effort,risk,hypothesis,first_step,evidence,source from audit.opportunities where run_id=%s on conflict(run_id,title) do nothing""", (new, prev)) cur.execute("""update audit.findings n set owner=o.owner,status=o.status,gitea_issue_url=o.gitea_issue_url, gitea_issue_number=o.gitea_issue_number,gitea_assignee=o.gitea_assignee,updated_at=now() from audit.findings o where n.run_id=%s and o.run_id=%s and n.title=o.title""", (new, prev)) # Verify experiments whose finding became fixed in this run. cur.execute(""" update audit.experiments e set verification='confirmed',verify_run_id=%s,verified_at=now() from audit.decisions d join audit.findings old on old.id=d.finding_id join audit.findings cur on cur.run_id=%s and cur.title=old.title where e.decision_id=d.id and old.status<>'fixed' and cur.status='fixed' and e.verification='pending' """, (new, new)) cur.execute(""" update audit.experiments e set verification='not_confirmed',verify_run_id=%s,verified_at=now() from audit.decisions d join audit.findings old on old.id=d.finding_id join audit.findings cur on cur.run_id=%s and cur.title=old.title where e.decision_id=d.id and cur.status<>'fixed' and e.status='done' and e.verification='pending' """, (new, new)) conn.commit() cur.close(); conn.close() run([sys.executable, str(BASE / "audit360_report.py"), str(new)]) # Publish through the standalone service (it picks latest run with capabilities). body = json.dumps({}).encode() req = urllib.request.Request("http://127.0.0.1:9160/api/gitea/publish", data=body, headers={"Content-Type": "application/json"}, method="POST") with urllib.request.urlopen(req, timeout=60) as r: print("publish", r.read().decode()[:1000], flush=True)