The dashboard's host widgets took 18-60 s. Every "latest reading" query (hosts overview, host-agent fleet, host page, vitals, SNMP, MCP get_host) searched plugin_metrics for the newest row per metric. Postgres has no skip scan, so each one read every raw sample in the 7-day window. - plugin_metric_latest: one row per (source, resource, metric). record_metrics upserts it in the same transaction, moving forward only and in sorted key order. Migration 0030 seeds it by stepping the existing index. - Retention drops a latest row once its sample leaves the raw window, so a resource that stopped reporting disappears when its history does, as before. Renames carry it along. - A quiet host keeps its last values, marked stale. A time-bounded query would have shown it as missing. - Vitals sparklines span the 6 h they claim, averaged in SQL to ~120 points, instead of fetching 6 h raw and drawing the last 120 samples. - Dashboard and share-view cells: "Loading…" until the first answer; a 20 s deadline per fetch; "Couldn't load (why)" with Retry now and an automatic retry for cells that don't poll; "as of HH:MM" in the header, and "couldn't refresh · as of" when a refresh fails over shown data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
150 lines
6.0 KiB
Python
150 lines
6.0 KiB
Python
"""Integration: plugin_metric_latest, the newest sample per metric (#5208).
|
|
|
|
record_metrics keeps it beside plugin_metrics so "what is it now" reads one row
|
|
per metric instead of every raw sample in the window. Requires
|
|
STEWARD_DATABASE_URL.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import os
|
|
import uuid
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
if not os.environ.get("STEWARD_DATABASE_URL"):
|
|
pytest.skip("needs Postgres")
|
|
from steward.app import create_app
|
|
return create_app(testing=False)
|
|
|
|
|
|
def _sample(resource, metric, value, at):
|
|
from steward.models.metrics import PluginMetric
|
|
return PluginMetric(source_module="host_agent", resource_name=resource,
|
|
metric_name=metric, value=value, recorded_at=at)
|
|
|
|
|
|
async def _latest(s, resource):
|
|
from sqlalchemy import select
|
|
from steward.models.metrics import PluginMetricLatest
|
|
rows = (await s.execute(select(PluginMetricLatest).where(
|
|
PluginMetricLatest.source_module == "host_agent",
|
|
PluginMetricLatest.resource_name == resource))).scalars().all()
|
|
return {r.metric_name: (r.value, r.recorded_at) for r in rows}
|
|
|
|
|
|
def test_the_newest_sample_wins_and_a_late_backlog_does_not_move_it_back(app):
|
|
from steward.core.alerts import record_metrics
|
|
|
|
now = datetime.now(timezone.utc).replace(microsecond=0)
|
|
host = "latest-" + uuid.uuid4().hex[:8]
|
|
|
|
async def go():
|
|
async with app.db_sessionmaker() as s:
|
|
async with s.begin():
|
|
# One batch holding two samples of a metric: the newer is kept.
|
|
await record_metrics(s, [
|
|
_sample(host, "cpu_pct", 10.0, now - timedelta(minutes=2)),
|
|
_sample(host, "cpu_pct", 20.0, now - timedelta(minutes=1)),
|
|
_sample(host, "mem_used_pct", 40.0, now - timedelta(minutes=1)),
|
|
])
|
|
after_first = await _latest(s, host)
|
|
async with s.begin():
|
|
# An agent catching up posts older samples after newer ones.
|
|
await record_metrics(s, [
|
|
_sample(host, "cpu_pct", 99.0, now - timedelta(minutes=5)),
|
|
_sample(host, "mem_used_pct", 41.0, now),
|
|
])
|
|
return after_first, await _latest(s, host)
|
|
|
|
first, second = asyncio.run(go())
|
|
assert first["cpu_pct"] == (20.0, now - timedelta(minutes=1))
|
|
assert second["cpu_pct"] == (20.0, now - timedelta(minutes=1)) # backlog ignored
|
|
assert second["mem_used_pct"] == (41.0, now) # newer moves it
|
|
|
|
|
|
def test_a_quiet_host_keeps_its_last_values_and_reads_stale_not_missing(app):
|
|
from steward.core.alerts import record_metrics
|
|
from steward.hosts.service import agent_metrics_by_host, agent_overview_by_host
|
|
|
|
now = datetime.now(timezone.utc)
|
|
quiet, live = ("quiet-" + uuid.uuid4().hex[:8]), ("live-" + uuid.uuid4().hex[:8])
|
|
two_hours_ago = now - timedelta(hours=2)
|
|
|
|
async def go():
|
|
async with app.db_sessionmaker() as s:
|
|
async with s.begin():
|
|
await record_metrics(s, [
|
|
_sample(quiet, "cpu_pct", 12.0, two_hours_ago),
|
|
_sample(quiet, "mem_used_pct", 34.0, two_hours_ago),
|
|
_sample(quiet + ":/", "disk_used_pct", 56.0, two_hours_ago),
|
|
_sample(live, "cpu_pct", 7.0, now - timedelta(seconds=20)),
|
|
])
|
|
overview = await agent_overview_by_host(s, [quiet, live, "never-" + quiet], 180)
|
|
metrics = await agent_metrics_by_host(s, [quiet, live])
|
|
return overview, metrics
|
|
|
|
overview, metrics = asyncio.run(go())
|
|
q = overview[quiet]
|
|
assert (q["cpu_pct"], q["mem_used_pct"], q["disk_root"]) == (12.0, 34.0, 56.0)
|
|
assert q["fresh"] is False
|
|
assert abs((q["last_at"] - two_hours_ago).total_seconds()) < 1
|
|
assert overview[live]["fresh"] is True
|
|
assert "never-" + quiet not in overview # no samples at all: absent
|
|
assert metrics[quiet] == {"cpu_pct": 12.0, "mem_used_pct": 34.0}
|
|
|
|
|
|
def test_retention_drops_a_latest_value_with_its_raw_history(app):
|
|
from steward.core.alerts import record_metrics
|
|
from steward.core.metrics_retention import rollup_plugin_metrics
|
|
|
|
now = datetime.now(timezone.utc)
|
|
gone, here = ("gone-" + uuid.uuid4().hex[:8]), ("here-" + uuid.uuid4().hex[:8])
|
|
|
|
async def go():
|
|
async with app.db_sessionmaker() as s:
|
|
async with s.begin():
|
|
await record_metrics(s, [
|
|
_sample(gone, "cpu_pct", 1.0, now - timedelta(days=10)),
|
|
_sample(here, "cpu_pct", 2.0, now - timedelta(hours=1)),
|
|
])
|
|
async with s.begin():
|
|
counts = await rollup_plugin_metrics(s, raw_days=7, rollup_days=90, now=now)
|
|
return counts, await _latest(s, gone), await _latest(s, here)
|
|
|
|
counts, gone_latest, here_latest = asyncio.run(go())
|
|
assert counts["latest_pruned"] >= 1
|
|
assert gone_latest == {}
|
|
assert here_latest["cpu_pct"][0] == 2.0
|
|
|
|
|
|
def test_a_renamed_host_carries_its_latest_values(app):
|
|
from steward.core.alerts import record_metrics
|
|
from steward.core.renames import HOST_SOURCES, carry_rename
|
|
|
|
now = datetime.now(timezone.utc)
|
|
old, new = ("old-" + uuid.uuid4().hex[:8]), ("new-" + uuid.uuid4().hex[:8])
|
|
|
|
async def go():
|
|
async with app.db_sessionmaker() as s:
|
|
async with s.begin():
|
|
await record_metrics(s, [
|
|
_sample(old, "cpu_pct", 5.0, now),
|
|
_sample(old + ":/", "disk_used_pct", 60.0, now),
|
|
])
|
|
async with s.begin():
|
|
await carry_rename(s, HOST_SOURCES, old, new)
|
|
return (await _latest(s, old), await _latest(s, new),
|
|
await _latest(s, new + ":/"))
|
|
|
|
old_latest, new_latest, new_root = asyncio.run(go())
|
|
assert old_latest == {}
|
|
assert new_latest["cpu_pct"][0] == 5.0
|
|
assert new_root["disk_used_pct"][0] == 60.0
|