Skip to content

Commit f31948e

Browse files
committed
Add /api/nodes/status endpoint
Introduce a new GET endpoint /api/nodes/status that reports registered nodes as online or offline. It consults registered_nodes from config_manager, uses storage.active_nodes' last-seen timestamps and storage.expire_seconds (derived from config.yaml data_expire_minutes) to determine online status, and returns sorted "online" and "offline" lists. Matches existing data expiry logic.
1 parent 233f425 commit f31948e

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

server/server.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,50 @@ async def health():
817817

818818

819819

820+
# 接口:节点状态检查
821+
822+
823+
824+
@app.get("/api/nodes/status")
825+
async def nodes_status():
826+
"""
827+
检查节点在线状态
828+
829+
根据 active_nodes 中最后上报时间判断节点是否在线(未过期)。
830+
在线/离线判断标准与数据过期时间一致(config.yaml 中的 data_expire_minutes)。
831+
832+
返回:
833+
{
834+
"online": ["node-001", "node-002"],
835+
"offline": ["node-003"]
836+
}
837+
"""
838+
now = time.time()
839+
expire_seconds = storage.expire_seconds
840+
841+
# 已注册的所有节点 ID
842+
registered_ids = {
843+
n["id"] for n in config_manager.config.get("registered_nodes", [])
844+
}
845+
846+
# 在线 = 已注册 且 active_nodes 中有记录且未过期
847+
online = []
848+
offline = []
849+
850+
for node_id in registered_ids:
851+
last_seen = storage.active_nodes.get(node_id)
852+
if last_seen is not None and now - last_seen < expire_seconds:
853+
online.append(node_id)
854+
else:
855+
offline.append(node_id)
856+
857+
return {
858+
"online": sorted(online),
859+
"offline": sorted(offline),
860+
}
861+
862+
863+
820864
# 接口:手动触发配置重载
821865

822866

0 commit comments

Comments
 (0)