perf(mqtt): serve device status from an in-memory latest-row cache
Device list and device detail took several seconds to show online status
because every /mqtt/status call ran SELECT DISTINCT ON (device_serial) over
the whole heartbeats + alert-event history (one heartbeat row per device
every 30s, kept for the full retention window). /mqtt/latest-metrics and
the 60s ping loop paid the same fleet-wide scans for ping samples and
diagnostics.
- database/latest_cache.py: newest heartbeat / alert event / ping sample /
diagnostics row per device, held in memory. Seeded once at startup
(warm_latest_cache) or lazily on first read; every insert writes through
via RETURNING, keeping whichever row is newer so writes that land while
the seed query runs are never lost. Per-device deletes and the retention
purge drop the matching entries so the cache never outlives the DB.
Single uvicorn process owns the MQTT client, so no cross-worker drift.
- GET /api/mqtt/status/{serial}: one device's status (cache, or a single
indexed lookup) so DeviceDetail no longer downloads the whole fleet.
- devices router: 11 GET/notes/tags handlers were `async def` but call the
synchronous Firestore client, blocking the event loop (MQTT ingest,
WebSocket, every other request) for each Firestore round trip. They are
plain `def` now so FastAPI runs them in its threadpool.
- DELETE /api/mqtt/logs/{serial}?source=&level=&until=: filtered delete of
console-stored device logs, for the Health tab's new "Delete logs".
Requires devices:edit and is audit-logged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+11
-11
@@ -27,7 +27,7 @@ CRM_COLLECTION = "crm_customers"
|
||||
|
||||
|
||||
@router.get("", response_model=DeviceListResponse)
|
||||
async def list_devices(
|
||||
def list_devices(
|
||||
search: Optional[str] = Query(None),
|
||||
online: Optional[bool] = Query(None),
|
||||
tier: Optional[str] = Query(None),
|
||||
@@ -42,7 +42,7 @@ async def list_devices(
|
||||
|
||||
|
||||
@router.get("/{device_id}", response_model=DeviceInDB)
|
||||
async def get_device(
|
||||
def get_device(
|
||||
device_id: str,
|
||||
_user: TokenPayload = Depends(require_permission("devices", "view")),
|
||||
):
|
||||
@@ -50,7 +50,7 @@ async def get_device(
|
||||
|
||||
|
||||
@router.get("/{device_id}/users", response_model=DeviceUsersResponse)
|
||||
async def get_device_users(
|
||||
def get_device_users(
|
||||
device_id: str,
|
||||
_user: TokenPayload = Depends(require_permission("devices", "view")),
|
||||
):
|
||||
@@ -119,7 +119,7 @@ async def get_device_alerts(
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/{device_id}/notes")
|
||||
async def list_device_notes(
|
||||
def list_device_notes(
|
||||
device_id: str,
|
||||
_user: TokenPayload = Depends(require_permission("devices", "view")),
|
||||
):
|
||||
@@ -139,7 +139,7 @@ async def list_device_notes(
|
||||
|
||||
|
||||
@router.post("/{device_id}/notes", status_code=201)
|
||||
async def create_device_note(
|
||||
def create_device_note(
|
||||
device_id: str,
|
||||
body: DeviceNoteCreate,
|
||||
_user: TokenPayload = Depends(require_permission("devices", "edit")),
|
||||
@@ -163,7 +163,7 @@ async def create_device_note(
|
||||
|
||||
|
||||
@router.put("/{device_id}/notes/{note_id}")
|
||||
async def update_device_note(
|
||||
def update_device_note(
|
||||
device_id: str,
|
||||
note_id: str,
|
||||
body: DeviceNoteUpdate,
|
||||
@@ -187,7 +187,7 @@ async def update_device_note(
|
||||
|
||||
|
||||
@router.delete("/{device_id}/notes/{note_id}", status_code=204)
|
||||
async def delete_device_note(
|
||||
def delete_device_note(
|
||||
device_id: str,
|
||||
note_id: str,
|
||||
_user: TokenPayload = Depends(require_permission("devices", "edit")),
|
||||
@@ -210,7 +210,7 @@ class TagsUpdate(BaseModel):
|
||||
|
||||
|
||||
@router.put("/{device_id}/tags", response_model=DeviceInDB)
|
||||
async def update_device_tags(
|
||||
def update_device_tags(
|
||||
device_id: str,
|
||||
body: TagsUpdate,
|
||||
_user: TokenPayload = Depends(require_permission("devices", "edit")),
|
||||
@@ -236,7 +236,7 @@ class AssignCustomerBody(BaseModel):
|
||||
|
||||
|
||||
@router.get("/{device_id}/customer-search")
|
||||
async def search_customers_for_device(
|
||||
def search_customers_for_device(
|
||||
device_id: str,
|
||||
q: str = Query(""),
|
||||
_user: TokenPayload = Depends(require_permission("devices", "view")),
|
||||
@@ -357,7 +357,7 @@ async def unassign_device_from_customer(
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/{device_id}/customer")
|
||||
async def get_device_customer(
|
||||
def get_device_customer(
|
||||
device_id: str,
|
||||
_user: TokenPayload = Depends(require_permission("devices", "view")),
|
||||
):
|
||||
@@ -399,7 +399,7 @@ class UserSearchResult(BaseModel):
|
||||
|
||||
|
||||
@router.get("/{device_id}/user-search")
|
||||
async def search_users_for_device(
|
||||
def search_users_for_device(
|
||||
device_id: str,
|
||||
q: str = Query(""),
|
||||
_user: TokenPayload = Depends(require_permission("devices", "view")),
|
||||
|
||||
Reference in New Issue
Block a user