优化:按分析章节整合图表解读与完整报告导出
This commit is contained in:
+100
-6
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user