WarpWing commited on
Commit
02af70c
1 Parent(s): 0953f7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
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 (in GB and percentage of 50GB limit)
44
  @app.get("/metrics")
45
  def get_metrics(token: str = Depends(get_current_token)):
46
- # CPU percentage (rounded to the nearest whole number)
47
- cpu_percent = round(psutil.cpu_percent(interval=1))
48
 
49
- # Memory stats in GB
50
  memory = psutil.virtual_memory()
51
- memory_total_gb = memory.total / (1024 ** 3)
52
- memory_available_gb = memory.available / (1024 ** 3)
53
 
54
- # Disk stats for uploads directory
55
  uploads_size_bytes = get_directory_size(UPLOAD_DIRECTORY)
56
  uploads_size_gb = uploads_size_bytes / (1024 ** 3)
57
- uploads_percent = (uploads_size_gb / 50) * 100 # Assuming 50GB total space limit
58
 
59
  return {
60
- "cpu_percent": cpu_percent, # Rounded CPU percentage
61
  "memory": {
62
- "total_gb": round(memory_total_gb, 2),
63
- "available_gb": round(memory_available_gb, 2),
64
- "percent": memory.percent
65
  },
66
  "disk": {
67
  "uploads_folder_size_gb": round(uploads_size_gb, 2),
68
- "uploads_usage_percent_of_50gb": round(uploads_percent, 2),
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