diff --git a/app/routes/export.py b/app/routes/export.py index 5a08e45..7294982 100644 --- a/app/routes/export.py +++ b/app/routes/export.py @@ -23,6 +23,10 @@ def convert_cpu_to_cores(cpu): 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: @@ -41,7 +45,7 @@ def fetch_k8s_data_with_usage(): nodes = [{ "node_name": node.metadata.name, "cpu": node.status.capacity.get("cpu"), - "memory": node.status.capacity.get("memory"), + "memory": round(convert_memory_to_mib(node.status.capacity.get("memory")), 2), # Convert to MiB "pods_allocatable": node.status.allocatable.get("pods"), } for node in v1.list_node().items] @@ -81,6 +85,9 @@ def fetch_k8s_data_with_usage(): # Export endpoint @router.get("/export") def export_data(format: str = "yaml"): + """ + API endpoint to export data in YAML or JSON format. + """ data = { "metal_nodes": fetch_all("metal_nodes"), "virtual_machines": fetch_all("virtual_machines"), @@ -93,4 +100,4 @@ def export_data(format: str = "yaml"): yaml_data = yaml.safe_dump(data, sort_keys=False) return Response(content=yaml_data, media_type="text/yaml") - return json.dumps(data, indent=2) + return Response(content=json.dumps(data, indent=2), media_type="application/json")