Files
37duAutomation/desktop/controlplane/apis/fileserver_status.py
T
puddy b1ae4e5111 feat: 新增内网文件服务器离线监控,升级ERP报表导出插件适配新旧系统差异
1.  新增内网文件服务器心跳上报与离线监控功能:
    - 为文件服务器添加周期心跳上报到中心机的能力
    - 中心机新增文件服务器状态监控线程,超时自动判离线并钉钉告警管理员
    - 新增数据库表存储文件服务器状态,提供管理端API查看在线状态
2.  修复ERP物料收发汇总报表插件:
    - 调整插件版本到1.0.9,更新前端配置与UI适配新旧ERP系统
    - 重构组织与仓库默认值逻辑,区分新版/旧版ERP的默认配置
    - 统一所有场景下显式设置查询组织,移除旧版默认组织的兼容逻辑
    - 新增代管仓仓库编码适配旧版系统
3.  优化文件服务器打包脚本路径,更新文档示例命令
2026-09-19 14:55:22 +08:00

87 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""内网文件服务器状态 API:心跳上报接收 + 管理端状态查看。
- 文件服务器(fileserver,8802)周期主动上报心跳,本模块落库 fileserver_status;
- 中心机监控线程(core/monitor.py)依据 last_seen 超时判离线,并钉钉告警管理员;
- 心跳上报仅需 X-API-Token(底座凭证),查看需管理员票据。
"""
from __future__ import annotations
from fastapi import APIRouter, Header, HTTPException, Query
from pydantic import BaseModel, Field
from controlplane.core.auth import _check_token, _require_admin
from controlplane.core.db import _db, _now
router = APIRouter()
class FileserverHeartbeatIn(BaseModel):
"""文件服务器心跳载荷(主动上报状态)。host 为唯一标识,默认=对外下载根。"""
host: str = Field(default="", max_length=255)
base_url: str = Field(default="", max_length=255)
hostname: str = Field(default="", max_length=128)
ip: str = Field(default="", max_length=64)
port: int = 0
file_count: int = 0
total_size: int = 0
version: str = Field(default="", max_length=64)
uptime_sec: int = 0
@router.post("/api/fileserver/heartbeat")
def fileserver_heartbeat(b: FileserverHeartbeatIn,
x_api_token: str = Header(default="", alias="X-API-Token")) -> dict:
"""接收文件服务器心跳:upsert 状态快照,last_seen 刷新;离线中收到心跳则恢复在线。"""
_check_token(x_api_token)
host = b.host.strip()
if not host:
raise HTTPException(400, "host 不能为空")
now = _now()
with _db() as conn:
row = conn.execute(
"SELECT status, offline_notified FROM fileserver_status WHERE host = %s",
(host,)).fetchone()
if row:
# 离线期间恢复上线:标记在线、清除离线告警标记(恢复时不再推送)
was_offline = (not row["status"]) or bool(row["offline_notified"])
conn.execute(
"UPDATE fileserver_status SET base_url=%s, hostname=%s, ip=%s, port=%s, "
"file_count=%s, total_size=%s, version=%s, uptime_sec=%s, last_seen=%s, "
"status=1, offline_notified=0"
+ (", last_status_change=%s" if was_offline else "") +
" WHERE host=%s",
(b.base_url.strip(), b.hostname.strip(), b.ip.strip(), int(b.port or 0),
int(b.file_count or 0), int(b.total_size or 0), b.version.strip(),
int(b.uptime_sec or 0), now) +
((now, host) if was_offline else (host,)))
else:
conn.execute(
"INSERT INTO fileserver_status(host, base_url, hostname, ip, port, file_count, "
"total_size, version, uptime_sec, last_seen, status, offline_notified, "
"created_at, last_status_change) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 1, 0, %s, %s)",
(host, b.base_url.strip(), b.hostname.strip(), b.ip.strip(), int(b.port or 0),
int(b.file_count or 0), int(b.total_size or 0), b.version.strip(),
int(b.uptime_sec or 0), now, now, now))
conn.commit()
return {"ok": True, "host": host, "last_seen": now}
@router.get("/api/admin/fileserver/status")
def fileserver_status_list(limit: int = Query(default=100, ge=1, le=1000),
x_admin_token: str = Header(default="", alias="X-Admin-Token"),
x_api_token: str = Header(default="", alias="X-API-Token")) -> dict:
"""管理端查看文件服务器在线状态列表(按最近心跳倒序)。"""
_require_admin(x_admin_token, x_api_token)
with _db() as conn:
rows = [dict(r) for r in conn.execute(
"SELECT id, host, base_url, hostname, ip, port, file_count, total_size, version, "
" uptime_sec, last_seen, status, last_status_change, offline_notified, "
" created_at "
"FROM fileserver_status ORDER BY last_seen DESC LIMIT %s", (limit,))]
for r in rows:
r["status"] = bool(r["status"])
r["offline_notified"] = bool(r["offline_notified"])
return {"fileservers": rows}