douyinWEB/scripts/_stash_audit.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

82 lines
2.5 KiB
Python

import paramiko
HOST = "107.173.30.245"
USER = "root"
PASSWORD = "Xiao4669805"
SERVER_PY = r"""
import sqlite3
import subprocess
import json
import os
REPO = "/opt/douyinWEB"
DB_REL = "backend/data/monitor.db"
DB_LIVE = "/opt/douyinWEB/backend/data/monitor.db"
TABLES = ["blacklist", "rooms", "sessions", "events", "channel_groups", "shorts_channels", "shorts_jobs"]
def table_counts(db_file: str) -> dict:
conn = sqlite3.connect(db_file)
out = {}
for t in TABLES:
exists = conn.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?", (t,)).fetchone()
if not exists:
out[t] = None
continue
out[t] = int(conn.execute("SELECT count(*) FROM " + t).fetchone()[0])
conn.close()
return out
def get_stashes() -> list[str]:
p = subprocess.run(["git", "-C", REPO, "stash", "list"], capture_output=True, text=True)
lines = [l.strip() for l in (p.stdout or "").splitlines() if l.strip()]
stashes = []
for l in lines:
stashes.append(l.split(":")[0])
return stashes
stashes = get_stashes()
print("=== git stash list ===")
print("\\n".join([f"{s}" for s in stashes]) or "(empty)")
print("\\n=== stash monitor.db counts ===")
for s in stashes[:10]:
tmp = "/tmp/" + s.replace("{","_").replace("}","_") + "_monitor.db"
try:
subprocess.run(["bash", "-lc", f"cd {REPO} && rm -f {tmp} && git show '{s}:{DB_REL}' > {tmp} 2>/dev/null || true"], capture_output=True, text=True)
if os.path.exists(tmp) and os.path.getsize(tmp) > 0:
print(f"{s}: " + json.dumps(table_counts(tmp), ensure_ascii=False))
else:
print(f"{s}: (missing monitor.db in stash)")
except Exception as e:
print(f"{s}: ERR {e}")
print("\\n=== server live counts ===")
print(json.dumps(table_counts(DB_LIVE), ensure_ascii=False))
"""
def main() -> None:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
with sftp.open("/tmp/_stash_audit_server.py", "w") as f:
f.write(SERVER_PY)
sftp.close()
_stdin, stdout, stderr = c.exec_command("python3 /tmp/_stash_audit_server.py 2>&1", timeout=120)
out = stdout.read().decode("utf-8", "replace").strip()
err = stderr.read().decode("utf-8", "replace").strip()
if err:
out += "\nERR: " + err
print(out)
c.close()
if __name__ == "__main__":
main()