|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from datetime import datetime |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | +from urllib.parse import urlencode |
| 8 | + |
| 9 | +import typer |
| 10 | +from rich.console import Console |
| 11 | +from rich.table import Table |
| 12 | + |
| 13 | +from ..async_typer import AsyncTyper |
| 14 | +from .client import initialize_client |
| 15 | +from .parameters import CONFIG_PARAM |
| 16 | +from .utils import catch_exception |
| 17 | + |
| 18 | +app = AsyncTyper() |
| 19 | +console = Console() |
| 20 | + |
| 21 | +EXPORT_PAGE_SIZE = 1000 |
| 22 | + |
| 23 | + |
| 24 | +@app.command(name="list") |
| 25 | +@catch_exception(console=console) |
| 26 | +async def list_snapshots( |
| 27 | + start_date: datetime | None = typer.Option( |
| 28 | + None, help="Start date filter (ISO 8601)", formats=["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S%z"] |
| 29 | + ), |
| 30 | + end_date: datetime | None = typer.Option( |
| 31 | + None, help="End date filter (ISO 8601)", formats=["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S%z"] |
| 32 | + ), |
| 33 | + limit: int = typer.Option(50, help="Maximum number of results"), |
| 34 | + _: str = CONFIG_PARAM, |
| 35 | +) -> None: |
| 36 | + """List telemetry snapshots with summary information.""" |
| 37 | + client = initialize_client() |
| 38 | + |
| 39 | + params: dict[str, str | int] = {"limit": limit} |
| 40 | + if start_date: |
| 41 | + params["start_date"] = start_date.isoformat() |
| 42 | + if end_date: |
| 43 | + params["end_date"] = end_date.isoformat() |
| 44 | + |
| 45 | + url = f"{client.address}/api/telemetry/snapshots?{urlencode(params)}" |
| 46 | + response = await client._get(url=url, timeout=client.default_timeout) |
| 47 | + response.raise_for_status() |
| 48 | + data = response.json() |
| 49 | + |
| 50 | + snapshots = data.get("snapshots", []) |
| 51 | + if not snapshots: |
| 52 | + console.print("No telemetry snapshots found.") |
| 53 | + return |
| 54 | + |
| 55 | + table = Table() |
| 56 | + table.add_column("Date") |
| 57 | + table.add_column("Version") |
| 58 | + table.add_column("Type") |
| 59 | + table.add_column("Deployment") |
| 60 | + table.add_column("Remote Status") |
| 61 | + |
| 62 | + for snap in snapshots: |
| 63 | + table.add_row( |
| 64 | + snap.get("created_at", ""), |
| 65 | + snap.get("infrahub_version", ""), |
| 66 | + snap.get("kind", ""), |
| 67 | + snap.get("deployment_id", ""), |
| 68 | + snap.get("remote_send_status", ""), |
| 69 | + ) |
| 70 | + |
| 71 | + console.print(table) |
| 72 | + console.print(f"Showing {len(snapshots)} of {data.get('count', len(snapshots))} total snapshots") |
| 73 | + |
| 74 | + |
| 75 | +@app.command(name="export") |
| 76 | +@catch_exception(console=console) |
| 77 | +async def export_snapshots( |
| 78 | + output: str = typer.Option("telemetry-export.json", help="Output file path"), |
| 79 | + start_date: datetime | None = typer.Option( |
| 80 | + None, help="Start date filter (ISO 8601)", formats=["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S%z"] |
| 81 | + ), |
| 82 | + end_date: datetime | None = typer.Option( |
| 83 | + None, help="End date filter (ISO 8601)", formats=["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S%z"] |
| 84 | + ), |
| 85 | + _: str = CONFIG_PARAM, |
| 86 | +) -> None: |
| 87 | + """Export telemetry snapshots to a JSON file. |
| 88 | +
|
| 89 | + Pages through the API automatically so that all matching snapshots are exported, |
| 90 | + not just the first page. |
| 91 | + """ |
| 92 | + client = initialize_client() |
| 93 | + |
| 94 | + base_params: dict[str, str | int] = {} |
| 95 | + if start_date: |
| 96 | + base_params["start_date"] = start_date.isoformat() |
| 97 | + if end_date: |
| 98 | + base_params["end_date"] = end_date.isoformat() |
| 99 | + |
| 100 | + snapshots: list[dict[str, Any]] = [] |
| 101 | + offset = 0 |
| 102 | + total: int | None = None |
| 103 | + |
| 104 | + while True: |
| 105 | + params: dict[str, str | int] = {**base_params, "limit": EXPORT_PAGE_SIZE, "offset": offset} |
| 106 | + url = f"{client.address}/api/telemetry/snapshots?{urlencode(params)}" |
| 107 | + response = await client._get(url=url, timeout=client.default_timeout) |
| 108 | + response.raise_for_status() |
| 109 | + data = response.json() |
| 110 | + |
| 111 | + page: list[dict[str, Any]] = data.get("snapshots", []) |
| 112 | + snapshots.extend(page) |
| 113 | + |
| 114 | + if total is None: |
| 115 | + total = int(data.get("count", len(page))) |
| 116 | + |
| 117 | + if len(page) < EXPORT_PAGE_SIZE or len(snapshots) >= total: |
| 118 | + break |
| 119 | + |
| 120 | + offset += EXPORT_PAGE_SIZE |
| 121 | + |
| 122 | + if not snapshots: |
| 123 | + console.print("No telemetry snapshots found.") |
| 124 | + raise typer.Exit(code=2) |
| 125 | + |
| 126 | + output_path = Path(output) |
| 127 | + output_path.write_text(json.dumps(snapshots, indent=2), encoding="utf-8") |
| 128 | + console.print(f"Exported {len(snapshots)} snapshots to {output_path}") |
0 commit comments