"""`findata` operator CLI.""" from __future__ import annotations import json from pathlib import Path import typer app = typer.Typer(no_args_is_help=True, help="FinData OS operator CLI") @app.callback() def _main() -> None: import atexit from findata_core.db import close_pool atexit.register(close_pool) ingest_app = typer.Typer(no_args_is_help=True) app.add_typer(ingest_app, name="ingest") @app.command() def migrate() -> None: """Apply database migrations.""" from findata_core.db import migrate as _m applied = _m() typer.echo(f"applied: {applied or 'nothing'}") @app.command() def discover(source: str = "all") -> None: """Sanitized live probe of vendor capabilities (no bulk download).""" out = {} if source in ("all", "firstrate"): from findata_market_firstrate import FirstRateMarketConnector out["firstrate"] = FirstRateMarketConnector().discover() if source in ("all", "firstrate_fundamentals"): from findata_fundamentals import FirstRateFundamentalsConnector out["firstrate_fundamentals"] = FirstRateFundamentalsConnector().discover() if source in ("all", "sec_edgar"): from findata_fundamentals import SecEdgarConnector out["sec_edgar"] = SecEdgarConnector().status() typer.echo(json.dumps(out, indent=2, default=str)) @app.command() def worker(once: bool = False, no_scheduler: bool = False, kinds: str | None = None, role: str = "primary") -> None: """Run an ingest worker: role=primary (lake lock + scheduler) or role=bulk (parallel backfill/options/fundamentals).""" from findata_ingest.worker import main main(once=once, kinds=kinds.split(",") if kinds else None, with_scheduler=not no_scheduler, role=role) @ingest_app.command("plan-all") def ingest_plan_all(types: str | None = None, no_options: bool = False, no_delisted: bool = False) -> None: """Show the complete vendor download plan (counts per dataset), without enqueuing anything.""" from collections import Counter from findata_market_firstrate import build_full_plan plan = build_full_plan(types.split(",") if types else None, include_options=not no_options, include_delisted=not no_delisted) typer.echo(f"{len(plan)} jobs") for ds, n in sorted(Counter(j["dataset_id"] for j in plan).items()): typer.echo(f" {ds:<48} {n}") @ingest_app.command("backfill-all") def ingest_backfill_all(types: str | None = None, no_options: bool = False, no_delisted: bool = False, yes: bool = False) -> None: """Enqueue the complete vendor download (every licensed archive). Needs the cold lake root (external SSD).""" from findata_core.config import get_settings from findata_market_firstrate import build_full_plan from findata_ingest import queue from findata_ingest.lake import Lake lake = Lake() s = get_settings() free_hot = lake.free_gb() free_cold = lake.free_gb(lake.cold) if lake.cold else None typer.echo(f"hot root {lake.root}: {free_hot:.0f} GB free ยท cold root {lake.cold}: {free_cold if free_cold is None else round(free_cold)} GB free") if not lake.cold: typer.echo("refusing: FINDATA_LAKE_COLD is not configured (the full catalog needs the external SSD)") raise typer.Exit(2) if free_cold < 1200 or free_hot < 300: typer.echo("refusing: need >= 1200 GB free on the cold root and >= 300 GB on the hot root for the full catalog") raise typer.Exit(2) plan = build_full_plan(types.split(",") if types else None, include_options=not no_options, include_delisted=not no_delisted) if not yes and not typer.confirm(f"enqueue {len(plan)} jobs?"): raise typer.Exit(1) n = 0 for j in plan: if queue.enqueue(j["kind"], j["dataset_id"], j["params"], j["priority"], dedupe_key=j["dedupe_key"], created_by="cli:backfill-all", max_attempts=4): n += 1 typer.echo(f"enqueued {n} new jobs ({len(plan) - n} already queued/running); bulk workers configured: {s.bulk_workers}") @ingest_app.command("options") def ingest_options(year: int | None = None, quarter: str | None = None, month: bool = False) -> None: from findata_ingest import queue params = {"period": "month"} if month else {"year": year, "quarter": quarter} typer.echo(queue.enqueue("options", "options.eod", params, priority=55, created_by="cli")) @ingest_app.command("metadata") def ingest_metadata(types: str = "stock,etf") -> None: from findata_ingest import queue typer.echo(queue.enqueue("metadata", None, {"types": types.split(",")}, priority=10, created_by="cli")) @ingest_app.command("incremental") def ingest_incremental(type_: str = typer.Option("stock", "--type"), period: str = "day", timeframe: str = "1min", adjustment: str = "UNADJUSTED", force: bool = False) -> None: from findata_ingest import queue from findata_ingest.tasks.common import DATASET_ASSET typer.echo(queue.enqueue("incremental", DATASET_ASSET[type_], {"type": type_, "period": period, "timeframe": timeframe, "adjustment": adjustment, "force": force}, priority=20, created_by="cli")) @ingest_app.command("backfill") def ingest_backfill(ticker_range: str, type_: str = typer.Option("stock", "--type"), timeframe: str = "1min", adjustment: str = "UNADJUSTED", symbols: str | None = None, max_symbols: int = 0) -> None: """Bounded backfill of one ticker_range letter (full archive). Disk budget enforced by the lake.""" from findata_ingest import queue from findata_ingest.tasks.common import DATASET_ASSET for letter in ticker_range.upper(): jid = queue.enqueue("backfill", DATASET_ASSET[type_], {"type": type_, "ticker_range": letter, "timeframe": timeframe, "adjustment": adjustment, "symbols": symbols.split(",") if symbols else None, "max_symbols": max_symbols}, priority=35, created_by="cli", dedupe_key=f"backfill:{type_}:{letter}:{timeframe}:{adjustment}") typer.echo(f"{letter}: {jid or 'already queued'}") @ingest_app.command("fundamentals") def ingest_fundamentals(tickers: str | None = typer.Argument(None), all_active: bool = False) -> None: """Fundamentals for a ticker list, or --all-active for every active US stock (checkpointed per ticker).""" from findata_ingest import queue params = {"all_active": True} if all_active else {"tickers": (tickers or "").split(",")} typer.echo(queue.enqueue("fundamentals", "fundamentals.financials", params, priority=58, created_by="cli", dedupe_key="fundamentals:all_active" if all_active else None)) @ingest_app.command("derive") def ingest_derive(dates: str | None = None, source: str = "market_bars.stock.1m.raw") -> None: from findata_ingest import queue typer.echo(queue.enqueue("derive", source.replace(".1m.", ".5m."), {"source_dataset": source, "target_interval": "5m", "dates": dates.split(",") if dates else []}, created_by="cli")) @ingest_app.command("compaction") def ingest_compaction(dataset: str = "market_bars.stock.1m.raw", min_age_days: int = 35) -> None: from findata_ingest import queue typer.echo(queue.enqueue("compaction", dataset, {"min_age_days": min_age_days}, priority=60, created_by="cli")) @ingest_app.command("jobs") def ingest_jobs(limit: int = 20) -> None: from findata_core.db import fetch_all for r in fetch_all("SELECT id, kind, dataset_id, status, attempts, created_at, finished_at, left(last_error, 80) err FROM ingest_jobs ORDER BY created_at DESC LIMIT %s", (limit,)): typer.echo(f"{str(r['id'])[:8]} {r['kind']:<12} {r['dataset_id']!s:<28} {r['status']:<9} a={r['attempts']} {r['created_at']:%m-%d %H:%M} {r['err'] or ''}") @app.command() def backup(now: bool = True) -> None: from findata_ingest import queue typer.echo(queue.enqueue("backup", None, {}, priority=5, created_by="cli")) @app.command("restore-drill") def restore_drill(dest: Path = Path("/tmp/findata-restore-drill"), dataset: str = "market_bars.stock.1m.raw") -> None: from findata_core.config import get_settings from findata_ingest.tasks.backup import restore_drill as rd typer.echo(json.dumps(rd(get_settings().backup_target, dest, dataset), indent=2, default=str)) @app.command("create-owner") def create_owner(email: str, password: str | None = None) -> None: """Create (or reset the password of) the owner account; prints the TOTP provisioning URI.""" from findata_api.auth import create_owner as _co typer.echo(json.dumps(_co(email, password), indent=2)) @app.command("create-key") def create_key(email: str, name: str, scopes: str = "read:market,read:fundamentals,query,export") -> None: from findata_api.auth import create_api_key_for_email typer.echo(json.dumps(create_api_key_for_email(email, name, scopes.split(",")), indent=2)) @app.command() def manifest(dataset: str, version: int | None = None, out: Path | None = None) -> None: """Write the full file manifest of a dataset version (reconstructed from lake_files) as JSON.""" from findata_ingest.lake import Lake files = Lake().snapshot_files(dataset, version) doc = {"dataset_id": dataset, "version": version, "file_count": len(files), "rows": sum(f["rows"] for f in files), "files": files} text = json.dumps(doc, indent=1) if out: out.write_text(text) typer.echo(f"{out} ({len(files)} files)") else: typer.echo(text) @app.command() def status() -> None: from findata_core.db import fetch_all, fetch_one from findata_ingest.lake import Lake lake = Lake() u = fetch_one("SELECT coalesce(sum(bytes) FILTER (WHERE version_removed IS NULL),0) AS live FROM lake_files") r = fetch_one("SELECT coalesce(sum(bytes),0) AS raw FROM raw_archives WHERE path IS NOT NULL") cold = f" cold {lake.cold} free {lake.free_gb(lake.cold):.0f} GB" if lake.cold else "" typer.echo(f"lake {lake.root} free {lake.free_gb():.1f} GB curated {u['live']/1e9:.2f} GB raw {r['raw']/1e9:.2f} GB{cold}") for r in fetch_all("SELECT d.id, d.status, d.current_version, v.file_count, v.row_count, v.bytes FROM datasets d LEFT JOIN dataset_versions v ON v.dataset_id=d.id AND v.version=d.current_version ORDER BY d.id"): typer.echo(f" {r['id']:<36} {r['status']:<22} v{r['current_version']:<4} files={r['file_count'] or 0:<6} rows={r['row_count'] or 0:<12} {(r['bytes'] or 0)/1e6:.1f} MB") q = fetch_one("SELECT count(*) FILTER (WHERE status='queued') q, count(*) FILTER (WHERE status='running') r, count(*) FILTER (WHERE status='dead') d FROM ingest_jobs") typer.echo(f"jobs queued={q['q']} running={q['r']} dead={q['d']}") if __name__ == "__main__": app()