Files
37duAutomation/desktop/electron-shell/electron/flow-versions.js
T
puddy 4b84f9c149 feat(electron-shell): 1.8.0 执行队列、流程编排、流程定时与版本回滚
- 执行队列:多任务串行排队,可单独取消,超时自动放弃,异常退出后标记已中断

- 流程编排:画布多场景依赖图,内置确保登录/等待/通知/人工确认节点

- 流程定时:场景与流程统一配置定时(每天/工作日/每周/每月/每隔N小时)

- 流程版本历史:自动留档 20 版,画布一键回滚且回滚可撤销

- 流程配置云备份:随定时任务备份到中心机,换机自动恢复

- 总览页/场景库布局调整,通知浮窗右下角堆叠显示
2026-10-27 13:36:38 +08:00

130 lines
3.8 KiB
JavaScript

// 流程版本快照:每次"内容变化"(名称/失败策略/节点)前保存一份上一版,供回滚。
//
// 刻意不存 schedule:定时开关与 cron 属于运行期配置,回滚一个旧版本不应该把
// 用户后来改的排期一起改回去(计划页与流程编辑器共用同一份 schedule)。
//
// 存储:flow_versions.json,按 flow_id 分组,每组最多 MAX_PER_FLOW 条(先进先出)。
"use strict";
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const config = require("./config");
const { makeLogger } = require("./logger");
const logger = makeLogger("flow-versions");
const VERSIONS_PATH = path.join(config.APP_DATA, "flow_versions.json");
const MAX_PER_FLOW = 20;
let _data = {}; // flow_id -> [{ vid, at, name, on_fail, nodes }]
let _loaded = false;
function load() {
if (_loaded) return;
_loaded = true;
try {
if (fs.existsSync(VERSIONS_PATH)) {
const d = JSON.parse(fs.readFileSync(VERSIONS_PATH, "utf-8"));
if (d && typeof d === "object" && !Array.isArray(d)) _data = d;
}
} catch (e) {
logger.warning(`流程版本读取失败:${e.message}`);
_data = {};
}
}
function persist() {
try {
fs.mkdirSync(config.APP_DATA, { recursive: true });
const tmp = VERSIONS_PATH + ".tmp";
fs.writeFileSync(tmp, JSON.stringify(_data, null, 2), "utf-8");
fs.renameSync(tmp, VERSIONS_PATH);
} catch (e) {
logger.warning(`流程版本写入失败:${e.message}`);
}
}
function nowStr() {
const d = new Date();
const p = (n) => String(n).padStart(2, "0");
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
}
// 只保留"内容"字段,schedule 等运行期配置不进快照
function snapshotOf(flow) {
return {
name: flow.name || "",
on_fail: flow.on_fail === "continue" ? "continue" : "abort",
nodes: (flow.nodes || []).map((n) => ({ ...n })),
};
}
function contentKey(flow) {
const s = snapshotOf(flow);
return JSON.stringify({ name: s.name, on_fail: s.on_fail, nodes: s.nodes });
}
// 保存一份快照(由 flows.update / rollback 在变更前调用)
function push(flowId, flow) {
load();
if (!flowId || !flow) return null;
const snap = snapshotOf(flow);
const rec = { vid: crypto.randomBytes(6).toString("hex"), at: nowStr(), ...snap };
const list = _data[flowId] || [];
// 与最新一条完全相同就不重复记(比如连续点保存)
if (list.length && JSON.stringify(list[list.length - 1].nodes) === JSON.stringify(rec.nodes)
&& list[list.length - 1].name === rec.name
&& list[list.length - 1].on_fail === rec.on_fail) {
return null;
}
list.push(rec);
while (list.length > MAX_PER_FLOW) list.shift();
_data[flowId] = list;
persist();
return rec;
}
// 新的在前(前端直接遍历展示)
function list(flowId) {
load();
return [...(_data[flowId] || [])].reverse().map((v) => ({
vid: v.vid,
at: v.at,
name: v.name,
on_fail: v.on_fail,
node_count: (v.nodes || []).length,
// 只给摘要,不把整份节点喂给列表
type_counts: (v.nodes || []).reduce((m, n) => {
const t = n.type || "plugin";
m[t] = (m[t] || 0) + 1;
return m;
}, {}),
}));
}
function get(flowId, vid) {
load();
return (_data[flowId] || []).find((v) => v.vid === vid) || null;
}
// 流程删除时清理它的历史,避免文件无限增长
function removeAll(flowId) {
load();
if (!_data[flowId]) return;
delete _data[flowId];
persist();
}
module.exports = {
VERSIONS_PATH,
MAX_PER_FLOW,
load,
push,
list,
get,
removeAll,
snapshotOf,
contentKey,
};