Spaces:
Sleeping
Sleeping
Update app/routers/devices.py
Browse files- app/routers/devices.py +8 -9
app/routers/devices.py
CHANGED
|
@@ -3,6 +3,7 @@ from datetime import datetime, timedelta, timezone
|
|
| 3 |
from fastapi import APIRouter
|
| 4 |
|
| 5 |
from app.models.schemas import (
|
|
|
|
| 6 |
DeviceStatusRow,
|
| 7 |
HeartbeatRequest,
|
| 8 |
HeartbeatResponse,
|
|
@@ -58,16 +59,16 @@ async def heartbeat(payload: HeartbeatRequest) -> HeartbeatResponse:
|
|
| 58 |
return HeartbeatResponse()
|
| 59 |
|
| 60 |
|
| 61 |
-
@router.get("")
|
| 62 |
-
async def list_devices() -> list[
|
| 63 |
now = datetime.now(timezone.utc)
|
| 64 |
online_threshold = now - timedelta(seconds=90)
|
| 65 |
rows = await device_repo.list()
|
| 66 |
-
output: list[
|
| 67 |
for row in rows:
|
| 68 |
last_heartbeat_at = _to_utc_aware(row.get("last_heartbeat_at"))
|
| 69 |
output.append(
|
| 70 |
-
|
| 71 |
device_id=row.get("device_id", ""),
|
| 72 |
hostname=row.get("hostname", ""),
|
| 73 |
agent_version=row.get("agent_version", ""),
|
|
@@ -76,12 +77,10 @@ async def list_devices() -> list[dict[str, str | bool]]:
|
|
| 76 |
active_restriction_window=bool(
|
| 77 |
row.get("active_restriction_window", False)
|
| 78 |
),
|
| 79 |
-
|
| 80 |
-
| {
|
| 81 |
-
"is_online": bool(
|
| 82 |
last_heartbeat_at is not None
|
| 83 |
and last_heartbeat_at >= online_threshold
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
)
|
| 87 |
return output
|
|
|
|
| 3 |
from fastapi import APIRouter
|
| 4 |
|
| 5 |
from app.models.schemas import (
|
| 6 |
+
DeviceStatusResponse,
|
| 7 |
DeviceStatusRow,
|
| 8 |
HeartbeatRequest,
|
| 9 |
HeartbeatResponse,
|
|
|
|
| 59 |
return HeartbeatResponse()
|
| 60 |
|
| 61 |
|
| 62 |
+
@router.get("", response_model=list[DeviceStatusResponse])
|
| 63 |
+
async def list_devices() -> list[DeviceStatusResponse]:
|
| 64 |
now = datetime.now(timezone.utc)
|
| 65 |
online_threshold = now - timedelta(seconds=90)
|
| 66 |
rows = await device_repo.list()
|
| 67 |
+
output: list[DeviceStatusResponse] = []
|
| 68 |
for row in rows:
|
| 69 |
last_heartbeat_at = _to_utc_aware(row.get("last_heartbeat_at"))
|
| 70 |
output.append(
|
| 71 |
+
DeviceStatusResponse(
|
| 72 |
device_id=row.get("device_id", ""),
|
| 73 |
hostname=row.get("hostname", ""),
|
| 74 |
agent_version=row.get("agent_version", ""),
|
|
|
|
| 77 |
active_restriction_window=bool(
|
| 78 |
row.get("active_restriction_window", False)
|
| 79 |
),
|
| 80 |
+
is_online=bool(
|
|
|
|
|
|
|
| 81 |
last_heartbeat_at is not None
|
| 82 |
and last_heartbeat_at >= online_threshold
|
| 83 |
+
),
|
| 84 |
+
)
|
| 85 |
)
|
| 86 |
return output
|