|
| 1 | +"""kalibr status - Show Kalibr SDK and service health.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import httpx |
| 6 | +from rich.console import Console |
| 7 | +from rich.rule import Rule |
| 8 | + |
| 9 | +console = Console() |
| 10 | + |
| 11 | + |
| 12 | +def _mask(value: str, visible: int = 10) -> str: |
| 13 | + """Mask a credential, showing only the first `visible` characters.""" |
| 14 | + if len(value) <= visible: |
| 15 | + return value |
| 16 | + return value[:visible] + "..." |
| 17 | + |
| 18 | + |
| 19 | +def _check_service(url: str, headers: dict) -> str: |
| 20 | + """ |
| 21 | + Hit a /health endpoint and return a status string. |
| 22 | + Returns a Rich-formatted string. |
| 23 | + """ |
| 24 | + try: |
| 25 | + r = httpx.get(url, headers=headers, timeout=8) |
| 26 | + if r.status_code == 200: |
| 27 | + data = r.json() |
| 28 | + parts = [] |
| 29 | + # Collect sub-service statuses from common health response shapes |
| 30 | + for key in ("clickhouse", "redis", "database"): |
| 31 | + val = data.get(key) or data.get("services", {}).get(key) |
| 32 | + if val: |
| 33 | + label = "connected" if str(val).lower() in ("ok", "connected", "healthy", "true") else str(val) |
| 34 | + parts.append(f"{key}: {label}") |
| 35 | + detail = f" ({', '.join(parts)})" if parts else "" |
| 36 | + return f"[green]✓ healthy{detail}[/green]" |
| 37 | + else: |
| 38 | + return f"[red]✗ unhealthy (HTTP {r.status_code})[/red]" |
| 39 | + except httpx.TimeoutException: |
| 40 | + return "[red]✗ timed out[/red]" |
| 41 | + except Exception as e: |
| 42 | + return f"[red]✗ unreachable ({e})[/red]" |
| 43 | + |
| 44 | + |
| 45 | +def status() -> None: |
| 46 | + """Show Kalibr SDK version, credentials, and live service health.""" |
| 47 | + from kalibr import __version__ |
| 48 | + |
| 49 | + api_key = os.environ.get("KALIBR_API_KEY", "") |
| 50 | + tenant_id = os.environ.get("KALIBR_TENANT_ID", "") |
| 51 | + base_url = os.environ.get("KALIBR_INTELLIGENCE_URL", "https://kalibr-intelligence.fly.dev") |
| 52 | + backend_url = "https://kalibr-backend.fly.dev" |
| 53 | + |
| 54 | + headers = {} |
| 55 | + if api_key: |
| 56 | + headers["X-API-Key"] = api_key |
| 57 | + if tenant_id: |
| 58 | + headers["X-Tenant-ID"] = tenant_id |
| 59 | + |
| 60 | + console.print() |
| 61 | + console.print("[bold]Kalibr Status[/bold]") |
| 62 | + console.print(Rule(style="dim")) |
| 63 | + |
| 64 | + # SDK version |
| 65 | + console.print(f" [dim]SDK version:[/dim] {__version__}") |
| 66 | + |
| 67 | + # Credentials |
| 68 | + if api_key: |
| 69 | + console.print(f" [dim]API key:[/dim] {_mask(api_key)} [green]✓ set[/green]") |
| 70 | + else: |
| 71 | + console.print(" [dim]API key:[/dim] [red]✗ not set[/red] (export KALIBR_API_KEY=...)") |
| 72 | + |
| 73 | + if tenant_id: |
| 74 | + console.print(f" [dim]Tenant ID:[/dim] {_mask(tenant_id)} [green]✓ set[/green]") |
| 75 | + else: |
| 76 | + console.print(" [dim]Tenant ID:[/dim] [red]✗ not set[/red] (export KALIBR_TENANT_ID=...)") |
| 77 | + |
| 78 | + # Service health |
| 79 | + console.print(f" [dim]Intelligence:[/dim] {_check_service(f'{base_url}/api/v1/intelligence/health', headers)}") |
| 80 | + console.print(f" [dim]Backend:[/dim] {_check_service(f'{backend_url}/api/health', headers)}") |
| 81 | + |
| 82 | + console.print(Rule(style="dim")) |
| 83 | + console.print() |
0 commit comments