#5473 "Opening an alert rule takes up to 15 s: the Resource picker scans every raw host-agent sample". The access log showed the edit page at 6-15 s (one load gave up) while /alerts/ took 0.2 s. resources_for ran SELECT DISTINCT over plugin_metrics, and Postgres has no skip scan, so it read every host-agent sample; every rule page, source swap, rule save and MCP rule call paid it. resources_for and metrics_for now read plugin_metric_latest, one row per series, which lists the same names. A test fails if either reads plugin_metrics again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
28 lines
1.0 KiB
Python
28 lines
1.0 KiB
Python
"""The rule form's Resource and Metric pickers read the latest-value table,
|
|
one row per series, never the raw history: a DISTINCT over plugin_metrics
|
|
reads every sample and held the rule page for up to 15 s on host_agent."""
|
|
from types import SimpleNamespace
|
|
|
|
from steward.alerts import service as alert_svc
|
|
|
|
|
|
async def _sql(fn, source) -> list[str]:
|
|
"""The SQL `fn` sends for `source`, against a session that returns no rows."""
|
|
sent: list[str] = []
|
|
|
|
async def execute(stmt):
|
|
sent.append(str(stmt.compile(compile_kwargs={"literal_binds": True})))
|
|
return []
|
|
await fn(SimpleNamespace(execute=execute), source)
|
|
return sent
|
|
|
|
|
|
async def test_resources_come_from_the_latest_value_table():
|
|
(sql,) = await _sql(alert_svc.resources_for, "host_agent")
|
|
assert "FROM plugin_metric_latest" in sql and "plugin_metrics" not in sql
|
|
|
|
|
|
async def test_a_dynamic_source_s_metrics_come_from_the_latest_value_table():
|
|
(sql,) = await _sql(alert_svc.metrics_for, "snmp")
|
|
assert "FROM plugin_metric_latest" in sql and "plugin_metrics" not in sql
|