decomposer: generate deliverable files for Define the service contract and project architecture for the FastAPI endpoint monitoring service.; Implement the typed monitor CRUD API and concurrency-safe in-memory state according to the service design.; Implement secure on-demand endpoint checks with status updates, latency measurement, robust error handling, and redacted structured logs.; Add operational API endpoints and environment-driven runtime configuration to the monitoring service.; Create automated tests for the monitoring service.; Package the service with Docker and developer documentation.; Validate the complete project.
This commit is contained in:
69
app/store.py
69
app/store.py
@@ -1,68 +1,53 @@
|
||||
import asyncio
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timezone
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from app.models import CheckStatus, Monitor, MonitorCreate, MonitorUpdate
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Snapshot:
|
||||
monitor: Monitor
|
||||
revision: int
|
||||
from .models import CheckResult, Monitor, MonitorCreate, MonitorStatus, MonitorUpdate, utcnow
|
||||
|
||||
|
||||
@dataclass
|
||||
class MonitorStore:
|
||||
def __init__(self) -> None:
|
||||
def __post_init__(self) -> None:
|
||||
self._items: dict[UUID, Monitor] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
self._items: dict[UUID, tuple[Monitor, int]] = {}
|
||||
|
||||
async def create(self, data: MonitorCreate) -> Monitor:
|
||||
now = datetime.now(timezone.utc)
|
||||
item = Monitor(id=uuid4(), name=data.name, url=data.url, created_at=now,
|
||||
updated_at=now, status=CheckStatus())
|
||||
now = utcnow()
|
||||
item = Monitor(id=uuid4(), name=data.name, url=data.url, created_at=now, updated_at=now)
|
||||
async with self._lock:
|
||||
self._items[item.id] = (item, 1)
|
||||
return item.model_copy(deep=True)
|
||||
self._items[item.id] = item
|
||||
return item
|
||||
|
||||
async def list(self) -> list[Monitor]:
|
||||
async with self._lock:
|
||||
values = [item.model_copy(deep=True) for item, _ in self._items.values()]
|
||||
return sorted(values, key=lambda item: (item.created_at, str(item.id)))
|
||||
return sorted(self._items.values(), key=lambda item: (item.created_at, str(item.id)))
|
||||
|
||||
async def snapshot(self, monitor_id: UUID) -> Snapshot | None:
|
||||
async def get(self, monitor_id: UUID) -> Monitor | None:
|
||||
async with self._lock:
|
||||
stored = self._items.get(monitor_id)
|
||||
if stored is None:
|
||||
return None
|
||||
item, revision = stored
|
||||
return Snapshot(item.model_copy(deep=True), revision)
|
||||
return self._items.get(monitor_id)
|
||||
|
||||
async def update(self, monitor_id: UUID, data: MonitorUpdate) -> Monitor | None:
|
||||
async with self._lock:
|
||||
stored = self._items.get(monitor_id)
|
||||
if stored is None:
|
||||
current = self._items.get(monitor_id)
|
||||
if current is None:
|
||||
return None
|
||||
item, revision = stored
|
||||
changes = data.model_dump(exclude_none=True)
|
||||
if "url" in changes:
|
||||
changes["status"] = CheckStatus()
|
||||
changes["updated_at"] = datetime.now(timezone.utc)
|
||||
updated = item.model_copy(update=changes)
|
||||
self._items[monitor_id] = (updated, revision + 1)
|
||||
return updated.model_copy(deep=True)
|
||||
updated = current.model_copy(update={
|
||||
"name": data.name, "url": data.url, "updated_at": utcnow(),
|
||||
"status": MonitorStatus.unknown, "checked_at": None,
|
||||
"latency_ms": None, "http_status": None, "error": None,
|
||||
})
|
||||
self._items[monitor_id] = updated
|
||||
return updated
|
||||
|
||||
async def delete(self, monitor_id: UUID) -> bool:
|
||||
async with self._lock:
|
||||
return self._items.pop(monitor_id, None) is not None
|
||||
|
||||
async def apply_status(self, monitor_id: UUID, revision: int,
|
||||
status: CheckStatus) -> bool:
|
||||
async def record(self, monitor_id: UUID, result: CheckResult) -> Monitor | None:
|
||||
async with self._lock:
|
||||
stored = self._items.get(monitor_id)
|
||||
if stored is None or stored[1] != revision:
|
||||
return False
|
||||
item, current_revision = stored
|
||||
updated = item.model_copy(update={"status": status})
|
||||
self._items[monitor_id] = (updated, current_revision)
|
||||
return True
|
||||
current = self._items.get(monitor_id)
|
||||
if current is None:
|
||||
return None
|
||||
updated = current.model_copy(update={**result.model_dump(), "updated_at": utcnow()})
|
||||
self._items[monitor_id] = updated
|
||||
return updated
|
||||
|
||||
Reference in New Issue
Block a user