-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcardops_cli.py
More file actions
367 lines (319 loc) · 11.8 KB
/
Copy pathcardops_cli.py
File metadata and controls
367 lines (319 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
"""CardOpsAI operational CLI (works with upstream SQL-native OS)."""
from __future__ import annotations
import argparse
import json
import os
import sys
from typing import Any
import psycopg2
from psycopg2.extras import RealDictCursor
DSN = os.environ.get(
"CARDOPS_DSN",
"postgresql://cardops:cardops_secret@localhost:5432/cardops_db",
)
GREEN = "\033[92m"
RED = "\033[91m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
RESET = "\033[0m"
def connect():
return psycopg2.connect(DSN)
def ok(msg: str) -> None:
print(f"{GREEN}✅ {msg}{RESET}")
def err(msg: str) -> None:
print(f"{RED}❌ {msg}{RESET}", file=sys.stderr)
def warn(msg: str) -> None:
print(f"{YELLOW}⚠️ {msg}{RESET}")
def table(headers: list[str], rows: list[list[Any]]) -> None:
widths = [len(h) for h in headers]
str_rows = []
for row in rows:
cells = ["" if c is None else str(c) for c in row]
str_rows.append(cells)
for i, c in enumerate(cells):
widths[i] = max(widths[i], len(c))
print(" | ".join(h.ljust(widths[i]) for i, h in enumerate(headers)))
print("-+-".join("-" * w for w in widths))
for cells in str_rows:
print(" | ".join(cells[i].ljust(widths[i]) for i in range(len(headers))))
def resolve_tenant(cur, tenant_id: int | None) -> int:
if tenant_id is not None:
cur.execute("SELECT set_tenant(%s)", (tenant_id,))
return tenant_id
cur.execute("ALTER TABLE tenants DISABLE ROW LEVEL SECURITY")
cur.execute(
"SELECT id FROM tenants WHERE status = 'active' ORDER BY id LIMIT 1"
)
row = cur.fetchone()
cur.execute("ALTER TABLE tenants ENABLE ROW LEVEL SECURITY")
if not row:
raise RuntimeError("No active tenant found. Run seed first.")
tid = row[0] if not isinstance(row, dict) else row["id"]
cur.execute("SELECT set_tenant(%s)", (tid,))
return tid
def cmd_status(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor(cursor_factory=RealDictCursor) as cur:
tid = resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute(
"SELECT count(*) AS n FROM decision_queue WHERE upper(status)='PENDING'"
)
queue_depth = cur.fetchone()["n"]
cur.execute(
"SELECT count(*) AS n FROM risk_rules WHERE COALESCE(is_active, active, TRUE)"
)
active_rules = cur.fetchone()["n"]
cur.execute(
"""
SELECT snapshot_id::text AS id, rules_hash, created_at
FROM config_snapshots
ORDER BY created_at DESC LIMIT 1
"""
)
snap = cur.fetchone()
cur.execute("SELECT count(*) AS n FROM transactions")
txn_count = cur.fetchone()["n"]
conn.commit()
ok(f"CardOpsAI healthy — tenant {tid}")
table(
["metric", "value"],
[
["tenant_id", tid],
["queue_depth", queue_depth],
["active_rules", active_rules],
["transactions", txn_count],
["active_snapshot", snap["id"] if snap else "-"],
["rules_hash", (snap["rules_hash"][:16] + "…") if snap else "-"],
],
)
return 0
def cmd_audit(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
if args.tenant_id:
cur.execute("SELECT set_tenant(%s)", (args.tenant_id,))
cur.execute(
"SELECT verify_decision_ledger_integrity(%s)",
(args.tenant_id,),
)
raw = cur.fetchone()[0]
conn.commit()
result = raw if isinstance(raw, dict) else json.loads(raw)
if result.get("ok"):
ok(f"Ledger intact — checked {result.get('checked')} blocks")
else:
err(f"Tamper detected at id={result.get('first_broken_id')}")
print(json.dumps(result, indent=2))
return 0 if result.get("ok") else 2
def cmd_stress(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
tid = resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute(
"SELECT run_monte_carlo_stress(%s, %s, %s, %s)",
(args.iterations, args.horizon, 0.95, tid),
)
raw = cur.fetchone()[0]
conn.commit()
result = raw if isinstance(raw, dict) else json.loads(raw)
ok("Monte Carlo stress complete")
table(["metric", "value"], [[k, result[k]] for k in sorted(result.keys())])
return 0
def cmd_rings(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
tid = resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute(
"SELECT cluster_id, member_merchant, cluster_size FROM detect_fraud_rings(%s, %s)",
(tid, args.min_size),
)
rows = cur.fetchall()
conn.commit()
if not rows:
warn("No fraud rings detected")
return 0
ok(f"Detected {len(rows)} ring membership row(s)")
table(
["cluster_id", "merchant", "size"],
[[r[0], r[1], r[2]] for r in rows],
)
return 0
def cmd_simulate(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor(cursor_factory=RealDictCursor) as cur:
resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute(
"""
SELECT run_counterfactual(
%s, %s,
cardops_now() - INTERVAL '30 days',
cardops_now()
)
""",
(f"rule-{args.rule_id}", args.new_threshold),
)
run_id = cur.fetchone()["run_counterfactual"]
cur.execute("SELECT * FROM counterfactual_runs WHERE run_id = %s", (run_id,))
row = dict(cur.fetchone())
conn.commit()
ok(f"Counterfactual run {run_id}")
print(json.dumps(row, indent=2, default=str))
return 0
def cmd_process(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute("SELECT process_decision_queue(%s)", (args.batch_size,))
raw = cur.fetchone()[0]
conn.commit()
result = raw if isinstance(raw, dict) else json.loads(raw)
ok(f"Processed {result.get('processed')} queue item(s)")
table(["metric", "value"], [[k, v] for k, v in result.items()])
return 0
def cmd_snapshot(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute("SELECT create_config_snapshot()")
snap_id = cur.fetchone()[0]
cur.execute(
"SELECT rules_hash FROM config_snapshots WHERE snapshot_id = %s",
(snap_id,),
)
rules_hash = cur.fetchone()[0]
conn.commit()
ok(f"Snapshot {snap_id}")
print(f"{CYAN}rules_hash{RESET}: {rules_hash}")
return 0
def cmd_triage(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
tid = resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute(
"SELECT * FROM top_alerts_by_expected_loss(%s, %s)",
(tid, args.limit),
)
rows = cur.fetchall()
conn.commit()
if not rows:
warn("No triage alerts")
return 0
ok(f"Top {len(rows)} alerts by expected loss")
table(
["tx_id", "merchant", "amount", "score", "expected_loss", "band"],
[[r[0], r[1], r[2], r[3], r[4], r[5]] for r in rows],
)
return 0
def cmd_score(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute("SELECT score_transaction(%s)", (args.tx_id,))
raw = cur.fetchone()[0]
conn.commit()
result = raw if isinstance(raw, dict) else json.loads(raw)
ok(f"Scored transaction {args.tx_id}")
print(json.dumps(result, indent=2, default=str))
return 0
def cmd_shadow(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute(
"SELECT run_shadow_score(%s, %s)",
(args.tx_id, args.challenger_threshold),
)
raw = cur.fetchone()[0]
conn.commit()
result = raw if isinstance(raw, dict) else json.loads(raw)
if result.get("diverged"):
warn("Champion and challenger diverged")
else:
ok("Champion and challenger agree")
print(json.dumps(result, indent=2, default=str))
return 0
def cmd_health(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor(cursor_factory=RealDictCursor) as cur:
resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute("SELECT * FROM system_health_panel")
row = cur.fetchone()
conn.commit()
ok("System health panel")
table(["metric", "value"], [[k, row[k]] for k in row.keys()])
return 0
def cmd_velocity(args: argparse.Namespace) -> int:
with connect() as conn, conn.cursor() as cur:
tid = resolve_tenant(cur, args.tenant_id)
conn.commit()
cur.execute("SELECT refresh_velocity_features(%s)", (tid,))
n = cur.fetchone()[0]
cur.execute(
"""
SELECT entity_type, entity_id, tx_count_1h, tx_count_24h, amount_spike_ratio
FROM velocity_features
WHERE tenant_id = %s
ORDER BY tx_count_24h DESC
LIMIT %s
""",
(tid, args.limit),
)
rows = cur.fetchall()
conn.commit()
ok(f"Refreshed velocity features ({n} upserts)")
table(
["type", "entity", "1h", "24h", "spike"],
[[r[0], r[1], r[2], r[3], r[4]] for r in rows],
)
return 0
def build_parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(description="CardOpsAI Risk Operations CLI")
p.add_argument("--tenant-id", type=int, default=None)
sub = p.add_subparsers(dest="command", required=True)
sub.add_parser("status")
sub.add_parser("audit")
sub.add_parser("health", help="System health panel")
sp = sub.add_parser("stress")
sp.add_argument("--iterations", type=int, default=1000)
sp.add_argument("--horizon", type=int, default=30)
rp = sub.add_parser("rings")
rp.add_argument("--min-size", type=int, default=3)
sim = sub.add_parser("simulate")
sim.add_argument("--rule-id", type=int, required=True)
sim.add_argument("--new-threshold", type=float, required=True)
proc = sub.add_parser("process")
proc.add_argument("--batch-size", type=int, default=100)
sub.add_parser("snapshot")
tr = sub.add_parser("triage", help="Alerts ranked by expected loss")
tr.add_argument("--limit", type=int, default=25)
sc = sub.add_parser("score", help="Explainable transaction score")
sc.add_argument("--tx-id", type=int, required=True)
sh = sub.add_parser("shadow", help="Champion/challenger shadow compare")
sh.add_argument("--tx-id", type=int, required=True)
sh.add_argument("--challenger-threshold", type=float, default=35)
vel = sub.add_parser("velocity", help="Refresh velocity features")
vel.add_argument("--limit", type=int, default=20)
return p
def main(argv: list[str] | None = None) -> int:
args = build_parser().parse_args(argv)
handlers = {
"status": cmd_status,
"audit": cmd_audit,
"health": cmd_health,
"stress": cmd_stress,
"rings": cmd_rings,
"simulate": cmd_simulate,
"process": cmd_process,
"snapshot": cmd_snapshot,
"triage": cmd_triage,
"score": cmd_score,
"shadow": cmd_shadow,
"velocity": cmd_velocity,
}
try:
return handlers[args.command](args)
except Exception as exc: # noqa: BLE001
err(str(exc))
return 1
if __name__ == "__main__":
raise SystemExit(main())