feat(alerts): pick the host, then its part, and see what the metric reads (#5475)
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 1m8s
CI / unit (push) Failing after 1m29s
CI / publish (push) Skipped

#5475 "Rule form: pick the host, then its part; metrics that part reports;
a link to groups; a preview of the metric", from the operator's review of
the New rule page: hosts and their parts shared one long picker.

- Host agent rules pick a Host, then a Part: "The whole host", then its
  mounts, CPU cores, disks, interfaces and sensors under a heading each,
  cores in number order. A part that reports nothing a rule can watch
  (an interface's traffic) isn't offered.
- The Metric list holds what the picked resource reports, in catalog order.
- Under it, a preview: the reading now, and the last 24 h low to high with
  an hourly sparkline drawn from zero (UI ruling 8), read from one series.
- The group picker links to Ansible > Inventory to make a group, in a new
  tab, and re-reads the groups when the tab comes back into view.
- Picking a host or part swaps the pickers alone (/alerts/api/rule_target);
  picking a metric swaps the preview (/alerts/api/metric_preview).
- A rule's own resource and metric stay on offer after they go quiet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 09:54:08 -04:00
co-authored by Claude Opus 5.5
parent d5601575d8
commit d19df51ffb
12 changed files with 453 additions and 73 deletions
+65
View File
@@ -0,0 +1,65 @@
"""The rule form's target pickers (#5475): for a host source, the hosts in
one picker and the chosen host's parts in another, grouped by kind, each
offering only the metrics it reports that a rule can watch; a part that
reports nothing a rule can watch (an interface's traffic) is left out."""
from types import SimpleNamespace
import pytest
from steward.alerts import service as alert_svc
CATALOG = {"host_agent": ["cpu_pct", "disk_used_pct", "uptime_secs", "seconds_since_seen"]}
REPORTED = [("h1", "cpu_pct"), ("h1", "uptime_secs"), ("h1", "seconds_since_seen"),
("h1:/", "disk_used_pct"), ("h1:/var", "disk_used_pct"),
("h1:core10", "cpu_pct"), ("h1:core2", "cpu_pct"),
("h1:net:eth0", "net_rx_bps"), ("h1:temp:Core 0", "temp_c"),
("h2", "cpu_pct")]
@pytest.fixture(autouse=True)
def _catalog(monkeypatch):
monkeypatch.setattr(alert_svc, "metric_catalog", lambda: CATALOG)
def _db(rows=REPORTED):
async def execute(stmt):
return SimpleNamespace(all=lambda: list(rows))
return SimpleNamespace(execute=execute)
@pytest.mark.parametrize("part, kind, label", [
("/", "mount", "/"), ("/nfs/media", "mount", "/nfs/media"), ("core7", "core", "core 7"),
("diskio:sda", "disk", "sda"), ("net:bond0", "net", "bond0"),
("temp:Package id 0", "temp", "Package id 0"), ("corex", "other", "corex")])
def test_a_part_is_known_by_its_name(part, kind, label):
assert alert_svc.part_kind(part) == (kind, label)
async def test_hosts_and_their_parts_are_picked_apart():
got = await alert_svc.rule_target(_db(), "host_agent")
assert got["hosts"] == ["h1", "h2"] and (got["host"], got["resource"]) == ("h1", "h1")
assert got["parts"] == [
("", [("h1", "The whole host")]),
("Mounts", [("h1:/", "/"), ("h1:/var", "/var")]),
("CPU cores", [("h1:core2", "core 2"), ("h1:core10", "core 10")])]
assert got["metrics"] == ["cpu_pct", "uptime_secs", "seconds_since_seen"] # catalog order
async def test_a_part_offers_only_what_it_reports():
got = await alert_svc.rule_target(_db(), "host_agent", resource="h1:/var", metric="cpu_pct")
assert (got["host"], got["resource"]) == ("h1", "h1:/var")
assert got["metrics"] == ["disk_used_pct"] and got["metric"] == "" # cpu_pct isn't a mount's
async def test_picking_another_host_starts_at_the_whole_host():
got = await alert_svc.rule_target(_db(), "host_agent", host="h2", resource="h1:/var")
assert (got["host"], got["resource"]) == ("h2", "h2")
assert got["parts"] == [("", [("h2", "The whole host")])]
async def test_a_rule_s_own_resource_stays_on_offer_after_it_goes_quiet():
got = await alert_svc.rule_target(_db(), "host_agent", resource="h3:/data",
metric="disk_used_pct")
assert "h3" in got["hosts"] and got["host"] == "h3"
assert got["parts"] == [("Mounts", [("h3:/data", "/data")])]
assert got["metric"] == "disk_used_pct"
+8 -5
View File
@@ -119,7 +119,8 @@ async def test_deleting_a_rule_is_on_its_own_page_and_says_what_goes(app):
listing = await render_template("alerts/list.html", active=[], rules=[rule], operators=[])
page = await render_template(
"alerts/rules_form.html", rule=rule, operators=[], source_modules=["host_agent"],
initial_source="host_agent", resources=["web-1"], metrics=["cpu_pct"], ansible_sources=[],
initial_source="host_agent", resources=["web-1"], resource="web-1", metrics=["cpu_pct"],
metric="cpu_pct", ansible_sources=[],
catalog={}, history_count=12, fired=None, notify=[])
assert "/delete" not in listing
assert 'action="/alerts/rules/r1/delete"' in page
@@ -138,7 +139,8 @@ async def test_the_rule_page_is_named_for_the_rule(app):
session["user_role"] = "admin"
page = await render_template(
"alerts/rules_form.html", rule=rule, operators=[(">", ">")], source_modules=["host_agent"],
initial_source="host_agent", resources=["VDNT-Docker02"], metrics=["seconds_since_seen"],
initial_source="host_agent", resources=["VDNT-Docker02"], resource="VDNT-Docker02",
metrics=["seconds_since_seen"], metric="seconds_since_seen",
ansible_sources=[], catalog={}, history_count=10,
fired=datetime.now(timezone.utc) - timedelta(hours=6), notify=["email to me@example.com"])
page = " ".join(page.split())
@@ -265,8 +267,8 @@ async def test_the_rule_form_speaks_in_names_and_units(app):
session["user_role"] = "operator"
page = await render_template(
"alerts/rules_form.html", rule=rule, operators=_OPERATORS,
source_modules=["http", "host_agent"], initial_source="http", resources=["nas"],
metrics=["is_up", "response_ms"], ansible_sources=[],
source_modules=["http", "host_agent"], initial_source="http", resources=["nas"], resource="nas",
metrics=["is_up", "response_ms"], metric="is_up", ansible_sources=[],
catalog={"http": ["is_up", "response_ms"], "snmp": []})
assert ">HTTP monitors</option>" in page and ">Host agent</option>" in page
assert ">Up or down</option>" in page and 'data-unit="ms"' in page
@@ -294,7 +296,8 @@ async def test_a_rule_covering_hosts_lists_its_hosts_under_it(app):
page = await render_template(
"alerts/rules_form.html", rule=parent, operators=[], source_modules=["host_agent"],
initial_source="host_agent", resources=[], metrics=["seconds_since_seen"],
ansible_sources=[], catalog={}, history_count=0, fired=None, notify=[],
metric="seconds_since_seen", ansible_sources=[], catalog={}, history_count=0,
fired=None, notify=[],
covered=["node-a", "node-b"], host_source=True, groups=["swarm"],
current_covers="group", current_group="swarm", covers_locked=True)
listing, page = " ".join(listing.split()), " ".join(page.split())