feat: server view and previews

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
rickzkmandClaude Sonnet 5.5 committed 2026-10-22 13:23:35 +02:00
1 parent 32f8c288be
commit 6713e09791
4 files changed
+286 -2

No files matched your search

+139
View File
@@ -0,0 +1,139 @@
"""Casio-style server stats: CPU/RAM/disk bars, load, temperature, uptime, containers, busiest processes."""
from datetime import datetime
from PIL import Image, ImageDraw
import lcdkit
from lcdkit import BG, H, INK, W
from poller import Poller
from stats_data import Container, Stats, status_label
from view import Patch
ATTENTION = ("UNHEALTHY", "RESTART", "EXITED")
CONTAINER_ROWS = 7
ROW_STEP, ROWS_TOP = 17, 184
DIGITS = dict(w=16, h=26, t=3, gap=5) # small segment digits used for all numbers
BAR_ROWS = (("CPU", "cpu_pct", 54), ("RAM", "mem_pct", 88), ("DISK", "disk_pct", 122))
def fmt_uptime(seconds: float | None) -> str:
if seconds is None:
return "--"
minutes = int(seconds // 60)
days, rest = divmod(minutes, 24 * 60)
hours, minutes = divmod(rest, 60)
if days:
return f"{days}D {hours}H"
return f"{hours}H {minutes}M" if hours else f"{minutes}M"
def _fit(d: ImageDraw.ImageDraw, text: str, font, max_w: float) -> str:
if d.textlength(text, font=font) <= max_w:
return text
while text and d.textlength(text + "…", font=font) > max_w:
text = text[:-1]
return text + "…"
class ServerView:
name = "server"
STALE_AFTER = 30.0 # seconds without a successful read before the numbers are marked old
def __init__(self, poller: Poller[Stats]) -> None:
self._poller = poller
def start(self) -> None:
self._poller.start()
def stop(self) -> None:
self._poller.stop()
def _is_stale(self) -> bool:
age = self._poller.age()
return age is not None and age > self.STALE_AFTER
def frame_key(self, now: datetime):
return (now.second // 5, self._poller.version, self._is_stale())
def patch(self, now: datetime) -> Patch | None:
return None
def render(self, now: datetime) -> Image.Image:
stats: Stats | None = self._poller.value
img = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(img)
f_small, f_tiny, f_row = lcdkit.load_font(15), lcdkit.load_font(12), lcdkit.load_font(14)
d.rectangle((6, 6, W - 7, H - 7), outline=INK, width=2)
self._header(d, stats, f_small, f_tiny)
d.line((12, 46, W - 13, 46), fill=INK, width=2)
self._bars(d, stats, f_small)
self._load_and_temp(d, stats, f_small, f_tiny)
d.line((12, 160, W - 13, 160), fill=INK, width=2)
self._docker(d, stats, f_tiny, f_row)
d.line((306, 168, 306, H - 14), fill=INK, width=2)
self._processes(d, stats, f_tiny, f_row)
return img
def _header(self, d, stats: Stats | None, f_small, f_tiny) -> None:
lcdkit.tag(d, (14, 14), "SERVER", f_small)
d.text((100, 16), f"UP {fmt_uptime(stats.uptime_s if stats else None)}", font=f_small, fill=INK)
if self._is_stale():
width = d.textbbox((0, 0), "STARÉ", font=f_tiny)[2] + 6
lcdkit.tag(d, (W - 14 - width, 17), "STARÉ", f_tiny)
@staticmethod
def _bars(d, stats: Stats | None, f_small) -> None:
for label, field, y in BAR_ROWS:
pct = getattr(stats, field) if stats else None
d.text((18, y + 3), label, font=f_small, fill=INK)
lcdkit.h_bar(d, 72, y, pct, 24, 16, 8, 2)
text = "--" if pct is None else str(round(pct))
x = lcdkit.number(d, text, 244, y - 1, **DIGITS, ghost=False)
if pct is not None:
d.text((x + 2, y + 6), "%", font=f_small, fill=INK)
@staticmethod
def _load_and_temp(d, stats: Stats | None, f_small, f_tiny) -> None:
load = stats.load if stats else None
d.text((336, 50), "LOAD", font=f_tiny, fill=INK)
lcdkit.number(d, f"{load[0]:.2f}" if load else "--", 336, 64, **DIGITS, ghost=False)
d.text((336, 96), f"{load[1]:.2f} {load[2]:.2f}" if load else "-- --", font=f_tiny, fill=INK)
temp = stats.temp_c if stats else None
x = lcdkit.number(d, lcdkit.fmt_temp(temp), 336, 116, **DIGITS, ghost=False)
d.text((x + 2, 126), "°C" if temp is not None else "", font=f_small, fill=INK)
@staticmethod
def _docker(d, stats: Stats | None, f_tiny, f_row) -> None:
containers = stats.containers if stats else None
if containers is None:
d.text((14, 166), "DOCKER", font=f_tiny, fill=INK)
d.text((22, 230), "NO DOCKER", font=lcdkit.load_font(18), fill=INK)
return
running = sum(1 for c in containers if c.state == "running")
d.text((14, 166), f"DOCKER {running}/{len(containers)}", font=f_tiny, fill=INK)
ordered = sorted(containers, key=lambda c: (status_label(c) not in ATTENTION, c.name))
shown = ordered if len(ordered) <= CONTAINER_ROWS else ordered[:CONTAINER_ROWS - 1]
for i, c in enumerate(shown):
ServerView._container_row(d, c, ROWS_TOP + i * ROW_STEP, f_tiny, f_row)
if len(shown) < len(ordered):
d.text((14, ROWS_TOP + len(shown) * ROW_STEP), f"+{len(ordered) - len(shown)}", font=f_row, fill=INK)
@staticmethod
def _container_row(d, c: Container, y: int, f_tiny, f_row) -> None:
label = status_label(c)
d.text((14, y), _fit(d, c.name, f_row, 170), font=f_row, fill=INK)
if label in ATTENTION:
width = d.textbbox((0, 0), label, font=f_tiny)[2] + 6
lcdkit.tag(d, (294 - width, y), label, f_tiny)
else:
d.text((294 - d.textlength(label, font=f_tiny), y + 2), label, font=f_tiny, fill=INK)
@staticmethod
def _processes(d, stats: Stats | None, f_tiny, f_row) -> None:
d.text((318, 166), "PROCESY", font=f_tiny, fill=INK)
for i, proc in enumerate(stats.procs if stats else ()):
y = ROWS_TOP + i * 22
pct = f"{proc.cpu_pct:.1f}%"
d.text((W - 16 - d.textlength(pct, font=f_row), y), pct, font=f_row, fill=INK)
d.text((318, y), _fit(d, proc.name, f_row, 80), font=f_row, fill=INK)