|
| 1 | +"""Regression tests for the public 90-day history roll-up. |
| 2 | +
|
| 3 | +These reproduce the two production symptoms observed on the live status page: |
| 4 | +
|
| 5 | +* an always-yellow trailing bar caused by a short ``unknown`` scrape gap on the |
| 6 | + current (partial) day, even though the component never went degraded/down; and |
| 7 | +* uptime collapsing toward ~0% because historical no-data days were counted as |
| 8 | + downtime. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from datetime import UTC, datetime, timedelta |
| 14 | + |
| 15 | +import pytest |
| 16 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 17 | + |
| 18 | +from ai_horde_service_alerts.db.models import Component, ComponentStatusHistory |
| 19 | +from ai_horde_service_alerts.db.repositories.history import HistoryRepository |
| 20 | +from ai_horde_service_alerts.db.types import Audience, ComponentStatusValue, HistorySource |
| 21 | + |
| 22 | +NOON_TODAY = datetime(2026, 6, 21, 12, 0, tzinfo=UTC) |
| 23 | +COMPONENT_ID = "api" |
| 24 | + |
| 25 | + |
| 26 | +async def _seed_component(session: AsyncSession) -> None: |
| 27 | + session.add( |
| 28 | + Component( |
| 29 | + id=COMPONENT_ID, |
| 30 | + name="API", |
| 31 | + description="", |
| 32 | + audience=Audience.PUBLIC, |
| 33 | + ), |
| 34 | + ) |
| 35 | + await session.flush() |
| 36 | + |
| 37 | + |
| 38 | +def _slice( |
| 39 | + status: ComponentStatusValue, |
| 40 | + started_at: datetime, |
| 41 | + ended_at: datetime | None, |
| 42 | +) -> ComponentStatusHistory: |
| 43 | + return ComponentStatusHistory( |
| 44 | + component_id=COMPONENT_ID, |
| 45 | + status=status, |
| 46 | + source=HistorySource.PROBER, |
| 47 | + started_at=started_at, |
| 48 | + ended_at=ended_at, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_trailing_unknown_gap_does_not_paint_bar_minor(db_session: AsyncSession) -> None: |
| 54 | + """A ~71-minute unknown gap today, with no degraded/down time, stays level 0.""" |
| 55 | + await _seed_component(db_session) |
| 56 | + day_start = NOON_TODAY.replace(hour=0, minute=0, second=0, microsecond=0) |
| 57 | + gap_start = NOON_TODAY - timedelta(seconds=4254) |
| 58 | + # operational -> unknown (scrape gap) -> operational, all on the current day. |
| 59 | + db_session.add_all( |
| 60 | + [ |
| 61 | + _slice(ComponentStatusValue.OPERATIONAL, day_start, gap_start), |
| 62 | + _slice(ComponentStatusValue.UNKNOWN, gap_start, NOON_TODAY - timedelta(seconds=600)), |
| 63 | + _slice(ComponentStatusValue.OPERATIONAL, NOON_TODAY - timedelta(seconds=600), None), |
| 64 | + ], |
| 65 | + ) |
| 66 | + await db_session.flush() |
| 67 | + |
| 68 | + repo = HistoryRepository(db_session) |
| 69 | + buckets = await repo.daily_buckets(COMPONENT_ID, days=1, now=NOON_TODAY) |
| 70 | + |
| 71 | + today = buckets[-1] |
| 72 | + assert today.degraded_seconds == 0 |
| 73 | + assert today.down_seconds == 0 |
| 74 | + assert today.maintenance_seconds == 0 |
| 75 | + assert today.unknown_seconds > 0 # the gap is recorded... |
| 76 | + assert today.status_level == 0 # ...but it must NOT fold the bar to minor |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.asyncio |
| 80 | +async def test_uptime_excludes_no_data_days(db_session: AsyncSession) -> None: |
| 81 | + """No-data historical days must not count as downtime (was driving ~0.5%).""" |
| 82 | + await _seed_component(db_session) |
| 83 | + # The only signal in a 90-day window is one fully-operational slice covering |
| 84 | + # the last ~2 days. Everything before it is a genuine no-data gap. |
| 85 | + db_session.add( |
| 86 | + _slice(ComponentStatusValue.OPERATIONAL, NOON_TODAY - timedelta(days=2), None), |
| 87 | + ) |
| 88 | + await db_session.flush() |
| 89 | + |
| 90 | + repo = HistoryRepository(db_session) |
| 91 | + uptime = await repo.uptime_percent(COMPONENT_ID, days=90, now=NOON_TODAY) |
| 92 | + |
| 93 | + assert uptime == 100.0 |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_uptime_none_when_no_signal_at_all(db_session: AsyncSession) -> None: |
| 98 | + """A window with no operational/degraded/down signal returns None, not 0/100.""" |
| 99 | + await _seed_component(db_session) |
| 100 | + repo = HistoryRepository(db_session) |
| 101 | + |
| 102 | + uptime = await repo.uptime_percent(COMPONENT_ID, days=90, now=NOON_TODAY) |
| 103 | + |
| 104 | + assert uptime is None |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.asyncio |
| 108 | +async def test_uptime_counts_real_downtime(db_session: AsyncSession) -> None: |
| 109 | + """Sanity: actual down time is reflected in the ratio (operational / signal).""" |
| 110 | + await _seed_component(db_session) |
| 111 | + start = NOON_TODAY - timedelta(days=1) |
| 112 | + midpoint = start + timedelta(hours=12) |
| 113 | + db_session.add_all( |
| 114 | + [ |
| 115 | + _slice(ComponentStatusValue.OPERATIONAL, start, midpoint), |
| 116 | + _slice(ComponentStatusValue.DOWN, midpoint, NOON_TODAY), |
| 117 | + ], |
| 118 | + ) |
| 119 | + await db_session.flush() |
| 120 | + |
| 121 | + repo = HistoryRepository(db_session) |
| 122 | + uptime = await repo.uptime_percent(COMPONENT_ID, days=2, now=NOON_TODAY) |
| 123 | + |
| 124 | + assert uptime == pytest.approx(50.0, abs=0.5) |
0 commit comments