feat(platform): 支持工作流单号本地缓存秒级定位与自建应用业务对象智能反查
- 数据库迁移与本地缓存:在 `core/db/migrations.ts` 新增 version 5 迁移 (`005_workflow_job_cache`),创建 `workflow_job_index` 缓存表及高效复合索引;实现 `core/db/workflow-job-cache.ts`,支持按企业+工作流+单号快速检索与幂等增量缓存; - 流式早停探查与业务编排:在 `domains/platform/workflow/execution/search-by-flow.ts` 实现 `searchWorkflowJobByFlowCode`,采用「本地 SQLite 缓存 0ms 直达 ➔ 远端流式探查早停熔断并自动建立本地索引」机制,彻底解决云端 Piper API 无单号索引导致的排查低效问题; - CLI 命令层升级: - `workflow logs <pipelineId> --flow=<单号>`:直接秒级定位单据关联 Job 实例; - `platform records <业务对象>`:支持单参数业务对象名称,自动在企业全量自建应用中全局反查与智能寻址,若跨应用重名则抛出精准候选; - 命令行元数据对齐:在 `command-metadata.ts` 为 `workflow` 补齐 `--max-pages`、`--maxPages` 与 `--page-limit` 选项注册,满足严格参数词汇表静态检查; - 测试与文档:新增并更新 `workflow-job-cache.test.ts`、`search-by-flow.test.ts`、`platform.test.ts` 与 `db.test.ts`,更新核心与域层文档,全量 1126 个测试与类型检查全部通过。
This commit is contained in:
1 parent
ea5eb22c51
commit
0c9eb27f85
16 files changed
+798
-19
No files matched your search
@@ -28,8 +28,9 @@ src/core/
|
||||
│ ├── multi-level.ts # 多级联动门面与 in-flight 并发防击穿包装器
|
||||
│ └── types.ts
|
||||
├── db/ # 统一 SQLite 数据库与结构化版本迁移
|
||||
│ ├── connection.ts # SQLite 单例、WAL 模式、文件权限 0o600 保护
|
||||
│ ├── migrations.ts # 结构化增量迁移运行器(_schema_migrations)
|
||||
│ ├── connection.ts # SQLite 单例、WAL 模式、文件权限 0o600 保护
|
||||
│ ├── migrations.ts # 结构化增量迁移运行器(_schema_migrations)
|
||||
│ ├── workflow-job-cache.ts # 工作流 Job 索引缓存与单据触发单号秒级检索
|
||||
│ └── types.ts
|
||||
├── config/ # 企业配置管理
|
||||
│ ├── loader.ts # 全局与多企业配置读写(强制 0o600 权限与字段归一化)
|
||||
|
||||
@@ -39,6 +39,7 @@ describe("Core DB - 数据库连接与结构化迁移机制 (加固测试)", ()
|
||||
expect(tableNames.has("cache_entries")).toBe(true);
|
||||
expect(tableNames.has("observed_events")).toBe(true);
|
||||
expect(tableNames.has("workflow_error_alerts")).toBe(true);
|
||||
expect(tableNames.has("workflow_job_index")).toBe(true);
|
||||
|
||||
// 检查加固索引:UPPER(short_id)、findings(rule_id)
|
||||
const indices = db
|
||||
@@ -55,6 +56,8 @@ describe("Core DB - 数据库连接与结构化迁移机制 (加固测试)", ()
|
||||
expect(indexNames.has("idx_cache_expires_at")).toBe(true);
|
||||
expect(indexNames.has("idx_observed_events_scope")).toBe(true);
|
||||
expect(indexNames.has("idx_workflow_alerts_scope")).toBe(true);
|
||||
expect(indexNames.has("idx_wf_jobs_corp_pipeline_code")).toBe(true);
|
||||
expect(indexNames.has("idx_wf_jobs_code_upper")).toBe(true);
|
||||
});
|
||||
|
||||
test("cache_entries 具备复合主键 (namespace, key) 约束", () => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./types.js";
|
||||
export * from "./connection.js";
|
||||
export * from "./migrations.js";
|
||||
export * from "./workflow-job-cache.js";
|
||||
@@ -242,6 +242,30 @@ export const migrations: DatabaseMigration[] = [
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
version: 5,
|
||||
name: "005_workflow_job_cache",
|
||||
up(db: Database): void {
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS workflow_job_index (
|
||||
corp_id TEXT NOT NULL,
|
||||
pipeline_id TEXT NOT NULL,
|
||||
job_id TEXT NOT NULL,
|
||||
flow_code TEXT,
|
||||
flow_id TEXT,
|
||||
execute_time INTEGER,
|
||||
status TEXT,
|
||||
user_name TEXT,
|
||||
updated_at INTEGER NOT NULL,
|
||||
PRIMARY KEY (corp_id, pipeline_id, job_id)
|
||||
);
|
||||
`);
|
||||
db.run("CREATE INDEX IF NOT EXISTS idx_wf_jobs_code ON workflow_job_index(corp_id, flow_code);");
|
||||
db.run("CREATE INDEX IF NOT EXISTS idx_wf_jobs_pipeline ON workflow_job_index(corp_id, pipeline_id);");
|
||||
db.run("CREATE INDEX IF NOT EXISTS idx_wf_jobs_corp_pipeline_code ON workflow_job_index(corp_id, pipeline_id, flow_code);");
|
||||
db.run("CREATE INDEX IF NOT EXISTS idx_wf_jobs_code_upper ON workflow_job_index(corp_id, pipeline_id, UPPER(flow_code));");
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
function seedDefaultPreference(db: Database): void {
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
||||
import { closeDatabase, getDatabase } from "./connection.js";
|
||||
import {
|
||||
getCachedWorkflowJobByCode,
|
||||
upsertWorkflowJobIndex,
|
||||
type WorkflowJobCacheRecord,
|
||||
} from "./workflow-job-cache.js";
|
||||
|
||||
describe("Core DB - Workflow Job 本地缓存与索引检索 (workflow_job_index)", () => {
|
||||
let db: Database;
|
||||
|
||||
beforeEach(() => {
|
||||
db = getDatabase(":memory:");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
closeDatabase();
|
||||
});
|
||||
|
||||
test("批量插入 Job 缓存并支持单号精准与大小写不敏感检索", () => {
|
||||
const records: WorkflowJobCacheRecord[] = [
|
||||
{
|
||||
corpId: "corp_winner",
|
||||
pipelineId: "pipe_101",
|
||||
jobId: "job_001",
|
||||
flowCode: "XZ2610474",
|
||||
flowId: "flow_abc",
|
||||
executeTime: 1700000000000,
|
||||
status: "COMPLETED",
|
||||
userName: "欧阳亚玲",
|
||||
},
|
||||
{
|
||||
corpId: "corp_winner",
|
||||
pipelineId: "pipe_101",
|
||||
jobId: "job_002",
|
||||
flowCode: "XZ2610475",
|
||||
status: "RUNNING",
|
||||
},
|
||||
];
|
||||
|
||||
upsertWorkflowJobIndex(records, ":memory:");
|
||||
|
||||
// 精确匹配
|
||||
const hitExact = getCachedWorkflowJobByCode("corp_winner", "pipe_101", "XZ2610474", ":memory:");
|
||||
expect(hitExact).not.toBeNull();
|
||||
expect(hitExact?.jobId).toBe("job_001");
|
||||
expect(hitExact?.userName).toBe("欧阳亚玲");
|
||||
expect(hitExact?.status).toBe("COMPLETED");
|
||||
|
||||
// 小写模糊匹配
|
||||
const hitLower = getCachedWorkflowJobByCode("corp_winner", "pipe_101", "xz2610474", ":memory:");
|
||||
expect(hitLower).not.toBeNull();
|
||||
expect(hitLower?.jobId).toBe("job_001");
|
||||
|
||||
// 不存在的单号或不同企业/工作流应返回 null
|
||||
expect(getCachedWorkflowJobByCode("corp_winner", "pipe_101", "NON_EXISTENT", ":memory:")).toBeNull();
|
||||
expect(getCachedWorkflowJobByCode("other_corp", "pipe_101", "XZ2610474", ":memory:")).toBeNull();
|
||||
expect(getCachedWorkflowJobByCode("corp_winner", "other_pipe", "XZ2610474", ":memory:")).toBeNull();
|
||||
});
|
||||
|
||||
test("同单号存在多笔记录时,按 execute_time DESC 优先返回最新执行的一笔", () => {
|
||||
const records: WorkflowJobCacheRecord[] = [
|
||||
{
|
||||
corpId: "corp_winner",
|
||||
pipelineId: "pipe_101",
|
||||
jobId: "job_old",
|
||||
flowCode: "XZ2610474",
|
||||
executeTime: 1000,
|
||||
status: "FAILED",
|
||||
},
|
||||
{
|
||||
corpId: "corp_winner",
|
||||
pipelineId: "pipe_101",
|
||||
jobId: "job_new",
|
||||
flowCode: "XZ2610474",
|
||||
executeTime: 2000,
|
||||
status: "COMPLETED",
|
||||
},
|
||||
];
|
||||
|
||||
upsertWorkflowJobIndex(records, ":memory:");
|
||||
|
||||
const hit = getCachedWorkflowJobByCode("corp_winner", "pipe_101", "XZ2610474", ":memory:");
|
||||
expect(hit).not.toBeNull();
|
||||
expect(hit?.jobId).toBe("job_new");
|
||||
expect(hit?.status).toBe("COMPLETED");
|
||||
});
|
||||
|
||||
test("相同主键冲突时,执行增量更新并保持已有非空字段 (COALESCE)", () => {
|
||||
upsertWorkflowJobIndex(
|
||||
[
|
||||
{
|
||||
corpId: "corp_winner",
|
||||
pipelineId: "pipe_101",
|
||||
jobId: "job_001",
|
||||
flowCode: "XZ2610474",
|
||||
userName: "欧阳亚玲",
|
||||
status: "RUNNING",
|
||||
executeTime: 1000,
|
||||
},
|
||||
],
|
||||
":memory:"
|
||||
);
|
||||
|
||||
// 再次写入,只更新 status,未传 userName
|
||||
upsertWorkflowJobIndex(
|
||||
[
|
||||
{
|
||||
corpId: "corp_winner",
|
||||
pipelineId: "pipe_101",
|
||||
jobId: "job_001",
|
||||
status: "COMPLETED",
|
||||
},
|
||||
],
|
||||
":memory:"
|
||||
);
|
||||
|
||||
const hit = getCachedWorkflowJobByCode("corp_winner", "pipe_101", "XZ2610474", ":memory:");
|
||||
expect(hit).not.toBeNull();
|
||||
expect(hit?.jobId).toBe("job_001");
|
||||
expect(hit?.status).toBe("COMPLETED");
|
||||
// userName 依然保留
|
||||
expect(hit?.userName).toBe("欧阳亚玲");
|
||||
});
|
||||
|
||||
test("空数组调用 upsert 不抛异常", () => {
|
||||
expect(() => upsertWorkflowJobIndex([], ":memory:")).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
import { getDatabase } from "./connection.js";
|
||||
|
||||
export interface WorkflowJobCacheRecord {
|
||||
corpId: string;
|
||||
pipelineId: string;
|
||||
jobId: string;
|
||||
flowCode?: string;
|
||||
flowId?: string;
|
||||
executeTime?: number;
|
||||
status?: string;
|
||||
userName?: string;
|
||||
updatedAt?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按企业 + 工作流 + 单号快速从本地 SQLite 缓存中检索匹配的 Job 记录
|
||||
*/
|
||||
export function getCachedWorkflowJobByCode(
|
||||
corpId: string,
|
||||
pipelineId: string,
|
||||
flowCode: string,
|
||||
dbPath?: string
|
||||
): WorkflowJobCacheRecord | null {
|
||||
const db = getDatabase(dbPath);
|
||||
const normalizedCode = flowCode.trim();
|
||||
const row = db
|
||||
.query(
|
||||
`SELECT corp_id AS corpId, pipeline_id AS pipelineId, job_id AS jobId,
|
||||
flow_code AS flowCode, flow_id AS flowId, execute_time AS executeTime,
|
||||
status, user_name AS userName, updated_at AS updatedAt
|
||||
FROM workflow_job_index
|
||||
WHERE corp_id = ? AND pipeline_id = ? AND (flow_code = ? OR UPPER(flow_code) = UPPER(?))
|
||||
ORDER BY execute_time DESC, updated_at DESC
|
||||
LIMIT 1`
|
||||
)
|
||||
.get(corpId, pipelineId, normalizedCode, normalizedCode) as WorkflowJobCacheRecord | null;
|
||||
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入或更新 Job 缓存索引
|
||||
*/
|
||||
export function upsertWorkflowJobIndex(
|
||||
records: WorkflowJobCacheRecord[],
|
||||
dbPath?: string
|
||||
): void {
|
||||
if (!records.length) return;
|
||||
const db = getDatabase(dbPath);
|
||||
const now = Date.now();
|
||||
|
||||
const insertStmt = db.prepare(`
|
||||
INSERT INTO workflow_job_index (
|
||||
corp_id, pipeline_id, job_id, flow_code, flow_id, execute_time, status, user_name, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(corp_id, pipeline_id, job_id) DO UPDATE SET
|
||||
flow_code = COALESCE(excluded.flow_code, workflow_job_index.flow_code),
|
||||
flow_id = COALESCE(excluded.flow_id, workflow_job_index.flow_id),
|
||||
execute_time = COALESCE(excluded.execute_time, workflow_job_index.execute_time),
|
||||
status = COALESCE(excluded.status, workflow_job_index.status),
|
||||
user_name = COALESCE(excluded.user_name, workflow_job_index.user_name),
|
||||
updated_at = excluded.updated_at
|
||||
`);
|
||||
|
||||
db.transaction(() => {
|
||||
for (const r of records) {
|
||||
insertStmt.run(
|
||||
r.corpId,
|
||||
r.pipelineId,
|
||||
r.jobId,
|
||||
r.flowCode || null,
|
||||
r.flowId || null,
|
||||
r.executeTime || null,
|
||||
r.status || null,
|
||||
r.userName || null,
|
||||
now
|
||||
);
|
||||
}
|
||||
})();
|
||||
}
|
||||
Reference in new issue
Block a user