feat(monitors): unify ping/dns/http into one Monitor entity + custom targets
Collapse the three former check types into a single core `Monitor` entity
with one management surface (/monitors), one result table (monitor_results),
and a single scheduled task. Every type can now watch a free-standing custom
destination (optional host_id) — not just a registered Host.
- models: Monitor + MonitorResult replace PingResult/DnsResult; Host loses its
ping/dns facet columns (now Monitor rows linked by host_id).
- checks: monitors/{ping,dns,http}.py pure probes + runner.run_monitor
dispatcher; one monitor_check scheduler with a per-monitor due-filter.
- status: single monitor_status_source replaces the three sources.
- UI: /monitors blueprint (type-aware add/edit/list/widget); host hub shows a
host's linked monitors + "add monitor for this host"; nav + widget registry
+ alert metric catalog rewired. http plugin folded into core and removed.
- migration 0022 merges the http branch, data-migrates host facets +
http_monitors + all three result histories, drops the old tables/columns.
Resolves the per-host ping/dns auto-attach issue (#275): monitors are now
explicit, never auto-added to every host.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016Jg27rgypiW2efULXJDtMC
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
"""Integration: unified Monitor schema + status source against live Postgres.
|
||||
|
||||
Validates the 0022 unification migration applied (monitors/monitor_results
|
||||
exist, the old per-type tables are gone) and that monitor_status_source rolls
|
||||
up MonitorResult history into a StatusEntry. Requires STEWARD_DATABASE_URL.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
|
||||
_NEEDS_DB = pytest.mark.skipif(
|
||||
not os.environ.get("STEWARD_DATABASE_URL"),
|
||||
reason="integration test needs a live Postgres (STEWARD_DATABASE_URL)",
|
||||
)
|
||||
|
||||
|
||||
@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)
|
||||
|
||||
|
||||
@_NEEDS_DB
|
||||
def test_old_monitor_tables_dropped(app):
|
||||
from sqlalchemy import text
|
||||
|
||||
async def _go():
|
||||
async with app.db_sessionmaker() as s:
|
||||
# New tables exist…
|
||||
await s.execute(text("SELECT COUNT(*) FROM monitors"))
|
||||
await s.execute(text("SELECT COUNT(*) FROM monitor_results"))
|
||||
# …old ones are gone (regclass of a missing table is NULL).
|
||||
for tbl in ("ping_results", "dns_results", "http_monitors", "http_results"):
|
||||
missing = (await s.execute(
|
||||
text("SELECT to_regclass(:t)"), {"t": tbl})).scalar()
|
||||
assert missing is None, f"{tbl} should have been dropped"
|
||||
|
||||
asyncio.run(_go())
|
||||
|
||||
|
||||
@_NEEDS_DB
|
||||
def test_status_source_rolls_up_results(app):
|
||||
from sqlalchemy import text
|
||||
from steward.models.monitors import Monitor, MonitorResult
|
||||
from steward.core.status import monitor_status_source
|
||||
|
||||
mid = str(uuid.uuid4())
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
async def _go():
|
||||
async with app.db_sessionmaker() as s:
|
||||
async with s.begin():
|
||||
# Clean slate for a deterministic assertion.
|
||||
await s.execute(text("DELETE FROM monitor_results"))
|
||||
await s.execute(text("DELETE FROM monitors"))
|
||||
s.add(Monitor(id=mid, name="custom-http", type="http",
|
||||
target="https://example.test", host_id=None,
|
||||
config_json="{}", enabled=True))
|
||||
async with s.begin():
|
||||
for i, up in enumerate([True, True, False, True]):
|
||||
s.add(MonitorResult(
|
||||
id=str(uuid.uuid4()), monitor_id=mid,
|
||||
checked_at=now - timedelta(minutes=4 - i),
|
||||
is_up=up, response_ms=10.0 if up else None,
|
||||
))
|
||||
entries = await monitor_status_source(s)
|
||||
return entries
|
||||
|
||||
entries = asyncio.run(_go())
|
||||
assert len(entries) == 1
|
||||
e = entries[0]
|
||||
assert e.kind == "http" and e.name == "custom-http"
|
||||
assert e.target == "https://example.test"
|
||||
assert e.status == "up" # latest result is up
|
||||
assert len(e.heartbeat) == 4
|
||||
assert e.uptime["24h"] == 75.0 # 3 of 4 up
|
||||
Reference in New Issue
Block a user