add network monitor (global uasge only)

This commit is contained in:
rambros 2026-03-12 21:50:06 +05:30
parent 6a42cdf008
commit 057d5a94fc
2 changed files with 45 additions and 20 deletions

View file

@ -8,3 +8,4 @@ pydantic # Data validation using Python type hints
lottie # Lottie file manipulation and conversion lottie # Lottie file manipulation and conversion
Pillow # Image processing (required for GIF rendering) Pillow # Image processing (required for GIF rendering)
cairosvg # SVG rendering (required for Lottie conversion) cairosvg # SVG rendering (required for Lottie conversion)
psutil

View file

@ -1,35 +1,59 @@
import psutil
import time
from textual.widgets import Static from textual.widgets import Static
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class RamDisplay(Static): class RamDisplay(Static):
"""Widget to display current RAM usage.""" """Widget to display current RAM usage and Network speed."""
def on_mount(self) -> None: def on_mount(self) -> None:
self.update_ram() self._prev_net_io = psutil.net_io_counters()
self.set_interval(1.0, self.update_ram) self._prev_time = time.time()
self.update_stats()
self.set_interval(1.0, self.update_stats)
def update_ram(self) -> None: def _format_speed(self, bytes_per_sec: float) -> str:
"""Fetch and display RAM usage (RSS).""" if bytes_per_sec < 1024:
return f"{bytes_per_sec:.1f} B/s"
elif bytes_per_sec < 1024 * 1024:
return f"{bytes_per_sec / 1024:.1f} KB/s"
else:
return f"{bytes_per_sec / (1024 * 1024):.1f} MB/s"
def update_stats(self) -> None:
"""Fetch and display RAM and Network speed."""
try: try:
# RSS is in KB in /proc/self/status on Linux # 1. RAM Usage
with open("/proc/self/status", "r") as f: process = psutil.Process()
for line in f: rss = process.memory_info().rss
if line.startswith("VmRSS:"): if rss > 1024 * 1024 * 1024:
rss_kb = int(line.split()[1]) ram_usage = f"{rss / (1024 * 1024 * 1024):.2f} GB"
break
else: else:
rss_kb = 0 ram_usage = f"{rss / (1024 * 1024):.2f} MB"
if rss_kb > 1024 * 1024: # 2. Network Speed
usage = f"{rss_kb / (1024 * 1024):.2f} GB" curr_net_io = psutil.net_io_counters()
else: curr_time = time.time()
usage = f"{rss_kb / 1024:.2f} MB"
self.update(f" RAM: [yellow]{usage}[/yellow]") delta_time = curr_time - self._prev_time
except Exception: if delta_time <= 0:
self.update(" RAM: [bold red]N/A[/bold red]") delta_time = 1.0
sent_speed = (curr_net_io.bytes_sent - self._prev_net_io.bytes_sent) / delta_time
recv_speed = (curr_net_io.bytes_recv - self._prev_net_io.bytes_recv) / delta_time
# Simple combined speed display or separately
speed_str = self._format_speed(sent_speed + recv_speed)
self._prev_net_io = curr_net_io
self._prev_time = curr_time
self.update(f" RAM: [yellow]{ram_usage}[/yellow] | Net: [cyan]{speed_str}[/cyan]")
except Exception as e:
logger.error(f"Error updating stats: {e}")
self.update(" Stats: [bold red]N/A[/bold red]")
class Footnote(Static): class Footnote(Static):
"""Widget to display branding text at the bottom right.""" """Widget to display branding text at the bottom right."""