From f0c1458cde3607c2f4bfa278f65e07d29fadae34 Mon Sep 17 00:00:00 2001 From: Aleksandr Tcitlionok <803797+terghalin@users.noreply.github.com> Date: Thu, 5 Dec 2024 08:33:48 +0000 Subject: [PATCH] fix(think): correctly get list --- app/routes/think.py | 45 ++++----------------------------------------- 1 file changed, 4 insertions(+), 41 deletions(-) diff --git a/app/routes/think.py b/app/routes/think.py index 0ddbfbd..67daf3e 100644 --- a/app/routes/think.py +++ b/app/routes/think.py @@ -1,40 +1,3 @@ -from fastapi import APIRouter, HTTPException -from kubernetes import client, config -from typing import List, Dict -import requests -import os - -router = APIRouter() - -def fetch_ai_summary(cluster_name: str, nodes: List[Dict], api_url: str, auth_token: str) -> str: - """ - Sends node data to an external AI API endpoint and retrieves a summary response. - """ - payload = { - "data": { - "cluster_name": cluster_name, - "nodes": nodes - } - } - - headers = { - "Authorization": f"Bearer {auth_token}", - "Content-Type": "application/json" - } - - try: - response = requests.post(api_url, json=payload, headers=headers) - if response.status_code == 200: - result = response.json() - if result.get("success"): - return result.get("result", "No result provided by the API.") - else: - return "API responded with success=false." - else: - return f"API request failed with status code {response.status_code}: {response.text}" - except requests.RequestException as e: - return f"An error occurred while contacting the API: {str(e)}" - @router.get("/think/k8s") def think_k8s(): """ @@ -58,15 +21,15 @@ def think_k8s(): "memory": f"{round(int(node.status.capacity.get('memory').replace('Ki', '')) / 1024 / 1024, 2)} GB", "storage": f"{round(int(ephemeral_storage.replace('Ki', '')) / (1024 ** 2), 2)} GB", "type": instance_type, - "namespaces": [ns.metadata.name for ns in v1.list_namespace().items()] + "namespaces": [ns.metadata.name for ns in v1.list_namespace().items] }) - # Fetch token from environment + # Fetch environment variables api_url = os.getenv("AI_API_URL") auth_token = os.getenv("AI_API_TOKEN") - if not auth_token: - raise HTTPException(status_code=500, detail="AI API token is not set. Please set the AI_API_TOKEN environment variable.") + if not api_url or not auth_token: + raise HTTPException(status_code=500, detail="AI API URL or token is not set. Please set AI_API_URL and AI_API_TOKEN environment variables.") # Call AI API summary = fetch_ai_summary(cluster_name, nodes, api_url, auth_token)