优化:按分析章节整合图表解读与完整报告导出

This commit is contained in:
2026-10-23 21:36:28 +08:00
parent 13695da433
commit ec71609343
21 changed files with 1607 additions and 259 deletions
@@ -19,14 +19,15 @@ import tools.jackson.databind.json.JsonMapper;
public final class AnalysisVisualSnapshotAccess {
public static final int SCHEMA_VERSION = 1;
public static final int MAX_SNAPSHOT_BYTES = 256 * 1024;
private static final int MAX_VISUALS = 3;
private static final int MAX_VISUALS = 12;
private static final int MAX_CATEGORIES = 36;
private static final int MAX_SERIES = 1;
private static final int MAX_TEXT_BYTES = 2048;
private static final Pattern SAFE_KEY = Pattern.compile("[A-Za-z0-9][A-Za-z0-9_.:-]{0,119}");
private static final Pattern SAFE_EVIDENCE_REF = Pattern.compile("[A-Za-z0-9:_-]{1,160}");
private static final Set<String> CHART_KEYS = Set.of("id", "type", "title", "subtitle", "unit", "categories", "series",
"evidenceRefs", "requiredFields", "scope", "period", "comparisonPeriod", "stage", "metric", "coverage", "note", "originTurnId");
"evidenceRefs", "requiredFields", "scope", "period", "comparisonPeriod", "stage", "metric", "coverage", "note",
"originTurnId", "sectionKey", "dimension");
private static final Set<String> SCOPE_KEYS = Set.of("year", "month", "quarter", "department", "group", "product",
"projectGroup", "view", "includeUnassigned", "releaseId", "extraFilters", "stage", "category", "defect");
private static final Set<String> PERIOD_KEYS = Set.of("start", "end", "label", "granularity", "year", "month", "quarter");
@@ -122,12 +123,18 @@ public final class AnalysisVisualSnapshotAccess {
String id = text(input.get("id"), 120, true);
if (!SAFE_KEY.matcher(id).matches()) return null;
String type = enumText(input.get("type"), Set.of("bar", "line", "column", "area", "heatmap", "donut", "pareto"));
String type = enumText(input.get("type"), Set.of("bar", "line", "column", "area", "heatmap", "donut", "pareto", "waterfall"));
String title = text(input.get("title"), 200, false);
String subtitle = input.containsKey("subtitle") ? text(input.get("subtitle"), 400, false) : null;
String unit = enumText(input.get("unit"), Set.of("ratio", "kg", "pp", "count"));
String metric = enumText(input.get("metric"), metricNames());
String origin = text(input.get("originTurnId"), 64, true);
String sectionKey = input.containsKey("sectionKey")
? enumText(input.get("sectionKey"), Set.of("overview", "stage", "department", "group", "product",
"category_black", "category_bright", "defect_black", "defect_bright", "trend", "composition")) : null;
String dimension = input.containsKey("dimension")
? enumText(input.get("dimension"), Set.of("department", "group", "product", "category", "defect")) : null;
if (input.containsKey("sectionKey") && sectionKey == null || input.containsKey("dimension") && dimension == null) return null;
if (turnId == null || !turnId.equals(origin)) return null;
MetricSemantics semantics = semantics(metric, input.get("stage"));
@@ -152,12 +159,14 @@ public final class AnalysisVisualSnapshotAccess {
Map<String, Object> coverage = normalizeCoverage(input.get("coverage"), requiredFields, semantics.stage(), type);
if (coverage == null) return null;
if (!validCompositionChart(type, metric, unit, semantics, categories, series, coverage, comparisonPeriod)
|| !validPeriodChart(type, metric, semantics, period, comparisonPeriod, categories, series)) return null;
|| !validPeriodChart(type, metric, semantics, period, comparisonPeriod, categories, series)
|| !validWaterfallChart(type, metric, unit, semantics, categories, series, coverage, period, comparisonPeriod)) return null;
if (!allowedFields.contains("*") && !allowedFields.containsAll(requiredFields)) return null;
Map<String, Object> scope = normalizeScope(input.get("scope"), visibleExtraFilterKeys, expectedScope);
Object scopedStage = scope == null ? null : scope.get("stage");
if (scope == null || scopedStage != null && !"all".equals(scopedStage)
&& (semantics.stage() == null || !semantics.stage().equals(scopedStage))) return null;
if (!validChapterMetadata(sectionKey, dimension, type, metric, semantics, scope, period)) return null;
if (semantics.sourceDefect() && !sourceDefectScopeMatches(scope, semantics.stage(), categories)) return null;
String note = input.containsKey("note") ? text(input.get("note"), 500, false) : null;
@@ -171,6 +180,8 @@ public final class AnalysisVisualSnapshotAccess {
if (semantics.stage() != null) result.put("stage", semantics.stage());
result.put("metric", metric); result.put("coverage", coverage);
if (note != null) result.put("note", note);
if (sectionKey != null) result.put("sectionKey", sectionKey);
if (dimension != null) result.put("dimension", dimension);
result.put("originTurnId", turnId);
return result;
}
@@ -219,7 +230,8 @@ public final class AnalysisVisualSnapshotAccess {
"bright_headquarters_scrap", "bright_plating_scrap", "bright_material_scrap",
"bright_processing_scrap", "bright_diffusion_scrap", "current_scrap_weight",
"source_defect_weight").contains(metric) && decimal.signum() < 0) return null;
if ("rate_drop_pp".equals(metric) && decimal.abs().compareTo(BigDecimal.valueOf(100)) > 0) return null;
if (Set.of("rate_drop_pp", "composite_impact_pp").contains(metric)
&& decimal.abs().compareTo(BigDecimal.valueOf(100)) > 0) return null;
numbers.add(decimal);
}
}
@@ -409,6 +421,86 @@ public final class AnalysisVisualSnapshotAccess {
return sum.signum() > 0;
}
private static boolean validWaterfallChart(String type, String metric, String unit, MetricSemantics semantics,
List<Map<String, Object>> categories,
List<Map<String, Object>> series,
Map<String, Object> coverage,
Map<String, Object> period,
Map<String, Object> comparisonPeriod) {
if (!"waterfall".equals(type)) return !"composite_impact_pp".equals(metric);
if (!"composite_impact_pp".equals(metric) || !"pp".equals(unit) || semantics.stage() != null
|| !Set.of("black_good_weight", "black_total_weight", "bright_good_weight", "bright_total_weight")
.equals(semantics.requiredFields())
|| !List.of("black", "bright", "total").equals(
categories.stream().map(item -> item.get("key")).toList())
|| series.size() != 1 || comparisonPeriod == null || samePeriod(period, comparisonPeriod)) return false;
Object totalRaw = coverage.get("totalRows"), returnedRaw = coverage.get("returnedRows");
if (!(totalRaw instanceof Number total) || !(returnedRaw instanceof Number returned)
|| total.longValue() != 3 || returned.longValue() != 3 || !Boolean.FALSE.equals(coverage.get("truncated"))) return false;
Object raw = series.getFirst().get("values");
if (!(raw instanceof List<?> values) || values.size() != 3) return false;
BigDecimal[] points = new BigDecimal[3];
for (int index = 0; index < values.size(); index++) {
points[index] = decimal(values.get(index));
if (points[index] == null || points[index].abs().compareTo(BigDecimal.valueOf(100)) > 0) return false;
}
BigDecimal tolerance = new BigDecimal("0.000001");
return points[0].add(points[1]).subtract(points[2]).abs().compareTo(tolerance) <= 0;
}
private static boolean samePeriod(Map<String, Object> period, Map<String, Object> comparisonPeriod) {
if (period.keySet().stream().anyMatch(Set.of("start", "end", "granularity")::contains)
|| comparisonPeriod.keySet().stream().anyMatch(Set.of("start", "end", "granularity")::contains)) {
return period.get("start") != null && period.get("start").equals(comparisonPeriod.get("start"))
&& java.util.Objects.equals(period.get("end"), comparisonPeriod.get("end"))
&& java.util.Objects.equals(period.get("granularity"), comparisonPeriod.get("granularity"));
}
return java.util.Objects.equals(period.get("year"), comparisonPeriod.get("year"))
&& java.util.Objects.equals(period.get("month"), comparisonPeriod.get("month"))
&& java.util.Objects.equals(period.get("quarter"), comparisonPeriod.get("quarter"));
}
private static boolean validChapterMetadata(String section, String dimension, String type, String metric,
MetricSemantics semantics, Map<String, Object> scope,
Map<String, Object> period) {
if (section == null && dimension == null) return true; // schema-1 snapshots created before chapter metadata
boolean grouped = Set.of("bar", "column").contains(type);
boolean sourceMetric = Set.of("source_defect_weight", "source_defect_delta_weight").contains(metric)
&& semantics.sourceDefect();
if (dimension != null && section == null) {
return switch (dimension) {
case "department", "group", "product" -> grouped
&& Set.of("current_scrap_weight", "scrap_delta", "rate_drop_pp").contains(metric);
case "category" -> sourceMetric && (grouped || Set.of("donut", "pareto").contains(type));
case "defect" -> sourceMetric && grouped;
default -> false;
};
}
return switch (section) {
case "overview" -> dimension == null && (("composite_rate".equals(metric) && Set.of("bar", "column").contains(type))
|| "composite_impact_pp".equals(metric) && "waterfall".equals(type));
case "stage" -> dimension == null && Set.of("black_rate", "bright_rate").contains(metric)
&& semantics.stage() != null && semantics.stage().equals(metric.substring(0, metric.indexOf('_')))
&& grouped;
case "department" -> "department".equals(dimension) && grouped
&& Set.of("current_scrap_weight", "scrap_delta", "rate_drop_pp").contains(metric);
case "group" -> "group".equals(dimension) && grouped
&& Set.of("current_scrap_weight", "scrap_delta", "rate_drop_pp").contains(metric);
case "product" -> "product".equals(dimension) && grouped
&& Set.of("current_scrap_weight", "scrap_delta", "rate_drop_pp").contains(metric);
case "category_black", "category_bright" -> "category".equals(dimension) && grouped && sourceMetric
&& semantics.stage().equals("category_black".equals(section) ? "black" : "bright");
case "defect_black", "defect_bright" -> "defect".equals(dimension) && grouped && sourceMetric
&& semantics.stage().equals("defect_black".equals(section) ? "black" : "bright");
case "trend" -> dimension == null && Set.of("line", "area", "heatmap").contains(type)
&& Set.of("composite_rate", "black_rate", "bright_rate", "current_scrap_weight").contains(metric)
&& period.containsKey("start") && period.containsKey("end") && period.containsKey("granularity");
case "composition" -> "category".equals(dimension) && Set.of("donut", "pareto").contains(type)
&& "source_defect_weight".equals(metric) && semantics.sourceDefect();
default -> false;
};
}
private static boolean validPeriodChart(String type, String metric, MetricSemantics semantics, Map<String, Object> period,
Map<String, Object> comparisonPeriod,
List<Map<String, Object>> categories,
@@ -555,6 +647,8 @@ public final class AnalysisVisualSnapshotAccess {
: new MetricSemantics("pp", stage,
stage.equals("black") ? Set.of("black_good_weight", "black_total_weight")
: Set.of("bright_good_weight", "bright_total_weight"), false);
case "composite_impact_pp" -> stage == null ? new MetricSemantics("pp", null,
Set.of("black_good_weight", "black_total_weight", "bright_good_weight", "bright_total_weight"), false) : null;
case "source_defect_weight", "source_defect_delta_weight" -> stage == null ? null
: new MetricSemantics("kg", stage, Set.of(), true);
default -> null;
@@ -564,7 +658,7 @@ public final class AnalysisVisualSnapshotAccess {
private static Set<String> metricNames() {
Set<String> result = new LinkedHashSet<>(WEIGHT_METRICS);
result.addAll(Set.of("composite_rate", "black_rate", "bright_rate", "scrap_delta", "rate_drop_pp",
"current_scrap_weight", "source_defect_weight", "source_defect_delta_weight"));
"current_scrap_weight", "source_defect_weight", "source_defect_delta_weight", "composite_impact_pp"));
return result;
}
@@ -131,9 +131,79 @@ class AnalysisVisualSnapshotAccessTest {
((List<Map<String, Object>>) wrongSeries.get("series")).getFirst().put("key", "bright_rate");
assertEquals("failed", normalize(List.of(wrongSeries), RATE_FIELDS, SCOPE).state());
List<Map<String, Object>> tooMany = new ArrayList<>();
for (int i = 0; i < 4; i++) tooMany.add(chart("black_rate", "ratio", "black", List.of(), List.of(0.9, 0.95), "v1-" + i));
assertEquals("failed", normalize(tooMany, RATE_FIELDS, SCOPE).state());
List<Map<String, Object>> maximum = new ArrayList<>();
for (int i = 0; i < 12; i++) maximum.add(chart("black_rate", "ratio", "black", List.of(), List.of(0.9, 0.95), "v1-" + i));
assertEquals("ready", normalize(maximum, RATE_FIELDS, SCOPE).state());
maximum.add(chart("black_rate", "ratio", "black", List.of(), List.of(0.9, 0.95), "v1-overflow"));
assertEquals("failed", normalize(maximum, RATE_FIELDS, SCOPE).state());
}
@Test
void acceptsReconciledMathOnlyWaterfallAndRejectsForgedDecompositionShapes() {
Set<String> allFields = Set.of("black_good_weight", "black_total_weight", "bright_good_weight", "bright_total_weight");
Map<String, Object> valid = waterfallChart();
AnalysisVisualSnapshotAccess.Snapshot accepted = normalize(List.of(valid), allFields, SCOPE);
assertEquals("ready", accepted.state());
assertEquals("overview", accepted.visuals().getFirst().get("sectionKey"));
assertEquals(allFields, Set.copyOf((List<String>) accepted.visuals().getFirst().get("requiredFields")));
Map<String, Object> wrongSum = waterfallChart();
wrongSum.put("series", List.of(Map.of("key", "composite_impact_pp", "label", "影响", "values", List.of(-0.3, -0.14, -0.5))));
Map<String, Object> wrongKeys = waterfallChart();
wrongKeys.put("categories", List.of(Map.of("key", "black", "label", "黑片"),
Map.of("key", "total", "label", "总计"), Map.of("key", "bright", "label", "亮片")));
Map<String, Object> missingComparisonPeriod = waterfallChart();
missingComparisonPeriod.remove("comparisonPeriod");
Map<String, Object> wrongChapter = waterfallChart();
wrongChapter.put("sectionKey", "stage");
Map<String, Object> wrongDimension = waterfallChart();
wrongDimension.put("dimension", "product");
Map<String, Object> nullPoint = waterfallChart();
nullPoint.put("series", List.of(Map.of("key", "composite_impact_pp", "label", "影响", "values", java.util.Arrays.asList(-0.3, null, -0.44))));
for (Map<String, Object> invalid : List.of(wrongSum, wrongKeys, missingComparisonPeriod, wrongChapter, wrongDimension, nullPoint))
assertEquals("failed", normalize(List.of(invalid), allFields, SCOPE).state());
}
@Test
void chapterMetadataMustMatchTheChartMeaningWhileLegacySnapshotsRemainValid() {
Map<String, Object> valid = chart("black_rate", "ratio", "black", List.of(), List.of(0.9, 0.95));
valid.put("sectionKey", "stage");
AnalysisVisualSnapshotAccess.Snapshot accepted = normalize(List.of(valid), RATE_FIELDS, SCOPE);
assertEquals("ready", accepted.state());
assertEquals("stage", accepted.visuals().getFirst().get("sectionKey"));
Map<String, Object> legacy = chart("black_rate", "ratio", "black", List.of(), List.of(0.9, 0.95));
assertEquals("ready", normalize(List.of(legacy), RATE_FIELDS, SCOPE).state());
Map<String, Object> wrongDimension = chart("black_rate", "ratio", "black", List.of(), List.of(0.9, 0.95));
wrongDimension.put("sectionKey", "stage");
wrongDimension.put("dimension", "product");
Map<String, Object> unknownSection = chart("black_rate", "ratio", "black", List.of(), List.of(0.9, 0.95));
unknownSection.put("sectionKey", "root_cause");
assertEquals("failed", normalize(List.of(wrongDimension), RATE_FIELDS, SCOPE).state());
assertEquals("failed", normalize(List.of(unknownSection), RATE_FIELDS, SCOPE).state());
}
@Test
void formalCategoryAndSpecificDefectSectionsRequireMatchingStageAndDimension() {
String formalField = "black_responsibility_scrap";
Map<String, Object> category = sourceDefectChart("black", formalField, List.of(formalField));
category.put("sectionKey", "category_black");
category.put("dimension", "category");
assertEquals("ready", normalize(List.of(category), Set.of(formalField), SCOPE).state());
Map<String, Object> rawDetail = sourceDefectChart("black", "责废其他重量", List.of("责废其他重量"));
rawDetail.put("sectionKey", "defect_black");
rawDetail.put("dimension", "defect");
assertEquals("ready", normalize(List.of(rawDetail), Set.of("责废其他重量"), SCOPE).state());
Map<String, Object> wrongCategoryStage = new LinkedHashMap<>(category);
wrongCategoryStage.put("sectionKey", "category_bright");
Map<String, Object> wrongDetailDimension = new LinkedHashMap<>(rawDetail);
wrongDetailDimension.put("dimension", "category");
assertEquals("failed", normalize(List.of(wrongCategoryStage), Set.of(formalField), SCOPE).state());
assertEquals("failed", normalize(List.of(wrongDetailDimension), Set.of("责废其他重量"), SCOPE).state());
}
@Test
@@ -220,6 +290,11 @@ class AnalysisVisualSnapshotAccessTest {
Map<String, Object> pareto = categoryShareChart("pareto", keys, List.of(3.0, 2.0, 1.0));
assertEquals("ready", normalize(List.of(pareto), Set.copyOf(keys), SCOPE).state());
Map<String, Object> taggedDonut = categoryShareChart("donut", keys, List.of(1.0, 2.0, 3.0));
taggedDonut.put("sectionKey", "composition");
taggedDonut.put("dimension", "category");
assertEquals("ready", normalize(List.of(taggedDonut), Set.copyOf(keys), SCOPE).state());
Map<String, Object> unsorted = categoryShareChart("pareto", keys, List.of(2.0, 3.0, 1.0));
Map<String, Object> negative = categoryShareChart("donut", keys, List.of(2.0, -1.0, 1.0));
Map<String, Object> missing = categoryShareChart("donut", keys, java.util.Arrays.asList(2.0, null, 1.0));
@@ -345,4 +420,19 @@ class AnalysisVisualSnapshotAccessTest {
chart.put("series", List.of(Map.of("key", "source_defect_weight", "label", "来源细分重量", "values", List.of(1.25))));
return chart;
}
private static Map<String, Object> waterfallChart() {
Set<String> fields = Set.of("black_good_weight", "black_total_weight", "bright_good_weight", "bright_total_weight");
Map<String, Object> chart = chart("composite_impact_pp", "pp", null, List.copyOf(fields),
List.of(-0.3, -0.14, -0.44), "waterfall-v1");
chart.put("type", "waterfall");
chart.put("sectionKey", "overview");
chart.put("categories", List.of(Map.of("key", "black", "label", "黑片数学影响"),
Map.of("key", "bright", "label", "亮片数学影响"), Map.of("key", "total", "label", "合计")));
chart.put("series", List.of(Map.of("key", "composite_impact_pp", "label", "数学影响(pp)",
"values", List.of(-0.3, -0.14, -0.44))));
chart.put("comparisonPeriod", Map.of("year", 2026, "month", 8, "label", "2026-08", "granularity", "month"));
chart.put("coverage", Map.of("totalRows", 3, "returnedRows", 3, "truncated", false));
return chart;
}
}