feat: 新增多插件 UI 支持及运行日志集中回传功能
本次提交包含多项功能更新: 1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持 2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储 3. 新增插件分类字段支持,用于场景库分组管理 4. 优化 xq0034/0037/0048 等插件的功能及描述 5. 修复控制面响应兼容问题,支持新旧版本数据格式 6. 新增 xq0033 插件的状态存储模块,用于历史记录管理 7. 优化 CRM 登录窗口模式,支持全屏置顶
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// 冒烟测试:CLI 模式启动整个壳(app.init),验证 log-upload 集成不破坏启动流程。
|
||||
"use strict";
|
||||
|
||||
const path = require("path");
|
||||
const TMP = path.join(__dirname, "_tmp_boot");
|
||||
process.env.LOCALAPPDATA = TMP;
|
||||
process.env.CONTROLPLANE_URL = process.env.TEST_CP_URL || "http://127.0.0.1:8890";
|
||||
process.env.CONTROLPLANE_TOKEN = "test-token-123";
|
||||
process.env.SHELL_PLUGIN_ROOT = path.join(TMP, "plugins");
|
||||
process.env.SHELL_PYTHON_ENV_ROOT = path.join(TMP, "python-env");
|
||||
|
||||
const { init } = require(path.join(__dirname, "..", "desktop", "electron-shell", "electron", "app.js"));
|
||||
|
||||
(async () => {
|
||||
const r = await init();
|
||||
console.log("BOOT OK deviceId=", r.deviceId, "account=", r.account);
|
||||
// 等 2 秒让 log-upload 首轮回传(async)跑完
|
||||
setTimeout(() => {
|
||||
console.log("SMOKE TEST DONE");
|
||||
process.exit(0);
|
||||
}, 2000);
|
||||
})().catch((e) => {
|
||||
console.error("BOOT FAIL:", e);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
setTimeout(() => { console.error("SMOKE TIMEOUT"); process.exit(2); }, 30000);
|
||||
@@ -0,0 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""测试保留期清理:回填一条 40 天前的运行日志 + 一个 40 天前的设备壳日志后执行 cleanup_expired。"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, r"D:\work\code\37duAutomation\desktop")
|
||||
import pymysql
|
||||
|
||||
from controlplane.core import config, logstore
|
||||
|
||||
c = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456",
|
||||
database="37du_center_test")
|
||||
with c.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO run_logs(run_id, account, device_id, plugin_id, name, version, `trigger`, "
|
||||
"schedule_id, status, ok, error, summary, started, finished, duration_sec, log_count, "
|
||||
"logs, uploaded_at) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"
|
||||
"DATE_SUB(NOW(), INTERVAL 40 DAY))",
|
||||
("oldrun0000000000000000000000000001", "olduser", "dev-old-1", "XQ1", "老插件",
|
||||
"1.0", "manual", "", "error", 0, "x", "y", 0, 0, 0, 0, "[]"))
|
||||
cur.execute(
|
||||
"INSERT INTO device_logs(device_id, account, hostname, client_version, size_bytes, "
|
||||
"updated_at) VALUES (%s,%s,%s,%s,%s, DATE_SUB(NOW(), INTERVAL 40 DAY))",
|
||||
("dev-old-1", "olduser", "OLD-PC", "1.0", 10))
|
||||
c.commit()
|
||||
c.close()
|
||||
|
||||
config.SHELL_LOGS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
f = os.path.join(config.SHELL_LOGS_DIR, "dev-old-1.log")
|
||||
with open(f, "wb") as fh:
|
||||
fh.write(b"old content\n")
|
||||
|
||||
r = logstore.cleanup_expired()
|
||||
print("cleanup result:", r)
|
||||
print("file exists after cleanup:", os.path.exists(f))
|
||||
|
||||
c = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456",
|
||||
database="37du_center_test")
|
||||
with c.cursor() as cur:
|
||||
cur.execute("SELECT COUNT(*) FROM run_logs WHERE run_id LIKE %s", ("oldrun%",))
|
||||
print("old run_logs left:", cur.fetchone()[0])
|
||||
cur.execute("SELECT COUNT(*) FROM device_logs WHERE device_id = %s", ("dev-old-1",))
|
||||
print("old device_logs left:", cur.fetchone()[0])
|
||||
c.close()
|
||||
print("CLEANUP OK")
|
||||
@@ -0,0 +1,83 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""本地集成测试:中心机日志回传 + 管理端查询(对 127.0.0.1:8890 测试库)。"""
|
||||
import sys
|
||||
import json
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
sys.path.insert(0, r"D:\work\code\37duAutomation\desktop")
|
||||
from controlplane.core.auth import _sign_admin_token
|
||||
|
||||
BASE = "http://127.0.0.1:8890"
|
||||
TOKEN = "test-token-123"
|
||||
ADMIN = _sign_admin_token("puddy")
|
||||
|
||||
|
||||
def call(method, path, body=None, raw=None, headers=None):
|
||||
h = dict(headers or {})
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||||
h["Content-Type"] = "application/json"
|
||||
elif raw is not None:
|
||||
data = raw
|
||||
req = urllib.request.Request(BASE + path, data=data, method=method, headers=h)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
return r.status, r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.read()
|
||||
|
||||
|
||||
H = {"X-API-Token": TOKEN}
|
||||
A = {"X-Admin-Token": ADMIN}
|
||||
|
||||
# --- 1) 客户端回传运行日志(两次同 run_id → 幂等) ---
|
||||
run = {
|
||||
"run_id": "feed0000000000000000000000000001",
|
||||
"account": "lisi", "device_id": "dev-lisi-9", "plugin_id": "XQ0021",
|
||||
"name": "工资条下载", "version": "2.1.0", "trigger": "manual", "schedule_id": "",
|
||||
"status": "done", "ok": True, "error": "", "summary": "下载完成,共 3 条",
|
||||
"started": 1700001000, "finished": 1700001200, "duration_sec": 200,
|
||||
"logs": [
|
||||
{"t": "10:00:01", "level": "INFO", "msg": "开始"},
|
||||
{"t": "10:00:02", "level": "INFO", "msg": "解析工资条 1/3"},
|
||||
{"t": "10:00:03", "level": "INFO", "msg": "完成"},
|
||||
],
|
||||
}
|
||||
s, b = call("POST", "/api/runtime/logs", body=run, headers=H)
|
||||
print("run-logs POST", s, b.decode("utf-8"))
|
||||
s, b = call("POST", "/api/runtime/logs", body=run, headers=H)
|
||||
print("run-logs POST#2", s, b.decode("utf-8"))
|
||||
|
||||
# --- 2) 客户端壳日志增量(reset 首传 + 追加) ---
|
||||
line1 = "2026-10-22 10:00:01 INFO app 启动完成\n".encode("utf-8")
|
||||
qs = "device_id=dev-lisi-9&account=lisi&hostname=PC-LISI&client_version=1.6.4&reset=1"
|
||||
s, b = call("POST", "/api/runtime/shell-log?" + qs, raw=line1, headers=H)
|
||||
print("shell-log reset", s, b.decode("utf-8"))
|
||||
line2 = "2026-10-22 10:01:00 ERROR plugin-sync 同步失败\n".encode("utf-8")
|
||||
qs2 = "device_id=dev-lisi-9&account=lisi&hostname=PC-LISI&client_version=1.6.4"
|
||||
s, b = call("POST", "/api/runtime/shell-log?" + qs2, raw=line2, headers=H)
|
||||
print("shell-log append", s, b.decode("utf-8"))
|
||||
|
||||
# --- 3) 管理端查询 ---
|
||||
s, b = call("GET", "/api/admin/run-logs?plugin=XQ0021&page=1&page_size=10", headers=A)
|
||||
print("admin run-logs", s, b.decode("utf-8")[:400])
|
||||
s, b = call("GET", "/api/admin/run-logs/feed0000000000000000000000000001", headers=A)
|
||||
print("admin run-logs detail", s, b.decode("utf-8")[:300])
|
||||
s, b = call("GET", "/api/admin/device-logs", headers=A)
|
||||
print("admin device-logs", s, b.decode("utf-8")[:300])
|
||||
s, b = call("GET", "/api/admin/device-logs/dev-lisi-9", headers=A)
|
||||
print("admin device-log detail", s, b.decode("utf-8")[:300])
|
||||
s, b = call("GET", "/api/admin/device-logs/dev-lisi-9/download", headers=A)
|
||||
print("admin device-log download", s, b.decode("utf-8"))
|
||||
s, b = call("GET", "/api/admin/run-logs/../etc/passwd", headers=A)
|
||||
print("admin path-traversal run-logs", s)
|
||||
|
||||
# --- 4) 无 token 鉴权 ---
|
||||
s, b = call("POST", "/api/runtime/logs", body=run)
|
||||
print("no-token POST", s)
|
||||
|
||||
# --- 5) 清理接口 ---
|
||||
s, b = call("POST", "/api/admin/logs/cleanup", headers=A)
|
||||
print("admin cleanup", s, b.decode("utf-8"))
|
||||
@@ -0,0 +1,106 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""controlplane-next(新版服务端)集中日志接口集成测试。
|
||||
前置:本地起好新版服务(NODE_ENV=local,端口 8001,测试库 37du_center_next_test)。
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
import sys
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
import pymysql
|
||||
|
||||
BASE = "http://127.0.0.1:8001"
|
||||
TOKEN = "test-token-123"
|
||||
|
||||
# ---------- 1) 种一个管理员账号(pbkdf2 算法与新版 crypto.ts 一致) ----------
|
||||
def pbkdf2(pwd: str, salt: str) -> str:
|
||||
return hashlib.pbkdf2_hmac("sha256", pwd.encode("utf-8"), salt.encode("utf-8"), 120000, 32).hex()
|
||||
|
||||
SALT = "aabbccddeeff0011"
|
||||
HASH = pbkdf2("123456", SALT)
|
||||
c = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456",
|
||||
database="37du_center_next_test")
|
||||
with c.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO accounts(account, password_hash, salt, is_admin, dept, nickname, status) "
|
||||
"VALUES (%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE password_hash=VALUES(password_hash), "
|
||||
"salt=VALUES(salt), is_admin=1",
|
||||
("puddy", HASH, SALT, 1, "IT", "管理员", 1))
|
||||
c.commit()
|
||||
c.close()
|
||||
print("seeded admin puddy/123456")
|
||||
|
||||
# ---------- HTTP 辅助 ----------
|
||||
def call(method, path, body=None, raw=None, headers=None):
|
||||
h = dict(headers or {})
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||||
h["Content-Type"] = "application/json"
|
||||
elif raw is not None:
|
||||
data = raw
|
||||
# 与 Electron 客户端一致:显式 octet-stream,避免中间件按 form 消费
|
||||
h.setdefault("Content-Type", "application/octet-stream")
|
||||
req = urllib.request.Request(BASE + path, data=data, method=method, headers=h)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=15) as r:
|
||||
return r.status, r.read()
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.read()
|
||||
|
||||
H = {"X-API-Token": TOKEN}
|
||||
|
||||
# ---------- 2) 客户端回传运行日志(幂等) ----------
|
||||
run = {
|
||||
"run_id": "abababababababababababababababab",
|
||||
"account": "zhangsan", "device_id": "dev-next-01", "plugin_id": "XQ0011",
|
||||
"name": "考勤月报封存检查", "version": "1.0.0", "trigger": "schedule", "schedule_id": "s1",
|
||||
"status": "error", "ok": False, "error": "登录失败", "summary": "未完成",
|
||||
"started": 1700000000, "finished": 1700000060, "duration_sec": 60,
|
||||
"logs": [
|
||||
{"t": "14:00:01", "level": "INFO", "msg": "开始执行"},
|
||||
{"t": "14:00:02", "level": "ERROR", "msg": "登录失败:密码不正确"},
|
||||
],
|
||||
}
|
||||
s, b = call("POST", "/api/runtime/logs", body=run, headers=H)
|
||||
print("runtime/logs POST:", s, b.decode("utf-8"))
|
||||
s, b = call("POST", "/api/runtime/logs", body=run, headers=H)
|
||||
print("runtime/logs POST#2(幂等):", s, b.decode("utf-8"))
|
||||
|
||||
# ---------- 3) 客户端壳日志(reset 首传 + 追加) ----------
|
||||
line1 = "2026-10-22 15:00:01 INFO app 启动完成\n".encode("utf-8")
|
||||
qs = "device_id=dev-next-01&account=zhangsan&hostname=PC-NEXT&client_version=1.6.4&reset=1"
|
||||
s, b = call("POST", "/api/runtime/shell-log?" + qs, raw=line1, headers=H)
|
||||
print("shell-log reset:", s, b.decode("utf-8"))
|
||||
line2 = "2026-10-22 15:01:00 ERROR plugin-sync 同步失败\n".encode("utf-8")
|
||||
qs2 = "device_id=dev-next-01&account=zhangsan&hostname=PC-NEXT&client_version=1.6.4"
|
||||
s, b = call("POST", "/api/runtime/shell-log?" + qs2, raw=line2, headers=H)
|
||||
print("shell-log append:", s, b.decode("utf-8"))
|
||||
|
||||
# ---------- 4) 管理端:登录拿票据 → 查日志 ----------
|
||||
s, b = call("POST", "/api/admin/auth", body={"account": "puddy", "password": "123456"})
|
||||
auth = json.loads(b.decode("utf-8"))
|
||||
print("admin/auth:", s, "is_admin=", auth.get("is_admin"))
|
||||
A = {"X-Admin-Token": auth.get("token", "")}
|
||||
|
||||
s, b = call("GET", "/api/admin/run-logs?plugin=XQ0011&page=1&page_size=10", headers=A)
|
||||
print("admin run-logs:", s, b.decode("utf-8")[:360])
|
||||
s, b = call("GET", "/api/admin/run-logs/abababababababababababababababab", headers=A)
|
||||
print("admin run-logs detail:", s, b.decode("utf-8")[:300])
|
||||
s, b = call("GET", "/api/admin/device-logs", headers=A)
|
||||
print("admin device-logs:", s, b.decode("utf-8")[:300])
|
||||
s, b = call("GET", "/api/admin/device-logs/dev-next-01", headers=A)
|
||||
print("admin device-log detail:", s, b.decode("utf-8")[:300])
|
||||
s, b = call("GET", "/api/admin/device-logs/dev-next-01/download", headers=A)
|
||||
print("admin device-log download:", s, b.decode("utf-8"))
|
||||
s, b = call("GET", "/api/admin/run-logs/../etc/passwd", headers=A)
|
||||
print("admin path-traversal run-logs:", s)
|
||||
|
||||
# ---------- 5) 鉴权 ----------
|
||||
s, b = call("POST", "/api/runtime/logs", body=run)
|
||||
print("no-token POST:", s)
|
||||
|
||||
# ---------- 6) 清理 ----------
|
||||
s, b = call("POST", "/api/admin/logs/cleanup", headers=A)
|
||||
print("admin logs/cleanup:", s, b.decode("utf-8"))
|
||||
@@ -0,0 +1,95 @@
|
||||
// 客户端 log-upload 集成测试:对本地测试控制面(127.0.0.1:8890) 回传运行日志 + 壳日志增量。
|
||||
// 用法:node tests/test_log_upload_client.js (需先启动测试服务端)
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const TMP = path.join(__dirname, "_tmp_logupload");
|
||||
const APP_DATA = path.join(TMP, "37duAutomation");
|
||||
|
||||
// 必须在 require 任何 electron 模块前设置(config.js 只在变量未定义时读 .env)
|
||||
process.env.LOCALAPPDATA = TMP;
|
||||
process.env.CONTROLPLANE_URL = process.env.TEST_CP_URL || "http://127.0.0.1:8890";
|
||||
process.env.CONTROLPLANE_TOKEN = "test-token-123";
|
||||
process.env.CONTROLPLANE_ACCOUNT = "tester";
|
||||
process.env.SHELL_PLUGIN_ROOT = path.join(TMP, "plugins"); // 防 pluginRoot() 扫仓库根
|
||||
|
||||
const SHELL_DIR = path.join(__dirname, "..", "desktop", "electron-shell", "electron");
|
||||
|
||||
// ---- 造本地数据 ----
|
||||
fs.rmSync(TMP, { recursive: true, force: true });
|
||||
fs.mkdirSync(path.join(APP_DATA, "logs"), { recursive: true });
|
||||
fs.mkdirSync(path.join(APP_DATA, "run_history"), { recursive: true });
|
||||
fs.writeFileSync(path.join(APP_DATA, "device_id"), "dev-client-test-01", "utf-8");
|
||||
fs.writeFileSync(path.join(APP_DATA, "logs", "shell.log"),
|
||||
"2026-10-22 09:00:00 INFO app 启动\n2026-10-22 09:00:01 INFO plugin-sync 同步完成\n", "utf-8");
|
||||
|
||||
const RUN_A = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
const RUN_B = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
fs.writeFileSync(path.join(APP_DATA, "run_history.jsonl"),
|
||||
JSON.stringify({ run_id: RUN_A, account: "tester", plugin_id: "XQ0011", name: "插件甲", version: "1.0.0",
|
||||
trigger: "manual", status: "error", ok: false, error: "登录失败", summary: "",
|
||||
started: now - 2000, finished: now - 1900, duration_sec: 100 }) + "\n" +
|
||||
JSON.stringify({ run_id: RUN_B, account: "tester", plugin_id: "XQ0021", name: "插件乙", version: "2.0.0",
|
||||
trigger: "schedule", schedule_id: "s-1", status: "done", ok: true, error: "", summary: "完成",
|
||||
started: now - 1000, finished: now - 900, duration_sec: 100 }) + "\n", "utf-8");
|
||||
fs.writeFileSync(path.join(APP_DATA, "run_history", `${RUN_A}.jsonl`),
|
||||
JSON.stringify({ t: "09:00:01", level: "INFO", msg: "开始执行" }) + "\n" +
|
||||
JSON.stringify({ t: "09:00:02", level: "ERROR", msg: "登录失败:密码不正确" }) + "\n", "utf-8");
|
||||
fs.writeFileSync(path.join(APP_DATA, "run_history", `${RUN_B}.jsonl`),
|
||||
JSON.stringify({ t: "10:00:01", level: "INFO", msg: "开始" }) + "\n" +
|
||||
JSON.stringify({ t: "10:00:02", level: "INFO", msg: "处理完成" }) + "\n", "utf-8");
|
||||
|
||||
const logUpload = require(path.join(SHELL_DIR, "log-upload.js"));
|
||||
|
||||
function assert(cond, name) {
|
||||
if (!cond) { console.error("FAIL:", name); process.exitCode = 1; }
|
||||
else console.log("PASS:", name);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
// 1) 整次运行日志回传(新 run_id)
|
||||
const ok = await logUpload.uploadRun({
|
||||
run_id: "cccccccccccccccccccccccccccccccc", account: "tester", plugin_id: "XQ0031",
|
||||
name: "插件丙", version: "3.0.0", trigger: "manual", status: "done", ok: true,
|
||||
error: "", summary: "OK", started: 1700000400, finished: 1700000500, duration_sec: 100,
|
||||
logs: [{ t: "11:00:01", level: "INFO", msg: "start" }],
|
||||
});
|
||||
assert(ok === true, "uploadRun 成功回传");
|
||||
|
||||
// 2) 壳日志增量:第一次全量 + 追加新行后只传增量
|
||||
await logUpload.uploadShellLog();
|
||||
fs.appendFileSync(path.join(APP_DATA, "logs", "shell.log"),
|
||||
"2026-10-22 09:10:00 ERROR runner 执行进程异常退出\n", "utf-8");
|
||||
await logUpload.uploadShellLog();
|
||||
|
||||
// 3) 启动补报:run_history 中两条(RUN_A/RUN_B)→ 服务端幂等 upsert
|
||||
await logUpload.backfillRecentRuns();
|
||||
|
||||
// 4) 离线暂存 + 补报:patch cp.request 模拟服务端不可达 → 应入 pending;恢复后 flush 应成功
|
||||
const cp = require(path.join(SHELL_DIR, "cpclient.js"));
|
||||
const realRequest = cp.request;
|
||||
const offlineRunId = "dddddddddddddddddddddddddddddddd";
|
||||
cp.request = async (method, urlPath, opts) => {
|
||||
if (urlPath === "/api/runtime/logs" && JSON.stringify(opts && opts.json || "").includes(offlineRunId)) {
|
||||
throw new Error("simulated offline");
|
||||
}
|
||||
return realRequest(method, urlPath, opts);
|
||||
};
|
||||
const offline = await logUpload.uploadRun({
|
||||
run_id: offlineRunId, account: "tester", plugin_id: "XQ0041",
|
||||
name: "插件丁", version: "4.0.0", trigger: "manual", status: "error", ok: false,
|
||||
error: "offline 测试", summary: "", started: 1700000600, finished: 1700000700, duration_sec: 100,
|
||||
logs: [{ t: "12:00:01", level: "ERROR", msg: "boom" }],
|
||||
});
|
||||
assert(offline === false, "不可达时 uploadRun 返回 false");
|
||||
const pending = fs.readFileSync(path.join(APP_DATA, "pending_log_uploads.jsonl"), "utf-8").trim();
|
||||
assert(pending.includes(offlineRunId), "离线日志已暂存 pending");
|
||||
cp.request = realRequest; // 恢复在线
|
||||
const flushed = await logUpload.flushPending();
|
||||
assert(flushed === 1, "恢复后 flushPending 补报 1 条");
|
||||
|
||||
console.log("客户端 log-upload 测试完成");
|
||||
})().catch((e) => { console.error("TEST ERROR:", e); process.exitCode = 1; });
|
||||
Reference in New Issue
Block a user