diff --git a/plugins/host_agent/routes.py b/plugins/host_agent/routes.py index 8134ca9..b73237d 100644 --- a/plugins/host_agent/routes.py +++ b/plugins/host_agent/routes.py @@ -349,6 +349,39 @@ async def _fleet_rows(session) -> list[dict]: continue latest.setdefault(row.resource_name, {})[row.metric_name] = row.value + # Per-host recent series for the inline sparklines (cpu/mem/load host-level, + # disk from the root mount), so each metric shows its trend beside the value — + # the host-panel at-a-glance, on the fleet widget. 1h window keeps it cheap. + host_names = [h.name for h in hosts.values()] + spark_series: dict[str, dict[str, list]] = { + n: {"cpu": [], "mem": [], "disk": [], "load": []} for n in host_names + } + if host_names: + since = datetime.now(timezone.utc) - timedelta(hours=1) + sp_rows = (await session.execute( + select(PluginMetric).where( + PluginMetric.source_module == SOURCE_MODULE, + PluginMetric.recorded_at >= since, + or_( + and_(PluginMetric.resource_name.in_(host_names), + PluginMetric.metric_name.in_(("cpu_pct", "mem_used_pct", "load_1m"))), + and_(PluginMetric.resource_name.in_([n + ":/" for n in host_names]), + PluginMetric.metric_name == "disk_used_pct"), + ), + ).order_by(PluginMetric.recorded_at) + )).scalars().all() + for r in sp_rows: + if r.resource_name.endswith(":/"): + name = r.resource_name[:-2] + if name in spark_series: + spark_series[name]["disk"].append(r.value) + else: + key = {"cpu_pct": "cpu", "mem_used_pct": "mem", "load_1m": "load"}.get(r.metric_name) + if key and r.resource_name in spark_series: + spark_series[r.resource_name][key].append(r.value) + + from steward.core.status import sparkline_svg + stale_after = _stale_after_seconds() now = datetime.now(timezone.utc) rows = [] @@ -359,6 +392,11 @@ async def _fleet_rows(session) -> list[dict]: m = latest.get(host.name, {}) ls = reg.last_seen_at stale = ls is None or (now - ls).total_seconds() > stale_after + ser = spark_series.get(host.name, {}) + sparks = { + k: (sparkline_svg(v[-60:], width=72, height=16) if len(v) >= 2 else "") + for k, v in ser.items() + } rows.append({ "host": host, "reg": reg, @@ -371,6 +409,7 @@ async def _fleet_rows(session) -> list[dict]: "temp_max": m.get("temp_c_max"), "net_rx_bps": m.get("net_rx_bps"), "net_tx_bps": m.get("net_tx_bps"), + "sparks": sparks, }) rows.sort(key=lambda r: r["host"].name.lower()) return rows diff --git a/plugins/host_agent/templates/widget_table.html b/plugins/host_agent/templates/widget_table.html index df3f489..aa5ffc7 100644 --- a/plugins/host_agent/templates/widget_table.html +++ b/plugins/host_agent/templates/widget_table.html @@ -1,10 +1,20 @@ -{# host_agent widget — fleet glance, flex rows (no header wrap) #} +{# host_agent widget — fleet glance: per-host row with a sparkline beside each + metric (cpu/mem/disk/load), mirroring the host-page AGENT panel. #} {% from "_macros.html" import metric_style %} {% if rows %} +{% macro metric_cell(label, value, fmt, svg, style="", title="") %} +
+
{{ label }}
+
+ {% if value is not none %}{{ fmt|format(value) }}{% else %}—{% endif %} +
+
{{ svg | safe }}
+
+{% endmacro %}
{% for r in rows %} {% set stale = r.stale %} -
+
{% set name_style = "flex:1;min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:500;" %} @@ -13,21 +23,13 @@ {% else %} {{ r.host.name }} {% endif %} - - {% if r.cpu_pct is not none %} - cpu {{ "%.0f"|format(r.cpu_pct) }}% - {% endif %} - {% if r.mem_used_pct is not none %} - mem {{ "%.0f"|format(r.mem_used_pct) }}% - {% endif %} - {% if r.disk_root is not none %} - disk / {{ "%.0f"|format(r.disk_root) }}% - {% endif %} - {% if r.load_1m is not none %} - load {{ "%.2f"|format(r.load_1m) }} - {% endif %} - - +
+ {{ metric_cell("cpu", r.cpu_pct, "%.0f%%", r.sparks.cpu, metric_style(r.cpu_pct), "Average CPU utilization") }} + {{ metric_cell("mem", r.mem_used_pct, "%.0f%%", r.sparks.mem, metric_style(r.mem_used_pct), "Memory in use") }} + {{ metric_cell("disk /", r.disk_root, "%.0f%%", r.sparks.disk, metric_style(r.disk_root), "Root filesystem (/) usage") }} + {{ metric_cell("load", r.load_1m, "%.2f", r.sparks.load, "", "Load average (1m)") }} +
+ {{ r.reg.last_seen_at.strftime("%H:%M:%S") if r.reg.last_seen_at else "never" }}
diff --git a/steward/core/widgets.py b/steward/core/widgets.py index a4f0349..401db37 100644 --- a/steward/core/widgets.py +++ b/steward/core/widgets.py @@ -270,7 +270,7 @@ WIDGET_REGISTRY: dict[str, dict] = { "host_resources": { "key": "host_resources", "label": "Host Agent — Resources", - "description": "Fleet view: CPU, memory, disk, and load per monitored host", + "description": "Fleet view: CPU, memory, disk, and load per host — each with a trend sparkline", "hx_url": "/plugins/host_agent/widget", "detail_url": "/hosts/", "plugin": "host_agent",