#!/bin/bash
export LC_ALL=C
python3 - <<'PY'
import sqlite3, json
c=sqlite3.connect('/root/.9router/db/data.sqlite'); c.row_factory=sqlite3.Row
cols=[r[1] for r in c.execute("pragma table_info(usageHistory)")]
print("cols:", cols[:12])
print("\n=== opencode usage (last 24h) ===")
q="""select * from usageHistory where provider='opencode' order by rowid desc limit 12"""
try:
    for r in c.execute(q):
        d=dict(r)
        keys=[k for k in ('timestamp','createdAt','provider','model','status','statusCode','error','latency') if k in d]
        print("  ", {k:str(d[k])[:60] for k in keys})
except Exception as e:
    print("  err", e)

print("\n=== any 429 records ===")
try:
    for r in c.execute("select * from usageHistory where cast(status as text) like '%429%' or cast(data as text) like '%FreeUsageLimit%' limit 5"):
        d=dict(r); print("  ", json.dumps({k:str(v)[:80] for k,v in d.items()}, ensure_ascii=False)[:300])
except Exception as e:
    print("  err", e)
PY
