Spaces:
Paused
Paused
mohammed978 commited on
Commit ·
3426fb8
1
Parent(s): 403794b
Fix: App-specific resource monitoring
Browse files- engine/metrics.py +41 -9
engine/metrics.py
CHANGED
|
@@ -1,13 +1,45 @@
|
|
| 1 |
import psutil
|
| 2 |
import time
|
|
|
|
| 3 |
|
| 4 |
class SystemMonitor:
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import psutil
|
| 2 |
import time
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
class SystemMonitor:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
try:
|
| 8 |
+
self.process = psutil.Process(os.getpid())
|
| 9 |
+
except:
|
| 10 |
+
self.process = None
|
| 11 |
+
|
| 12 |
+
def get_metrics(self):
|
| 13 |
+
try:
|
| 14 |
+
if not self.process: return {"cpu": 0, "ram": 0, "disk": 0, "uptime": 0}
|
| 15 |
+
|
| 16 |
+
# App-specific CPU usage
|
| 17 |
+
cpu = self.process.cpu_percent(interval=None)
|
| 18 |
+
|
| 19 |
+
# App-specific RAM usage (RSS)
|
| 20 |
+
mem_info = self.process.memory_info()
|
| 21 |
+
ram_mb = mem_info.rss / (1024 * 1024)
|
| 22 |
+
|
| 23 |
+
# Estimated percentage based on 2GB HF limit
|
| 24 |
+
ram_percent = (ram_mb / 2048) * 100
|
| 25 |
+
|
| 26 |
+
return {
|
| 27 |
+
"cpu": round(cpu, 1),
|
| 28 |
+
"ram": round(ram_percent, 1),
|
| 29 |
+
"disk": psutil.disk_usage('/').percent,
|
| 30 |
+
"uptime": int(time.time() - self.process.create_time()),
|
| 31 |
+
"ram_mb": round(ram_mb, 1)
|
| 32 |
+
}
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Metrics Error: {e}")
|
| 35 |
+
return {"cpu": 0, "ram": 0, "disk": 0, "uptime": 0}
|
| 36 |
+
|
| 37 |
+
def get_process_stats(self, pid):
|
| 38 |
+
try:
|
| 39 |
+
p = psutil.Process(pid)
|
| 40 |
+
return {
|
| 41 |
+
"cpu": p.cpu_percent(interval=None),
|
| 42 |
+
"memory": p.memory_percent()
|
| 43 |
+
}
|
| 44 |
+
except:
|
| 45 |
+
return {"cpu": 0, "memory": 0}
|