Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,11 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
| 18 |
# Hugging Face secret token from environment variables
|
| 19 |
VALID_TOKEN = os.environ.get("SECRET_TOKEN")
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Helper function to calculate the size of a directory
|
| 22 |
def get_directory_size(directory: Path) -> int:
|
| 23 |
total_size = 0
|
|
@@ -40,33 +45,31 @@ def health_check(token: str = Depends(get_current_token)):
|
|
| 40 |
"status": "healthy"
|
| 41 |
}
|
| 42 |
|
| 43 |
-
# System metrics endpoint: CPU, RAM, and disk usage for uploads folder
|
| 44 |
@app.get("/metrics")
|
| 45 |
def get_metrics(token: str = Depends(get_current_token)):
|
| 46 |
-
# CPU percentage
|
| 47 |
-
cpu_percent = round(psutil.cpu_percent(interval=1))
|
| 48 |
|
| 49 |
-
# Memory
|
| 50 |
memory = psutil.virtual_memory()
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
# Disk
|
| 55 |
uploads_size_bytes = get_directory_size(UPLOAD_DIRECTORY)
|
| 56 |
uploads_size_gb = uploads_size_bytes / (1024 ** 3)
|
| 57 |
-
|
| 58 |
|
| 59 |
return {
|
| 60 |
-
"
|
| 61 |
"memory": {
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"percent": memory.percent
|
| 65 |
},
|
| 66 |
"disk": {
|
| 67 |
"uploads_folder_size_gb": round(uploads_size_gb, 2),
|
| 68 |
-
"uploads_usage_percent_of_50gb": round(
|
| 69 |
-
"total_space_gb": 50
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
|
|
|
| 18 |
# Hugging Face secret token from environment variables
|
| 19 |
VALID_TOKEN = os.environ.get("SECRET_TOKEN")
|
| 20 |
|
| 21 |
+
# Constants to represent the limits for the container resources
|
| 22 |
+
MAX_CPU_CORES = 2 # vCPUs
|
| 23 |
+
MAX_MEMORY_GB = 16 # GB
|
| 24 |
+
MAX_DISK_GB = 50 # GB
|
| 25 |
+
|
| 26 |
# Helper function to calculate the size of a directory
|
| 27 |
def get_directory_size(directory: Path) -> int:
|
| 28 |
total_size = 0
|
|
|
|
| 45 |
"status": "healthy"
|
| 46 |
}
|
| 47 |
|
| 48 |
+
# System metrics endpoint: CPU, RAM, and disk usage for uploads folder relative to 2 vCPUs, 16GB RAM, and 50GB disk
|
| 49 |
@app.get("/metrics")
|
| 50 |
def get_metrics(token: str = Depends(get_current_token)):
|
| 51 |
+
# CPU percentage relative to 2 vCPUs
|
| 52 |
+
cpu_percent = round((psutil.cpu_percent(interval=1) / 100) * MAX_CPU_CORES, 2)
|
| 53 |
|
| 54 |
+
# Memory usage relative to 16GB of RAM
|
| 55 |
memory = psutil.virtual_memory()
|
| 56 |
+
memory_used_gb = (memory.total - memory.available) / (1024 ** 3)
|
| 57 |
+
memory_usage_percent = (memory_used_gb / MAX_MEMORY_GB) * 100
|
| 58 |
|
| 59 |
+
# Disk usage for uploads directory relative to 50GB storage
|
| 60 |
uploads_size_bytes = get_directory_size(UPLOAD_DIRECTORY)
|
| 61 |
uploads_size_gb = uploads_size_bytes / (1024 ** 3)
|
| 62 |
+
uploads_usage_percent = (uploads_size_gb / MAX_DISK_GB) * 100
|
| 63 |
|
| 64 |
return {
|
| 65 |
+
"cpu_usage_cores": cpu_percent, # Relative CPU usage as cores (out of 2 vCPUs)
|
| 66 |
"memory": {
|
| 67 |
+
"used_gb": round(memory_used_gb, 2),
|
| 68 |
+
"used_percent_of_16gb": round(memory_usage_percent, 2)
|
|
|
|
| 69 |
},
|
| 70 |
"disk": {
|
| 71 |
"uploads_folder_size_gb": round(uploads_size_gb, 2),
|
| 72 |
+
"uploads_usage_percent_of_50gb": round(uploads_usage_percent, 2)
|
|
|
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|