Rajdhani font (OFL), chamfered/glitch drawing kit, Docker list every 5 s and process ranking over 3 s windows so the 1 s refresh stays cheap and calm. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
150 lines
6.8 KiB
Python
150 lines
6.8 KiB
Python
"""Cyberpunk-style server stats: CPU/RAM/disk gauges, load, temperature, uptime, containers, busiest processes."""
|
|
from datetime import datetime
|
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
import cyber
|
|
from cyber import CYAN, GREY, INK, MAGENTA, RED, TEXT, W, H, YELLOW
|
|
from poller import Poller
|
|
from stats_data import Container, Stats, status_label
|
|
from view import Patch
|
|
|
|
TITLE = "HOME // SERVER"
|
|
ATTENTION = ("UNHEALTHY", "RESTART", "EXITED")
|
|
CONTAINER_ROWS = 5
|
|
GAUGES = (("CPU", "cpu_pct", 54), ("RAM", "mem_pct", 94), ("DISK", "disk_pct", 134))
|
|
|
|
|
|
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"
|
|
|
|
|
|
class ServerView:
|
|
name = "server"
|
|
STALE_AFTER = 10.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 (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 = cyber.background()
|
|
d = ImageDraw.Draw(img)
|
|
cyber.chamfer(d, (5, 5, W - 6, H - 6), 16, outline=YELLOW, width=2, cuts="tl br")
|
|
d.line((W - 26, 10, W - 10, 10), fill=CYAN, width=2) # corner brackets
|
|
d.line((W - 10, 10, W - 10, 26), fill=CYAN, width=2)
|
|
d.line((10, H - 26, 10, H - 10), fill=CYAN, width=2)
|
|
d.line((10, H - 10, 26, H - 10), fill=CYAN, width=2)
|
|
|
|
self._header(d, stats)
|
|
self._gauges(d, stats)
|
|
self._load_and_temp(d, stats)
|
|
d.line((22, 176, 318, 176), fill=YELLOW, width=1)
|
|
self._docker(d, stats)
|
|
d.line((324, 190, 324, H - 22), fill=YELLOW, width=1)
|
|
self._processes(d, stats)
|
|
return img
|
|
|
|
def _header(self, d: ImageDraw.ImageDraw, stats: Stats | None) -> None:
|
|
box = cyber.tag(d, (22, 14), TITLE, cyber.font(22))
|
|
d.text((box[2] + 16, 17), "UP", font=cyber.font(13, False), fill=GREY)
|
|
d.text((box[2] + 36, 14), fmt_uptime(stats.uptime_s if stats else None), font=cyber.font(22), fill=CYAN)
|
|
if self._is_stale():
|
|
cyber.tag(d, (W - 22 - 70, 14), "STARÉ", cyber.font(16), colour=RED)
|
|
else:
|
|
tick = int(stats.uptime_s) & 0xFFFF if stats and stats.uptime_s is not None else 0
|
|
d.text((W - 22 - 92, 18), f"0x{tick:04X}", font=cyber.font(13, False), fill=MAGENTA)
|
|
d.rectangle((W - 22 - 40, 21, W - 22 - 36, 25), fill=CYAN)
|
|
d.text((W - 22 - 30, 17), "ON", font=cyber.font(13), fill=CYAN)
|
|
d.line((22, 46, W - 22, 46), fill=YELLOW, width=1)
|
|
d.line((22, 48, box[2], 48), fill=YELLOW, width=1)
|
|
|
|
@staticmethod
|
|
def _gauges(d: ImageDraw.ImageDraw, stats: Stats | None) -> None:
|
|
big = cyber.font(36)
|
|
for label, field, y in GAUGES:
|
|
pct = getattr(stats, field) if stats else None
|
|
d.text((24, y), label, font=cyber.font(15), fill=CYAN)
|
|
cyber.slanted_bar(d, 24, y + 22, pct, 10, 21, 7, 3)
|
|
text = "--" if pct is None else str(round(pct))
|
|
cyber.glitch_text(d, (246, y - 4), text, big)
|
|
if pct is not None:
|
|
d.text((246 + d.textlength(text, font=big) + 4, y + 8), "%", font=cyber.font(16), fill=CYAN)
|
|
|
|
@staticmethod
|
|
def _load_and_temp(d: ImageDraw.ImageDraw, stats: Stats | None) -> None:
|
|
load = stats.load if stats else None
|
|
cyber.chamfer(d, (330, 56, W - 22, 108), 8, outline=CYAN, cuts="tr bl")
|
|
d.text((340, 58), "LOAD AVG", font=cyber.font(13, False), fill=GREY)
|
|
d.text((340, 68), f"{load[0]:.2f}" if load else "--", font=cyber.font(30), fill=YELLOW)
|
|
d.text((410, 78), f"{load[1]:.2f} {load[2]:.2f}" if load else "", font=cyber.font(13, False), fill=CYAN)
|
|
temp = stats.temp_c if stats else None
|
|
cyber.chamfer(d, (330, 114, W - 22, 166), 8, outline=CYAN, cuts="tr bl")
|
|
d.text((340, 116), "CORE TEMP", font=cyber.font(13, False), fill=GREY)
|
|
d.text((340, 126), f"{round(temp)}°C" if temp is not None else "--", font=cyber.font(30), fill=YELLOW)
|
|
cyber.hazard_stripe(d, 336, 170, 118)
|
|
|
|
@staticmethod
|
|
def _docker(d: ImageDraw.ImageDraw, stats: Stats | None) -> None:
|
|
d.text((24, 180), "DOCKER", font=cyber.font(16), fill=YELLOW)
|
|
containers = stats.containers if stats else None
|
|
if containers is None:
|
|
d.text((24, 226), "NO DOCKER", font=cyber.font(26), fill=RED)
|
|
d.text((24, 256), "SOCKET UNAVAILABLE", font=cyber.font(13, False), fill=GREY)
|
|
return
|
|
running = sum(1 for c in containers if c.state == "running")
|
|
d.text((86, 183), f"{running}/{len(containers)} ACTIVE", font=cyber.font(13, False), fill=CYAN)
|
|
ordered = sorted(containers, key=lambda c: (status_label(c) not in ATTENTION, c.name))
|
|
y = 202
|
|
for c in ordered[:CONTAINER_ROWS]:
|
|
ServerView._container_row(d, c, y)
|
|
y += 17
|
|
if len(ordered) > CONTAINER_ROWS:
|
|
d.text((24, y + 1), f"+{len(ordered) - CONTAINER_ROWS} MORE", font=cyber.font(13, False), fill=GREY)
|
|
|
|
@staticmethod
|
|
def _container_row(d: ImageDraw.ImageDraw, c: Container, y: int) -> None:
|
|
label = status_label(c)
|
|
d.text((24, y), cyber.fit(d, c.name.upper(), cyber.font(15), 226), font=cyber.font(15), fill=TEXT)
|
|
if label in ATTENTION:
|
|
colour = MAGENTA if label == "RESTART" else RED
|
|
width = d.textlength(label, font=cyber.font(13))
|
|
cyber.chamfer(d, (int(314 - width - 14), y + 1, 314, y + 16), 4, fill=colour, cuts="tl br")
|
|
d.text((314 - width - 7, y - 1), label, font=cyber.font(13), fill=INK)
|
|
else:
|
|
d.text((312 - d.textlength(label, font=cyber.font(13)), y), label, font=cyber.font(13), fill=CYAN)
|
|
|
|
@staticmethod
|
|
def _processes(d: ImageDraw.ImageDraw, stats: Stats | None) -> None:
|
|
d.text((336, 180), "TOP PROCESSES", font=cyber.font(16), fill=YELLOW)
|
|
y = 204
|
|
for proc in stats.procs if stats else ():
|
|
d.text((336, y), cyber.fit(d, proc.name.upper(), cyber.font(15), 76), font=cyber.font(15), fill=CYAN)
|
|
pct = f"{proc.cpu_pct:.1f}%"
|
|
d.text((W - 24 - d.textlength(pct, font=cyber.font(15)), y), pct, font=cyber.font(15), fill=YELLOW)
|
|
d.rectangle((336, y + 17, 336 + int(min(proc.cpu_pct, 10) * 11), y + 18), fill=MAGENTA)
|
|
y += 22
|