fix(alerts): a rule's page doesn't wait for its metric preview (#5485); Send test up top (#5486)
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 1m10s
CI / unit (push) Successful in 1m26s
CI / publish (push) Successful in 1m5s

#5485 "A rule's page waits up to 25 s for its metric preview: a cold 24 h
series read from plugin_metrics". Opening "Agent offline" took 25.6 s; a
cold day of one series measured 18.3 s over MCP, a warm one 63 ms. The
pickers still come with the page from plugin_metric_latest; the preview is
its own request (hx-trigger=load), showing "Reading the last day…" until it
lands. Picker swaps no longer read it either.

#5486 "Send test where it can be found": the button moves from inside "What
happens" to the page header beside History, its result under the verdict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 11:44:19 -04:00
co-authored by Claude Opus 5.5
parent a642b4c37c
commit 73cfbda1da
7 changed files with 40 additions and 23 deletions
+13 -9
View File
@@ -281,9 +281,11 @@ async def rule_target_api():
@alerts_bp.get("/api/metric_preview")
@require_role(UserRole.viewer)
async def metric_preview_api():
"""What the picked metric reads now and over the last day (#5475)."""
"""What the picked metric reads now and over the last day (#5475). Its own
request, after the page: a cold day of one series can take many seconds
to read (#5485), and the page doesn't wait for it."""
async with current_app.db_sessionmaker() as db:
ctx = await _target_from_args(db)
ctx = await _target_from_args(db, with_preview=True)
return await render_template("alerts/_metric_preview.html", **ctx)
@@ -300,28 +302,30 @@ async def cover_groups_api():
async def _target(db, source: str, *, host: str = "", resource: str = "", metric: str = "",
covers: str = "resource", keep: bool = False) -> dict:
"""The rule form's target pickers and the chosen metric's preview (#5475);
`keep` on a rule's own page, so its resource and metric stay on offer."""
covers: str = "resource", keep: bool = False, with_preview: bool = False) -> dict:
"""The rule form's target pickers (#5475), from the latest-value table;
`keep` on a rule's own page, so its resource and metric stay on offer.
The chosen metric's preview only `with_preview`: it reads a day of raw
samples, so pages and picker swaps leave it to its own request (#5485)."""
from steward.core.status import sparkline_svg
target = await alert_svc.rule_target(db, source, host=host, resource=resource, metric=metric,
keep=keep)
preview = None
if target["resource"] and target["metric"]:
if with_preview and target["resource"] and target["metric"]:
preview = await alert_svc.metric_preview(db, source, target["resource"], target["metric"])
if preview:
# From zero to the series' own peak (UI & Design ruling 8), so a
# disk at 61-62% reads flat rather than as a cliff.
preview["spark"] = sparkline_svg(preview["series"], width=120, height=20,
stroke="currentColor", floor=1.0)
return {**target, "preview": preview, "current_covers": covers}
return {**target, "preview": preview, "preview_read": with_preview, "current_covers": covers}
async def _target_from_args(db) -> dict:
async def _target_from_args(db, *, with_preview: bool = False) -> dict:
a = request.args
return await _target(db, a.get("source_module", ""), host=a.get("pick_host", ""),
resource=a.get("resource_name", ""), metric=a.get("metric_name", ""),
covers=a.get("covers", "resource"))
covers=a.get("covers", "resource"), with_preview=with_preview)
async def _cover_choices(db, source: str, rule: AlertRule | None = None) -> dict: