update(k8s): use time_on_duty in export and ui
This commit is contained in:
@@ -1,13 +1,57 @@
|
||||
from fastapi import APIRouter, Response
|
||||
from database import fetch_all
|
||||
from kubernetes import client, config
|
||||
from datetime import datetime, timezone
|
||||
import yaml
|
||||
import json
|
||||
import logging
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Helper functions for conversions
|
||||
def calculate_time_on_duty(creation_timestamp):
|
||||
"""
|
||||
Calculate the time on duty in hours, days, or minutes from the creation timestamp.
|
||||
"""
|
||||
now = datetime.now(timezone.utc)
|
||||
delta = now - creation_timestamp
|
||||
|
||||
# If less than an hour, return minutes
|
||||
if delta.days < 1 and delta.seconds < 3600:
|
||||
minutes = delta.seconds // 60
|
||||
return f"{minutes} minutes" if minutes > 1 else "less than a minute"
|
||||
|
||||
# If less than a day, return hours
|
||||
if delta.days < 1:
|
||||
hours = delta.seconds // 3600
|
||||
return f"{hours} hours" if hours > 1 else "1 hour"
|
||||
|
||||
# Otherwise, return days
|
||||
return f"{delta.days} days" if delta.days > 1 else "1 day"
|
||||
|
||||
def convert_memory_to_gb(memory):
|
||||
"""
|
||||
Convert memory to GB (gigabytes) for ephemeral-storage.
|
||||
"""
|
||||
if "Ki" in memory:
|
||||
return int(memory.replace("Ki", "")) / (1024 ** 2)
|
||||
elif "Mi" in memory:
|
||||
return int(memory.replace("Mi", "")) / 1024
|
||||
elif "Gi" in memory:
|
||||
return int(memory.replace("Gi", ""))
|
||||
return float(memory)
|
||||
|
||||
def convert_memory_to_mib(memory):
|
||||
"""
|
||||
Convert memory to MiB (mebibytes).
|
||||
"""
|
||||
if "Ki" in memory:
|
||||
return int(memory.replace("Ki", "")) / 1024
|
||||
elif "Mi" in memory:
|
||||
return int(memory.replace("Mi", ""))
|
||||
elif "Gi" in memory:
|
||||
return int(memory.replace("Gi", "")) * 1024
|
||||
return float(memory)
|
||||
|
||||
def convert_cpu_to_cores(cpu):
|
||||
"""
|
||||
Convert CPU usage to cores for human-readable format.
|
||||
@@ -22,19 +66,6 @@ def convert_cpu_to_cores(cpu):
|
||||
return round(int(cpu.replace("m", "")) / 1000, 4)
|
||||
return float(cpu) # Already in cores
|
||||
|
||||
def convert_memory_to_mib(memory):
|
||||
"""
|
||||
Convert memory to MiB (mebibytes).
|
||||
Handles units: Ki (kibibytes), Mi (mebibytes), Gi (gibibytes).
|
||||
"""
|
||||
if "Ki" in memory:
|
||||
return int(memory.replace("Ki", "")) / 1024
|
||||
elif "Mi" in memory:
|
||||
return int(memory.replace("Mi", ""))
|
||||
elif "Gi" in memory:
|
||||
return int(memory.replace("Gi", "")) * 1024
|
||||
return float(memory)
|
||||
|
||||
# Fetch Kubernetes data with namespace resource usage
|
||||
def fetch_k8s_data_with_usage():
|
||||
config.load_incluster_config()
|
||||
@@ -47,6 +78,10 @@ def fetch_k8s_data_with_usage():
|
||||
# Extract storage (ephemeral-storage) and instance type
|
||||
ephemeral_storage = node.status.capacity.get("ephemeral-storage", "0")
|
||||
instance_type = node.metadata.labels.get("beta.kubernetes.io/instance-type", "N/A")
|
||||
creation_timestamp = node.metadata.creation_timestamp
|
||||
|
||||
# Calculate time on duty
|
||||
time_on_duty = calculate_time_on_duty(creation_timestamp) if creation_timestamp else "N/A"
|
||||
|
||||
nodes.append({
|
||||
"node_name": node.metadata.name,
|
||||
@@ -55,6 +90,7 @@ def fetch_k8s_data_with_usage():
|
||||
"storage": f"{round(convert_memory_to_gb(ephemeral_storage), 2)} GB",
|
||||
"instance_type": instance_type,
|
||||
"pods_allocatable": node.status.allocatable.get("pods"),
|
||||
"time_on_duty": time_on_duty, # Add time on duty
|
||||
})
|
||||
|
||||
# Fetch namespaces
|
||||
@@ -89,19 +125,6 @@ def fetch_k8s_data_with_usage():
|
||||
|
||||
return {"nodes": nodes, "namespaces": namespaces, "namespace_usage": namespace_usage}
|
||||
|
||||
|
||||
def convert_memory_to_gb(memory):
|
||||
"""
|
||||
Convert memory to GB (gigabytes) for ephemeral-storage.
|
||||
"""
|
||||
if "Ki" in memory:
|
||||
return int(memory.replace("Ki", "")) / (1024 ** 2)
|
||||
elif "Mi" in memory:
|
||||
return int(memory.replace("Mi", "")) / 1024
|
||||
elif "Gi" in memory:
|
||||
return int(memory.replace("Gi", ""))
|
||||
return float(memory)
|
||||
|
||||
# Export endpoint
|
||||
@router.get("/export")
|
||||
def export_data(format: str = "yaml"):
|
||||
|
||||
Reference in New Issue
Block a user