update(app): refactor commands
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
import click
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from rich.progress import Progress
|
||||
import requests
|
||||
import click
|
||||
from datetime import datetime, timezone
|
||||
|
||||
# Define constants
|
||||
BACKEND_BASE_URL = "http://localhost:8000"
|
||||
THINK_K8S_URL = f"{BACKEND_BASE_URL}/think/k8s"
|
||||
METAL_NODES_URL = f"{BACKEND_BASE_URL}/metal/data"
|
||||
VM_URL = f"{BACKEND_BASE_URL}/vm/data"
|
||||
K8S_URL = f"{BACKEND_BASE_URL}/k8s/data"
|
||||
|
||||
console = Console()
|
||||
|
||||
# Helper function to set backend URLs
|
||||
def get_backend_urls(base_url):
|
||||
return {
|
||||
"THINK_K8S_URL": f"{base_url}/think/k8s",
|
||||
"METAL_NODES_URL": f"{base_url}/metal/data",
|
||||
"VM_URL": f"{base_url}/vm/data",
|
||||
"K8S_URL": f"{base_url}/k8s/data",
|
||||
}
|
||||
|
||||
# Helper functions for formatting
|
||||
def calculate_time_on_duty_hours(hours):
|
||||
if hours < 1:
|
||||
@@ -29,7 +31,7 @@ def calculate_time_on_duty_hours(hours):
|
||||
return f"{int(days)} days"
|
||||
|
||||
# Fetch and display metal nodes
|
||||
def display_metal_nodes():
|
||||
def display_metal_nodes(metal_nodes_url):
|
||||
table = Table(title="🖥️ Metal Nodes", style="bold green")
|
||||
table.add_column("ID", justify="right", style="cyan")
|
||||
table.add_column("Name", style="magenta")
|
||||
@@ -41,7 +43,7 @@ def display_metal_nodes():
|
||||
table.add_column("Time on Duty", justify="right", style="magenta")
|
||||
|
||||
try:
|
||||
response = requests.get(METAL_NODES_URL)
|
||||
response = requests.get(metal_nodes_url)
|
||||
if response.status_code == 200:
|
||||
metal_nodes = response.json().get("metal_nodes", [])
|
||||
for node in metal_nodes:
|
||||
@@ -64,7 +66,7 @@ def display_metal_nodes():
|
||||
console.print(table)
|
||||
|
||||
# Fetch and display virtual machines
|
||||
def display_virtual_machines():
|
||||
def display_virtual_machines(vm_url):
|
||||
table = Table(title="💻 Virtual Machines", style="bold blue")
|
||||
table.add_column("ID", justify="right", style="cyan")
|
||||
table.add_column("Name", style="magenta")
|
||||
@@ -75,7 +77,7 @@ def display_virtual_machines():
|
||||
table.add_column("Type", style="green")
|
||||
|
||||
try:
|
||||
response = requests.get(VM_URL)
|
||||
response = requests.get(vm_url)
|
||||
if response.status_code == 200:
|
||||
virtual_machines = response.json().get("virtual_machines", [])
|
||||
for vm in virtual_machines:
|
||||
@@ -96,7 +98,7 @@ def display_virtual_machines():
|
||||
console.print(table)
|
||||
|
||||
# Fetch and display Kubernetes nodes
|
||||
def display_kubernetes_nodes():
|
||||
def display_kubernetes_nodes(k8s_url):
|
||||
table = Table(title="📦 Kubernetes Nodes", style="bold yellow")
|
||||
table.add_column("Node Name", style="white")
|
||||
table.add_column("CPU", justify="right", style="yellow")
|
||||
@@ -107,7 +109,7 @@ def display_kubernetes_nodes():
|
||||
table.add_column("Time on Duty", justify="right", style="magenta")
|
||||
|
||||
try:
|
||||
response = requests.get(K8S_URL)
|
||||
response = requests.get(k8s_url)
|
||||
if response.status_code == 200:
|
||||
nodes = response.json().get("nodes", [])
|
||||
for node in nodes:
|
||||
@@ -128,7 +130,7 @@ def display_kubernetes_nodes():
|
||||
console.print(table)
|
||||
|
||||
# Fetch and display AI summary
|
||||
def display_ai_summary():
|
||||
def display_ai_summary(think_k8s_url):
|
||||
with Progress() as progress:
|
||||
task = progress.add_task("[cyan]Fetching AI Summary...", total=100)
|
||||
|
||||
@@ -137,7 +139,7 @@ def display_ai_summary():
|
||||
progress.update(task, advance=10)
|
||||
import time; time.sleep(0.1)
|
||||
|
||||
response = requests.get(THINK_K8S_URL)
|
||||
response = requests.get(think_k8s_url)
|
||||
progress.update(task, completed=100)
|
||||
|
||||
if response.status_code == 200:
|
||||
@@ -153,15 +155,19 @@ def display_ai_summary():
|
||||
# Click command for visual dashboard
|
||||
@click.command()
|
||||
@click.option('--summary', is_flag=True, help="Include AI summary in the dashboard.")
|
||||
def visual_dashboard(summary):
|
||||
@click.pass_context
|
||||
def visual_dashboard(ctx, summary):
|
||||
"""
|
||||
Displays the visual dashboard with Metal Nodes, Virtual Machines,
|
||||
Kubernetes Nodes, and optionally AI Summary.
|
||||
"""
|
||||
base_url = ctx.obj["BASE_URL"]
|
||||
urls = get_backend_urls(base_url)
|
||||
|
||||
console.print("✨ [bold green]Welcome to the Metal Check Dashboard![/bold green] ✨\n")
|
||||
display_metal_nodes()
|
||||
display_virtual_machines()
|
||||
display_kubernetes_nodes()
|
||||
display_metal_nodes(urls["METAL_NODES_URL"])
|
||||
display_virtual_machines(urls["VM_URL"])
|
||||
display_kubernetes_nodes(urls["K8S_URL"])
|
||||
|
||||
if summary:
|
||||
display_ai_summary()
|
||||
display_ai_summary(urls["THINK_K8S_URL"])
|
||||
|
||||
Reference in New Issue
Block a user