Cyberpunk 2077 look for the server view, refreshed every second
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]>
This commit is contained in:
1 parent
edba341f30
commit
5e64bfb64e
15 files changed
+524
-95
No files matched your search
+75
-65
@@ -1,19 +1,18 @@
|
||||
"""Casio-style server stats: CPU/RAM/disk bars, load, temperature, uptime, containers, busiest processes."""
|
||||
"""Cyberpunk-style server stats: CPU/RAM/disk gauges, load, temperature, uptime, containers, busiest processes."""
|
||||
from datetime import datetime
|
||||
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
import lcdkit
|
||||
from lcdkit import BG, H, INK, W
|
||||
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 = 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))
|
||||
CONTAINER_ROWS = 5
|
||||
GAUGES = (("CPU", "cpu_pct", 54), ("RAM", "mem_pct", 94), ("DISK", "disk_pct", 134))
|
||||
|
||||
|
||||
def fmt_uptime(seconds: float | None) -> str:
|
||||
@@ -27,17 +26,9 @@ def fmt_uptime(seconds: float | None) -> str:
|
||||
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
|
||||
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
|
||||
@@ -53,87 +44,106 @@ class ServerView:
|
||||
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())
|
||||
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 = Image.new("RGB", (W, H), BG)
|
||||
img = cyber.background()
|
||||
d = ImageDraw.Draw(img)
|
||||
f_small, f_tiny, f_row = lcdkit.load_font(15), lcdkit.load_font(12), lcdkit.load_font(14)
|
||||
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)
|
||||
|
||||
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)
|
||||
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, 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)
|
||||
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():
|
||||
width = d.textbbox((0, 0), "STARÉ", font=f_tiny)[2] + 6
|
||||
lcdkit.tag(d, (W - 14 - width, 17), "STARÉ", f_tiny)
|
||||
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 _bars(d, stats: Stats | None, f_small) -> None:
|
||||
for label, field, y in BAR_ROWS:
|
||||
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((18, y + 3), label, font=f_small, fill=INK)
|
||||
lcdkit.h_bar(d, 72, y, pct, 24, 16, 8, 2)
|
||||
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))
|
||||
x = lcdkit.number(d, text, 244, y - 1, **DIGITS, ghost=False)
|
||||
cyber.glitch_text(d, (246, y - 4), text, big)
|
||||
if pct is not None:
|
||||
d.text((x + 2, y + 6), "%", font=f_small, fill=INK)
|
||||
d.text((246 + d.textlength(text, font=big) + 4, y + 8), "%", font=cyber.font(16), fill=CYAN)
|
||||
|
||||
@staticmethod
|
||||
def _load_and_temp(d, stats: Stats | None, f_small, f_tiny) -> None:
|
||||
def _load_and_temp(d: ImageDraw.ImageDraw, stats: Stats | None) -> 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)
|
||||
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
|
||||
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)
|
||||
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, stats: Stats | None, f_tiny, f_row) -> None:
|
||||
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((14, 166), "DOCKER", font=f_tiny, fill=INK)
|
||||
d.text((22, 230), "NO DOCKER", font=lcdkit.load_font(18), fill=INK)
|
||||
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((14, 166), f"DOCKER {running}/{len(containers)}", font=f_tiny, fill=INK)
|
||||
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))
|
||||
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)
|
||||
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, c: Container, y: int, f_tiny, f_row) -> None:
|
||||
def _container_row(d: ImageDraw.ImageDraw, c: Container, y: int) -> None:
|
||||
label = status_label(c)
|
||||
d.text((14, y), _fit(d, c.name, f_row, 170), font=f_row, fill=INK)
|
||||
d.text((24, y), cyber.fit(d, c.name.upper(), cyber.font(15), 226), font=cyber.font(15), fill=TEXT)
|
||||
if label in ATTENTION:
|
||||
width = d.textbbox((0, 0), label, font=f_tiny)[2] + 6
|
||||
lcdkit.tag(d, (294 - width, y), label, f_tiny)
|
||||
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((294 - d.textlength(label, font=f_tiny), y + 2), label, font=f_tiny, fill=INK)
|
||||
d.text((312 - d.textlength(label, font=cyber.font(13)), y), label, font=cyber.font(13), fill=CYAN)
|
||||
|
||||
@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
|
||||
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 - 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)
|
||||
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
|
||||
Reference in new issue
Block a user