|
| 1 | +import hashlib |
| 2 | +import json |
1 | 3 | import os
|
2 | 4 | import subprocess
|
3 | 5 | from datetime import datetime
|
@@ -33,66 +35,107 @@ def format_ip_display(ip: Optional[Union[str, List[str]]]) -> str:
|
33 | 35 | def list(
|
34 | 36 | limit: int = typer.Option(100, help="Maximum number of pods to list"),
|
35 | 37 | offset: int = typer.Option(0, help="Number of pods to skip"),
|
| 38 | + watch: bool = typer.Option( |
| 39 | + False, "--watch", "-w", help="Watch pods list in real-time" |
| 40 | + ), |
36 | 41 | ) -> None:
|
37 | 42 | """List your running pods"""
|
38 | 43 | try:
|
39 | 44 | # Create API clients
|
40 | 45 | base_client = APIClient()
|
41 | 46 | pods_client = PodsClient(base_client)
|
42 | 47 |
|
43 |
| - # Get pods list |
44 |
| - pods_list = pods_client.list(offset=offset, limit=limit) |
| 48 | + last_pods_hash = None |
45 | 49 |
|
46 |
| - # Create display table |
47 |
| - table = Table( |
48 |
| - title=f"Compute Pods (Total: {pods_list.total_count})", show_lines=True |
49 |
| - ) |
50 |
| - table.add_column("ID", style="cyan", no_wrap=True) |
51 |
| - table.add_column("Name", style="blue") |
52 |
| - table.add_column("GPU", style="green") |
53 |
| - table.add_column("Status", style="yellow") |
54 |
| - table.add_column("Created", style="blue") |
55 |
| - |
56 |
| - # Add rows for each pod |
57 |
| - for pod in pods_list.data: |
58 |
| - # Format status with color |
59 |
| - display_status = pod.status |
60 |
| - if pod.status == "ACTIVE" and pod.installation_status != "FINISHED": |
61 |
| - display_status = "INSTALLING" |
62 |
| - |
63 |
| - status_color = { |
64 |
| - "ACTIVE": "green", |
65 |
| - "PENDING": "yellow", |
66 |
| - "ERROR": "red", |
67 |
| - "INSTALLING": "yellow", |
68 |
| - }.get(display_status, "white") |
69 |
| - |
70 |
| - # Format created time |
71 |
| - created_at = datetime.fromisoformat(pod.created_at.replace("Z", "+00:00")) |
72 |
| - created_str = created_at.strftime("%Y-%m-%d %H:%M:%S UTC") |
| 50 | + while True: |
| 51 | + # Get pods list |
| 52 | + pods_list = pods_client.list(offset=offset, limit=limit) |
73 | 53 |
|
74 |
| - table.add_row( |
75 |
| - pod.id, |
76 |
| - pod.name or "N/A", |
77 |
| - f"{pod.gpu_type} x{pod.gpu_count}", |
78 |
| - Text(display_status, style=status_color), |
79 |
| - created_str, |
80 |
| - ) |
| 54 | + current_pods_hash = hashlib.md5( |
| 55 | + json.dumps( |
| 56 | + [pod.model_dump() for pod in pods_list.data], sort_keys=True |
| 57 | + ).encode() |
| 58 | + ).hexdigest() |
81 | 59 |
|
82 |
| - console.print(table) |
83 |
| - console.print( |
84 |
| - "\n[blue]Use 'prime pods status <pod-id>' to see detailed information " |
85 |
| - "about a specific pod[/blue]" |
86 |
| - ) |
| 60 | + # Only update display if data changed or first run |
| 61 | + if current_pods_hash != last_pods_hash: |
| 62 | + # Clear screen if watching |
| 63 | + if watch: |
| 64 | + os.system("cls" if os.name == "nt" else "clear") |
87 | 65 |
|
88 |
| - # If there are more pods, show a message |
89 |
| - if pods_list.total_count > offset + limit: |
90 |
| - remaining = pods_list.total_count - (offset + limit) |
91 |
| - console.print( |
92 |
| - f"\n[yellow]Showing {limit} of {pods_list.total_count} pods. " |
93 |
| - f"Use --offset {offset + limit} to see the next " |
94 |
| - f"{min(limit, remaining)} pods.[/yellow]" |
95 |
| - ) |
| 66 | + # Create display table |
| 67 | + table = Table( |
| 68 | + title=f"Compute Pods (Total: {pods_list.total_count})", |
| 69 | + show_lines=True, |
| 70 | + ) |
| 71 | + table.add_column("ID", style="cyan", no_wrap=True) |
| 72 | + table.add_column("Name", style="blue") |
| 73 | + table.add_column("GPU", style="green") |
| 74 | + table.add_column("Status", style="yellow") |
| 75 | + table.add_column("Created", style="blue") |
| 76 | + |
| 77 | + # Add rows for each pod |
| 78 | + for pod in pods_list.data: |
| 79 | + # Format status with color |
| 80 | + display_status = pod.status |
| 81 | + if pod.status == "ACTIVE" and pod.installation_status != "FINISHED": |
| 82 | + display_status = "INSTALLING" |
| 83 | + |
| 84 | + status_color = { |
| 85 | + "ACTIVE": "green", |
| 86 | + "PENDING": "yellow", |
| 87 | + "ERROR": "red", |
| 88 | + "INSTALLING": "yellow", |
| 89 | + }.get(display_status, "white") |
| 90 | + |
| 91 | + # Format created time |
| 92 | + created_at = datetime.fromisoformat( |
| 93 | + pod.created_at.replace("Z", "+00:00") |
| 94 | + ) |
| 95 | + created_str = created_at.strftime("%Y-%m-%d %H:%M:%S UTC") |
| 96 | + |
| 97 | + table.add_row( |
| 98 | + pod.id, |
| 99 | + pod.name or "N/A", |
| 100 | + f"{pod.gpu_type} x{pod.gpu_count}", |
| 101 | + Text(display_status, style=status_color), |
| 102 | + created_str, |
| 103 | + ) |
| 104 | + |
| 105 | + console.print(table) |
| 106 | + |
| 107 | + # Update hash after displaying |
| 108 | + if not watch: |
| 109 | + console.print( |
| 110 | + "\n[blue]Use 'prime pods status <pod-id>' to " |
| 111 | + "see detailed information about a specific pod[/blue]" |
| 112 | + ) |
| 113 | + |
| 114 | + # If there are more pods, show a message |
| 115 | + if pods_list.total_count > offset + limit: |
| 116 | + remaining = pods_list.total_count - (offset + limit) |
| 117 | + console.print( |
| 118 | + f"\n[yellow]Showing {limit} of {pods_list.total_count} pods. " |
| 119 | + f"Use --offset {offset + limit} to see the next " |
| 120 | + f"{min(limit, remaining)} pods.[/yellow]" |
| 121 | + ) |
| 122 | + |
| 123 | + break |
| 124 | + else: |
| 125 | + # Only print the message when we're not repeating due to unchanged data |
| 126 | + if current_pods_hash != last_pods_hash or last_pods_hash is None: |
| 127 | + console.print("\n[dim]Press Ctrl+C to exit watch mode[/dim]") |
| 128 | + last_pods_hash = current_pods_hash |
| 129 | + try: |
| 130 | + # Wait before refreshing |
| 131 | + import time |
| 132 | + |
| 133 | + time.sleep(5) |
| 134 | + except KeyboardInterrupt: |
| 135 | + # Clear the progress dots on exit |
| 136 | + if current_pods_hash == last_pods_hash: |
| 137 | + console.print("\n") |
| 138 | + break |
96 | 139 |
|
97 | 140 | except APIError as e:
|
98 | 141 | console.print(f"[red]Error:[/red] {str(e)}")
|
|
0 commit comments