Spaces:
Sleeping
Sleeping
Download src/utils/supabase_tracker.py from yashpixl09/IP-Safe: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/spaces/yashpixl09/IP-Safe/resolve/main/src/utils/supabase_tracker.py
- Command line
-
hf download hf://spaces/yashpixl09/IP-Safe/src/utils/supabase_tracker.py
-
curl -L -o supabase_tracker.py https://huggingface.co/spaces/yashpixl09/IP-Safe/resolve/main/src/utils/supabase_tracker.py
3.71 kB
| """ | |
| Optional Supabase run tracker. | |
| Tracks pipeline runs in a Supabase `runs` table for persistent history across sessions. | |
| All methods are silent no-ops when SUPABASE_URL / SUPABASE_KEY are not set. | |
| Required Supabase schema (run once in SQL editor): | |
| create table runs ( | |
| id uuid primary key default gen_random_uuid(), | |
| pipeline text not null, | |
| source_file text, | |
| status text default 'running', | |
| total_items int, | |
| completed_items int default 0, | |
| results_sheet_url text, | |
| cost_usd numeric(10,4), | |
| started_at timestamptz default now(), | |
| completed_at timestamptz, | |
| metadata jsonb | |
| ); | |
| """ | |
| import os | |
| from typing import Optional | |
| from loguru import logger | |
| def _get_client(): | |
| """Return a Supabase client if credentials are configured, else None.""" | |
| url = os.getenv("SUPABASE_URL") | |
| key = os.getenv("SUPABASE_KEY") | |
| if not url or not key: | |
| return None | |
| try: | |
| from supabase import create_client | |
| return create_client(url, key) | |
| except ImportError: | |
| logger.warning( | |
| "supabase package not installed. Run: pip install supabase>=2.0.0" | |
| ) | |
| return None | |
| except Exception as e: | |
| logger.warning(f"Could not connect to Supabase: {e}") | |
| return None | |
| class RunTracker: | |
| """ | |
| Tracks a single pipeline run in Supabase. | |
| Usage: | |
| tracker = RunTracker("ip_check", source_file="comic.pdf") | |
| tracker.start(total_items=62) | |
| tracker.update(10) | |
| tracker.finish("completed", results_sheet_url="https://...", cost_usd=4.20) | |
| """ | |
| def __init__(self, pipeline: str, source_file: str = None): | |
| self._client = _get_client() | |
| self._run_id: Optional[str] = None | |
| self.pipeline = pipeline | |
| self.source_file = source_file | |
| def start(self, total_items: int) -> Optional[str]: | |
| """Insert a new run record. Returns run_id or None.""" | |
| if not self._client: | |
| return None | |
| try: | |
| res = ( | |
| self._client.table("runs") | |
| .insert( | |
| { | |
| "pipeline": self.pipeline, | |
| "source_file": self.source_file, | |
| "total_items": total_items, | |
| "status": "running", | |
| } | |
| ) | |
| .execute() | |
| ) | |
| self._run_id = res.data[0]["id"] | |
| logger.info(f"Supabase run started: {self._run_id}") | |
| return self._run_id | |
| except Exception as e: | |
| logger.warning(f"Supabase run tracking unavailable: {e}") | |
| return None | |
| def update(self, completed: int, **extra): | |
| """Update completed_items and any extra fields.""" | |
| if not self._client or not self._run_id: | |
| return | |
| try: | |
| self._client.table("runs").update( | |
| {"completed_items": completed, **extra} | |
| ).eq("id", self._run_id).execute() | |
| except Exception: | |
| pass | |
| def finish(self, status: str = "completed", **extra): | |
| """Mark run as finished with final status and any extra fields.""" | |
| if not self._client or not self._run_id: | |
| return | |
| try: | |
| from datetime import datetime, timezone | |
| self._client.table("runs").update( | |
| { | |
| "status": status, | |
| "completed_at": datetime.now(timezone.utc).isoformat(), | |
| **extra, | |
| } | |
| ).eq("id", self._run_id).execute() | |
| logger.info(f"Supabase run finished: {self._run_id} [{status}]") | |
| except Exception: | |
| pass | |