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.
Some checks failed
ci / test (push) Has been cancelled
Some checks failed
ci / test (push) Has been cancelled
This commit is contained in:
61
app/store.py
61
app/store.py
@@ -1,63 +1,58 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
from app.models import CurrentStatus, Monitor, MonitorCreate, MonitorUpdate, utc_now
|
from .models import CheckSnapshot, Monitor, MonitorInput, now
|
||||||
|
|
||||||
|
|
||||||
|
class StoreFullError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MonitorStore:
|
class MonitorStore:
|
||||||
def __init__(self) -> None:
|
def __init__(self, max_monitors: int) -> None:
|
||||||
self._lock = asyncio.Lock()
|
|
||||||
self._items: dict[UUID, Monitor] = {}
|
self._items: dict[UUID, Monitor] = {}
|
||||||
|
self._lock = asyncio.Lock()
|
||||||
|
self._max = max_monitors
|
||||||
|
|
||||||
async def create(self, value: MonitorCreate) -> Monitor:
|
async def create(self, data: MonitorInput) -> Monitor:
|
||||||
now = utc_now()
|
|
||||||
item = Monitor(
|
|
||||||
id=uuid4(), name=value.name, url=value.url, created_at=now, updated_at=now, revision=1
|
|
||||||
)
|
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
|
if len(self._items) >= self._max:
|
||||||
|
raise StoreFullError
|
||||||
|
timestamp = now()
|
||||||
|
item = Monitor(id=uuid4(), **data.model_dump(), created_at=timestamp,
|
||||||
|
updated_at=timestamp, revision=1, status=CheckSnapshot())
|
||||||
self._items[item.id] = item
|
self._items[item.id] = item
|
||||||
return item.model_copy(deep=True)
|
return item
|
||||||
|
|
||||||
async def list(self) -> list[Monitor]:
|
async def list(self) -> list[Monitor]:
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
values = sorted(self._items.values(), key=lambda item: item.created_at)
|
return sorted(self._items.values(), key=lambda item: item.created_at)
|
||||||
return [item.model_copy(deep=True) for item in values]
|
|
||||||
|
|
||||||
async def get(self, monitor_id: UUID) -> Monitor | None:
|
async def get(self, monitor_id: UUID) -> Monitor | None:
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
item = self._items.get(monitor_id)
|
return self._items.get(monitor_id)
|
||||||
return item.model_copy(deep=True) if item else None
|
|
||||||
|
|
||||||
async def update(self, monitor_id: UUID, value: MonitorUpdate) -> Monitor | None:
|
async def replace(self, monitor_id: UUID, data: MonitorInput) -> Monitor | None:
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
old = self._items.get(monitor_id)
|
old = self._items.get(monitor_id)
|
||||||
if old is None:
|
if old is None:
|
||||||
return None
|
return None
|
||||||
changed_url = old.url != value.url
|
item = old.model_copy(update={**data.model_dump(), "updated_at": now(),
|
||||||
item = old.model_copy(
|
"revision": old.revision + 1,
|
||||||
update={
|
"status": CheckSnapshot()})
|
||||||
"name": value.name,
|
|
||||||
"url": value.url,
|
|
||||||
"updated_at": utc_now(),
|
|
||||||
"revision": old.revision + 1,
|
|
||||||
"current_status": None if changed_url else old.current_status,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
self._items[monitor_id] = item
|
self._items[monitor_id] = item
|
||||||
return item.model_copy(deep=True)
|
return item
|
||||||
|
|
||||||
async def delete(self, monitor_id: UUID) -> bool:
|
async def delete(self, monitor_id: UUID) -> bool:
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
return self._items.pop(monitor_id, None) is not None
|
return self._items.pop(monitor_id, None) is not None
|
||||||
|
|
||||||
async def set_status_if_current(
|
async def set_status(self, monitor_id: UUID, revision: int,
|
||||||
self, monitor_id: UUID, revision: int, status: CurrentStatus
|
status: CheckSnapshot) -> Monitor | None:
|
||||||
) -> bool:
|
|
||||||
async with self._lock:
|
async with self._lock:
|
||||||
old = self._items.get(monitor_id)
|
old = self._items.get(monitor_id)
|
||||||
if old is None or old.revision != revision:
|
if old is None or old.revision != revision:
|
||||||
return False
|
return None
|
||||||
self._items[monitor_id] = old.model_copy(
|
item = old.model_copy(update={"status": status, "updated_at": now()})
|
||||||
update={"current_status": status, "updated_at": utc_now()}
|
self._items[monitor_id] = item
|
||||||
)
|
return item
|
||||||
return True
|
|
||||||
|
|||||||
Reference in New Issue
Block a user