Spaces:
Sleeping
Sleeping
Ekimart commited on
Commit ·
7d2d28e
1
Parent(s): 343bdad
Refactorer WebSocketManager pour améliorer le formatage des statistiques (temps de fonctionnement et mémoire) en ajoutant des méthodes utilitaires `_format_size` et `format_uptime`.
Browse files
core/manager/websocket_manager.py
CHANGED
|
@@ -224,10 +224,10 @@ class WebSocketManager:
|
|
| 224 |
def get_current_timestamp() -> str:
|
| 225 |
return current_utc_iso()
|
| 226 |
|
| 227 |
-
def get_uptime(self) ->
|
| 228 |
if not self._start_time:
|
| 229 |
-
return
|
| 230 |
-
return (datetime.now(timezone.utc) - self._start_time).total_seconds()
|
| 231 |
|
| 232 |
def get_memory_usage(self) -> Dict[str, Any]:
|
| 233 |
try:
|
|
@@ -235,17 +235,17 @@ class WebSocketManager:
|
|
| 235 |
memory_info = process.memory_info()
|
| 236 |
|
| 237 |
return {
|
| 238 |
-
"
|
| 239 |
-
"
|
| 240 |
-
"percent":
|
| 241 |
-
"available_mb":
|
| 242 |
-
"total_mb":
|
| 243 |
}
|
| 244 |
except Exception as e:
|
| 245 |
self.logger.error(f"Error getting memory usage: {e}")
|
| 246 |
return {
|
| 247 |
-
"
|
| 248 |
-
"
|
| 249 |
"percent": 0,
|
| 250 |
"available_mb": 0,
|
| 251 |
"total_mb": 0,
|
|
@@ -296,8 +296,7 @@ class WebSocketManager:
|
|
| 296 |
async def _stats_collector(self) -> None:
|
| 297 |
while not self._shutdown_event.is_set():
|
| 298 |
try:
|
| 299 |
-
|
| 300 |
-
self.logger.info(f"Service stats: {stats}")
|
| 301 |
|
| 302 |
await asyncio.sleep(self.settings.get("STATS_INTERVAL", 60)) # 1 minute
|
| 303 |
|
|
@@ -356,3 +355,26 @@ class WebSocketManager:
|
|
| 356 |
@staticmethod
|
| 357 |
def _format_notification(notification: Notification) -> Dict[str, Any]:
|
| 358 |
return {"type": "notification", **notification.model_dump(exclude_unset=True)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
def get_current_timestamp() -> str:
|
| 225 |
return current_utc_iso()
|
| 226 |
|
| 227 |
+
def get_uptime(self) -> str:
|
| 228 |
if not self._start_time:
|
| 229 |
+
return self.format_uptime(0)
|
| 230 |
+
return self.format_uptime((datetime.now(timezone.utc) - self._start_time).total_seconds())
|
| 231 |
|
| 232 |
def get_memory_usage(self) -> Dict[str, Any]:
|
| 233 |
try:
|
|
|
|
| 235 |
memory_info = process.memory_info()
|
| 236 |
|
| 237 |
return {
|
| 238 |
+
"resident_set_size_mb": self._format_size(memory_info.rss),
|
| 239 |
+
"virtual_memory_size_mb": self._format_size(memory_info.vms),
|
| 240 |
+
"percent": self._format_size(process.memory_percent()),
|
| 241 |
+
"available_mb": self._format_size(psutil.virtual_memory().available),
|
| 242 |
+
"total_mb": self._format_size(psutil.virtual_memory().total),
|
| 243 |
}
|
| 244 |
except Exception as e:
|
| 245 |
self.logger.error(f"Error getting memory usage: {e}")
|
| 246 |
return {
|
| 247 |
+
"resident_set_size_mb": 0,
|
| 248 |
+
"virtual_memory_size_mb": 0,
|
| 249 |
"percent": 0,
|
| 250 |
"available_mb": 0,
|
| 251 |
"total_mb": 0,
|
|
|
|
| 296 |
async def _stats_collector(self) -> None:
|
| 297 |
while not self._shutdown_event.is_set():
|
| 298 |
try:
|
| 299 |
+
_ = self.get_stats()
|
|
|
|
| 300 |
|
| 301 |
await asyncio.sleep(self.settings.get("STATS_INTERVAL", 60)) # 1 minute
|
| 302 |
|
|
|
|
| 355 |
@staticmethod
|
| 356 |
def _format_notification(notification: Notification) -> Dict[str, Any]:
|
| 357 |
return {"type": "notification", **notification.model_dump(exclude_unset=True)}
|
| 358 |
+
|
| 359 |
+
@staticmethod
|
| 360 |
+
def _format_size(bytes_value) -> str:
|
| 361 |
+
if bytes_value < 1024:
|
| 362 |
+
return f"{bytes_value} B"
|
| 363 |
+
elif bytes_value < 1024 ** 2:
|
| 364 |
+
return f"{bytes_value / 1024:.2f} KB"
|
| 365 |
+
elif bytes_value < 1024 ** 3:
|
| 366 |
+
return f"{bytes_value / (1024 ** 2):.2f} MB"
|
| 367 |
+
else:
|
| 368 |
+
return f"{bytes_value / (1024 ** 3):.2f} GB"
|
| 369 |
+
|
| 370 |
+
@staticmethod
|
| 371 |
+
def format_uptime(seconds: float) -> str:
|
| 372 |
+
minutes, sec = divmod(int(seconds), 60)
|
| 373 |
+
hours, minutes = divmod(minutes, 60)
|
| 374 |
+
days, hours = divmod(hours, 24)
|
| 375 |
+
parts = []
|
| 376 |
+
if days: parts.append(f"{days}d")
|
| 377 |
+
if hours: parts.append(f"{hours}h")
|
| 378 |
+
if minutes: parts.append(f"{minutes}m")
|
| 379 |
+
if sec or not parts: parts.append(f"{sec}s")
|
| 380 |
+
return " ".join(parts)
|