update(ui): add time on duty for metal

This commit is contained in:
Aleksandr Tcitlionok
2024-12-06 04:10:14 +00:00
parent 956ad10e1c
commit 3ef8b2a4a8

View File

@@ -27,6 +27,22 @@ def calculate_time_on_duty(creation_timestamp):
return f"{hours} hours" if hours > 1 else "1 hour" return f"{hours} hours" if hours > 1 else "1 hour"
return f"{delta.days} days" if delta.days > 1 else "1 day" return f"{delta.days} days" if delta.days > 1 else "1 day"
def calculate_time_on_duty_hours(hours):
"""
Convert hours into a human-readable time format (days, hours, or minutes).
"""
if hours < 1:
minutes = round(hours * 60)
return f"{minutes} minutes" if minutes > 1 else "less than a minute"
elif hours < 24:
return f"{round(hours)} hours" if hours > 1 else "1 hour"
else:
days = hours // 24
remaining_hours = hours % 24
if remaining_hours:
return f"{int(days)} days {int(remaining_hours)} hours"
return f"{int(days)} days"
def convert_cpu_to_cores(cpu): def convert_cpu_to_cores(cpu):
if "n" in cpu: if "n" in cpu:
return round(int(cpu.replace("n", "")) / 1e9, 4) return round(int(cpu.replace("n", "")) / 1e9, 4)
@@ -64,9 +80,11 @@ def display_metal_nodes():
table.add_column("CPU", justify="right", style="yellow") table.add_column("CPU", justify="right", style="yellow")
table.add_column("Memory (GB)", justify="right", style="cyan") table.add_column("Memory (GB)", justify="right", style="cyan")
table.add_column("Storage", style="magenta") table.add_column("Storage", style="magenta")
table.add_column("Time on Duty", justify="right", style="magenta")
nodes = fetch_all("metal_nodes") nodes = fetch_all("metal_nodes")
for node in nodes: for node in nodes:
time_on_duty = calculate_time_on_duty_hours(node[7])
table.add_row( table.add_row(
f"{node[0]}", f"{node[0]}",
node[1], node[1],
@@ -74,7 +92,8 @@ def display_metal_nodes():
node[3], node[3],
f"{node[4]}", f"{node[4]}",
node[5], node[5],
node[6] node[6],
time_on_duty
) )
console.print(table) console.print(table)
@@ -88,9 +107,11 @@ def display_virtual_machines():
table.add_column("Memory (GB)", justify="right", style="cyan") table.add_column("Memory (GB)", justify="right", style="cyan")
table.add_column("Storage", style="magenta") table.add_column("Storage", style="magenta")
table.add_column("Type", style="green") table.add_column("Type", style="green")
table.add_column("Time on Duty", justify="right", style="magenta")
vms = fetch_all("virtual_machines") vms = fetch_all("virtual_machines")
for vm in vms: for vm in vms:
time_on_duty = calculate_time_on_duty_hours(vm[7])
table.add_row( table.add_row(
f"{vm[0]}", f"{vm[0]}",
vm[1], vm[1],
@@ -98,7 +119,8 @@ def display_virtual_machines():
f"{vm[3]}", f"{vm[3]}",
vm[4], vm[4],
vm[5], vm[5],
vm[6] vm[6],
time_on_duty
) )
console.print(table) console.print(table)