F1-4b: 批量转换接 attachScriptOnEvent,产物补挂 onEvent 重入库双库
- BatchConvertCli: 闸一通过后按 rewritten/<tid>/*.FULL_AUTO.groovy 挂 onEvent(第5参数=产物根);guardUrl 改为禁 ceshi+允许本地库 - application-dev.yml: eip4.script.rewritten-root 配置(实际部署建议绝对路径启动参覆盖) - 双库各 379/1,493 条产物带 execute-template 钩子;e2e script-replay 2/2 绿 - 坑: yml 是 jar 内资源,改后须重打包;启动工作目录影响相对路径解析 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
@@ -29,13 +30,21 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
* <p>流程:从客户库(PostgreSQL,只读)拉全部 view_template.xml → 逐个 {@link TemplateToAmisConverter}
|
||||
* 转换 → 闸一 {@link SchemaGate} 官方 schema 校验 → 产物落盘 + 分级清单 CSV。
|
||||
*
|
||||
* <p>用法:
|
||||
* <p>F1-4b 扩展:第 5 参数为改写产物根目录(含 full_auto_index.json),转换完成后自动调用
|
||||
* {@link TemplateToAmisConverter#attachScriptOnEvent} 补挂 onEvent 脚本钩子。
|
||||
*
|
||||
* <p>用法(本地 eip4dev):
|
||||
* <pre>{@code
|
||||
* java -cp ... com.eip4.platform.converter.BatchConvertCli \
|
||||
* "jdbc:postgresql://localhost:5433/eip4dev?currentSchema=eip3" zhengwr "" \
|
||||
* 转换产物 tools/s3_rewriter/output
|
||||
* }</pre>
|
||||
* 原客户库用法(5435):
|
||||
* <pre>{@code
|
||||
* java -cp ... com.eip4.platform.converter.BatchConvertCli \
|
||||
* "jdbc:postgresql://192.168.20.68:5435/postgres?currentSchema=eip3" ceshi ceshi 转换产物
|
||||
* }</pre>
|
||||
* 参数缺省即上述客户库(只读)。⚠️ 5435 上有个不完整的 ceshi 库——严禁连 dbname=ceshi;本 CLI 硬校验
|
||||
* URL 必须 dbname=postgres,否则拒绝启动。
|
||||
* ⚠️ 禁止连 dbname=ceshi(5435 上有不完整的 ceshi 库);localhost 允许 eip4dev/eip4m0/postgres。
|
||||
*/
|
||||
public final class BatchConvertCli {
|
||||
|
||||
@@ -44,6 +53,8 @@ public final class BatchConvertCli {
|
||||
private static final String DEFAULT_USER = "ceshi";
|
||||
private static final String DEFAULT_PASS = "ceshi";
|
||||
private static final String DEFAULT_OUT = "转换产物";
|
||||
/** 第 5 参数:改写产物根目录(含 full_auto_index.json),为空则不挂 onEvent。 */
|
||||
private static final String DEFAULT_REWRITER_DIR = "";
|
||||
|
||||
private BatchConvertCli() {
|
||||
}
|
||||
@@ -53,6 +64,9 @@ public final class BatchConvertCli {
|
||||
String user = args.length > 1 ? args[1] : DEFAULT_USER;
|
||||
String pass = args.length > 2 ? args[2] : DEFAULT_PASS;
|
||||
Path outDir = Path.of(args.length > 3 ? args[3] : DEFAULT_OUT);
|
||||
String rewriterDirStr = args.length > 4 ? args[4] : DEFAULT_REWRITER_DIR;
|
||||
Path rewriterDir = rewriterDirStr != null && !rewriterDirStr.isBlank()
|
||||
? Path.of(rewriterDirStr) : null;
|
||||
|
||||
guardUrl(url);
|
||||
|
||||
@@ -60,24 +74,66 @@ public final class BatchConvertCli {
|
||||
conn.setReadOnly(true); // 只读保护(任务红线)
|
||||
conn.setAutoCommit(true);
|
||||
try {
|
||||
BatchSummary summary = run(conn, outDir);
|
||||
BatchSummary summary = run(conn, outDir, rewriterDir);
|
||||
System.out.println(summary.render());
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
/** dbname 必须是 postgres——拦住误连 5435 上不完整的 ceshi 库(任务红线)。 */
|
||||
/**
|
||||
* URL 安全校验(任务红线):
|
||||
* - 禁止 dbname=ceshi(5435 上有不完整的 ceshi 库)
|
||||
* - localhost 允许 eip4dev / eip4m0 / postgres
|
||||
* - 非 localhost 只允许 dbname=postgres
|
||||
*/
|
||||
static void guardUrl(String url) {
|
||||
String afterHost = url.replaceFirst("^jdbc:postgresql://[^/]+/", "");
|
||||
String db = afterHost.split("[?]", 2)[0];
|
||||
if (!"postgres".equals(db)) {
|
||||
if ("ceshi".equals(db)) {
|
||||
throw new IllegalArgumentException(
|
||||
"拒绝连接:dbname=" + db + ",只允许 dbname=postgres(5435 上有不完整的 ceshi 库,严禁连)");
|
||||
"拒绝连接:dbname=ceshi 是红线库,严禁连接");
|
||||
}
|
||||
boolean isLocalhost = url.contains("//localhost:") || url.contains("//127.0.0.1:");
|
||||
if (!isLocalhost && !"postgres".equals(db)) {
|
||||
throw new IllegalArgumentException(
|
||||
"拒绝连接:非 localhost 连接 dbname=" + db + ",只允许 dbname=postgres(5435 上有不完整的 ceshi 库,严禁连)");
|
||||
}
|
||||
// localhost 允许 eip4dev / eip4m0 / postgres
|
||||
if (isLocalhost && !"eip4dev".equals(db) && !"eip4m0".equals(db) && !"postgres".equals(db)) {
|
||||
throw new IllegalArgumentException(
|
||||
"拒绝连接:localhost dbname=" + db + ",只允许 eip4dev / eip4m0 / postgres");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 full_auto_index.json 加载 templateId → seqs 映射。
|
||||
* 文件结构:[{"template_id":"...","seq":1,...}, ...]
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
static Map<String, List<Integer>> loadFullAutoIndex(Path rewriterDir) throws IOException {
|
||||
Path indexFile = rewriterDir.resolve("full_auto_index.json");
|
||||
if (!Files.exists(indexFile)) {
|
||||
return Map.of();
|
||||
}
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
List<Map<String, Object>> entries = om.readValue(indexFile.toFile(), List.class);
|
||||
Map<String, List<Integer>> result = new LinkedHashMap<>();
|
||||
for (Map<String, Object> entry : entries) {
|
||||
String tid = (String) entry.get("template_id");
|
||||
Object seqObj = entry.get("seq");
|
||||
if (tid == null || seqObj == null) continue;
|
||||
int seq = ((Number) seqObj).intValue();
|
||||
result.computeIfAbsent(tid, k -> new ArrayList<>()).add(seq);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static BatchSummary run(Connection conn, Path outDir) throws IOException, SQLException {
|
||||
return run(conn, outDir, null);
|
||||
}
|
||||
|
||||
static BatchSummary run(Connection conn, Path outDir, Path rewriterDir) throws IOException, SQLException {
|
||||
Path schemaDir = outDir.resolve("schema");
|
||||
Files.createDirectories(schemaDir);
|
||||
|
||||
@@ -86,14 +142,19 @@ public final class BatchConvertCli {
|
||||
SchemaGate gate = new SchemaGate();
|
||||
Function<String, String> pageResolver = pageId -> loadPageXml(conn, pageId);
|
||||
|
||||
// F1-4b:加载 full_auto_index.json(rewriterDir 为空则不挂 onEvent)
|
||||
Map<String, List<Integer>> fullAutoIndex = (rewriterDir != null)
|
||||
? loadFullAutoIndex(rewriterDir) : Map.of();
|
||||
System.out.println("[F1-4b] full_auto_index 已加载:" + fullAutoIndex.size() + " 个模板有 FULL_AUTO 脚本");
|
||||
|
||||
ObjectMapper json = new ObjectMapper();
|
||||
json.enable(SerializationFeature.INDENT_OUTPUT);
|
||||
|
||||
BatchSummary summary = new BatchSummary();
|
||||
List<String[]> csvRows = new ArrayList<>();
|
||||
// CSV 表头:模板id,名称,type,根元素,分级,闸一(schema),映射条数,原因归类
|
||||
// CSV 表头:模板id,名称,type,根元素,分级,闸一(schema),映射条数,onEvent挂载,原因归类
|
||||
csvRows.add(new String[]{"view_template_id", "name", "type", "root", "grade",
|
||||
"gate1_schema_valid", "mapping_notes", "reasons"});
|
||||
"gate1_schema_valid", "mapping_notes", "onEvent_attached", "reasons"});
|
||||
|
||||
String sql = "SELECT view_template_id, name, type, xml::text AS xmltext "
|
||||
+ "FROM view_template ORDER BY type, view_template_id";
|
||||
@@ -106,16 +167,25 @@ public final class BatchConvertCli {
|
||||
String xml = rs.getString("xmltext");
|
||||
|
||||
if (xml == null || xml.isBlank()) {
|
||||
summary.record("(null-xml)", ConversionGrade.FAILED, false);
|
||||
summary.record("(null-xml)", ConversionGrade.FAILED, false, false);
|
||||
csvRows.add(new String[]{id, name, String.valueOf(type), "(null-xml)",
|
||||
"FAILED", "false", "0", "模板数据问题:xml 为空(孤儿模板)"});
|
||||
"FAILED", "false", "0", "false", "模板数据问题:xml 为空(孤儿模板)"});
|
||||
continue;
|
||||
}
|
||||
|
||||
List<String> notes = new ArrayList<>();
|
||||
ConversionResult result = converter.convertGraded(xml, notes, pageResolver);
|
||||
boolean gate1 = false;
|
||||
boolean onEventAttached = false;
|
||||
if (result.getSchema() != null) {
|
||||
// F1-4b:闸一通过前先挂 onEvent(挂后再校验,确保含 onEvent 的 schema 也能过闸)
|
||||
List<Integer> seqs = fullAutoIndex.get(id);
|
||||
if (seqs != null && !seqs.isEmpty()) {
|
||||
converter.attachScriptOnEvent(result.getSchema(), id, seqs);
|
||||
onEventAttached = true;
|
||||
summary.onEventCount++;
|
||||
}
|
||||
|
||||
List<String> errors = gate.validate(result.getSchema());
|
||||
gate1 = errors.isEmpty();
|
||||
if (!gate1) {
|
||||
@@ -129,11 +199,12 @@ public final class BatchConvertCli {
|
||||
}
|
||||
|
||||
ConversionGrade effective = effectiveGrade(result.getGrade(), gate1);
|
||||
summary.record(result.getRootElement(), effective, gate1);
|
||||
summary.record(result.getRootElement(), effective, gate1, onEventAttached);
|
||||
csvRows.add(new String[]{
|
||||
id, name, String.valueOf(type), result.getRootElement(),
|
||||
effective.name(), String.valueOf(gate1),
|
||||
String.valueOf(result.getMappingNotes().size()),
|
||||
String.valueOf(onEventAttached),
|
||||
classifyReasons(result)});
|
||||
}
|
||||
}
|
||||
@@ -215,19 +286,21 @@ public final class BatchConvertCli {
|
||||
}
|
||||
}
|
||||
|
||||
/** 跑批统计累加器:总量 + 各根元素分布 + 分级 + 闸一通过率。 */
|
||||
/** 跑批统计累加器:总量 + 各根元素分布 + 分级 + 闸一通过率 + onEvent 挂载数。 */
|
||||
static final class BatchSummary {
|
||||
int total;
|
||||
int gate1Pass;
|
||||
int onEventCount; // F1-4b:挂上 onEvent 的模板数
|
||||
final Map<ConversionGrade, Integer> grades = new LinkedHashMap<>();
|
||||
final Map<String, int[]> byRoot = new TreeMap<>(); // root → [total, gate1Pass]
|
||||
|
||||
void record(String root, ConversionGrade grade, boolean gate1) {
|
||||
void record(String root, ConversionGrade grade, boolean gate1, boolean onEvent) {
|
||||
total++;
|
||||
grades.merge(grade, 1, Integer::sum);
|
||||
if (gate1) {
|
||||
gate1Pass++;
|
||||
}
|
||||
// onEventCount 在主循环中直接累加,此处保持签名对称
|
||||
int[] r = byRoot.computeIfAbsent(root, k -> new int[2]);
|
||||
r[0]++;
|
||||
if (gate1) {
|
||||
@@ -237,10 +310,12 @@ public final class BatchConvertCli {
|
||||
|
||||
String render() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("=== P4 批量转换汇总 ===\n");
|
||||
sb.append("=== P4 批量转换汇总(F1-4b)===\n");
|
||||
sb.append("总量: ").append(total).append('\n');
|
||||
sb.append("闸一(schema)通过: ").append(gate1Pass)
|
||||
.append(" (").append(pct(gate1Pass, total)).append(")\n");
|
||||
sb.append("onEvent 脚本钩子挂载: ").append(onEventCount)
|
||||
.append(" 个模板\n");
|
||||
sb.append("分级: ");
|
||||
grades.forEach((g, n) -> sb.append(g).append('=').append(n).append(" "));
|
||||
sb.append('\n');
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user