perf(monitors): a monitor's latest results by a LATERAL probe, not row_number over its history (#5208)
_last_n_results (Status, the monitors list, host monitors), host_status (hosts overview, list_hosts) and the report's down-monitor list each ranked every result a monitor ever recorded to keep the newest few. Each monitor now gets ORDER BY checked_at DESC LIMIT n in a LATERAL subquery, which walks ix_monitor_results_monitor_checked backwards and stops. Also fixes the latest-table test, which read between two begin() blocks and so autobegan a transaction the second block then refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+22
-14
@@ -105,25 +105,33 @@ def sparkline_svg(values: list[float], width: int = 80, height: int = 20,
|
||||
|
||||
async def _last_n_results(db, monitor_ids: list[str],
|
||||
n: int = HEARTBEAT_COUNT) -> dict[str, list]:
|
||||
"""Return {monitor_id: [MonitorResult]} of the last n results, oldest-first."""
|
||||
from steward.models.monitors import MonitorResult
|
||||
"""Return {monitor_id: [MonitorResult]} of the last n results, oldest-first.
|
||||
|
||||
One LATERAL probe per monitor walks ix_monitor_results_monitor_checked
|
||||
backwards and stops after n rows. A row_number() over each monitor's
|
||||
results read its whole history first, which held Status and the monitor
|
||||
widgets at ~16 s (#5208).
|
||||
"""
|
||||
from sqlalchemy import true
|
||||
from sqlalchemy.orm import aliased
|
||||
from steward.models.monitors import Monitor, MonitorResult
|
||||
if not monitor_ids:
|
||||
return {}
|
||||
rn = func.row_number().over(
|
||||
partition_by=MonitorResult.monitor_id, order_by=MonitorResult.checked_at.desc()
|
||||
).label("rn")
|
||||
subq = select(MonitorResult.id, rn).where(
|
||||
MonitorResult.monitor_id.in_(monitor_ids)
|
||||
).subquery()
|
||||
res = await db.execute(
|
||||
ids = select(Monitor.id.label("mid")).where(Monitor.id.in_(monitor_ids)).subquery("wanted")
|
||||
recent = (
|
||||
select(MonitorResult)
|
||||
.join(subq, MonitorResult.id == subq.c.id)
|
||||
.where(subq.c.rn <= n)
|
||||
.order_by(MonitorResult.monitor_id, MonitorResult.checked_at.asc())
|
||||
.where(MonitorResult.monitor_id == ids.c.mid)
|
||||
.order_by(MonitorResult.checked_at.desc())
|
||||
.limit(n)
|
||||
.lateral("recent")
|
||||
)
|
||||
row = aliased(MonitorResult, recent)
|
||||
res = await db.execute(select(row).select_from(ids).join(recent, true()))
|
||||
out: dict[str, list] = {mid: [] for mid in monitor_ids}
|
||||
for row in res.scalars():
|
||||
out[row.monitor_id].append(row)
|
||||
for r in res.scalars():
|
||||
out[r.monitor_id].append(r)
|
||||
for rows in out.values():
|
||||
rows.sort(key=lambda r: r.checked_at)
|
||||
return out
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user