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:
71
app/store.py
71
app/store.py
@@ -1,29 +1,28 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID
|
||||||
|
|
||||||
from app.models import CurrentStatus, Monitor, MonitorCreate, MonitorUpdate, utc_now
|
from .models import CheckResult, Monitor, MonitorCreate, MonitorStatus, MonitorUpdate, now_utc
|
||||||
|
|
||||||
|
|
||||||
class MonitorNotFound(KeyError):
|
class NotFoundError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class StaleCheckError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MonitorStore:
|
class MonitorStore:
|
||||||
"""Copy-in/copy-out process-local store guarded by one asyncio lock."""
|
def __init__(self, default_timeout: float, max_timeout: float) -> None:
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self._items: dict[UUID, Monitor] = {}
|
self._items: dict[UUID, Monitor] = {}
|
||||||
self._lock = asyncio.Lock()
|
self._lock = asyncio.Lock()
|
||||||
|
self._default_timeout = default_timeout
|
||||||
|
self._max_timeout = max_timeout
|
||||||
|
|
||||||
async def create(self, data: MonitorCreate) -> Monitor:
|
async def create(self, data: MonitorCreate) -> Monitor:
|
||||||
now = utc_now()
|
timeout = data.timeout_seconds or self._default_timeout
|
||||||
monitor = Monitor(
|
self._validate_timeout(timeout)
|
||||||
id=uuid4(),
|
monitor = Monitor(name=data.name, url=data.url, timeout_seconds=timeout)
|
||||||
created_at=now,
|
|
||||||
updated_at=now,
|
|
||||||
current_status=CurrentStatus(),
|
|
||||||
**data.model_dump(),
|
|
||||||
)
|
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
self._items[monitor.id] = monitor
|
self._items[monitor.id] = monitor
|
||||||
return monitor.model_copy(deep=True)
|
return monitor.model_copy(deep=True)
|
||||||
@@ -37,29 +36,41 @@ class MonitorStore:
|
|||||||
async with self._lock:
|
async with self._lock:
|
||||||
item = self._items.get(monitor_id)
|
item = self._items.get(monitor_id)
|
||||||
if item is None:
|
if item is None:
|
||||||
raise MonitorNotFound(monitor_id)
|
raise NotFoundError
|
||||||
return item.model_copy(deep=True)
|
return item.model_copy(deep=True)
|
||||||
|
|
||||||
async def update(self, monitor_id: UUID, data: MonitorUpdate) -> Monitor:
|
async def update(self, monitor_id: UUID, data: MonitorUpdate) -> Monitor:
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
item = self._items.get(monitor_id)
|
current = self._items.get(monitor_id)
|
||||||
if item is None:
|
if current is None:
|
||||||
raise MonitorNotFound(monitor_id)
|
raise NotFoundError
|
||||||
updated = item.model_copy(update={**data.model_dump(), "updated_at": utc_now()})
|
changes = data.model_dump(exclude_unset=True)
|
||||||
|
if "timeout_seconds" in changes:
|
||||||
|
self._validate_timeout(changes["timeout_seconds"])
|
||||||
|
material = "url" in changes or "timeout_seconds" in changes
|
||||||
|
changes["updated_at"] = now_utc()
|
||||||
|
if material:
|
||||||
|
changes["status"] = MonitorStatus()
|
||||||
|
updated = current.model_copy(update=changes, deep=True)
|
||||||
self._items[monitor_id] = updated
|
self._items[monitor_id] = updated
|
||||||
return updated.model_copy(deep=True)
|
return updated.model_copy(deep=True)
|
||||||
|
|
||||||
async def set_status(self, monitor_id: UUID, status: CurrentStatus) -> CurrentStatus:
|
|
||||||
async with self._lock:
|
|
||||||
item = self._items.get(monitor_id)
|
|
||||||
if item is None:
|
|
||||||
raise MonitorNotFound(monitor_id)
|
|
||||||
self._items[monitor_id] = item.model_copy(
|
|
||||||
update={"current_status": status.model_copy(deep=True), "updated_at": utc_now()}
|
|
||||||
)
|
|
||||||
return status.model_copy(deep=True)
|
|
||||||
|
|
||||||
async def delete(self, monitor_id: UUID) -> None:
|
async def delete(self, monitor_id: UUID) -> None:
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
if self._items.pop(monitor_id, None) is None:
|
if self._items.pop(monitor_id, None) is None:
|
||||||
raise MonitorNotFound(monitor_id)
|
raise NotFoundError
|
||||||
|
|
||||||
|
async def apply_check(self, monitor_id: UUID, expected_url: str, result: CheckResult) -> Monitor:
|
||||||
|
async with self._lock:
|
||||||
|
current = self._items.get(monitor_id)
|
||||||
|
if current is None:
|
||||||
|
raise NotFoundError
|
||||||
|
if str(current.url) != expected_url:
|
||||||
|
raise StaleCheckError
|
||||||
|
updated = current.model_copy(update={"status": result, "updated_at": now_utc()}, deep=True)
|
||||||
|
self._items[monitor_id] = updated
|
||||||
|
return updated.model_copy(deep=True)
|
||||||
|
|
||||||
|
def _validate_timeout(self, timeout: float) -> None:
|
||||||
|
if timeout > self._max_timeout:
|
||||||
|
raise ValueError(f"timeout_seconds must not exceed {self._max_timeout}")
|
||||||
|
|||||||
Reference in New Issue
Block a user