Improve timestamp handling and event navigation

This commit is contained in:
Stanislav Rossovskii
2026-05-27 11:01:27 +04:00
parent edc51e9c59
commit 71f0035b0b
37 changed files with 485 additions and 109 deletions

View File

@@ -6,6 +6,7 @@ from ..cursors import decode, encode
from ..db import get_session
from ..models import Agent as AgentModel
from ..schemas import Agent, AgentsPage
from ..timeutil import to_utc
router = APIRouter()
@@ -15,7 +16,7 @@ def _to_schema(a: AgentModel) -> Agent:
agent_id=a.agent_id,
hostname=a.hostname,
status=a.status,
last_seen_at=a.last_seen_at,
last_seen_at=to_utc(a.last_seen_at),
features=a.features or {"push": False, "metrics": False},
labels=a.labels or {},
)

View File

@@ -6,6 +6,7 @@ from ..cursors import decode, encode
from ..db import get_session
from ..models import Check
from ..schemas import ID_PATTERN, ChecksPage, CheckState
from ..timeutil import to_utc
router = APIRouter()
@@ -38,7 +39,7 @@ async def list_checks(
agent_id=r.agent_id,
check_id=r.check_id,
status=r.status,
last_observed_at=r.last_observed_at,
last_observed_at=to_utc(r.last_observed_at),
exit_code=r.exit_code,
incident_key=r.incident_key,
)

View File

@@ -16,12 +16,13 @@ from ..schemas import (
StoredCheckResultEvent,
)
from ..services.ingestion import ingest_event
from ..timeutil import to_utc
router = APIRouter()
def _encode_cursor(received_at: datetime, event_id) -> str:
raw = f"{received_at.isoformat()}|{event_id}".encode()
raw = f"{to_utc(received_at).isoformat()}|{event_id}".encode()
return base64.urlsafe_b64encode(raw).decode().rstrip("=")
@@ -30,7 +31,7 @@ def _decode_cursor(cursor: str) -> tuple[datetime, str]:
try:
raw = base64.urlsafe_b64decode(cursor + pad).decode()
ts_str, eid = raw.split("|", 1)
return datetime.fromisoformat(ts_str), eid
return to_utc(datetime.fromisoformat(ts_str)), eid
except (binascii.Error, ValueError) as exc:
raise HTTPException(status_code=400, detail="invalid cursor") from exc
@@ -87,8 +88,8 @@ async def query_events(
event_id=str(r.event_id),
agent_id=r.agent_id,
check_id=r.check_id,
observed_at=r.observed_at,
received_at=r.received_at,
observed_at=to_utc(r.observed_at),
received_at=to_utc(r.received_at),
status=r.status,
exit_code=r.exit_code,
duration_ms=r.duration_ms,

View File

@@ -6,6 +6,7 @@ from ..cursors import decode, decode_datetime, encode
from ..db import get_session
from ..models import Incident as IncidentModel
from ..schemas import Incident, IncidentsPage
from ..timeutil import to_utc
router = APIRouter()
@@ -18,8 +19,8 @@ def _to_schema(i: IncidentModel) -> Incident:
severity=i.severity,
agent_id=i.agent_id,
check_id=i.check_id,
opened_at=i.opened_at,
resolved_at=i.resolved_at,
opened_at=to_utc(i.opened_at),
resolved_at=to_utc(i.resolved_at) if i.resolved_at else None,
summary=i.summary,
)
@@ -49,5 +50,5 @@ async def list_incidents(
next_cursor = None
if len(rows) > limit:
rows = rows[:limit]
next_cursor = encode(rows[-1].opened_at.isoformat(), rows[-1].id)
next_cursor = encode(to_utc(rows[-1].opened_at).isoformat(), rows[-1].id)
return IncidentsPage(items=[_to_schema(r) for r in rows], next_cursor=next_cursor)

View File

@@ -6,6 +6,7 @@ from ..cursors import decode, decode_datetime, encode
from ..db import get_session
from ..models import NotificationOutbox
from ..schemas import NotificationOutboxItem, OutboxPage
from ..timeutil import to_utc
router = APIRouter()
@@ -38,7 +39,7 @@ async def list_outbox(
next_cursor = None
if len(rows) > limit:
rows = rows[:limit]
next_cursor = encode(rows[-1].created_at.isoformat(), rows[-1].id)
next_cursor = encode(to_utc(rows[-1].created_at).isoformat(), rows[-1].id)
return OutboxPage(
items=[
NotificationOutboxItem(
@@ -48,9 +49,9 @@ async def list_outbox(
incident_id=str(r.incident_id),
event_type=r.event_type,
attempts=r.attempts,
next_attempt_at=r.next_attempt_at,
next_attempt_at=to_utc(r.next_attempt_at) if r.next_attempt_at else None,
last_error=r.last_error,
updated_at=r.updated_at,
updated_at=to_utc(r.updated_at),
)
for r in rows
],