|
| 1 | +""" |
| 2 | +Sync ALL local RL data (events + policy stats) to Supabase. |
| 3 | +
|
| 4 | +Expected layout: |
| 5 | +FileSense/ |
| 6 | +└─ scripts/ |
| 7 | + └─ logs/ |
| 8 | + ├─ rl_events.jsonl |
| 9 | + └─ rl_policy_stats.json |
| 10 | +""" |
| 11 | + |
| 12 | +import json |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | +from typing import Dict, Any |
| 16 | + |
| 17 | +# ------------------------------------------------------------------ |
| 18 | +# Resolve project root: FileSense/ |
| 19 | +# sync_all.py -> scripts/RL/ -> parents[2] |
| 20 | +# ------------------------------------------------------------------ |
| 21 | +ROOT = Path(__file__).resolve().parents[2] |
| 22 | + |
| 23 | +if str(ROOT) not in sys.path: |
| 24 | + sys.path.insert(0, str(ROOT)) |
| 25 | + |
| 26 | +# ------------------------------------------------------------------ |
| 27 | +# Local paths (EXPLICIT, NO MAGIC) |
| 28 | +# ------------------------------------------------------------------ |
| 29 | +LOGS_DIR = ROOT / "scripts" / "logs" |
| 30 | +EVENTS_FILE = LOGS_DIR / "rl_events.jsonl" |
| 31 | +POLICY_STATS_FILE = LOGS_DIR / "rl_policy_stats.json" |
| 32 | + |
| 33 | +# ------------------------------------------------------------------ |
| 34 | +# Supabase helpers (your existing code) |
| 35 | +# ------------------------------------------------------------------ |
| 36 | +from scripts.RL.rl_supabase import ( |
| 37 | + upload_events_batch, |
| 38 | + upload_policy_stats, |
| 39 | +) |
| 40 | + |
| 41 | +# ------------------------------------------------------------------ |
| 42 | +# Loaders |
| 43 | +# ------------------------------------------------------------------ |
| 44 | +def load_events(path: Path): |
| 45 | + events = [] |
| 46 | + if not path.exists(): |
| 47 | + return events |
| 48 | + |
| 49 | + with path.open("r", encoding="utf-8") as f: |
| 50 | + for line in f: |
| 51 | + try: |
| 52 | + events.append(json.loads(line)) |
| 53 | + except Exception: |
| 54 | + pass |
| 55 | + return events |
| 56 | + |
| 57 | + |
| 58 | +def load_policy_stats(path: Path) -> Dict[str, Any] | None: |
| 59 | + if not path.exists(): |
| 60 | + return None |
| 61 | + try: |
| 62 | + return json.loads(path.read_text(encoding="utf-8")) |
| 63 | + except Exception as e: |
| 64 | + print(f"[ERROR] Failed to parse policy stats: {e}") |
| 65 | + return None |
| 66 | + |
| 67 | + |
| 68 | +# ------------------------------------------------------------------ |
| 69 | +# Main |
| 70 | +# ------------------------------------------------------------------ |
| 71 | +def main(): |
| 72 | + print("\n[SYNC] FileSense → Supabase\n") |
| 73 | + |
| 74 | + # ---------------- events ---------------- |
| 75 | + events = load_events(EVENTS_FILE) |
| 76 | + |
| 77 | + if not events: |
| 78 | + print("[EVENTS] No local events found") |
| 79 | + else: |
| 80 | + uploaded, failed = upload_events_batch(events) |
| 81 | + print("[EVENTS]") |
| 82 | + print(f" Uploaded : {uploaded}") |
| 83 | + print(f" Failed : {failed}") |
| 84 | + |
| 85 | + # ---------------- policy stats ---------------- |
| 86 | + stats = load_policy_stats(POLICY_STATS_FILE) |
| 87 | + |
| 88 | + if not stats: |
| 89 | + print("\n[POLICY STATS] No local policy stats found") |
| 90 | + else: |
| 91 | + ok, err = upload_policy_stats(stats) |
| 92 | + print("\n[POLICY STATS]") |
| 93 | + if ok: |
| 94 | + print(" Uploaded latest snapshot") |
| 95 | + else: |
| 96 | + print(f" Upload failed: {err}") |
| 97 | + |
| 98 | + print("\n[SYNC] Done.\n") |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments