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:
53
src/monitor_service/store.py
Normal file
53
src/monitor_service/store.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import asyncio
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from .models import CurrentStatus, Monitor, MonitorCreate, MonitorUpdate, utc_now
|
||||
|
||||
|
||||
class MonitorStore:
|
||||
"""Concurrency-safe process-local monitor state."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._items: dict[UUID, Monitor] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def create(self, data: MonitorCreate) -> Monitor:
|
||||
now = utc_now()
|
||||
item = Monitor(
|
||||
id=uuid4(), name=data.name, url=data.url, created_at=now,
|
||||
updated_at=now, status=CurrentStatus(),
|
||||
)
|
||||
async with self._lock:
|
||||
self._items[item.id] = item
|
||||
return item
|
||||
|
||||
async def list(self) -> list[Monitor]:
|
||||
async with self._lock:
|
||||
return sorted(self._items.values(), key=lambda item: item.created_at)
|
||||
|
||||
async def get(self, monitor_id: UUID) -> Monitor | None:
|
||||
async with self._lock:
|
||||
return self._items.get(monitor_id)
|
||||
|
||||
async def update(self, monitor_id: UUID, data: MonitorUpdate) -> Monitor | None:
|
||||
changes = data.model_dump(exclude_unset=True)
|
||||
async with self._lock:
|
||||
item = self._items.get(monitor_id)
|
||||
if item is None:
|
||||
return None
|
||||
updated = item.model_copy(update={**changes, "updated_at": utc_now()})
|
||||
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 set_status(self, monitor_id: UUID, status: CurrentStatus) -> Monitor | None:
|
||||
async with self._lock:
|
||||
item = self._items.get(monitor_id)
|
||||
if item is None:
|
||||
return None
|
||||
updated = item.model_copy(update={"status": status})
|
||||
self._items[monitor_id] = updated
|
||||
return updated
|
||||
Reference in New Issue
Block a user