优化:未分类连续维护与柏拉图预汇总,同步已发布修复
This commit is contained in:
@@ -763,7 +763,7 @@ public class AnalysisChangesService {
|
||||
}
|
||||
|
||||
private boolean validRate(BigDecimal value) {
|
||||
return value != null && value.compareTo(BigDecimal.ZERO) >= 0 && value.compareTo(BigDecimal.ONE) <= 0;
|
||||
return value != null && value.signum() >= 0;
|
||||
}
|
||||
|
||||
private boolean positive(BigDecimal value) { return value != null && value.signum() > 0; }
|
||||
|
||||
+73
-13
@@ -9,7 +9,7 @@ import cn.yunsheng.quality.governance.GovernanceAccessService;
|
||||
import cn.yunsheng.quality.identity.AppUserPrincipal;
|
||||
import cn.yunsheng.quality.release.ReleaseService;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -18,8 +18,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.jdbc.core.ResultSetExtractor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Isolation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -63,6 +64,7 @@ public class AnalysisFineDefectsService {
|
||||
private final GovernanceAccessService governance;
|
||||
private final JsonMapper json;
|
||||
private SourceColumnQueries sourceColumns;
|
||||
private FineDefectRollups rollups;
|
||||
|
||||
public AnalysisFineDefectsService(JdbcTemplate jdbc, ReleaseService releases,
|
||||
GovernanceAccessService governance, JsonMapper json) {
|
||||
@@ -75,6 +77,9 @@ public class AnalysisFineDefectsService {
|
||||
@org.springframework.beans.factory.annotation.Autowired(required = false)
|
||||
public void setSourceColumns(SourceColumnQueries sourceColumns) { this.sourceColumns = sourceColumns; }
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired(required = false)
|
||||
public void setRollups(FineDefectRollups rollups) { this.rollups = rollups; }
|
||||
|
||||
@Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ)
|
||||
public Map<String, Object> query(Map<String, Object> input, AppUserPrincipal actor) {
|
||||
Set<String> allowed = governance.allowedFields(actor);
|
||||
@@ -117,6 +122,11 @@ public class AnalysisFineDefectsService {
|
||||
&& isAllowed(category.componentCode(), allowed)) componentCodes.add(category.componentCode());
|
||||
}
|
||||
Aggregator aggregate = new Aggregator(request, visibleFields, categories, componentCodes, json);
|
||||
if (rollups != null && request.dimension().equals("defect") && request.extraFilters().isEmpty()
|
||||
&& (!(input.get("columnFilters") instanceof List<?> filters) || filters.isEmpty())
|
||||
&& rollups.read(aggregate, request, allowed, predicate.toString(), args)) {
|
||||
return withScope(aggregate.response(request, release), scopeContext);
|
||||
}
|
||||
String sql = """
|
||||
SELECT %s AS raw_fields, %s AS formal_components, f.group_name AS business_group%s
|
||||
FROM release_member rm
|
||||
@@ -127,14 +137,19 @@ public class AnalysisFineDefectsService {
|
||||
""".formatted(jsonObject("f.raw_values", java.util.stream.Stream.concat(visibleFields.stream().map(DefectField::key),categories.stream().map(c->ExpectedSourceDetailPolicy.totalField(c.componentCode())).filter(java.util.Objects::nonNull).filter(k->isAllowed(k,allowed))).distinct().toList()),
|
||||
jsonObject("f.components", new ArrayList<>(componentCodes)),
|
||||
request.dimension().equals("product") ? ", f.product AS product_key" : "", factJoin, predicate);
|
||||
jdbc.query(sql, (RowCallbackHandler) rs -> {
|
||||
try {
|
||||
aggregate.add(readObject(rs.getString("raw_fields")), readObject(rs.getString("formal_components")),
|
||||
request.dimension().equals("product") ? rs.getString("product_key") : null,rs.getString("business_group"));
|
||||
} catch (SQLException error) {
|
||||
throw new IllegalStateException(error);
|
||||
jdbc.query(connection -> {
|
||||
var statement=connection.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
|
||||
// Connector/J's negative fetch size enables row streaming instead of buffering the full year.
|
||||
statement.setFetchSize(Integer.MIN_VALUE);
|
||||
new ArgumentPreparedStatementSetter(args.toArray()).setValues(statement);
|
||||
return statement;
|
||||
}, (ResultSetExtractor<Void>) resultSet -> {
|
||||
while(resultSet.next()) {
|
||||
aggregate.add(readObject(resultSet.getString("raw_fields")), readObject(resultSet.getString("formal_components")),
|
||||
request.dimension().equals("product") ? resultSet.getString("product_key") : null,resultSet.getString("business_group"));
|
||||
}
|
||||
}, args.toArray());
|
||||
return null;
|
||||
});
|
||||
return withScope(aggregate.response(request, release), scopeContext);
|
||||
}
|
||||
|
||||
@@ -311,14 +326,18 @@ public class AnalysisFineDefectsService {
|
||||
}
|
||||
}
|
||||
|
||||
private static String jsonObject(String column, List<String> keys) {
|
||||
static String jsonObject(String column, List<String> keys) {
|
||||
if (keys.isEmpty()) return "JSON_OBJECT()";
|
||||
List<String> pairs = new ArrayList<>();
|
||||
for (String key : keys) pairs.add("'" + key.replace("'", "''") + "', JSON_EXTRACT(" + column + ", '$.\"" + key + "\"')");
|
||||
return "JSON_OBJECT(" + String.join(",", pairs) + ")";
|
||||
}
|
||||
|
||||
private Map<String, Object> readObject(String value) {
|
||||
Map<String, Object> readObject(String value) {
|
||||
return readObject(json, value);
|
||||
}
|
||||
|
||||
static Map<String, Object> readObject(JsonMapper json, String value) {
|
||||
if (value == null || value.isBlank()) return Map.of();
|
||||
JsonNode node = json.readTree(value);
|
||||
if (node == null || !node.isObject()) return Map.of();
|
||||
@@ -416,18 +435,31 @@ public class AnalysisFineDefectsService {
|
||||
private long sourceRows;
|
||||
private final Map<String,Long> expectedRows=new LinkedHashMap<>();
|
||||
private final Map<String,BigDecimal> expectedWeights=new LinkedHashMap<>();
|
||||
private final Map<String,List<String>> categoryKeys = new LinkedHashMap<>();
|
||||
|
||||
Aggregator(Request request, List<DefectField> fields, List<Category> categories, Set<String> componentCodes, JsonMapper json) {
|
||||
this.request = request; this.fields = fields; this.categories = categories; this.componentCodes = componentCodes; this.json = json;
|
||||
fields.forEach(field -> fieldStats.put(field.key(), new FieldStats()));
|
||||
componentCodes.forEach(code -> componentStats.put(code, new FieldStats()));
|
||||
for (Category category : categories) {
|
||||
List<String> selected = fields.stream().filter(f -> f.category().equals(category.name())).map(DefectField::key).toList();
|
||||
if (selected.size() == fields(request.stage()).stream().filter(f -> f.category().equals(category.name())).count())
|
||||
categoryKeys.put(category.name(), selected);
|
||||
}
|
||||
}
|
||||
|
||||
void add(Map<String, Object> raw, Map<String, Object> components, String product) { add(raw,components,product,null); }
|
||||
void add(Map<String, Object> raw, Map<String, Object> components, String product,String group) {
|
||||
sourceRows++;
|
||||
for(Category category:categories){List<String> selected=fields.stream().filter(f->f.category().equals(category.name())).map(DefectField::key).toList();long full=fields(request.stage()).stream().filter(f->f.category().equals(category.name())).count();String total=ExpectedSourceDetailPolicy.totalField(category.componentCode());
|
||||
if(total!=null&&selected.size()==full&&ExpectedSourceDetailPolicy.applies(request.stage(),group,selected,raw,components.get(category.componentCode()),raw.get(total))){expectedRows.merge(category.name(),1L,Long::sum);expectedWeights.merge(category.name(),AnalysisInvestigationService.decimal(components.get(category.componentCode())),BigDecimal::add);}}
|
||||
for (Category category : categories) {
|
||||
String total = ExpectedSourceDetailPolicy.totalField(category.componentCode());
|
||||
List<String> selected = categoryKeys.get(category.name());
|
||||
if (total != null && selected != null && ExpectedSourceDetailPolicy.applies(request.stage(), group, selected, raw,
|
||||
components.get(category.componentCode()), raw.get(total))) {
|
||||
expectedRows.merge(category.name(), 1L, Long::sum);
|
||||
expectedWeights.merge(category.name(), AnalysisInvestigationService.decimal(components.get(category.componentCode())), BigDecimal::add);
|
||||
}
|
||||
}
|
||||
|
||||
for (DefectField field : fields) {
|
||||
FieldStats stats = fieldStats.get(field.key());
|
||||
@@ -442,6 +474,34 @@ public class AnalysisFineDefectsService {
|
||||
for (String code : componentCodes) componentStats.get(code).add(components.get(code));
|
||||
}
|
||||
|
||||
record RollupStat(String kind, String key, BigDecimal sum, long present, long missing, long invalid, long negative, long rows) {}
|
||||
|
||||
List<RollupStat> rollupStats() {
|
||||
List<RollupStat> result = new ArrayList<>();
|
||||
fieldStats.forEach((key, s) -> result.add(new RollupStat("source", key, s.sum, s.presentRows, s.missingRows, s.invalidRows, s.negativeRows, sourceRows)));
|
||||
componentStats.forEach((key, s) -> result.add(new RollupStat("formal", key, s.sum, s.presentRows, s.missingRows, s.invalidRows, s.negativeRows, sourceRows)));
|
||||
for (Category category : categories) if (ExpectedSourceDetailPolicy.totalField(category.componentCode()) != null)
|
||||
result.add(new RollupStat("expected", category.name(), expectedWeights.getOrDefault(category.name(), BigDecimal.ZERO),
|
||||
expectedRows.getOrDefault(category.name(), 0L), 0, 0, 0, sourceRows));
|
||||
return result;
|
||||
}
|
||||
|
||||
void mergeRollup(RollupStat stat, Set<String> allowed) {
|
||||
sourceRows = Math.max(sourceRows, stat.rows());
|
||||
FieldStats stats = stat.kind().equals("source") ? fieldStats.get(stat.key()) : stat.kind().equals("formal") ? componentStats.get(stat.key()) : null;
|
||||
if (stats != null) {
|
||||
stats.sum = stats.sum.add(stat.sum()); stats.presentRows += stat.present(); stats.missingRows += stat.missing();
|
||||
stats.invalidRows += stat.invalid(); stats.negativeRows += stat.negative();
|
||||
}
|
||||
if (stat.kind().equals("expected")) {
|
||||
Category category = categories.stream().filter(c -> c.name().equals(stat.key())).findFirst().orElseThrow();
|
||||
String total = ExpectedSourceDetailPolicy.totalField(category.componentCode());
|
||||
if (categoryKeys.containsKey(category.name()) && componentCodes.contains(category.componentCode()) && isAllowed(total, allowed)) {
|
||||
expectedRows.merge(category.name(), stat.present(), Long::sum); expectedWeights.merge(category.name(), stat.sum(), BigDecimal::add);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Object> response(Request request, Map<String, Object> release) {
|
||||
List<Map<String, Object>> rows = new ArrayList<>(request.dimension().equals("defect") ? defectRows() : productRows());
|
||||
rows.sort(Comparator.<Map<String, Object>, BigDecimal>comparing(row -> (BigDecimal) row.get("weight"),
|
||||
|
||||
@@ -76,8 +76,11 @@ final class AnalysisProtocolChanges {
|
||||
List<String> stages=selected instanceof String name?List.of(name):List.of("black","bright");int sourceStages=0;
|
||||
for(String stage:stages) {
|
||||
Map<String,Object> block=map(values.get(stage)),support=map(values.get("rateSupport"));
|
||||
boolean sourced=selected!=null||Boolean.TRUE.equals(support.get(stage+"Measured"))
|
||||
||List.of("rate","qualifiedWeight","scrapWeight","totalWeight").stream().anyMatch(field->decimal(block.get(field))!=null);
|
||||
String measuredKey=stage+"Measured";
|
||||
boolean hasMeasuredCoverage=support.containsKey(measuredKey);
|
||||
boolean sourced=hasMeasuredCoverage?Boolean.TRUE.equals(support.get(measuredKey))
|
||||
:selected!=null||List.of("rate","qualifiedWeight","scrapWeight","totalWeight").stream()
|
||||
.anyMatch(field->decimal(block.get(field))!=null);
|
||||
if(!sourced)continue;sourceStages++;
|
||||
for(String field:List.of("totalWeight","scrapWeight"))if(fields.contains(field)) {
|
||||
BigDecimal value=decimal(block.get(field));if(value==null||value.signum()<0)gaps.add("变化行"+side+"的"+stage+field+"缺失、不可见或非法");
|
||||
@@ -110,7 +113,7 @@ final class AnalysisProtocolChanges {
|
||||
}
|
||||
private static boolean sortFields(List<?> fields,String sort){return Set.of("rate_drop","current_rate").contains(sort)?fields.contains("rate")
|
||||
:Set.of("scrap_delta","current_scrap").contains(sort)&&fields.contains("scrapWeight");}
|
||||
private static boolean validRate(Object value){BigDecimal rate=decimal(value);return rate!=null&&rate.signum()>=0&&rate.compareTo(BigDecimal.ONE)<=0;}
|
||||
private static boolean validRate(Object value){BigDecimal rate=decimal(value);return rate!=null&&rate.signum()>=0;}
|
||||
static BigDecimal decimal(Object value){if(!(value instanceof Number))return null;try{return new BigDecimal(value.toString());}catch(NumberFormatException invalid){return null;}}
|
||||
private static boolean same(Object left,Object right){return Objects.equals(AnalysisProtocolService.canonical(left),AnalysisProtocolService.canonical(right));}
|
||||
static boolean sameFilters(Map<String,Object> left,Map<String,Object> right){return FILTERS.containsAll(left.keySet())&&FILTERS.containsAll(right.keySet())
|
||||
|
||||
+20
-4
@@ -18,8 +18,11 @@ final class AnalysisProtocolExploration {
|
||||
var rootStages=AnalysisProtocolService.list(root.get("stages"));var stages=AnalysisProtocolService.list(node.get("stages"));
|
||||
boolean bothStages=new HashSet<>(rootStages).equals(Set.of("black","bright"));
|
||||
boolean combinedRate=stages.isEmpty()&&bothStages&&Set.of("metrics","trend").contains(purpose)&&AnalysisProtocolService.list(node.get("fields")).equals(List.of("rate"));
|
||||
boolean combinedRanking=stages.isEmpty()&&bothStages&&"ranking".equals(purpose)
|
||||
&&AnalysisProtocolService.list(node.get("fields")).contains("scrapWeight")
|
||||
&&Set.of("rate","scrapWeight").containsAll(AnalysisProtocolService.list(node.get("fields")));
|
||||
boolean combinedChanges=stages.isEmpty()&&bothStages&&"changes".equals(purpose);
|
||||
if(!rootStages.isEmpty()&&(!rootStages.containsAll(stages)||stages.isEmpty()&&!combinedRate&&!combinedChanges))throw new IllegalArgumentException("自主步骤不能扩大根任务阶段");
|
||||
if(!rootStages.isEmpty()&&(!rootStages.containsAll(stages)||stages.isEmpty()&&!combinedRate&&!combinedRanking&&!combinedChanges))throw new IllegalArgumentException("自主步骤不能扩大根任务阶段");
|
||||
// A combined rate read returns both stage baselines. Keep their coverage obligations explicit;
|
||||
// the actual metrics request has no stage parameter. Combined changes retain stage=null.
|
||||
var next=new LinkedHashMap<>(node);if(combinedRate)next.put("stages",List.of("black","bright"));
|
||||
@@ -55,8 +58,11 @@ final class AnalysisProtocolExploration {
|
||||
var base=scopes.stream().filter(s->sameFilters(root,AnalysisProtocolService.map(s.get("task")))).toList();
|
||||
var stages=AnalysisProtocolService.list(root.get("stages"));List<?> requiredStages=stages.isEmpty()?List.of("overall"):stages;
|
||||
for(Object field:AnalysisProtocolService.list(root.get("fields")))for(Object stage:requiredStages) {
|
||||
var candidates=base.stream().filter(s->Set.of("metrics","trend").contains(s.get("purpose")))
|
||||
.filter(s->{var child=AnalysisProtocolService.map(s.get("task"));var own=AnalysisProtocolService.list(child.get("stages"));return AnalysisProtocolService.list(child.get("fields")).contains(field)&&("overall".equals(stage)?own.isEmpty():own.contains(stage));}).toList();
|
||||
var candidates=base.stream().filter(s->Set.of("metrics","trend").contains(s.get("purpose"))||combinedRanking(root,s))
|
||||
.filter(s->{var child=AnalysisProtocolService.map(s.get("task"));var own=AnalysisProtocolService.list(child.get("stages"));
|
||||
boolean combined=combinedRanking(root,s);
|
||||
return AnalysisProtocolService.list(child.get("fields")).contains(field)
|
||||
&&("overall".equals(stage)?own.isEmpty():own.contains(stage)||combined);}).toList();
|
||||
if(candidates.isEmpty())gaps.add("根任务指标阶段尚未核实:"+field+"/"+stage);
|
||||
if((Boolean.TRUE.equals(root.get("comparisonRequired"))||root.get("comparisonPeriod") instanceof Map<?,?>)&&candidates.stream().noneMatch(s->Boolean.TRUE.equals(AnalysisProtocolService.map(s.get("task")).get("comparisonRequired"))))gaps.add("根任务期间比较尚未核实:"+field+"/"+stage);
|
||||
if(Boolean.TRUE.equals(root.get("trendRequired"))&&candidates.stream().noneMatch(s->"trend".equals(s.get("purpose"))))gaps.add("根任务趋势尚未核实:"+field+"/"+stage);
|
||||
@@ -65,10 +71,20 @@ final class AnalysisProtocolExploration {
|
||||
var matching=scopes.stream().filter(s->AnalysisProtocolService.list(AnalysisProtocolService.map(s.get("task")).get("dimensions")).contains(dimension)).toList();
|
||||
var explicit=AnalysisProtocolService.list(AnalysisProtocolService.map(root.get("dimensionStages")).get(dimension));
|
||||
if(explicit.isEmpty()){if(matching.isEmpty())gaps.add("根任务分组目标尚未核实:"+dimension);}
|
||||
else for(Object stage:explicit)if(matching.stream().noneMatch(scope->AnalysisProtocolService.list(AnalysisProtocolService.map(scope.get("task")).get("stages")).contains(stage)))gaps.add("根任务分组阶段目标尚未核实:"+dimension+"/"+stage);
|
||||
else for(Object stage:explicit)if(matching.stream().noneMatch(scope->AnalysisProtocolService.list(AnalysisProtocolService.map(scope.get("task")).get("stages")).contains(stage)||combinedRanking(root,scope)))gaps.add("根任务分组阶段目标尚未核实:"+dimension+"/"+stage);
|
||||
}
|
||||
for(String purpose:List.of("source","records"))if(Boolean.TRUE.equals(root.get(purpose.equals("source")?"verifySource":"fullDetails"))&&base.stream().noneMatch(s->purpose.equals(s.get("purpose"))))gaps.add("根任务原件目标尚未核实:"+purpose);
|
||||
return gaps;
|
||||
}
|
||||
private static boolean combinedRanking(Map<String,Object> root,Map<String,Object> scope) {
|
||||
if(!"ranking".equals(scope.get("purpose")))return false;
|
||||
var child=AnalysisProtocolService.map(scope.get("task"));var own=AnalysisProtocolService.list(child.get("stages"));
|
||||
var rootStages=AnalysisProtocolService.list(root.get("stages"));var fields=AnalysisProtocolService.list(child.get("fields"));
|
||||
boolean compatibleRoot=rootStages.isEmpty()||new HashSet<>(rootStages).equals(Set.of("black","bright"));
|
||||
var rootDimensions=AnalysisProtocolService.list(root.get("dimensions"));var childDimensions=AnalysisProtocolService.list(child.get("dimensions"));
|
||||
boolean compatibleDimension=rootDimensions.isEmpty()||childDimensions.stream().anyMatch(rootDimensions::contains);
|
||||
return compatibleRoot&&compatibleDimension&&own.isEmpty()&&fields.contains("scrapWeight")
|
||||
&&Set.of("rate","scrapWeight").containsAll(fields);
|
||||
}
|
||||
private static boolean sameFilters(Map<String,Object> root,Map<String,Object> child){return AnalysisProtocolService.canonical(AnalysisProtocolQueryPlan.identityFilters(AnalysisProtocolService.map(root.get("filters")))).equals(AnalysisProtocolService.canonical(AnalysisProtocolQueryPlan.identityFilters(AnalysisProtocolService.map(child.get("filters")))));}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** Server-owned import of frozen, same-session business responses into a distinct report execution. */
|
||||
final class AnalysisProtocolHistory {
|
||||
private static final String VERIFIED_RESULTS_ONLY = "verified_results_only";
|
||||
@FunctionalInterface interface QueryValidator {
|
||||
Map<String,Object> validate(Map<String,Object> source,Map<String,Object> query,Map<String,Object> response,AppUserPrincipal actor);
|
||||
}
|
||||
@@ -33,9 +34,11 @@ final class AnalysisProtocolHistory {
|
||||
throw bad("历史报告只整理已核验的原范围;重新查询或改变范围需要新的分析任务");
|
||||
boolean first="first".equals(proposed.get("historyMode"));
|
||||
List<String> ids=jdbc.query("SELECT id,plan_json,status,answer_json FROM analysis_turn WHERE session_id=? AND user_id=? AND id<>? ORDER BY created_at,id",
|
||||
(rs,n)->Map.of("id",rs.getString(1),"plan",read(rs.getString(2)),"status",Objects.toString(rs.getString(3),""),"answer",first?map(read(rs.getString(4))):Map.of()),a.sessionId(),a.userId(),a.turnId()).stream()
|
||||
(rs,n)->Map.of("id",rs.getString(1),"plan",read(rs.getString(2)),"status",Objects.toString(rs.getString(3),""),"answer",map(read(rs.getString(4)))),a.sessionId(),a.userId(),a.turnId()).stream()
|
||||
.filter(row->factTurn(map(row.get("plan"))))
|
||||
.filter(row->!first||Set.of("COMPLETED","PARTIAL").contains(row.get("status"))&&!list(map(row.get("answer")).get("protocolQueryIds")).isEmpty())
|
||||
.filter(row->{String status=Objects.toString(row.get("status"),"");List<?> accepted=list(map(row.get("answer")).get("protocolQueryIds"));
|
||||
return first?Set.of("COMPLETED","PARTIAL").contains(status)&&!accepted.isEmpty()
|
||||
:"COMPLETED".equals(status)||"PARTIAL".equals(status)&&!accepted.isEmpty();})
|
||||
.map(row->Objects.toString(row.get("id"))).toList();
|
||||
if(first&&!ids.isEmpty())ids=ids.subList(0,1);
|
||||
if("last".equals(proposed.get("historyMode"))&&!ids.isEmpty())ids=ids.subList(ids.size()-1,ids.size());
|
||||
@@ -49,6 +52,9 @@ final class AnalysisProtocolHistory {
|
||||
for(String key:List.of("comparisonRequired","trendRequired","verifySource","fullDetails"))result.put(key,false);
|
||||
result.put("historySourceTurnIds",List.copyOf(ids));
|
||||
result.put("historySelectionHash",hash(selection(a,ids)));
|
||||
// This mode is issued only by the server after it has resolved the canonical
|
||||
// same-session source set. A client-supplied value is always overwritten.
|
||||
result.put("historyResultMode",VERIFIED_RESULTS_ONLY);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -89,26 +95,150 @@ final class AnalysisProtocolHistory {
|
||||
String title=Objects.toString(details.get("reportTitle"),"");
|
||||
boolean safeWorkerTitle=autonomous&&"verified_history_summary".equals(details.get("titleSource"))&&"历史质量分析汇总报告".equals(title)&&validTitle(title,payload,task);
|
||||
boolean titled=(Boolean.TRUE.equals(details.get("titleGeneratedByModel"))||safeWorkerTitle)&&validTitle(title,payload,task);
|
||||
List<String> gaps=new ArrayList<>(strings(map(payload.get("historyCoverage")).get("missingEvidence")));
|
||||
if(!titled)gaps.add("报告标题尚未完成有效概括,保留已核实数据");
|
||||
if(!facts.isEmpty()&&selected.isEmpty())gaps.add("报告重点尚未完成选择,保留完整数据明细");
|
||||
var sourceCoverage=map(payload.get("historyCoverage"));boolean sourceIncomplete=!Boolean.TRUE.equals(sourceCoverage.get("complete"));
|
||||
boolean selectionMissing=!facts.isEmpty()&&selected.isEmpty();
|
||||
var bodyCompletion=map(details.get("evidenceCompletion"));
|
||||
Object bodySufficient=bodyCompletion.containsKey("bodyEvidenceSufficient")
|
||||
?bodyCompletion.get("bodyEvidenceSufficient"):bodyCompletion.get("modelDeclaredEvidenceSufficient");
|
||||
boolean explanationIncomplete=autonomous&&Boolean.FALSE.equals(bodySufficient);
|
||||
boolean serverFallback=autonomous&&(selectionMissing||explanationIncomplete);
|
||||
boolean bodyVerified=!serverFallback;
|
||||
var gaps=new ArrayList<String>();if(sourceIncomplete)gaps.add("部分已选历史来源未能完整核验。");
|
||||
if(!titled)gaps.add("报告标题尚未生成或未通过内容核验。");
|
||||
if(selectionMissing)gaps.add("报告重点未通过模型选择,已使用已核验事实生成简短提示。");
|
||||
if(explanationIncomplete)gaps.add("正文解释未完成事实核验,已按已确认数据生成简短报告。");
|
||||
var displayIds=new ArrayList<String>();for(Object id:selected)displayIds.add(id.toString());
|
||||
if(autonomous&&selectionMissing)displayIds.addAll(fallbackHeadlineIds(facts));
|
||||
List<Map<String,Object>> projected=list(payload.get("sourceResults")).stream().map(AnalysisProtocolHistory::map).toList();
|
||||
var rendered=new LinkedHashMap<>(AnalysisProtocolHistoryFacts.render(titled?title:"历史质量分析汇总",projected,strings(selected),gaps));
|
||||
if(!facts.isEmpty()&&selected.isEmpty())rendered.put("answer","已核验的历史数据已保留,重点结论尚未整理完成,可查看报告中的完整表格。");
|
||||
var rendered=new LinkedHashMap<>(AnalysisProtocolHistoryFacts.render(titled?title:"历史质量分析汇总",projected,displayIds,gaps));
|
||||
if(!autonomous&&selectionMissing)
|
||||
rendered.put("answer","已核验的历史数据已保留;重点结论尚未整理完成,请查看本报告表格。");
|
||||
var reportVisuals=new ArrayList<Map<String,Object>>();
|
||||
for(Object raw:list(payload.get("visuals"))) {var chart=new LinkedHashMap<>(map(raw));chart.put("originTurnId",a.turnId());reportVisuals.add(chart);}
|
||||
rendered.put("releaseId",a.releaseId());rendered.put("visualSchemaVersion",payload.get("visualSchemaVersion"));
|
||||
rendered.put("visualState",payload.get("visualState"));rendered.put("visuals",List.copyOf(reportVisuals));
|
||||
rendered.put("historyVisualSources",payload.get("historyVisualSources"));
|
||||
if(autonomous) {
|
||||
var knownIds=new LinkedHashSet<String>();for(var fact:facts)if(fact.get("id") instanceof String id)knownIds.add(id);
|
||||
var presented=AnalysisProtocolHistorySummary.normalize(details,knownIds,Objects.toString(rendered.get("answer"),""));
|
||||
gaps.addAll(presented.gaps());rendered.put("answer",presented.answer());rendered.put("readingSummary",presented.summary());rendered.put("queryEvidenceRefs",presented.refs());
|
||||
rendered.put("titleSource",safeWorkerTitle?"verified_history_summary":"model");rendered.put("titleGeneratedByModel",Boolean.TRUE.equals(details.get("titleGeneratedByModel")));
|
||||
StringBuilder anchors=new StringBuilder("<details class=\"history-facts\"><summary>引用依据</summary><ul>");
|
||||
for(var fact:facts) {String id=Objects.toString(fact.get("id"),"");anchors.append("<li id=\"history-fact:").append(escape(id)).append("\" data-history-fact-id=\"").append(escape(id)).append("\">").append(escape(Objects.toString(fact.get("text"),""))).append("</li>");}
|
||||
anchors.append("</ul></details>");String report=Objects.toString(rendered.get("reportHtml"),"");int end=report.lastIndexOf("</article>");
|
||||
if(end>=0)rendered.put("reportHtml",report.substring(0,end)+anchors+report.substring(end));
|
||||
var finalGaps=new ArrayList<String>();
|
||||
if(sourceIncomplete)finalGaps.add("部分已选历史来源未能完整核验");
|
||||
if(!titled)finalGaps.add("报告标题尚未生成或未通过内容核验");
|
||||
if(selectionMissing)finalGaps.add("重点结论未通过模型选择,已由已核验事实补充");
|
||||
if(explanationIncomplete)finalGaps.add("正文解释未完成事实核验,已按已确认数据生成简短报告");
|
||||
AnalysisProtocolHistorySummary.Result presented;
|
||||
Map<String,Object> finalRender;
|
||||
if(serverFallback) {
|
||||
finalRender=AnalysisProtocolHistoryFacts.render(titled?title:"历史质量分析汇总",projected,displayIds,gaps);
|
||||
String safeAnswer=Objects.toString(finalRender.get("reportMarkdown"),"");
|
||||
String safeLead=Objects.toString(finalRender.get("answer"),"已确认历史事实已按原范围整理。");
|
||||
var safeSummary=new LinkedHashMap<String,Object>();safeSummary.put("version",1);safeSummary.put("lead",safeLead);
|
||||
safeSummary.put("evidence",List.of());safeSummary.put("notices",List.copyOf(finalGaps));
|
||||
// Facts are anchored by the server-side render. Keep source fact IDs out of
|
||||
// user-visible text and summary metadata; saved chart query refs are added below.
|
||||
safeSummary.put("evidenceRefs",List.of());
|
||||
presented=new AnalysisProtocolHistorySummary.Result(safeAnswer,safeSummary,List.of(),List.of());
|
||||
} else {
|
||||
var presentedModel=AnalysisProtocolHistorySummary.normalize(details,knownIds,anchoredFallback(facts,displayIds));
|
||||
if(!presentedModel.gaps().isEmpty()){bodyVerified=false;finalGaps.add("部分解释内容未通过事实或格式核验,已过滤");}
|
||||
finalRender=AnalysisProtocolHistoryFacts.render(titled?title:"历史质量分析汇总",projected,displayIds,finalGaps);
|
||||
var safeSummary=new LinkedHashMap<String,Object>(presentedModel.summary());
|
||||
var notices=new LinkedHashSet<String>(strings(safeSummary.get("notices")));notices.addAll(finalGaps);
|
||||
safeSummary.put("notices",List.copyOf(notices));
|
||||
presented=new AnalysisProtocolHistorySummary.Result(presentedModel.answer(),safeSummary,
|
||||
presentedModel.refs(),presentedModel.gaps());
|
||||
}
|
||||
if(!finalGaps.isEmpty())gaps= new ArrayList<>(List.of(String.join(";",finalGaps)+",报告保持部分完成。"));
|
||||
else gaps.clear();
|
||||
rendered.put("answer",presented.answer());rendered.put("reportMarkdown",finalRender.get("reportMarkdown"));
|
||||
rendered.put("reportHtml",finalRender.get("reportHtml"));rendered.put("historySections",finalRender.get("historySections"));
|
||||
var summary=historyReadingSummary(presented.summary(),presented.refs(),reportVisuals,facts);
|
||||
appendSourceDeliveryWarnings(summary,strings(payload.get("sourceDeliveryWarnings")));
|
||||
rendered.put("readingSummary",summary);
|
||||
rendered.put("queryEvidenceRefs",historyEvidenceRefs(presented.refs(),reportVisuals));
|
||||
rendered.put("titleSource",titled?(safeWorkerTitle?"verified_history_summary":"model_verified_content"):"safe_partial_fallback");
|
||||
rendered.put("titleGeneratedByModel",titled&&Boolean.TRUE.equals(details.get("titleGeneratedByModel")));
|
||||
} else {
|
||||
var summary=historyReadingSummary(Map.of(),List.of(),reportVisuals,facts);
|
||||
appendSourceDeliveryWarnings(summary,strings(payload.get("sourceDeliveryWarnings")));
|
||||
rendered.put("readingSummary",summary);
|
||||
rendered.put("queryEvidenceRefs",historyEvidenceRefs(List.of(),reportVisuals));
|
||||
}
|
||||
rendered.put("historySources",payload.get("historySources"));rendered.put("historyCoverage",Map.of("complete",gaps.isEmpty()&&Boolean.TRUE.equals(map(payload.get("historyCoverage")).get("complete")),
|
||||
"expectedSources",map(payload.get("historyCoverage")).get("expectedSources"),"importedSources",map(payload.get("historyCoverage")).get("importedSources"),"missingEvidence",gaps.stream().distinct().toList()));
|
||||
rendered.put("historySources",payload.get("historySources"));var finalCoverage=new LinkedHashMap<String,Object>();
|
||||
finalCoverage.put("complete",gaps.isEmpty()&&Boolean.TRUE.equals(sourceCoverage.get("complete")));
|
||||
finalCoverage.put("expectedSources",sourceCoverage.get("expectedSources"));finalCoverage.put("importedSources",sourceCoverage.get("importedSources"));
|
||||
finalCoverage.put("missingEvidence",gaps.stream().distinct().toList());
|
||||
finalCoverage.put("sourceDeliveryWarnings",strings(payload.get("sourceDeliveryWarnings")));
|
||||
rendered.put("historyCoverage",finalCoverage);
|
||||
rendered.put("historyEvidence",identity(a,stored));rendered.put("completionMethod","confirmed_history_report");
|
||||
rendered.put("reportTitle",titled?title:"历史质量分析汇总");return rendered;
|
||||
rendered.put("reportTitle",titled?title:"历史质量分析汇总");
|
||||
boolean titleRequired=!details.containsKey("titleRequired")||Boolean.TRUE.equals(details.get("titleRequired"));
|
||||
boolean modelTitleValid=titleRequired&&titled&&Boolean.TRUE.equals(details.get("titleValid"))
|
||||
&&Boolean.TRUE.equals(details.get("titleGeneratedByModel"));
|
||||
rendered.put("titleRequired",titleRequired);rendered.put("titleValid",modelTitleValid);
|
||||
if(autonomous) {
|
||||
var completion=new LinkedHashMap<String,Object>(bodyCompletion);
|
||||
completion.put("serverCoverageSufficient",Boolean.TRUE.equals(sourceCoverage.get("complete")));
|
||||
completion.put("modelDeclaredEvidenceSufficient",bodyVerified);completion.put("bodyEvidenceSufficient",bodyVerified);
|
||||
completion.put("bodyMissingEvidence",bodyVerified?List.of():strings(bodyCompletion.get("bodyMissingEvidence")).isEmpty()
|
||||
?List.of(explanationIncomplete?"正文解释未完成事实核验。":"正文摘要未通过引用或格式核验。")
|
||||
:strings(bodyCompletion.get("bodyMissingEvidence")));
|
||||
completion.put("titleRequired",titleRequired);completion.put("titleValid",modelTitleValid);
|
||||
completion.put("titleMissingEvidence",modelTitleValid?List.of():List.of("报告标题尚未生成或未通过内容核验。"));
|
||||
rendered.put("evidenceCompletion",completion);
|
||||
}
|
||||
return rendered;
|
||||
}
|
||||
|
||||
private List<String> fallbackHeadlineIds(List<Map<String,Object>> facts) {
|
||||
Set<String> seenScopes=new HashSet<>();List<String> selected=new ArrayList<>();
|
||||
Set<String> metrics=Set.of("rate","compositeRate","scrapRate","totalWeight","scrapWeight");
|
||||
for(var fact:facts) {
|
||||
if(!metrics.contains(Objects.toString(fact.get("metricCode"),""))||!(fact.get("id") instanceof String id))continue;
|
||||
var source=map(fact.get("scope"));var scope=new LinkedHashMap<String,Object>();
|
||||
for(String key:List.of("period","filters","dimension","stage"))if(source.containsKey(key))scope.put(key,source.get(key));
|
||||
String key=write(canonical(scope));if(!seenScopes.add(key))continue;
|
||||
selected.add(id);if(selected.size()==5)break;
|
||||
}
|
||||
return List.copyOf(selected);
|
||||
}
|
||||
|
||||
private String anchoredFallback(List<Map<String,Object>> facts,List<String> ids) {
|
||||
String sentence="已核验重点表格按各自期间与筛选保留,未合并不同范围。";
|
||||
var anchors=new ArrayList<String>();
|
||||
for(String id:ids)if(facts.stream().anyMatch(fact->Objects.equals(id,fact.get("id"))))anchors.add("["+id+"]");
|
||||
return sentence+String.join("",anchors);
|
||||
}
|
||||
|
||||
private Map<String,Object> historyReadingSummary(Map<String,Object> original,List<String> selectedRefs,
|
||||
List<Map<String,Object>> visuals,List<Map<String,Object>> facts) {
|
||||
var summary=new LinkedHashMap<String,Object>(original);var selected=new ArrayList<Map<String,Object>>();
|
||||
Set<String> selectedIds=new HashSet<>(selectedRefs);
|
||||
for(var fact:facts)if(selectedIds.contains(Objects.toString(fact.get("id"))))selected.add(fact);
|
||||
if(!summary.containsKey("lead")) {
|
||||
String lead=selected.isEmpty()?"已核验的历史质量结果已整理完成。":Objects.toString(selected.getFirst().getOrDefault("displayText",selected.getFirst().get("text")),"");
|
||||
summary.put("version",1);summary.put("lead",lead);
|
||||
summary.put("evidence",selected.stream().skip(1).map(f->Objects.toString(f.getOrDefault("displayText",f.get("text")),"")).filter(s->!s.isBlank()).limit(4).toList());
|
||||
summary.put("notices",List.of());
|
||||
}
|
||||
Set<String> refs=new LinkedHashSet<>(strings(summary.get("evidenceRefs")));refs.addAll(selectedRefs);
|
||||
for(var chart:visuals)for(Object ref:list(chart.get("evidenceRefs")))if(ref instanceof String value)refs.add(value);
|
||||
summary.put("evidenceRefs",List.copyOf(refs));
|
||||
var visualIds=visuals.stream().map(chart->Objects.toString(chart.get("id"),"")).filter(s->!s.isBlank()).limit(3).toList();
|
||||
if(!visualIds.isEmpty())summary.put("visualIds",visualIds);
|
||||
return summary;
|
||||
}
|
||||
|
||||
private List<String> historyEvidenceRefs(List<String> factRefs,List<Map<String,Object>> visuals) {
|
||||
var refs=new LinkedHashSet<String>(factRefs);
|
||||
for(var chart:visuals)for(Object ref:list(chart.get("evidenceRefs")))if(ref instanceof String value)refs.add(value);
|
||||
return List.copyOf(refs);
|
||||
}
|
||||
|
||||
void appendSourceDeliveryWarnings(Map<String,Object> summary,List<String> warnings) {
|
||||
if(warnings.stream().noneMatch(s->s!=null&&!s.isBlank()))return;
|
||||
var notices=new LinkedHashSet<>(strings(summary.get("notices")));
|
||||
notices.add("本报告沿用已核验的正式统计结果,未核验细项不纳入判断。");
|
||||
summary.put("notices",List.copyOf(notices));
|
||||
}
|
||||
|
||||
void authorizeSaved(String turnId,AppUserPrincipal actor,QueryValidator validator) {
|
||||
@@ -123,7 +253,8 @@ final class AnalysisProtocolHistory {
|
||||
|
||||
private Map<String,Object> build(AnalysisProtocolService.Attempt a,Map<String,Object> task,AppUserPrincipal actor,QueryValidator validator) {
|
||||
List<Map<String,Object>> sources=new ArrayList<>(),results=new ArrayList<>(),sections=new ArrayList<>(),facts=new ArrayList<>();
|
||||
List<String> gaps=new ArrayList<>();long budget=0;
|
||||
List<String> gaps=new ArrayList<>(),sourceDeliveryWarnings=new ArrayList<>();List<Map<String,Object>> visuals=new ArrayList<>(),visualSources=new ArrayList<>();Set<String> visualIds=new HashSet<>();
|
||||
boolean visualFailed=false,visualUnavailable=false;long budget=0;
|
||||
for(String id:sourceIds(task)) {
|
||||
Map<String,Object> source=source(a,id,actor);List<Map<String,Object>> queries=new ArrayList<>();
|
||||
Map<String,Object> meta=metadata(source);sources.add(meta);
|
||||
@@ -132,6 +263,11 @@ final class AnalysisProtocolHistory {
|
||||
List<?> accepted=list(map(source.get("result")).get("protocolQueryIds"));
|
||||
if(accepted.isEmpty()||new HashSet<>(accepted).size()!=accepted.size())throw denied("该轮缺少最终接受的正式查询证明");
|
||||
List<Map<String,Object>> all=queryRows(id);source.put("queryRecords",all);
|
||||
Map<String,Object> answer=map(source.get("answer"));
|
||||
boolean verifiedPartialResults="PARTIAL".equals(source.get("status"))
|
||||
&&VERIFIED_RESULTS_ONLY.equals(task.get("historyResultMode"))
|
||||
&&serverVerifiedPartialResults(source,accepted,all);
|
||||
List<String> deliveryWarnings=verifiedPartialResults?partialDeliveryWarnings(source):List.of();
|
||||
for(Object queryId:accepted) {
|
||||
var query=all.stream().filter(q->Objects.equals(q.get("queryId"),queryId)).findFirst().orElseThrow(()->denied("历史查询记录已不存在"));
|
||||
if(!Objects.equals(source.get("attemptId"),query.get("attemptId"))||!Objects.equals(source.get("executionId"),query.get("executionId"))
|
||||
@@ -143,9 +279,38 @@ final class AnalysisProtocolHistory {
|
||||
queries.add(validator.validate(source,query,response,actor));
|
||||
}
|
||||
source.put("queries",queries);
|
||||
if("PARTIAL".equals(source.get("status"))&&AnalysisProtocolExploration.nativeAgent(map(source.get("task"))))source.put("gaps",strings(map(map(source.get("result")).get("executionCoverage")).get("missingEvidence")));
|
||||
String priorVisualState=Objects.toString(answer.get("visualState"),"not_requested");
|
||||
boolean sourceVisualRequested=Set.of("ready","failed").contains(priorVisualState);
|
||||
if("unavailable".equals(priorVisualState))visualUnavailable=true;
|
||||
var checkedVisuals=checkedSourceVisuals(answer,id,sourceVisualScope(source),actor);
|
||||
if("ready".equals(checkedVisuals.state()))for(var chart:checkedVisuals.visuals()) {
|
||||
var copy=new LinkedHashMap<>(chart);String chartId=Objects.toString(copy.get("id"),"");
|
||||
if(!visualIds.add(chartId)) {
|
||||
String suffix=AnalysisProtocolService.hash(id).substring(0,8);
|
||||
copy.put("id",chartId.substring(0,Math.min(chartId.length(),110))+"-"+suffix);
|
||||
visualIds.add(Objects.toString(copy.get("id")));
|
||||
}
|
||||
if(visuals.size()<12) {
|
||||
visuals.add(copy);var lineage=new LinkedHashMap<String,Object>();
|
||||
lineage.put("visualId",copy.get("id"));lineage.put("sourceTurnId",id);lineage.put("scope",copy.get("scope"));
|
||||
lineage.put("period",copy.get("period"));lineage.put("evidenceRefs",copy.get("evidenceRefs"));
|
||||
for(String key:List.of("comparisonPeriod","dimension","stage","metric"))lineage.put(key,copy.get(key));
|
||||
visualSources.add(lineage);
|
||||
}
|
||||
}
|
||||
else if(sourceVisualRequested)visualFailed=true;
|
||||
if("PARTIAL".equals(source.get("status"))&&AnalysisProtocolExploration.nativeAgent(map(source.get("task")))) {
|
||||
source.put("gaps",verifiedPartialResults?List.of():strings(map(map(source.get("result")).get("executionCoverage")).get("missingEvidence")));
|
||||
if(verifiedPartialResults) {source.put("verifiedResultsMode",true);source.put("sourceDeliveryWarnings",deliveryWarnings);}
|
||||
}
|
||||
var result=new LinkedHashMap<>(AnalysisProtocolHistoryFacts.buildSource(source,json));
|
||||
result.put("sourceTurnId",id);result.put("task",source.get("task"));result.put("metadata",meta);
|
||||
result.put("verifiedResultsMode",verifiedPartialResults);
|
||||
if(!deliveryWarnings.isEmpty()) {
|
||||
result.put("sourceDeliveryWarnings",deliveryWarnings);meta.put("sourceDeliveryWarnings",deliveryWarnings);
|
||||
sourceDeliveryWarnings.addAll(deliveryWarnings);
|
||||
}
|
||||
result.put("sourceVisuals",checkedVisuals.visuals());
|
||||
for(String key:List.of("status","revision","executionId"))result.put(key,source.get(key));
|
||||
int sectionOffset=sections.size();
|
||||
var displayFacts=new ArrayList<Map<String,Object>>();
|
||||
@@ -155,7 +320,7 @@ final class AnalysisProtocolHistory {
|
||||
results.add(result);sections.addAll(list(result.get("sections")).stream().map(AnalysisProtocolHistory::map).toList());
|
||||
facts.addAll(displayFacts);
|
||||
meta.put("available",true);meta.put("queryIds",List.copyOf(accepted));
|
||||
if(!Boolean.TRUE.equals(result.get("complete"))||!"COMPLETED".equals(source.get("status")))gaps.add("历史轮次 "+id+" 尚未完整覆盖其原任务");
|
||||
if(!Boolean.TRUE.equals(result.get("complete"))||!"COMPLETED".equals(source.get("status"))&&!verifiedPartialResults)gaps.add("历史轮次 "+id+" 尚未完整覆盖其原任务");
|
||||
gaps.addAll(strings(result.get("gaps")));
|
||||
} catch(RuntimeException invalid) {
|
||||
meta.put("available",false);meta.put("reason",safeReason(invalid));gaps.add("历史轮次 "+id+":"+safeReason(invalid));
|
||||
@@ -163,10 +328,83 @@ final class AnalysisProtocolHistory {
|
||||
}
|
||||
if(sources.isEmpty())gaps.add("当前会话没有可复用的历史正式分析");
|
||||
var payload=new LinkedHashMap<String,Object>();payload.put("historySources",sources);payload.put("sourceResults",results);payload.put("sections",sections);payload.put("facts",facts);
|
||||
payload.put("visualSchemaVersion",AnalysisVisualSnapshotAccess.SCHEMA_VERSION);
|
||||
payload.put("visualState",visuals.isEmpty()?(visualFailed?"failed":visualUnavailable?"unavailable":"not_requested"):"ready");
|
||||
payload.put("visuals",List.copyOf(visuals));payload.put("historyVisualSources",List.copyOf(visualSources));
|
||||
payload.put("sourceDeliveryWarnings",sourceDeliveryWarnings.stream().filter(s->s!=null&&!s.isBlank()).distinct().toList());
|
||||
payload.put("historyCoverage",Map.of("complete",gaps.isEmpty(),"expectedSources",sources.size(),"importedSources",results.size(),"missingEvidence",gaps.stream().distinct().toList()));
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* A PARTIAL delivery may be reused only when the server's saved native
|
||||
* coverage proves the accepted facts complete. This never substitutes for
|
||||
* the per-query hash/scope/period/stage/ranking validation in build().
|
||||
*/
|
||||
private boolean serverVerifiedPartialResults(Map<String,Object> source,List<?> accepted,List<Map<String,Object>> queries) {
|
||||
if(!AnalysisProtocolExploration.nativeAgent(map(source.get("task"))))return false;
|
||||
Map<String,Object> coverage=map(map(source.get("result")).get("executionCoverage"));
|
||||
if(!Boolean.TRUE.equals(coverage.get("factsComplete")))return false;
|
||||
List<?> coveredIds=list(coverage.get("acceptedProtocolQueryIds"));
|
||||
if(coveredIds.isEmpty()||!coveredIds.equals(accepted)||new HashSet<>(coveredIds).size()!=coveredIds.size())return false;
|
||||
Map<String,Map<String,Object>> scopes=new HashMap<>();
|
||||
for(Object raw:list(coverage.get("scopeCoverage"))) {
|
||||
Map<String,Object> row=map(raw);String node=Objects.toString(row.get("nodeId"),""),scope=Objects.toString(row.get("scopeId"),"");
|
||||
if(node.isBlank()||scope.isBlank())return false;
|
||||
if(scopes.putIfAbsent(node+"\u0000"+scope,row)!=null)return false;
|
||||
}
|
||||
Map<Object,Map<String,Object>> queryById=new HashMap<>();
|
||||
for(Map<String,Object> query:queries)if(queryById.putIfAbsent(query.get("queryId"),query)!=null)return false;
|
||||
for(Object id:accepted) {
|
||||
if(!(id instanceof String)||id.toString().isBlank())return false;
|
||||
Map<String,Object> query=queryById.get(id);if(query==null)return false;
|
||||
Map<String,Object> scope=scopes.get(Objects.toString(query.get("nodeId"),"")+"\u0000"+Objects.toString(query.get("scopeId"),""));
|
||||
if(scope==null||!Boolean.TRUE.equals(scope.get("complete"))||!list(scope.get("queryIds")).contains(id))return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private List<String> partialDeliveryWarnings(Map<String,Object> source) {
|
||||
Map<String,Object> coverage=map(map(source.get("result")).get("executionCoverage"));
|
||||
var warnings=new LinkedHashSet<String>();
|
||||
for(Object raw:list(coverage.get("explorationWarnings")))if(raw instanceof String text&&!text.isBlank())warnings.add(text.trim());
|
||||
// Keep any non-fatal server coverage note visible as a limitation. It is
|
||||
// not added to the facts/query gap list and cannot establish completeness.
|
||||
for(Object raw:list(coverage.get("missingEvidence")))if(raw instanceof String text&&!text.isBlank())warnings.add(text.trim());
|
||||
warnings.add("原分析轮次交付状态为 PARTIAL;本报告仅整理已核验的正式结果。");
|
||||
warnings.add("未列入已接受正式查询的探索内容不作为本报告正式结论依据。");
|
||||
return List.copyOf(warnings);
|
||||
}
|
||||
|
||||
private Map<String,Object> sourceVisualScope(Map<String,Object> source) {
|
||||
Map<String,Object> scope=new LinkedHashMap<>();scope.put("releaseId",Objects.toString(source.get("releaseId"),""));
|
||||
Map<String,Object> task=map(source.get("task"));scope.putAll(map(task.get("filters")));
|
||||
scope.putAll(map(task.get("period")));
|
||||
if(!map(task.get("sourceFieldLabels")).isEmpty())scope.put("columnLabels",task.get("sourceFieldLabels"));
|
||||
List<String> stages=strings(task.get("stages"));if(stages.size()==1)scope.put("stage",stages.getFirst());
|
||||
List<String> dimensions=strings(task.get("dimensions"));if(dimensions.size()==1)scope.put("view",dimensions.getFirst());
|
||||
return scope;
|
||||
}
|
||||
|
||||
private AnalysisVisualSnapshotAccess.Snapshot checkedSourceVisuals(Map<String,Object> answer,String sourceTurnId,
|
||||
Map<String,Object> expectedScope,AppUserPrincipal actor) {
|
||||
Set<String> fields=governance.allowedFields(actor);
|
||||
return checkSavedVisuals(answer,fields,AnalysisVisualSnapshotAccess.visibleExtraFilterKeys(jdbc,fields),sourceTurnId,expectedScope);
|
||||
}
|
||||
|
||||
static AnalysisVisualSnapshotAccess.Snapshot checkSavedVisuals(Map<String,Object> answer,Set<String> allowedFields,
|
||||
Set<String> visibleFilterKeys,String sourceTurnId,Map<String,Object> expectedScope) {
|
||||
var checked=AnalysisVisualSnapshotAccess.fromAnswer(answer,allowedFields,visibleFilterKeys,sourceTurnId,expectedScope);
|
||||
if(!"ready".equals(checked.state()))return checked;
|
||||
Set<String> signedRefs=new HashSet<>();
|
||||
for(Object raw:list(answer.get("queryEvidenceRefs")))if(raw instanceof String ref&&ref.matches("Q[A-F0-9]{8}"))signedRefs.add(ref);
|
||||
var visuals=checked.visuals().stream().filter(chart->{
|
||||
List<?> refs=list(chart.get("evidenceRefs"));return !refs.isEmpty()&&signedRefs.containsAll(refs);
|
||||
}).toList();
|
||||
return visuals.isEmpty()?AnalysisVisualSnapshotAccess.empty("failed")
|
||||
:new AnalysisVisualSnapshotAccess.Snapshot(AnalysisVisualSnapshotAccess.SCHEMA_VERSION,"ready",visuals);
|
||||
}
|
||||
|
||||
/** A presentation recovery reads the original accepted responses, never a model/evidence file. */
|
||||
Map<String,Object> recoverySource(AnalysisProtocolService.Attempt a,AppUserPrincipal actor,QueryValidator validator) {
|
||||
requireAccess(a.releaseId(),actor);
|
||||
@@ -204,6 +442,9 @@ final class AnalysisProtocolHistory {
|
||||
var before=map(original.get("historyCoverage"));var after=map(current.get("historyCoverage"));
|
||||
for(String key:List.of("complete","expectedSources","importedSources"))if(!Objects.equals(before.get(key),after.get(key)))
|
||||
throw denied("历史来源覆盖状态已变化,请重新整理报告");
|
||||
for(String key:List.of("visualSchemaVersion","visualState","visuals"))
|
||||
if(!write(canonical(original.getOrDefault(key,"visualSchemaVersion".equals(key)?AnalysisVisualSnapshotAccess.SCHEMA_VERSION:"visuals".equals(key)?List.of():"not_requested")))
|
||||
.equals(write(canonical(current.get(key)))))throw denied("历史图表来源或当前授权已变化,请重新整理报告");
|
||||
return original;
|
||||
}
|
||||
|
||||
@@ -226,7 +467,13 @@ final class AnalysisProtocolHistory {
|
||||
||!Objects.equals(a.scopeHash(),AuthorizationFingerprint.of(actor,governance)))throw denied("历史来源发布版本或授权范围已变化");
|
||||
if(!map(source.get("task")).isEmpty()&&!Objects.equals(source.get("taskHash"),hash(source.get("task"))))throw denied("源任务已变化");
|
||||
if(!map(source.get("task")).isEmpty()&&!Objects.equals(canonical(map(source.get("plan")).get("task")),canonical(source.get("task"))))throw denied("源轮次与已确认任务不一致");
|
||||
if(!Objects.equals(canonical(map(source.get("answer")).get("protocolQueryIds")),canonical(map(source.get("result")).get("protocolQueryIds"))))throw denied("源轮次的最终查询清单已变化");
|
||||
// Failed/clarification turns are deliberately imported only as unavailable source metadata.
|
||||
// Their turn answer may contain a failure explanation while the execution has no validated
|
||||
// result payload. Business-source turns remain bound byte-for-byte to their accepted result.
|
||||
if(Set.of("COMPLETED","PARTIAL").contains(source.get("status"))) {
|
||||
if(!Objects.equals(canonical(source.get("answer")),canonical(source.get("result"))))throw denied("源轮次答复与已确认执行结果不一致");
|
||||
if(!Objects.equals(canonical(map(source.get("answer")).get("protocolQueryIds")),canonical(map(source.get("result")).get("protocolQueryIds"))))throw denied("源轮次的最终查询清单已变化");
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
@@ -258,11 +505,195 @@ final class AnalysisProtocolHistory {
|
||||
||!Objects.equals(task.get("historySelectionHash"),row.get("selectionHash")))throw denied("历史导入快照与当前报告任务不一致");
|
||||
}
|
||||
private Map<String,Object> decodeImport(Map<String,Object> row) {return exact.readValue(AnalysisProtocolEvidenceSnapshot.decode((byte[])row.get("payload"),((Number)row.get("bytes")).intValue(),Objects.toString(row.get("encoding"),null),Objects.toString(row.get("hash"),null)),Map.class);}
|
||||
private Map<String,Object> visibleImport(AnalysisProtocolService.Attempt a,Map<String,Object> stored,Map<String,Object> payload) {var result=new LinkedHashMap<>(payload);result.remove("sourceResults");result.put("kind","protocol_history_snapshot");result.put("version",1);result.put("releaseId",a.releaseId());result.put("historyEvidence",identity(a,stored));return result;}
|
||||
private Map<String,Object> visibleImport(AnalysisProtocolService.Attempt a,Map<String,Object> stored,Map<String,Object> payload) {
|
||||
var result=modelVisiblePayload(payload);
|
||||
result.put("kind","protocol_history_snapshot");result.put("version",1);
|
||||
result.put("releaseId",a.releaseId());result.put("historyEvidence",identity(a,stored));return result;
|
||||
}
|
||||
|
||||
static Map<String,Object> modelVisiblePayload(Map<String,Object> payload) {
|
||||
var result=deepCopyMap(payload);
|
||||
projectLegacyRankingDimensions(result);
|
||||
projectSourceTaskContexts(result);
|
||||
result.remove("sourceResults");
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Keep only the selected source's signed business task, never its saved
|
||||
* answer, execution material or complete result, in the model-visible copy. */
|
||||
static int projectSourceTaskContexts(Map<String,Object> visiblePayload) {
|
||||
Map<String,Map<String,Object>> sources=new HashMap<>();Set<String> duplicates=new HashSet<>();
|
||||
for(Object raw:list(visiblePayload.get("sourceResults"))) {
|
||||
Map<String,Object> source=mutableMap(raw);
|
||||
String turn=Objects.toString(source==null?null:source.get("sourceTurnId"),"");
|
||||
if(turn.isBlank())continue;
|
||||
if(sources.putIfAbsent(turn,source)!=null)duplicates.add(turn);
|
||||
}
|
||||
duplicates.forEach(sources::remove);
|
||||
Map<String,Integer> selectedCounts=new HashMap<>();
|
||||
for(Object raw:list(visiblePayload.get("historySources"))) {
|
||||
String turn=Objects.toString(map(raw).get("turnId"),"");
|
||||
selectedCounts.merge(turn,1,Integer::sum);
|
||||
}
|
||||
int projected=0;
|
||||
for(Object raw:list(visiblePayload.get("historySources"))) {
|
||||
Map<String,Object> selected=mutableMap(raw);if(selected==null)continue;
|
||||
selected.remove("taskContext");
|
||||
String turn=Objects.toString(selected.get("turnId"),"");
|
||||
Map<String,Object> source=sources.get(turn);
|
||||
if(!Boolean.TRUE.equals(selected.get("available"))||selectedCounts.getOrDefault(turn,0)!=1||source==null
|
||||
||!Set.of("COMPLETED","PARTIAL").contains(Objects.toString(selected.get("status"),""))
|
||||
||!Objects.equals(selected.get("status"),source.get("status"))
|
||||
||exactIndex(selected.get("revision"))<1
|
||||
||exactIndex(selected.get("revision"))!=exactIndex(source.get("revision"))
|
||||
||!(selected.get("executionId") instanceof String execution)||execution.isBlank()
|
||||
||!execution.equals(source.get("executionId")))continue;
|
||||
Map<String,Object> task=map(source.get("task"));
|
||||
if(task.isEmpty())continue;
|
||||
var context=new LinkedHashMap<String,Object>();
|
||||
for(String key:List.of("period","filters","dimensions","fields","stages","comparisonPeriod",
|
||||
"goal","rootGoal","question","sourceFieldLabels")) {
|
||||
if(task.containsKey(key))context.put(key,deepCopyValue(task.get(key)));
|
||||
}
|
||||
if(task.get("queryPlan") instanceof List<?> nodes) {
|
||||
var plan=new ArrayList<Map<String,Object>>();
|
||||
for(Object rawNode:nodes) {
|
||||
Map<String,Object> node=map(rawNode);var projectedNode=new LinkedHashMap<String,Object>();
|
||||
for(String key:List.of("purpose","dimension","fields","stages","limit")) {
|
||||
if(node.containsKey(key))projectedNode.put(key,deepCopyValue(node.get(key)));
|
||||
}
|
||||
if(!projectedNode.isEmpty())plan.add(projectedNode);
|
||||
}
|
||||
context.put("queryPlan",plan);
|
||||
}
|
||||
if(!context.isEmpty()) {selected.put("taskContext",context);projected++;}
|
||||
}
|
||||
return projected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Old signed imports predate the structured ranking dimension. Restore it
|
||||
* only in the model-visible copy, using the signed source task and the
|
||||
* source's exact section/row fact identity. The stored payload and hash
|
||||
* remain untouched.
|
||||
*/
|
||||
static int projectLegacyRankingDimensions(Map<String,Object> visiblePayload) {
|
||||
List<?> rawSources=list(visiblePayload.get("sourceResults"));
|
||||
Map<String,Map<String,Object>> sourcesByTurn=new HashMap<>();Set<String> duplicateTurns=new HashSet<>();
|
||||
for(Object raw:rawSources) {
|
||||
Map<String,Object> source=mutableMap(raw);String turn=Objects.toString(source==null?null:source.get("sourceTurnId"),"");
|
||||
if(turn.isBlank())continue;
|
||||
if(sourcesByTurn.putIfAbsent(turn,source)!=null)duplicateTurns.add(turn);
|
||||
}
|
||||
duplicateTurns.forEach(sourcesByTurn::remove);
|
||||
List<?> sections=list(visiblePayload.get("sections")),facts=list(visiblePayload.get("facts"));
|
||||
int projected=0;
|
||||
for(int sectionIndex=0;sectionIndex<sections.size();sectionIndex++) {
|
||||
Map<String,Object> section=mutableMap(sections.get(sectionIndex));if(section==null)continue;
|
||||
String turn=Objects.toString(section.get("sourceTurnId"),"");Map<String,Object> source=sourcesByTurn.get(turn);
|
||||
if(source==null)continue;
|
||||
List<String> dimensions=strings(map(source.get("task")).get("dimensions"));
|
||||
if(dimensions.size()!=1||!Set.of("product","department","group").contains(dimensions.getFirst()))continue;
|
||||
String dimension=dimensions.getFirst();Object existingSectionDimension=section.get("dimension");
|
||||
if(existingSectionDimension!=null&&!dimension.equals(existingSectionDimension))continue;
|
||||
List<?> columns=list(section.get("columns"));Set<String> columnKeys=new HashSet<>();
|
||||
for(Object rawColumn:columns)columnKeys.add(Objects.toString(map(rawColumn).get("key"),""));
|
||||
List<?> rows=list(section.get("rows"));
|
||||
if(!columnKeys.containsAll(Set.of("rank","label","weight"))||rows.isEmpty())continue;
|
||||
|
||||
List<?> sourceSections=list(source.get("sections"));int matchingSourceSections=0;
|
||||
for(int i=0;i<sourceSections.size();i++)if(sameRankingSection(section,mutableMap(sourceSections.get(i)))) {
|
||||
matchingSourceSections++;
|
||||
}
|
||||
if(matchingSourceSections!=1)continue;
|
||||
List<?> sourceFacts=list(source.get("facts"));List<Map<String,Object>> rowFacts=new ArrayList<>();boolean valid=true;
|
||||
for(int rowIndex=0;rowIndex<rows.size();rowIndex++) {
|
||||
Map<String,Object> row=mutableMap(rows.get(rowIndex));if(row==null||exactIndex(row.get("rank"))<1
|
||||
||!(row.get("label") instanceof String label)||label.isBlank()) {valid=false;break;}
|
||||
List<Map<String,Object>> topMatches=new ArrayList<>();
|
||||
for(Object rawFact:facts) {
|
||||
Map<String,Object> fact=mutableMap(rawFact);
|
||||
if(isBoundRankingFact(fact,turn,sectionIndex,rowIndex,section,row,dimension))topMatches.add(fact);
|
||||
}
|
||||
if(topMatches.size()!=1) {valid=false;break;}
|
||||
Map<String,Object> topFact=topMatches.getFirst();Object factId=topFact.get("id");
|
||||
List<Map<String,Object>> sourceMatches=new ArrayList<>();
|
||||
for(Object rawFact:sourceFacts) {
|
||||
Map<String,Object> fact=mutableMap(rawFact);
|
||||
if(fact!=null&&Objects.equals(fact.get("id"),factId)
|
||||
&&isBoundRankingFact(fact,turn,sectionIndex,rowIndex,section,row,dimension))sourceMatches.add(fact);
|
||||
}
|
||||
if(sourceMatches.size()!=1) {valid=false;break;}
|
||||
rowFacts.add(topFact);
|
||||
}
|
||||
if(!valid||rowFacts.size()!=rows.size())continue;
|
||||
section.put("dimension",dimension);
|
||||
for(Map<String,Object> fact:rowFacts) {
|
||||
Map<String,Object> scope=mutableMap(fact.get("scope"));
|
||||
if(scope==null)continue;
|
||||
if(scope.get("dimension")==null)scope.put("dimension",dimension);
|
||||
fact.put("scope",scope);
|
||||
}
|
||||
projected++;
|
||||
}
|
||||
return projected;
|
||||
}
|
||||
|
||||
private static boolean isBoundRankingFact(Map<String,Object> fact,String turn,int sectionIndex,int rowIndex,
|
||||
Map<String,Object> section,Map<String,Object> row,String dimension) {
|
||||
Object rawLabel=row==null?null:row.get("label");
|
||||
if(fact==null||!Objects.equals(turn,fact.get("sourceTurnId"))||!Objects.equals("scrapWeight",fact.get("metricCode"))
|
||||
||exactIndex(fact.get("sectionIndex"))!=sectionIndex||exactIndex(fact.get("rowIndex"))!=rowIndex
|
||||
||!(rawLabel instanceof String label)||label.isBlank()||!Objects.toString(fact.get("text"),"").contains(label)
|
||||
||!sameNumber(fact.get("rawValue"),row.get("weight")))return false;
|
||||
Map<String,Object> scope=map(fact.get("scope"));
|
||||
if(!Objects.equals(scope.get("period"),section.get("period"))
|
||||
||!Objects.equals(scope.get("filters"),section.get("filters"))
|
||||
||!Objects.equals(Objects.toString(scope.get("stage"),"overall"),Objects.toString(section.get("stage"),"overall")))return false;
|
||||
Object factDimension=scope.get("dimension");return factDimension==null||dimension.equals(factDimension);
|
||||
}
|
||||
|
||||
private static boolean sameRankingSection(Map<String,Object> first,Map<String,Object> second) {
|
||||
if(first==null||second==null)return false;
|
||||
for(String key:List.of("title","sourceTurnId","period","filters","stage","dimension","columns","rows"))
|
||||
if(!Objects.equals(first.get(key),second.get(key)))return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean sameNumber(Object first,Object second) {
|
||||
if(!(first instanceof Number)&&!(first instanceof String)||!(second instanceof Number)&&!(second instanceof String))return false;
|
||||
try{return new java.math.BigDecimal(first.toString()).compareTo(new java.math.BigDecimal(second.toString()))==0;}
|
||||
catch(NumberFormatException invalid){return false;}
|
||||
}
|
||||
|
||||
private static int exactIndex(Object value) {
|
||||
if(!(value instanceof Number))return -1;
|
||||
try{return new java.math.BigDecimal(value.toString()).intValueExact();}
|
||||
catch(NumberFormatException|ArithmeticException invalid){return -1;}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") private static Map<String,Object> mutableMap(Object raw) {
|
||||
return raw instanceof Map<?,?>?(Map<String,Object>)raw:null;
|
||||
}
|
||||
private static Map<String,Object> deepCopyMap(Map<String,Object> raw) {
|
||||
Object copy=deepCopyValue(raw);return mutableMap(copy);
|
||||
}
|
||||
private static Object deepCopyValue(Object value) {
|
||||
if(value instanceof Map<?,?> source) {
|
||||
Map<String,Object> copy=new LinkedHashMap<>();source.forEach((key,item)->{if(key instanceof String text)copy.put(text,deepCopyValue(item));});return copy;
|
||||
}
|
||||
if(value instanceof List<?> source) {List<Object> copy=new ArrayList<>();for(Object item:source)copy.add(deepCopyValue(item));return copy;}
|
||||
return value;
|
||||
}
|
||||
private Map<String,Object> identity(AnalysisProtocolService.Attempt a,Map<String,Object> stored) {return Map.of("importId",stored.get("id"),"attemptId",a.attemptId(),"executionId",a.executionId(),"revision",a.revision(),"sessionId",a.sessionId(),"turnId",a.turnId(),"snapshotHash",stored.get("hash"));}
|
||||
private Map<String,Object> metadata(Map<String,Object> source) {var m=new LinkedHashMap<String,Object>();for(String key:List.of("turnId","revision","executionId","status"))m.put(key,source.get(key));m.put("available",false);return m;}
|
||||
private void requireAccess(String release,AppUserPrincipal actor) {governance.requireDetail(actor);governance.requireBoundAction("QUERY","query.detail");governance.requireBoundAction("QUERY","query.aggregate");options.options(release,actor);}
|
||||
private boolean factTurn(Map<String,Object> plan) {var task=map(plan.get("task"));return !Boolean.TRUE.equals(task.get("needsClarification"))&&!task.containsKey("directResponse")&&!Set.of("report","explain","restate").contains(Objects.toString(task.get("kind"),""));}
|
||||
private boolean factTurn(Map<String,Object> plan) {
|
||||
var task=map(plan.get("task"));
|
||||
String kind=Objects.toString(task.get("kind"),"");
|
||||
return Set.of("analysis","metrics","members","source_verify","details",AnalysisProtocolInspection.KIND).contains(kind)
|
||||
&&!Boolean.TRUE.equals(task.get("needsClarification"))&&!task.containsKey("directResponse");
|
||||
}
|
||||
boolean validTitle(String title,Map<String,Object> payload,Map<String,Object> task) {if(title.isBlank()||title.length()>100||title.equals(task.get("question"))||title.chars().anyMatch(c->c<32)||title.matches(".*[<>].*")||mentionsPartialScope(title,payload))return false;var numeric=java.util.regex.Pattern.compile("[0-9]+(?:\\.[0-9]+)?");Set<String> allowed=new HashSet<>();var input=numeric.matcher(write(payload));while(input.find())allowed.add(input.group());var proposed=numeric.matcher(title);while(proposed.find())if(!allowed.contains(proposed.group()))return false;return true;}
|
||||
private boolean mentionsPartialScope(String title,Map<String,Object> payload) {
|
||||
List<Set<String>> frames=new ArrayList<>();Set<String> excluded=new HashSet<>();
|
||||
|
||||
+376
-42
@@ -12,6 +12,8 @@ final class AnalysisProtocolHistoryFacts {
|
||||
private static final Map<String,String> FIELD_LABELS = Map.of(
|
||||
"rate", "合格率", "scrapRate", "报废率", "totalWeight", "投入重量", "scrapWeight", "报废重量",
|
||||
"compositeRate", "综合合格率");
|
||||
private static final java.util.regex.Pattern HISTORY_INTERNAL_METADATA = java.util.regex.Pattern.compile(
|
||||
"(?is)(?:history-fact:|\\b(?:queryId|protocolQueryId|factId|evidenceRef|sourceTurnId|attemptId|executionId|snapshotHash|scopeHash)\\s*[:=]|\\bfilters\\s*=|\\{\\s*\")");
|
||||
|
||||
private static final JsonMapper HTML_JSON = JsonMapper.builder().build();
|
||||
|
||||
@@ -24,9 +26,15 @@ final class AnalysisProtocolHistoryFacts {
|
||||
List<Map<String,Object>> facts = new ArrayList<>();
|
||||
List<Map<String,Object>> metricContexts = new ArrayList<>();
|
||||
List<String> gaps = new ArrayList<>();
|
||||
List<String> sourceDeliveryWarnings = new ArrayList<>();
|
||||
addStrings(gaps, source.get("gaps"));
|
||||
String sourceStatus = text(source.get("status"), "");
|
||||
if (Set.of("PARTIAL", "FAILED", "CANCELLED", "NEEDS_CLARIFICATION").contains(sourceStatus.toUpperCase(Locale.ROOT)))
|
||||
boolean verifiedPartialResults = "PARTIAL".equalsIgnoreCase(sourceStatus)
|
||||
&& Boolean.TRUE.equals(source.get("verifiedResultsMode"));
|
||||
if (verifiedPartialResults) {
|
||||
addStrings(sourceDeliveryWarnings, source.get("sourceDeliveryWarnings"));
|
||||
sourceDeliveryWarnings.add("原分析轮次交付状态为 PARTIAL;本报告仅整理已核验的正式结果。");
|
||||
} else if (Set.of("PARTIAL", "FAILED", "CANCELLED", "NEEDS_CLARIFICATION").contains(sourceStatus.toUpperCase(Locale.ROOT)))
|
||||
gaps.add("该历史轮次状态为 " + sourceStatus + "。");
|
||||
|
||||
int[] sequence = {0};
|
||||
@@ -81,6 +89,9 @@ final class AnalysisProtocolHistoryFacts {
|
||||
.filter(s -> !s.isEmpty()).distinct().toList();
|
||||
var result=mapOf("sections", List.copyOf(sections), "facts", List.copyOf(facts),
|
||||
"complete", uniqueGaps.isEmpty(), "gaps", uniqueGaps);
|
||||
List<String> uniqueWarnings=sourceDeliveryWarnings.stream().filter(Objects::nonNull).map(String::trim)
|
||||
.filter(s->!s.isEmpty()).distinct().toList();
|
||||
if(!uniqueWarnings.isEmpty())result.put("sourceDeliveryWarnings",uniqueWarnings);
|
||||
if(!metricContexts.isEmpty())result.put("metricContexts",List.copyOf(metricContexts));
|
||||
return result;
|
||||
}
|
||||
@@ -124,7 +135,7 @@ final class AnalysisProtocolHistoryFacts {
|
||||
for (String field : fields) {
|
||||
Object value = stages.isEmpty() ? summary.get("compositeRate") : map(summary.get(stage)).get(field);
|
||||
String stageLabel = stages.isEmpty() ? "综合" : stageLabel(stage);
|
||||
Map<String,Object> row = mapOf("stage", stageLabel, "field", fieldLabel(field), "value", value);
|
||||
Map<String,Object> row = mapOf("stage", stageLabel, "field", fieldLabel(field), "metricCode",field,"value", value);
|
||||
summaryRows.add(row);
|
||||
addFact(facts, turnId, sequence, scopedText(period, filters, stageLabel + fieldLabel(field)
|
||||
+ " = " + valueText(value)));
|
||||
@@ -145,7 +156,7 @@ final class AnalysisProtocolHistoryFacts {
|
||||
for (String stage : stages.isEmpty() ? List.of("") : stages) for (String field : fields) {
|
||||
Object value = stages.isEmpty() ? raw.get("compositeRate") : map(raw.get(stage)).get(field);
|
||||
projected.add(mapOf("key", raw.get("key"), "label", raw.get("label"),
|
||||
"stage", stages.isEmpty() ? "综合" : stageLabel(stage), "field", fieldLabel(field), "value", value));
|
||||
"stage", stages.isEmpty() ? "综合" : stageLabel(stage), "field", fieldLabel(field), "metricCode",field,"value", value));
|
||||
addFact(facts, turnId, sequence, scopedText(period, filters,
|
||||
text(raw.get("label"), text(raw.get("key"), "未命名分组")) + " · "
|
||||
+ (stages.isEmpty() ? "综合" : stageLabel(stage)) + fieldLabel(field) + " = " + valueText(value)));
|
||||
@@ -167,7 +178,7 @@ final class AnalysisProtocolHistoryFacts {
|
||||
Object value = stages.isEmpty() ? raw.get("compositeRate") : map(raw.get(stage)).get(field);
|
||||
String month = text(raw.get("month"), text(raw.get("period"), ""));
|
||||
Map<String,Object> row = mapOf("month", month, "stage", stages.isEmpty() ? "综合" : stageLabel(stage),
|
||||
"field", fieldLabel(field), "value", value);
|
||||
"field", fieldLabel(field), "metricCode",field,"value", value);
|
||||
projected.add(row);
|
||||
addFact(facts, turnId, sequence, scopedText(period, filters, month + " · " + row.get("stage")
|
||||
+ row.get("field") + " = " + valueText(value)));
|
||||
@@ -194,7 +205,7 @@ final class AnalysisProtocolHistoryFacts {
|
||||
Object value=map(summary.get(stage)).get(field);if(!(value instanceof Number))continue;
|
||||
var componentTask=new LinkedHashMap<>(task);componentTask.put("fields",List.of(field));componentTask.put("stages",List.of(stage));componentTask.put("trendRequired",false);componentTask.put("periodCoverage","available");
|
||||
if(!Boolean.TRUE.equals(AnalysisProtocolMetricCoverage.inspect(response,componentTask,period).get("metricsComplete")))continue;
|
||||
rows.add(mapOf("stage",stageLabel(stage),"field",fieldLabel(field),"value",value));
|
||||
rows.add(mapOf("stage",stageLabel(stage),"field",fieldLabel(field),"metricCode",field,"value",value));
|
||||
addFact(facts,turnId,sequence,scopedText(period,filters,stageLabel(stage)+fieldLabel(field)+" = "+valueText(value)));
|
||||
displayFact(facts,sections.size(),rows.size()-1,period,filters,stage,field,value,null);facts.getLast().put("sourceKind","native_saved_aggregate");
|
||||
}
|
||||
@@ -210,41 +221,108 @@ final class AnalysisProtocolHistoryFacts {
|
||||
Map<String,Object> period = period(scope, task, parameters);
|
||||
if (!validPeriod(period)) gaps.add("正式分组排名缺少有效的已验证期间。");
|
||||
Map<String,Object> filters = filters(scope, task);
|
||||
String dimension=text(artifact.get("dimension"),"");
|
||||
boolean validDimension=Set.of("product","department","group").contains(dimension);
|
||||
List<String> allowedStages = strings(task.get("stages")), allowedFields = strings(task.get("fields"));
|
||||
List<Map<String,Object>> rankings = maps(artifact.get("rankings"));
|
||||
if (rankings.isEmpty()) { gaps.add("正式分组排名缺少已保存的 Top 快照。"); return; }
|
||||
for (Map<String,Object> rank : rankings) {
|
||||
String stage = text(rank.get("stage"), ""), metric = text(rank.get("metric"), "");
|
||||
if (!allowedStages.contains(stage) || !allowedFields.contains(metric)
|
||||
boolean overallCombinedScrap="overall".equals(stage)&&allowedStages.isEmpty()
|
||||
&&"scrapWeight".equals(metric)&&allowedFields.contains("scrapWeight");
|
||||
boolean stageScrap="scrapWeight".equals(metric)&&allowedStages.contains(stage)&&allowedFields.contains("scrapWeight");
|
||||
if ((!overallCombinedScrap&&!allowedStages.contains(stage)) || !allowedFields.contains(metric)
|
||||
|| !Boolean.TRUE.equals(rank.get("completeRanking"))) {
|
||||
gaps.add("正式分组排名快照超出本次任务授权范围或未声明完整。");
|
||||
continue;
|
||||
}
|
||||
if (rank.get("denominator") == null) gaps.add("正式分组排名缺少同范围正式分母。");
|
||||
if((overallCombinedScrap||stageScrap)&&validEmptyScrapRanking(rank)) {
|
||||
Object rate=rank.get("compositeRate");
|
||||
String stageText=overallCombinedScrap?"综合" : stageLabel(stage);
|
||||
String fact="所选范围"+stageText+"正式报废总重量为 0,没有正报废重量分组可排名。";
|
||||
if(overallCombinedScrap&&rate instanceof Number)fact+=" 综合合格率沿用正式值 = "+valueText(rate)+"。";
|
||||
else if(overallCombinedScrap&&allowedFields.contains("rate"))gaps.add("合计排名没有可引用的正式综合合格率;未按缺失阶段补值。");
|
||||
addFact(facts,turnId,sequence,scopedText(period,filters,fact));
|
||||
displayFact(facts,sections.size(),-1,period,filters,stage,"scrapWeight",rank.get("denominator"),null);
|
||||
if(validDimension)addRankingDimension(facts.getLast(),dimension);
|
||||
var emptyRow=new LinkedHashMap<String,Object>();emptyRow.put("message","没有正报废重量分组可排名");
|
||||
List<Map<String,Object>> emptyColumns=new ArrayList<>(columns("message","结果"));
|
||||
if(overallCombinedScrap&&rate instanceof Number) {emptyRow.put("compositeRate",rate);emptyColumns.addAll(columns("compositeRate","综合合格率"));}
|
||||
String note="正式分母为 0;原始完整分组覆盖已验证,空榜不代表缺失来源数据。";
|
||||
if(overallCombinedScrap&&rate instanceof Number)note+="综合合格率沿用同一正式响应的综合值,没有推算阶段率。";
|
||||
var emptySection=section((validDimension?dimension:"分组")+"报废重量排名",turnId,period,filters,overallCombinedScrap?null:stage,emptyColumns,List.of(emptyRow),note);
|
||||
if(validDimension)emptySection.put("dimension",dimension);
|
||||
sections.add(emptySection);
|
||||
continue;
|
||||
}
|
||||
int limit = integer(artifact.get("displayLimit"), 10);
|
||||
List<Map<String,Object>> top = maps(rank.get("rows"));
|
||||
if (limit > 0 && top.size() > limit) top = top.subList(0, limit);
|
||||
if(top.isEmpty()) {gaps.add("正式分组排名没有可展示的正重量分组。");continue;}
|
||||
List<Map<String,Object>> projected = new ArrayList<>();
|
||||
int shown = top.size();
|
||||
for (Map<String,Object> raw : top) {
|
||||
Map<String,Object> row = mapOf("rank", raw.get("rank"), "key", raw.get("key"),
|
||||
"label", raw.get("label"), "weight", raw.get("weight"));
|
||||
if(overallCombinedScrap)for(String key:List.of("compositeRate","blackScrapWeight","brightScrapWeight",
|
||||
"blackTotalWeight","brightTotalWeight","share","cumulativeShare"))
|
||||
if(raw.containsKey(key))row.put(key,raw.get(key));
|
||||
projected.add(row);
|
||||
addFact(facts, turnId, sequence, scopedText(period, filters,
|
||||
"Top " + rankBoundary(limit, shown) + " " + text(raw.get("label"), text(raw.get("key"), "未命名分组"))
|
||||
+ " · " + stageLabel(stage) + fieldLabel(metric) + " = " + valueText(raw.get("weight"))
|
||||
+ ";同范围正式分母 = " + valueText(rank.get("denominator"))));
|
||||
StringBuilder factText=new StringBuilder("Top ").append(rankBoundary(limit,shown)).append(' ')
|
||||
.append(text(raw.get("label"),text(raw.get("key"),"未命名分组"))).append(" · ")
|
||||
.append(stageLabel(stage)).append(fieldLabel(metric)).append(" = ").append(valueText(raw.get("weight")));
|
||||
if(overallCombinedScrap) {
|
||||
appendArtifactFact(factText,"综合合格率",raw.get("compositeRate"));
|
||||
appendArtifactFact(factText,"黑片报废重量",raw.get("blackScrapWeight"));
|
||||
appendArtifactFact(factText,"亮片报废重量",raw.get("brightScrapWeight"));
|
||||
appendArtifactFact(factText,"黑片投入重量",raw.get("blackTotalWeight"));
|
||||
appendArtifactFact(factText,"亮片投入重量",raw.get("brightTotalWeight"));
|
||||
appendArtifactFact(factText,"占同范围全部报废",raw.get("share"));
|
||||
appendArtifactFact(factText,"累计占比",raw.get("cumulativeShare"));
|
||||
}
|
||||
factText.append(";同范围正式分母 = ").append(valueText(rank.get("denominator")));
|
||||
addFact(facts, turnId, sequence, scopedText(period, filters, factText.toString()));
|
||||
displayFact(facts,sections.size(),projected.size()-1,period,filters,stage,metric,raw.get("weight"),null);
|
||||
if(validDimension)addRankingDimension(facts.getLast(),dimension);
|
||||
}
|
||||
String dimension = text(artifact.get("dimension"), "分组");
|
||||
sections.add(section("Top " + rankBoundary(limit, shown) + " " + dimension + "排名", turnId, period, filters,
|
||||
stage, columns("rank", "排名", "key", "分组键", "label", "分组", "weight", "重量"), projected,
|
||||
"正式分母(" + stageLabel(stage) + fieldLabel(metric) + ")为 " + valueText(rank.get("denominator"))
|
||||
+ "。此表只表示已保存的 Top " + rankBoundary(limit, shown) + "(本次展示 " + shown
|
||||
+ " 条),不代表全量分组;重量沿用来源单位。"));
|
||||
List<Map<String,Object>> rankingColumns=new ArrayList<>(columns("rank","排名","key","分组键","label","分组","weight",overallCombinedScrap?"报废总重(原单位)":"重量"));
|
||||
if(overallCombinedScrap)rankingColumns.addAll(columns("compositeRate","综合合格率","blackScrapWeight","黑片报废重量(原单位)",
|
||||
"brightScrapWeight","亮片报废重量(原单位)","blackTotalWeight","黑片投入重量(原单位)",
|
||||
"brightTotalWeight","亮片投入重量(原单位)","share","占同范围全部报废","cumulativeShare","累计占比"));
|
||||
String note="正式分母(" + stageLabel(stage) + fieldLabel(metric) + ")为 " + valueText(rank.get("denominator"))
|
||||
+ "。此表只表示已保存的 Top " + rankBoundary(limit, shown) + "(本次展示 " + shown
|
||||
+ " 条),不代表全量分组;重量沿用来源单位。";
|
||||
if(overallCombinedScrap)note+="综合合格率沿用同一正式响应返回值;阶段重量、投入重量及占比仅展示排名快照保存值。";
|
||||
var rankingSection=section("Top " + rankBoundary(limit, shown) + " " + (validDimension?dimension:"分组") + "排名", turnId, period, filters,
|
||||
overallCombinedScrap?null:stage, rankingColumns, projected, note);
|
||||
if(validDimension)rankingSection.put("dimension",dimension);
|
||||
sections.add(rankingSection);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addRankingDimension(Map<String,Object> fact,String dimension) {
|
||||
Map<String,Object> scope=new LinkedHashMap<>(map(fact.get("scope")));scope.put("dimension",dimension);fact.put("scope",scope);
|
||||
}
|
||||
|
||||
private static boolean validEmptyScrapRanking(Map<String,Object> rank) {
|
||||
Object denominator=rank.get("denominator"), rows=rank.get("rows");
|
||||
if(!(denominator instanceof Number number))return false;
|
||||
try {
|
||||
return new BigDecimal(number.toString()).signum()==0
|
||||
&&"scrapWeight".equals(rank.get("metric"))
|
||||
&&"no_positive_scrap".equals(rank.get("emptyReason"))
|
||||
&&integer(rank.get("totalRows"),-1)==0&&integer(rank.get("returnedRows"),-1)==0
|
||||
&&Boolean.TRUE.equals(rank.get("completeRanking"))
|
||||
&&"weight_desc_key_codepoint_asc".equals(rank.get("orderPolicy"))
|
||||
&&Boolean.FALSE.equals(rank.get("truncated"))&&rows instanceof List<?> list&&list.isEmpty();
|
||||
} catch(NumberFormatException invalid) {return false;}
|
||||
}
|
||||
|
||||
private static void appendArtifactFact(StringBuilder text,String label,Object value) {
|
||||
if(value!=null)text.append(";").append(label).append(" = ").append(valueText(value));
|
||||
}
|
||||
|
||||
private static void buildChanges(String turnId, Map<String,Object> task, Map<String,Object> scope,
|
||||
Map<String,Object> parameters, Map<String,Object> response, Map<String,Object> coverage,
|
||||
List<Map<String,Object>> sections, List<Map<String,Object>> facts, List<String> gaps, int[] sequence, JsonMapper json) {
|
||||
@@ -507,15 +585,24 @@ final class AnalysisProtocolHistoryFacts {
|
||||
allSections.add(copy);
|
||||
}
|
||||
allFacts.addAll(maps(source.get("facts")));
|
||||
addStrings(allGaps, source.get("gaps"));
|
||||
if (Boolean.FALSE.equals(source.get("complete"))) allGaps.add("历史来源 " + turn + " 存在未完成范围。");
|
||||
if(historical) {
|
||||
if(Boolean.FALSE.equals(source.get("complete")))allGaps.add("部分已选历史来源存在未完成范围。");
|
||||
} else {
|
||||
addStrings(allGaps, source.get("gaps"));
|
||||
if (Boolean.FALSE.equals(source.get("complete"))) allGaps.add("历史来源 " + turn + " 存在未完成范围。");
|
||||
}
|
||||
}
|
||||
List<Map<String,Object>> renderedSections=historical?uniqueSections(allSections):allSections;
|
||||
Map<String,Map<String,Object>> factsById = new LinkedHashMap<>();
|
||||
for (Map<String,Object> fact : allFacts) factsById.put(text(fact.get("id"), ""), fact);
|
||||
List<Map<String,Object>> headlines = new ArrayList<>();
|
||||
Set<String> headlineTexts=new HashSet<>();
|
||||
if (headlineFactIds != null) for (String id : headlineFactIds) {
|
||||
Map<String,Object> fact = factsById.get(id);
|
||||
if (fact != null && !text(fact.get("text"), "").isBlank() && !headlines.contains(fact)) headlines.add(fact);
|
||||
String headline=fact==null?"":headlineText(fact,allSections,sources,historical);
|
||||
if (fact != null && !headline.isBlank() && headlineTexts.add(headline)) {
|
||||
var presented=new LinkedHashMap<>(fact);presented.put("_displayText",headline);headlines.add(presented);
|
||||
}
|
||||
}
|
||||
String answer;
|
||||
if (headlines.isEmpty()) {
|
||||
@@ -523,43 +610,91 @@ final class AnalysisProtocolHistoryFacts {
|
||||
} else {
|
||||
List<String> lines = new ArrayList<>();
|
||||
for (Map<String,Object> fact : headlines) {
|
||||
String text = text(fact.get("text"), "");
|
||||
if (!text.isBlank()) lines.add("- " + md(text(fact.get("sourceTurnId"), "历史来源")) + ":" + md(text));
|
||||
String text = text(fact.get("_displayText"), text(fact.get("displayText"), Objects.toString(fact.get("text"), "")));
|
||||
if (!text.isBlank()) lines.add("- " + (historical?"":md(text(fact.get("sourceTurnId"), "历史来源"))+":") + md(text));
|
||||
}
|
||||
answer = lines.isEmpty() ? "没有可引用的已校验历史数值事实。" : String.join("\n", lines);
|
||||
}
|
||||
String markdown = renderMarkdown(safeTitle, headlines, allSections, allGaps, sources, historical);
|
||||
String html = renderHtml(safeTitle, headlines, allSections, allGaps, sources, historical);
|
||||
String markdown = renderMarkdown(safeTitle, headlines, renderedSections, allGaps, sources, historical);
|
||||
String html = renderHtml(safeTitle, headlines, renderedSections, allGaps, sources, historical);
|
||||
return mapOf("answer", answer, "reportMarkdown", markdown, "reportHtml", html,
|
||||
"historySections", List.copyOf(allSections));
|
||||
"historySections", List.copyOf(renderedSections));
|
||||
}
|
||||
|
||||
private static String headlineText(Map<String,Object> fact,List<Map<String,Object>> sections,
|
||||
List<Map<String,Object>> sources,boolean historical) {
|
||||
if(!historical)return text(fact.get("text"),"");
|
||||
Object rawIndex=fact.get("sectionIndex"),rawRow=fact.get("rowIndex");
|
||||
if(rawIndex instanceof Number sectionNumber&&rawRow instanceof Number rowNumber) {
|
||||
int sectionIndex=sectionNumber.intValue(),rowIndex=rowNumber.intValue();
|
||||
if(sectionIndex>=0&§ionIndex<sections.size()) {
|
||||
var section=sections.get(sectionIndex);var rows=maps(section.get("rows"));
|
||||
if(rowIndex>=0&&rowIndex<rows.size()) {
|
||||
var row=rows.get(rowIndex);String metric=text(fact.get("metricCode"),"");
|
||||
String scope=historyScopeText(section,sources);
|
||||
if("scrapWeight".equals(metric)&&row.containsKey("weight")) {
|
||||
String unit=verifiedWeightUnit(section,sources);var parts=new ArrayList<String>();
|
||||
String label=text(row.get("label"),"已确认对象");
|
||||
parts.add(scope+" · "+label+"报废重量 "+renderedValue(section,Map.of("key","weight"),row,unit));
|
||||
if(row.containsKey("compositeRate"))parts.add("综合合格率 "+renderedValue(section,Map.of("key","compositeRate"),row,unit));
|
||||
if(row.containsKey("share"))parts.add("占同范围全部报废 "+renderedValue(section,Map.of("key","share"),row,unit));
|
||||
if(row.containsKey("cumulativeShare"))parts.add("累计占比 "+renderedValue(section,Map.of("key","cumulativeShare"),row,unit));
|
||||
return String.join(";",parts);
|
||||
}
|
||||
if(row.containsKey("value")) {
|
||||
String value=renderedValue(section,Map.of("key","value"),row,verifiedWeightUnit(section,sources));
|
||||
return scope+" · "+fieldLabel(text(row.get("field"),"指标"))+" = "+value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
String display=text(fact.get("displayText"),text(fact.get("text"),""));
|
||||
if(hasHistoryInternalMetadata(display))return "";
|
||||
return display;
|
||||
}
|
||||
|
||||
private static List<Map<String,Object>> uniqueSections(List<Map<String,Object>> sections) {
|
||||
Set<String> signatures=new HashSet<>();List<Map<String,Object>> unique=new ArrayList<>();
|
||||
for(Map<String,Object> section:sections) {
|
||||
Map<String,Object> content=new LinkedHashMap<>();
|
||||
for(String key:List.of("title","period","filters","stage","columns","rows","note"))
|
||||
if(section.containsKey(key))content.put(key,section.get(key));
|
||||
if(signatures.add(HTML_JSON.writeValueAsString(content)))unique.add(section);
|
||||
}
|
||||
return unique;
|
||||
}
|
||||
|
||||
private static String renderMarkdown(String title, List<Map<String,Object>> headlines, List<Map<String,Object>> sections, List<String> gaps,
|
||||
List<Map<String,Object>> sources, boolean historical) {
|
||||
StringBuilder out = new StringBuilder("# ").append(md(title)).append("\n\n");
|
||||
out.append(historical?"本报告按每个历史来源的原查询范围分别列示;不同期间与重叠总体没有合并或求均值。\n\n":"本报告依据本轮受控正式类别查询;范围为当前发布版本全部已发布期间,不含未分类。\n\n");
|
||||
for (Map<String,Object> source : sources) out.append("- 来源轮次:").append(md(sourceReference(source)))
|
||||
.append(";状态:").append(md(text(source.get("status"), "未知"))).append("\n");
|
||||
if (!sources.isEmpty()) out.append('\n');
|
||||
if(!historical) {
|
||||
for (Map<String,Object> source : sources) out.append("- 来源轮次:").append(md(sourceReference(source)))
|
||||
.append(";状态:").append(md(text(source.get("status"), "未知"))).append("\n");
|
||||
if (!sources.isEmpty()) out.append('\n');
|
||||
}
|
||||
if (!headlines.isEmpty()) {
|
||||
out.append("## 重点结论\n\n");
|
||||
for (Map<String,Object> fact : headlines) out.append("- ").append(md(text(fact.get("text"), ""))).append('\n');
|
||||
for (Map<String,Object> fact : headlines) out.append("- ").append(md(text(fact.get("_displayText"),text(fact.get("text"), "")))).append('\n');
|
||||
out.append('\n');
|
||||
}
|
||||
for (Map<String,Object> section : sections) {
|
||||
out.append("## ").append(md(text(section.get("title"), "历史数据"))).append("\n\n");
|
||||
out.append("范围:").append(md(scopeText(map(section.get("period")), map(section.get("filters")), section.get("stage"))))
|
||||
.append("\n\n来源轮次:").append(md(text(section.get("sourceTurnId"), "未知"))).append("\n\n");
|
||||
List<Map<String,Object>> columns = maps(section.get("columns"));
|
||||
if(historical)columns=historyColumns(section,columns);
|
||||
out.append("## ").append(md(historical?historySectionTitle(section,columns):text(section.get("title"), "历史数据"))).append("\n\n");
|
||||
out.append("范围:").append(md(historical?historyScopeText(section,sources):scopeText(map(section.get("period")), map(section.get("filters")), section.get("stage"))))
|
||||
.append(historical?"\n\n":"\n\n来源轮次:"+md(text(section.get("sourceTurnId"), "未知"))+"\n\n");
|
||||
if (!columns.isEmpty()) {
|
||||
out.append('|'); for (Map<String,Object> column : columns) out.append(' ').append(md(text(column.get("label"), text(column.get("key"), "")))).append(" |");
|
||||
String unit=historical?verifiedWeightUnit(section,sources):null;
|
||||
out.append('|'); for (Map<String,Object> column : columns) out.append(' ').append(md(historical?historyColumnLabel(section,column,unit):displayColumnLabel(column,unit))).append(" |");
|
||||
out.append("\n|"); for (int i=0;i<columns.size();i++) out.append(" --- |"); out.append('\n');
|
||||
for (Map<String,Object> row : maps(section.get("rows"))) {
|
||||
out.append('|'); for (Map<String,Object> column : columns) out.append(' ').append(md(valueText(row.get(text(column.get("key"), ""))))).append(" |"); out.append('\n');
|
||||
out.append('|'); for (Map<String,Object> column : columns) out.append(' ').append(md(renderedValue(section,column,row,unit))).append(" |"); out.append('\n');
|
||||
}
|
||||
out.append('\n');
|
||||
}
|
||||
if (section.get("note") != null) out.append(md(text(section.get("note"), ""))).append("\n\n");
|
||||
String note=renderedNote(section,historical?verifiedWeightUnit(section,sources):null,historical);
|
||||
if (!note.isBlank()) out.append(md(note)).append("\n\n");
|
||||
}
|
||||
if (!gaps.isEmpty()) {
|
||||
out.append("## 尚未覆盖的范围\n\n");
|
||||
@@ -583,25 +718,28 @@ final class AnalysisProtocolHistoryFacts {
|
||||
.append("details{margin:8px 0}summary{cursor:pointer;color:#48627b}.empty{color:#77889a;font-style:italic}")
|
||||
.append("</style></head><body><main><article class=\"paper\"><h1>").append(html(title)).append("</h1>")
|
||||
.append(historical?"<p>按每个历史来源的原查询范围分别列示;不同期间与重叠总体没有合并或求均值。</p>":"<p>依据本轮受控正式类别查询;范围为当前发布版本全部已发布期间,不含未分类。</p>");
|
||||
for (Map<String,Object> source : sources) {
|
||||
if(!historical)for (Map<String,Object> source : sources) {
|
||||
out.append("<p class=\"source\">来源轮次:").append(html(sourceReference(source))).append(";状态:")
|
||||
.append(html(text(source.get("status"), "未知"))).append("</p>");
|
||||
}
|
||||
if (!headlines.isEmpty()) {
|
||||
out.append("<section class=\"highlights\"><h2>重点结论</h2><ul>");
|
||||
for (Map<String,Object> fact : headlines) out.append("<li>").append(html(text(fact.get("text"), ""))).append("</li>");
|
||||
for (Map<String,Object> fact : headlines) out.append("<li>").append(html(text(fact.get("_displayText"),text(fact.get("text"), "")))).append("</li>");
|
||||
out.append("</ul></section>");
|
||||
}
|
||||
for (Map<String,Object> section : sections) {
|
||||
out.append("<section><h2>").append(html(text(section.get("title"), "历史数据"))).append("</h2><p class=\"scope\">范围:")
|
||||
.append(html(scopeText(map(section.get("period")), map(section.get("filters")), section.get("stage"))))
|
||||
.append("</p><p class=\"source\">来源轮次:").append(html(text(section.get("sourceTurnId"), "未知"))).append("</p>");
|
||||
List<Map<String,Object>> columns = maps(section.get("columns"));
|
||||
if(historical)columns=historyColumns(section,columns);
|
||||
out.append("<section><h2>").append(html(historical?historySectionTitle(section,columns):text(section.get("title"), "历史数据"))).append("</h2><p class=\"scope\">范围:")
|
||||
.append(html(historical?historyScopeText(section,sources):scopeText(map(section.get("period")), map(section.get("filters")), section.get("stage")))).append("</p>");
|
||||
if(!historical)out.append("<p class=\"source\">来源轮次:").append(html(text(section.get("sourceTurnId"), "未知"))).append("</p>");
|
||||
List<Map<String,Object>> rows = maps(section.get("rows"));
|
||||
if (columns.isEmpty()) out.append("<p class=\"empty\">没有可展示的列。</p>");
|
||||
else if ("物理原始记录".equals(section.get("title")) && !rows.isEmpty()) appendRecordTables(out, columns, rows);
|
||||
else if(historical)appendHistoryHtmlTable(out,section,columns,rows,verifiedWeightUnit(section,sources));
|
||||
else appendHtmlTables(out, columns, rows);
|
||||
if (section.get("note") != null) out.append("<p class=\"note\">").append(html(text(section.get("note"), ""))).append("</p>");
|
||||
String note=renderedNote(section,historical?verifiedWeightUnit(section,sources):null,historical);
|
||||
if (!note.isBlank()) out.append("<p class=\"note\">").append(html(note)).append("</p>");
|
||||
out.append("</section>");
|
||||
}
|
||||
if (!gaps.isEmpty()) {
|
||||
@@ -650,6 +788,202 @@ final class AnalysisProtocolHistoryFacts {
|
||||
appendHtmlTables(out, compact, displayed, rows);
|
||||
}
|
||||
|
||||
private static void appendHistoryHtmlTable(StringBuilder out,Map<String,Object> section,
|
||||
List<Map<String,Object>> columns,List<Map<String,Object>> rows,String unit) {
|
||||
List<List<Map<String,Object>>> segments=new ArrayList<>();
|
||||
if(columns.size()<=6)segments.add(columns);
|
||||
else {
|
||||
segments.add(columns.subList(0,6));
|
||||
for(int start=6;start<columns.size();start+=5) {
|
||||
List<Map<String,Object>> segment=new ArrayList<>();segment.add(columns.getFirst());
|
||||
segment.addAll(columns.subList(start,Math.min(start+5,columns.size())));segments.add(segment);
|
||||
}
|
||||
}
|
||||
for(List<Map<String,Object>> segment:segments) {
|
||||
out.append("<table><thead><tr>");
|
||||
for(Map<String,Object> column:segment)out.append("<th>").append(html(historyColumnLabel(section,column,unit))).append("</th>");
|
||||
out.append("</tr></thead><tbody>");
|
||||
if(rows.isEmpty())out.append("<tr><td colspan=\"").append(segment.size()).append("\" class=\"empty\">无返回行</td></tr>");
|
||||
for(Map<String,Object> row:rows) {
|
||||
out.append("<tr>");
|
||||
for(Map<String,Object> column:segment)out.append("<td>").append(html(renderedValue(section,column,row,unit))).append("</td>");
|
||||
out.append("</tr>");
|
||||
}
|
||||
out.append("</tbody></table>");
|
||||
}
|
||||
}
|
||||
|
||||
private static String displayColumnLabel(Map<String,Object> column,String unit) {
|
||||
String key=text(column.get("key"),""),label=text(column.get("label"),key);
|
||||
if(unit==null||!weightField(key))return label;
|
||||
return label.replace("(原单位)","("+unit+")").replace("(原单位)","("+unit+")");
|
||||
}
|
||||
|
||||
private static List<Map<String,Object>> historyColumns(Map<String,Object> section,List<Map<String,Object>> columns) {
|
||||
List<Map<String,Object>> visible=new ArrayList<>();
|
||||
boolean product=isProductSection(section);
|
||||
for(Map<String,Object> column:columns) {
|
||||
String key=text(column.get("key"),"");String lower=key.toLowerCase(Locale.ROOT);
|
||||
String label=text(column.get("label"),key);
|
||||
if(key.isBlank()||Set.of("key","groupkey","productkey","dimensionkey","factid","historyfactid",
|
||||
"evidenceref","queryid","sourceid","sourceturnid","attemptid","executionid",
|
||||
"snapshotid","scopeid","releaseid","factkey","scopekey").contains(lower)
|
||||
||lower.endsWith("factid")||lower.endsWith("queryid")||lower.endsWith("sourceid")
|
||||
||lower.endsWith("executionid")||lower.endsWith("attemptid")||lower.endsWith("snapshotid")
|
||||
||lower.endsWith("scopeid")||lower.endsWith("hash")
|
||||
||Set.of("分组键","产品键","规格键","来源ID","查询ID").contains(label))continue;
|
||||
var safe=new LinkedHashMap<>(column);
|
||||
if("label".equals(key)&&product)safe.put("label","规格");
|
||||
else if("label".equals(key)&&"group".equals(section.get("dimension")))safe.put("label","业务分组");
|
||||
visible.add(safe);
|
||||
}
|
||||
return List.copyOf(visible);
|
||||
}
|
||||
|
||||
private static boolean isProductSection(Map<String,Object> section) {
|
||||
String title=text(section.get("title"),"").toLowerCase(Locale.ROOT);
|
||||
return "product".equals(section.get("dimension"))||title.contains("product")
|
||||
||title.contains("规格")||title.contains("产品");
|
||||
}
|
||||
|
||||
private static String historySectionTitle(Map<String,Object> section,List<Map<String,Object>> columns) {
|
||||
String title=text(section.get("title"),"历史数据");
|
||||
String lower=title.toLowerCase(Locale.ROOT);
|
||||
boolean ranking=lower.matches("(?s).*\\btop\\s*\\d+\\b.*")||columns.stream().anyMatch(c->"rank".equals(c.get("key")));
|
||||
if(!ranking)return title.replace("product","产品").replace("Product","产品");
|
||||
var match=java.util.regex.Pattern.compile("(?i)\\btop\\s*(\\d+)\\b").matcher(title);
|
||||
int count=maps(section.get("rows")).size();
|
||||
if(match.find())try{int requested=Integer.parseInt(match.group(1));if(requested>0)count=requested;}catch(NumberFormatException ignored){}
|
||||
String noun=isProductSection(section)?"规格":"项";
|
||||
boolean weightRanking=columns.stream().anyMatch(c->Set.of("weight","scrapWeight","blackScrapWeight","brightScrapWeight").contains(c.get("key")));
|
||||
return "Top "+count+noun+"排名"+(weightRanking?"(按合计报废重量排序)":"");
|
||||
}
|
||||
|
||||
private static String historyColumnLabel(Map<String,Object> section,Map<String,Object> column,String unit) {
|
||||
String key=text(column.get("key"),"");
|
||||
Map<String,Object> localized=new LinkedHashMap<>(column);
|
||||
if("label".equals(key)&&isProductSection(section))localized.put("label","规格");
|
||||
else if("label".equals(key)&&"group".equals(section.get("dimension")))localized.put("label","业务分组");
|
||||
return displayColumnLabel(localized,unit);
|
||||
}
|
||||
|
||||
private static String historyScopeText(Map<String,Object> section,List<Map<String,Object>> sources) {
|
||||
List<String> parts=new ArrayList<>();String date=periodText(map(section.get("period")));
|
||||
if(!date.isBlank())parts.add(date);
|
||||
Object stage=section.get("stage");if(stage instanceof String value&&!value.isBlank())parts.add(stageLabel(value));
|
||||
Map<String,Object> filters=map(section.get("filters"));
|
||||
addScopePart(parts,"产业部",filters.get("department"));addScopePart(parts,"业务分组",filters.get("group"));
|
||||
addScopePart(parts,"规格",filters.get("product"));addScopePart(parts,"项目组",filters.get("projectGroup"));
|
||||
Map<String,Object> extra=map(filters.get("extraFilters"));
|
||||
for(String key:extra.keySet().stream().sorted().toList()) {
|
||||
String label=historyFieldLabel(section,sources,key);
|
||||
addScopePart(parts,label,extra.get(key));
|
||||
}
|
||||
Map<String,Object> exclusions=map(filters.get("exclusions"));
|
||||
for(String key:exclusions.keySet().stream().sorted().toList()) {
|
||||
String label=switch(key){case "department"->"产业部";case "group"->"业务分组";case "product"->"规格";case "projectGroup"->"项目组";default->"对象";};
|
||||
List<String> values=scopeLiterals(exclusions.get(key));if(!values.isEmpty())parts.add("排除"+label+" "+String.join("、",values));
|
||||
}
|
||||
if(Boolean.TRUE.equals(filters.get("includeUnassigned")))parts.add("包含未分配项");
|
||||
Map<String,Object> columnLabels=map(filters.get("columnLabels"));
|
||||
for(Object raw:AnalysisProtocolService.list(filters.get("columnFilters"))) {
|
||||
Map<String,Object> rule=map(raw);String field=text(rule.get("field"),"");
|
||||
String label=text(columnLabels.get(field),historyFieldLabel(section,sources,field));
|
||||
if(label.isBlank())label="授权列";
|
||||
List<String> prefix=strings(rule.get("stages")).stream().map(AnalysisProtocolHistoryFacts::stageLabel).toList();
|
||||
String operator=switch(text(rule.get("operator"),"")){case "in"->"包含";case "not_in"->"不含";case "is_empty"->"为空";case "not_empty"->"非空";default->"筛选";};
|
||||
List<String> values=scopeLiterals(rule.get("values"));
|
||||
StringBuilder part=new StringBuilder();if(!prefix.isEmpty())part.append(String.join("、",prefix)).append(' ');
|
||||
part.append(label).append(' ').append(operator);if(!values.isEmpty())part.append(' ').append(String.join("、",values));parts.add(part.toString());
|
||||
}
|
||||
return parts.isEmpty()?"全范围(无额外筛选)":String.join(" · ",parts);
|
||||
}
|
||||
|
||||
private static String historyFieldLabel(Map<String,Object> section,List<Map<String,Object>> sources,String field) {
|
||||
if(field.isBlank())return "授权列";
|
||||
Map<String,Object> filters=map(section.get("filters"));String label=text(map(filters.get("columnLabels")).get(field),"");
|
||||
if(!label.isBlank())return label;
|
||||
String sourceTurn=text(section.get("sourceTurnId"),"");
|
||||
for(Map<String,Object> source:sources) {
|
||||
if(!sourceTurn.equals(text(source.get("sourceTurnId"),text(source.get("turnId"),""))))continue;
|
||||
Map<String,Object> task=map(source.get("task"));label=text(map(task.get("sourceFieldLabels")).get(field),"");
|
||||
if(!label.isBlank())return label;
|
||||
}
|
||||
return switch(field){case "customerCode"->"客户代码";case "workOrder"->"委托单";case "orderNo"->"订单号";default->"授权列";};
|
||||
}
|
||||
|
||||
private static void addScopePart(List<String> parts,String label,Object raw) {
|
||||
List<String> values=scopeLiterals(raw);if(!values.isEmpty())parts.add(label+" "+String.join("、",values));
|
||||
}
|
||||
|
||||
private static List<String> scopeLiterals(Object raw) {
|
||||
List<?> values=raw instanceof List<?> list?list:List.of(raw==null?"":raw);
|
||||
return values.stream().filter(value->value instanceof String||value instanceof Number||value instanceof Boolean)
|
||||
.map(Objects::toString).map(String::trim).filter(value->!value.isBlank()).distinct().sorted().toList();
|
||||
}
|
||||
|
||||
private static String renderedValue(Map<String,Object> section,Map<String,Object> column,
|
||||
Map<String,Object> row,String unit) {
|
||||
String key=text(column.get("key"),"");Object value=row.get(key);
|
||||
String metric="value".equals(key)?text(row.get("metricCode"),""):key;
|
||||
if(value instanceof Number number&&ratioField(metric)) {
|
||||
try{return new BigDecimal(number.toString()).multiply(BigDecimal.valueOf(100))
|
||||
.setScale(2,RoundingMode.HALF_UP).toPlainString()+"%";}
|
||||
catch(NumberFormatException ignored) {return valueText(value);}
|
||||
}
|
||||
if(value instanceof Number number&&weightField(metric)&&unit!=null) {
|
||||
try{return new BigDecimal(number.toString()).stripTrailingZeros().toPlainString()+" "+unit;}
|
||||
catch(NumberFormatException ignored) {return valueText(value);}
|
||||
}
|
||||
return valueText(value);
|
||||
}
|
||||
|
||||
private static boolean ratioField(String metric) {
|
||||
return Set.of("rate","compositeRate","scrapRate","share","cumulativeShare").contains(metric);
|
||||
}
|
||||
|
||||
private static boolean weightField(String metric) {
|
||||
return Set.of("weight","totalWeight","scrapWeight","blackScrapWeight","brightScrapWeight",
|
||||
"blackTotalWeight","brightTotalWeight","blackWeight","brightWeight").contains(metric);
|
||||
}
|
||||
|
||||
private static String verifiedWeightUnit(Map<String,Object> section,List<Map<String,Object>> sources) {
|
||||
Map<String,Object> filters=map(section.get("filters"));
|
||||
Set<String> supported=Set.of("department","group","product","projectGroup","includeUnassigned");
|
||||
if(filters.keySet().stream().anyMatch(key->!supported.contains(key)))return null;
|
||||
Map<String,Object> expected=new LinkedHashMap<>();
|
||||
for(String key:List.of("department","group","product","projectGroup"))if(filters.containsKey(key))expected.put(key,filters.get(key));
|
||||
String sourceTurn=text(section.get("sourceTurnId"),"");Set<String> units=new HashSet<>();
|
||||
for(Map<String,Object> source:sources) {
|
||||
String turn=text(source.get("sourceTurnId"),text(source.get("turnId"),""));
|
||||
if(!sourceTurn.equals(turn))continue;
|
||||
for(Map<String,Object> chart:maps(source.get("sourceVisuals"))) {
|
||||
Map<String,Object> chartScope=map(chart.get("scope"));
|
||||
Map<String,Object> chartFilters=new LinkedHashMap<>();
|
||||
for(String key:List.of("department","group","product","projectGroup"))if(chartScope.containsKey(key))chartFilters.put(key,chartScope.get(key));
|
||||
String stage=text(section.get("stage"),"overall");Object chartStage=chart.get("stage");
|
||||
if(!Objects.equals(chart.get("originTurnId"),sourceTurn)||!Objects.equals(chart.get("period"),section.get("period"))
|
||||
||!Objects.equals(chartFilters,expected)||chartScope.get("includeUnassigned")!=null
|
||||
&&!Objects.equals(chartScope.get("includeUnassigned"),filters.getOrDefault("includeUnassigned",false))
|
||||
||!"product".equals(chart.get("dimension"))
|
||||
||chartStage!=null&&!(Objects.equals(chartStage,stage)||Objects.equals(chartStage,"overall"))
|
||||
||!Set.of("current_scrap_weight","black_scrap_weight","bright_scrap_weight","scrapWeight","weight").contains(Objects.toString(chart.get("metric"),"")))continue;
|
||||
String unit=text(chart.get("unit"),"");if(Set.of("kg","g","mg","ton").contains(unit))units.add(unit);
|
||||
}
|
||||
}
|
||||
return units.size()==1?units.iterator().next():null;
|
||||
}
|
||||
|
||||
private static String renderedNote(Map<String,Object> section,String unit,boolean historical) {
|
||||
String note=text(section.get("note"),"");
|
||||
if(historical&&hasHistoryInternalMetadata(note))return "";
|
||||
return unit==null?note:note.replace("重量沿用来源单位","重量单位为 "+unit);
|
||||
}
|
||||
|
||||
private static boolean hasHistoryInternalMetadata(String value) {
|
||||
return value!=null&&HISTORY_INTERNAL_METADATA.matcher(value).find();
|
||||
}
|
||||
|
||||
private static void appendHtmlTables(StringBuilder out, List<Map<String,Object>> columns, List<Map<String,Object>> rows) {
|
||||
appendHtmlTables(out, columns, rows, List.of());
|
||||
}
|
||||
@@ -789,7 +1123,7 @@ final class AnalysisProtocolHistoryFacts {
|
||||
return String.join(" · ", parts);
|
||||
}
|
||||
private static String fieldLabel(String field) { return FIELD_LABELS.getOrDefault(field, field); }
|
||||
private static String stageLabel(String stage) { return "black".equals(stage) ? "黑片" : "bright".equals(stage) ? "亮片" : stage; }
|
||||
private static String stageLabel(String stage) { return "black".equals(stage) ? "黑片" : "bright".equals(stage) ? "亮片" : "overall".equals(stage) ? "综合" : stage; }
|
||||
private static String counts(Map<String,Object> row) { return valueText(row.get("presentRows")) + "/" + valueText(row.get("missingRows")) + "/" + valueText(row.get("invalidRows")) + "/" + valueText(row.get("negativeRows")); }
|
||||
private static String rankBoundary(int limit, int shown) { return Integer.toString(limit > 0 ? limit : shown); }
|
||||
private static void addFact(List<Map<String,Object>> facts, String turnId, int[] sequence, String text) {
|
||||
@@ -797,7 +1131,7 @@ final class AnalysisProtocolHistoryFacts {
|
||||
}
|
||||
private static void displayFact(List<Map<String,Object>> facts,int sectionIndex,int rowIndex,Map<String,Object> period,
|
||||
Map<String,Object> filters,String stage,String metric,Object raw,String displayText) {
|
||||
var fact=facts.getLast();fact.putAll(displayMetric(metric,raw));fact.put("sectionIndex",sectionIndex);fact.put("rowIndex",rowIndex);
|
||||
var fact=facts.getLast();fact.putAll(displayMetric(metric,raw));fact.put("sectionIndex",sectionIndex);if(rowIndex>=0)fact.put("rowIndex",rowIndex);
|
||||
var scope=mapOf("period",period,"filters",filters,"stage",stage.isBlank()?"overall":stage);fact.put("scope",scope);
|
||||
String original=text(fact.get("text"),"");String token=valueText(raw);
|
||||
fact.put("displayText",displayText!=null?displayText:raw==null?original:original.replace(" = "+token," = "+displayValue(metric,raw)));
|
||||
|
||||
+13
-10
@@ -7,41 +7,42 @@ import java.util.regex.Pattern;
|
||||
final class AnalysisProtocolHistorySummary {
|
||||
private static final Pattern BRACKET=Pattern.compile("\\[([^\\[\\]\\r\\n]{1,240})\\]");
|
||||
private static final Pattern ACTIVE_HTML=Pattern.compile("(?is)<(script|style|iframe|object|embed)\\b[^>]*>.*?</\\1\\s*>");
|
||||
private static final Pattern INTERNAL=Pattern.compile("(?i)(?:history-fact:|Q[A-F0-9]{8}|[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}|(?:queryId|sourceTurnId|attemptId|executionId|snapshotHash|scopeHash)\\s*[:=]|filters\\s*=|\\{\\s*\\\")");
|
||||
record Result(String answer,Map<String,Object> summary,List<String> refs,List<String> gaps){}
|
||||
private record Text(String value,List<String> refs,boolean filtered){}
|
||||
static Result normalize(Map<String,Object> details,Set<String> known,String fallback) {
|
||||
var raw=AnalysisProtocolService.map(details.get("readingSummary"));var gaps=new LinkedHashSet<String>();
|
||||
boolean shape=Objects.equals(raw.get("version"),1)&&raw.get("lead") instanceof String&&raw.get("evidence") instanceof List<?>&&raw.get("notices") instanceof List<?>&&raw.get("evidenceRefs") instanceof List<?>;
|
||||
if(!shape)gaps.add("历史解释摘要格式未通过核验,保留完整数据附录。");
|
||||
if(!shape)gaps.add("历史解释摘要格式未通过核验。");
|
||||
var used=new LinkedHashSet<String>();var summaryRefs=new LinkedHashSet<String>();
|
||||
var lead=clean(shape?raw.get("lead"):fallback,known,12000);summaryRefs.addAll(lead.refs());
|
||||
var evidence=new ArrayList<String>();var notices=new LinkedHashSet<String>();
|
||||
if(lead.filtered())gaps.add("历史解释中的未知引用或HTML已过滤,相关结论仍需核验。");
|
||||
if(lead.filtered())gaps.add("部分解释内容未通过引用或格式核验,已过滤。");
|
||||
if(shape) {
|
||||
collect(raw.get("evidence"),evidence,summaryRefs,gaps,known,20);
|
||||
var noticeList=new ArrayList<String>();collect(raw.get("notices"),noticeList,summaryRefs,gaps,known,40);notices.addAll(noticeList);
|
||||
var offered=AnalysisProtocolService.list(raw.get("evidenceRefs"));
|
||||
if(offered.size()>100||offered.stream().anyMatch(id->!(id instanceof String)||!known.contains(id)))gaps.add("历史解释中的未知引用或HTML已过滤,相关结论仍需核验。");
|
||||
if(offered.size()>100||offered.stream().anyMatch(id->!(id instanceof String)||!known.contains(id)))gaps.add("部分解释内容未通过引用或格式核验,已过滤。");
|
||||
}
|
||||
used.addAll(summaryRefs);
|
||||
var paragraphs=new ArrayList<String>();Object rawAnswer=details.get("answer");
|
||||
if(rawAnswer instanceof String text&&text.length()<=64000)for(String paragraph:text.split("\\n\\s*\\n")) {
|
||||
var cleaned=clean(paragraph,known,12000);if(!cleaned.value().isBlank()){paragraphs.add(cleaned.value());used.addAll(cleaned.refs());}
|
||||
if(cleaned.filtered())gaps.add("历史解释中的未知引用或HTML已过滤,相关结论仍需核验。");
|
||||
if(cleaned.filtered())gaps.add("部分解释内容未通过引用或格式核验,已过滤。");
|
||||
}
|
||||
if(paragraphs.isEmpty())gaps.add("历史解释正文尚未形成有效内容,保留完整数据附录。");
|
||||
if(!known.isEmpty()&&used.isEmpty())gaps.add("历史解释没有绑定已核验事实,保留完整数据附录。");
|
||||
if(known.isEmpty())gaps.add("历史快照没有可用于解释的事实,保留完整数据附录。");
|
||||
if(paragraphs.isEmpty())gaps.add("历史解释正文未形成有效内容。");
|
||||
if(!known.isEmpty()&&used.isEmpty())gaps.add("历史解释未绑定已核验事实。");
|
||||
if(known.isEmpty())gaps.add("历史快照没有可用于解释的已核验事实。");
|
||||
notices.addAll(gaps);
|
||||
var summary=new LinkedHashMap<String,Object>();summary.put("version",1);summary.put("lead",lead.value().isBlank()?fallback:lead.value());summary.put("evidence",List.copyOf(evidence));summary.put("notices",List.copyOf(notices));summary.put("evidenceRefs",List.copyOf(summaryRefs));
|
||||
return new Result(paragraphs.isEmpty()?fallback:String.join("\n\n",paragraphs),summary,List.copyOf(used),List.copyOf(gaps));
|
||||
}
|
||||
private static void collect(Object raw,List<String> target,Set<String> refs,Set<String> gaps,Set<String> known,int max) {
|
||||
var values=AnalysisProtocolService.list(raw);
|
||||
if(values.size()>max)gaps.add("历史解释摘要超过展示上限,已保留有限内容与完整附录。");
|
||||
if(values.size()>max)gaps.add("历史解释摘要超过展示上限,已保留可核验内容。");
|
||||
for(Object value:values.stream().limit(max).toList()) {
|
||||
var cleaned=clean(value,known,12000);if(!cleaned.value().isBlank()){target.add(cleaned.value());refs.addAll(cleaned.refs());}
|
||||
if(cleaned.filtered())gaps.add("历史解释中的未知引用或HTML已过滤,相关结论仍需核验。");
|
||||
if(cleaned.filtered())gaps.add("部分解释内容未通过引用或格式核验,已过滤。");
|
||||
}
|
||||
}
|
||||
private static Text clean(Object raw,Set<String> known,int limit) {
|
||||
@@ -51,6 +52,8 @@ final class AnalysisProtocolHistorySummary {
|
||||
while(matcher.find())if(known.contains(matcher.group(1)))refs.add(matcher.group(1));else unknown=true;
|
||||
// Do not retain an unsupported assertion after merely hiding its invented citation.
|
||||
if(unknown)return new Text("",List.of(),true);
|
||||
return new Text(safe.strip(),List.copyOf(refs),filtered);
|
||||
String visible=BRACKET.matcher(safe).replaceAll("");
|
||||
if(INTERNAL.matcher(visible).find())return new Text("",List.of(),true);
|
||||
return new Text(visible.strip(),List.copyOf(refs),filtered);
|
||||
}
|
||||
}
|
||||
|
||||
+42
-8
@@ -47,7 +47,9 @@ final class AnalysisProtocolMetricCoverage {
|
||||
&& STAGES.containsAll(stages)
|
||||
&& fields.stream().distinct().count() == fields.size()
|
||||
&& stages.stream().distinct().count() == stages.size()
|
||||
&& (stages.isEmpty() ? fields.equals(List.of("rate")) : true);
|
||||
&& (stages.isEmpty() ? fields.equals(List.of("rate"))
|
||||
|| !trendRequired && !AnalysisProtocolService.list(task.get("dimensions")).isEmpty()
|
||||
&& fields.contains("scrapWeight") && Set.of("rate","scrapWeight").containsAll(fields) : true);
|
||||
if (!taskValid) {
|
||||
missing.add("临时指标任务字段或期间覆盖模式无效。");
|
||||
result.put("availableMonths", List.of());
|
||||
@@ -87,7 +89,10 @@ final class AnalysisProtocolMetricCoverage {
|
||||
missing.add("综合合格率没有可核验的已发布来源月份。");
|
||||
continue;
|
||||
}
|
||||
if (!validCompositeRate(summary, months, coverage))
|
||||
if ("scrapWeight".equals(field)) {
|
||||
if (!validCombinedScrapWeight(summary, months, coverage))
|
||||
missing.add("合计报废重量正式值或双阶段来源覆盖无效。");
|
||||
} else if (!validCompositeRate(summary, months, coverage))
|
||||
missing.add("综合合格率正式值或双阶段来源覆盖无效。");
|
||||
continue;
|
||||
}
|
||||
@@ -111,7 +116,7 @@ final class AnalysisProtocolMetricCoverage {
|
||||
BigDecimal value = decimal(values.get(field));
|
||||
if (value == null) return false;
|
||||
if ("rate".equals(field))
|
||||
return inRange(value, BigDecimal.ZERO, BigDecimal.ONE)
|
||||
return nonNegative(value)
|
||||
&& positive(decimal(values.get("totalWeight")));
|
||||
if ("scrapRate".equals(field))
|
||||
return positive(decimal(values.get("totalWeight")))
|
||||
@@ -124,7 +129,7 @@ final class AnalysisProtocolMetricCoverage {
|
||||
private static boolean validCompositeRate(Map<String, Object> summary, List<Integer> months,
|
||||
MonthCoverage coverage) {
|
||||
BigDecimal value = decimal(summary.get("compositeRate"));
|
||||
if (!inRange(value, BigDecimal.ZERO, BigDecimal.ONE)) return false;
|
||||
if (!nonNegative(value)) return false;
|
||||
boolean blackSource = false, brightSource = false;
|
||||
for (int month : months) {
|
||||
Map<String, Object> item = coverage.byMonth().get(month);
|
||||
@@ -143,6 +148,39 @@ final class AnalysisProtocolMetricCoverage {
|
||||
|| (brightSource && positive(decimal(bright.get("totalWeight"))));
|
||||
}
|
||||
|
||||
private static boolean validCombinedScrapWeight(Map<String, Object> summary, List<Integer> months,
|
||||
MonthCoverage coverage) {
|
||||
if (months.isEmpty()) return false;
|
||||
Map<String, Object> summaryCoverage = map(summary.get("coverage"));
|
||||
Map<String, Long> stageSources = new LinkedHashMap<>();
|
||||
for (String stage : STAGES) stageSources.put(stage, 0L);
|
||||
for (int month : months) {
|
||||
Map<String,Object> item = coverage.byMonth().get(month);
|
||||
Long black = item == null ? null : count(item.get("blackRows"));
|
||||
Long bright = item == null ? null : count(item.get("brightRows"));
|
||||
if (black == null || bright == null || black < 0 || bright < 0 || black == 0 && bright == 0) return false;
|
||||
stageSources.put("black", stageSources.get("black") + black);
|
||||
stageSources.put("bright", stageSources.get("bright") + bright);
|
||||
}
|
||||
for (String stage : STAGES) {
|
||||
Map<String, Object> stageSummary = map(summary.get(stage));
|
||||
boolean hasValue = stageSummary.containsKey("scrapWeight");
|
||||
BigDecimal value = decimal(stageSummary.get("scrapWeight"));
|
||||
long monthlyRows = stageSources.get(stage);
|
||||
Long publishedRows = count(summaryCoverage.get(stage + "Rows"));
|
||||
if (publishedRows != null && publishedRows != monthlyRows) return false;
|
||||
if (monthlyRows > 0) {
|
||||
if (!hasValue || !nonNegative(value) || publishedRows != null && publishedRows == 0) return false;
|
||||
} else {
|
||||
// A missing metric is acceptable only when both the period-level and
|
||||
// summary-level coverage explicitly prove that this stage had no rows.
|
||||
if (hasValue && (!nonNegative(value) || value.signum() != 0)) return false;
|
||||
if (!hasValue && (publishedRows == null || publishedRows != 0)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void inspectTrend(Map<String, Object> response, List<String> fields, List<String> stages,
|
||||
Period period, MonthCoverage coverage, boolean complete,
|
||||
List<String> missing) {
|
||||
@@ -425,10 +463,6 @@ final class AnalysisProtocolMetricCoverage {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean inRange(BigDecimal value, BigDecimal minimum, BigDecimal maximum) {
|
||||
return value != null && value.compareTo(minimum) >= 0 && value.compareTo(maximum) <= 0;
|
||||
}
|
||||
|
||||
private static boolean positive(BigDecimal value) { return value != null && value.signum() > 0; }
|
||||
private static boolean nonNegative(BigDecimal value) { return value != null && value.signum() >= 0; }
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.yunsheng.quality.analysis;
|
||||
|
||||
import cn.yunsheng.quality.common.SelectionValues;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
@@ -39,7 +40,8 @@ final class AnalysisProtocolQueryPlan {
|
||||
if(!rawPurpose&&node.containsKey("sourceFields"))throw invalid("sourceFields 只能用于 source/records 节点");
|
||||
Map<String,List<String>> selectedSourceFields=rawPurpose&&node.containsKey("sourceFields")
|
||||
?normalizeSourceFields(node.get("sourceFields"),stages,catalog):Map.of();
|
||||
if(stages.isEmpty()&&!fields.equals(List.of("rate"))&&!changesPurpose)throw invalid("重量和报废率节点必须明确阶段,不能自行合并黑片亮片");
|
||||
boolean combinedRanking="ranking".equals(purpose)&&stages.isEmpty()&&fields.contains("scrapWeight")&&Set.of("rate","scrapWeight").containsAll(fields);
|
||||
if(stages.isEmpty()&&!fields.equals(List.of("rate"))&&!changesPurpose&&!combinedRanking)throw invalid("重量和报废率节点必须明确阶段;合计排名仅支持报废总重及综合合格率");
|
||||
boolean compare=bool(node.getOrDefault("comparisonRequired",false));
|
||||
if(compare&&(!Set.of("metrics","source","records","defects","changes").contains(purpose)||!(task.get("comparisonPeriod") instanceof Map<?,?>)))throw invalid("期间比较节点需要两个已确认期间");
|
||||
if("trend".equals(purpose)&&map(task.get("period")).containsKey("month"))throw invalid("月度趋势需要多个月份范围");
|
||||
@@ -71,7 +73,8 @@ final class AnalysisProtocolQueryPlan {
|
||||
String stage=string(selector.get("stage")),metric=string(selector.get("metric"));
|
||||
count=integer(selector.get("count"),1,10);
|
||||
if(ranking==null||!"ranking".equals(ranking.get("purpose"))||!"product".equals(ranking.get("dimension"))
|
||||
||!"base".equals(map(ranking.get("selector")).get("kind"))||!list(ranking.get("stages")).contains(stage)
|
||||
||!"base".equals(map(ranking.get("selector")).get("kind"))||!(list(ranking.get("stages")).contains(stage)
|
||||
||list(ranking.get("stages")).isEmpty()&&"overall".equals(stage)&&"scrapWeight".equals(metric))
|
||||
||!list(ranking.get("fields")).contains(metric)||count>((Number)ranking.get("limit")).intValue())throw invalid("规格下钻必须依赖前面已声明的实际产品排名");
|
||||
requestedDimensions.add("product");cleanSelector.put("sourceNodeId",source);cleanSelector.put("stage",stage);cleanSelector.put("metric",metric);cleanSelector.put("count",count);
|
||||
}
|
||||
@@ -104,7 +107,7 @@ final class AnalysisProtocolQueryPlan {
|
||||
clean.put("selector",cleanSelector);clean.put("comparisonRequired",compare);clean.put("visualPreference",preference);clean.put("visualType",visual);
|
||||
if("ranking".equals(purpose)) {
|
||||
String dimension=string(node.get("dimension"));
|
||||
if(!DIMENSIONS.contains(dimension)||!WEIGHTS.containsAll(fields)||stages.isEmpty()||compare||!kind.equals("base"))throw invalid("排名节点需要明确阶段、重量和基础分组");
|
||||
if(!DIMENSIONS.contains(dimension)||!(combinedRanking||WEIGHTS.containsAll(fields)&&!stages.isEmpty())||compare||!kind.equals("base"))throw invalid("排名节点需要正式重量和基础分组;合计口径使用空阶段");
|
||||
requestedDimensions.add(dimension);clean.put("dimension",dimension);clean.put("limit",integer(node.getOrDefault("limit",10),1,10));
|
||||
} else if(changesPurpose) {
|
||||
String dimension=string(node.get("dimension")),sort=string(node.get("sort"));
|
||||
@@ -121,7 +124,7 @@ final class AnalysisProtocolQueryPlan {
|
||||
if("product".equals(query.get("dimension")))requestedDimensions.add("product");
|
||||
} else if(node.containsKey("dimension")||node.containsKey("limit"))throw invalid("指标或趋势节点不能伪装为分组排名");
|
||||
int periodCount=compare?2:1;
|
||||
int readsPerScope=changesPurpose?1:"ranking".equals(purpose)?fields.size()*stages.size():rawPurpose||defectPurpose?stages.size()*periodCount:periodCount;
|
||||
int readsPerScope=changesPurpose||combinedRanking?1:"ranking".equals(purpose)?fields.size()*stages.size():rawPurpose||defectPurpose?stages.size()*periodCount:periodCount;
|
||||
scopeBound+=count;queryBound+=count*readsPerScope;
|
||||
if(scopeBound>20||queryBound>40)throw invalid("本轮查询对象或正式读取超过有界上限,请拆分任务");
|
||||
trend|="trend".equals(purpose);comparison|=compare;
|
||||
@@ -244,7 +247,7 @@ final class AnalysisProtocolQueryPlan {
|
||||
private Map<String,Object> childTask(Map<String,Object> parent,Map<String,Object> node,Map<String,Object> filters,Map<String,Object> catalog) {
|
||||
Map<String,Object> child=new LinkedHashMap<>(parent);child.remove("queryPlan");child.remove("comparisonMode");
|
||||
String purpose=string(node.get("purpose"));boolean ranking="ranking".equals(purpose),changes="changes".equals(purpose),defects="defects".equals(purpose),source="source".equals(purpose),records="records".equals(purpose),compare=Boolean.TRUE.equals(node.get("comparisonRequired"));
|
||||
child.put("kind",source?"source_verify":records?"details":ranking||defects||changes?"analysis":"metrics");child.put("filters",filters);child.put("fields",node.get("fields"));child.put("stages",node.get("stages"));
|
||||
child.put("kind",source?"source_verify":records?"details":singleObjectMetricRanking(parent,node,filters)?"metrics":ranking||defects||changes?"analysis":"metrics");child.put("filters",filters);child.put("fields",node.get("fields"));child.put("stages",node.get("stages"));
|
||||
child.put("dimensions",ranking?List.of(node.get("dimension")):List.of());child.put("dimensionStages",ranking?Map.of(string(node.get("dimension")),node.get("stages")):Map.of());
|
||||
child.remove("defectQuery");child.remove("changeQuery");
|
||||
if(changes){child.put("dimensions",List.of(node.get("dimension")));child.put("dimensionStages",Map.of(string(node.get("dimension")),node.get("stages")));child.put("changeQuery",Map.of("dimension",node.get("dimension"),"sort",node.get("sort"),"limit",node.get("limit")));}
|
||||
@@ -275,6 +278,35 @@ final class AnalysisProtocolQueryPlan {
|
||||
}
|
||||
return child;
|
||||
}
|
||||
|
||||
/**
|
||||
* A confirmed one-object metrics request may use the ranking endpoint only
|
||||
* to obtain combined scrap weight and composite rate. Keep that bounded
|
||||
* scalar intent on the child task so the visual gate does not mistake the
|
||||
* transport operator for a requested Top-N presentation.
|
||||
*/
|
||||
private boolean singleObjectMetricRanking(Map<String,Object> parent,Map<String,Object> node,Map<String,Object> filters) {
|
||||
if(!"metrics".equals(parent.get("kind"))||!"ranking".equals(node.get("purpose"))
|
||||
||!"auto".equals(node.get("visualPreference"))||!"auto".equals(parent.get("visualPreference"))
|
||||
||Boolean.TRUE.equals(parent.get("comparisonRequired"))||Boolean.TRUE.equals(node.get("comparisonRequired"))
|
||||
||Boolean.TRUE.equals(parent.get("trendRequired"))||parent.get("comparisonPeriod") instanceof Map<?,?>
|
||||
||!list(parent.get("stages")).isEmpty()||!list(node.get("stages")).isEmpty()
|
||||
||!new HashSet<>(list(parent.get("fields"))).equals(Set.of("rate","scrapWeight"))
|
||||
||!new HashSet<>(list(node.get("fields"))).equals(Set.of("rate","scrapWeight"))
|
||||
||!DIMENSIONS.contains(node.get("dimension"))||!Objects.equals(node.get("limit"),1)
|
||||
||!"base".equals(map(node.get("selector")).get("kind")))return false;
|
||||
String dimension=string(node.get("dimension"));
|
||||
if(!new HashSet<>(list(parent.get("dimensions"))).equals(Set.of(dimension)))return false;
|
||||
Object parentObject=map(parent.get("filters")).get(dimension),childObject=filters.get(dimension);
|
||||
List<String> parentValues=singleSelectionValues(parentObject),childValues=singleSelectionValues(childObject);
|
||||
if(parentValues.size()!=1||childValues.size()!=1||!parentValues.equals(childValues))return false;
|
||||
List<?> plan=list(parent.get("queryPlan"));
|
||||
return plan.size()==1&&Objects.equals(map(plan.getFirst()).get("nodeId"),node.get("nodeId"));
|
||||
}
|
||||
private List<String> singleSelectionValues(Object wire) {
|
||||
if(!(wire instanceof String value)||value.isBlank())return List.of();
|
||||
try{return SelectionValues.parse(value);}catch(RuntimeException invalid){return List.of();}
|
||||
}
|
||||
private static Map<String,List<String>> normalizeSourceFields(Object raw,List<String> stages,Map<String,Object> catalog) {
|
||||
if(!(raw instanceof Map<?,?> selected)||selected.isEmpty()||selected.size()>stages.size())throw invalid("sourceFields 必须按阶段选择授权源字段");
|
||||
Map<String,Map<String,Object>> available=sourceFieldCatalog(catalog);Map<String,List<String>> result=new LinkedHashMap<>();
|
||||
|
||||
@@ -10,9 +10,82 @@ final class AnalysisProtocolRankArtifact {
|
||||
List<Map<String,Object>> rankings=new ArrayList<>();
|
||||
Map<String,Object> perDimension=AnalysisProtocolService.map(task.get("dimensionStages"));
|
||||
List<?> stages=AnalysisProtocolService.list(perDimension.getOrDefault(dimension,task.get("stages")));
|
||||
if(stages.isEmpty()&&AnalysisProtocolRankingCoverage.complete(response,task,dimension)) {
|
||||
boolean compositeRateAvailable=AnalysisProtocolService.list(task.get("fields")).contains("rate")
|
||||
&&Boolean.TRUE.equals(AnalysisProtocolMetricCoverage.inspect(response,task,
|
||||
AnalysisProtocolService.map(task.get("period"))).get("metricsComplete"));
|
||||
List<Map<String,Object>> positive=new ArrayList<>();
|
||||
for(Object raw:AnalysisProtocolService.list(response.get("rows"))) {
|
||||
Map<String,Object> row=AnalysisProtocolService.map(raw),coverage=AnalysisProtocolService.map(row.get("coverage"));BigDecimal sum=BigDecimal.ZERO;
|
||||
Map<String,Object> measures=new LinkedHashMap<>();
|
||||
for(String stage:List.of("black","bright")) {
|
||||
Object value=AnalysisProtocolService.map(row.get(stage)).get("scrapWeight");
|
||||
boolean observed=number(coverage.get(stage+"Rows"))>0;
|
||||
if(value instanceof Number number) {
|
||||
BigDecimal component=new BigDecimal(number.toString());sum=sum.add(component);
|
||||
measures.put(stage+"ScrapWeight",component);
|
||||
} else measures.put(stage+"ScrapWeight",null);
|
||||
Object scale=AnalysisProtocolService.map(row.get(stage)).get("totalWeight");
|
||||
measures.put(stage+"TotalWeight",observed&&scale instanceof Number number?new BigDecimal(number.toString()):null);
|
||||
}
|
||||
Object compositeRate=row.get("compositeRate");
|
||||
if(sum.signum()>0) {
|
||||
var ranked=new LinkedHashMap<String,Object>();ranked.put("key",row.get("key"));ranked.put("label",Objects.toString(row.get("label"),row.get("key").toString()));ranked.put("weight",sum);
|
||||
boolean rateSource=false;
|
||||
for(String stage:List.of("black","bright")) {
|
||||
Object stageRows=coverage.get(stage+"Rows"),stageWeight=measures.get(stage+"TotalWeight");
|
||||
rateSource|=stageRows instanceof Number count&&count.intValue()>0&&stageWeight instanceof Number value&&new BigDecimal(value.toString()).signum()>0;
|
||||
}
|
||||
if(compositeRateAvailable&&rateSource) {
|
||||
BigDecimal officialRate=decimal(compositeRate);
|
||||
if(officialRate!=null&&officialRate.signum()>=0)ranked.put("compositeRate",officialRate);
|
||||
}
|
||||
ranked.putAll(measures);positive.add(ranked);
|
||||
}
|
||||
}
|
||||
positive.sort((a,b)->{int weight=((BigDecimal)b.get("weight")).compareTo((BigDecimal)a.get("weight"));return weight!=0?weight:codePointCompare(a.get("key").toString(),b.get("key").toString());});
|
||||
var summary=AnalysisProtocolService.map(response.get("summary"));BigDecimal denominator=BigDecimal.ZERO;
|
||||
for(String stage:List.of("black","bright")) {
|
||||
Object published=AnalysisProtocolService.map(summary.get(stage)).get("scrapWeight");
|
||||
if(published==null)continue; // RankingCoverage proved this stage has no source rows.
|
||||
BigDecimal formal=decimal(published);
|
||||
if(formal==null||formal.signum()<0)return Map.of();
|
||||
denominator=denominator.add(formal);
|
||||
}
|
||||
if(denominator.signum()==0) {
|
||||
Map<String,Object> empty=emptyRanking("overall",BigDecimal.ZERO);
|
||||
if(compositeRateAvailable) {
|
||||
BigDecimal officialRate=decimal(summary.get("compositeRate"));
|
||||
if(officialRate!=null&&officialRate.signum()>=0)empty.put("compositeRate",officialRate);
|
||||
}
|
||||
rankings.add(empty);
|
||||
return Map.of("version",1,"releaseId",release,"dimension",dimension,"period",task.get("period"),"filters",task.get("filters"),"rankings",rankings);
|
||||
}
|
||||
if(denominator.signum()<0)return Map.of();
|
||||
List<Map<String,Object>> rows=new ArrayList<>();
|
||||
BigDecimal cumulative=BigDecimal.ZERO;
|
||||
for(int i=0;i<Math.min(10,positive.size());i++) {
|
||||
var row=new LinkedHashMap<>(positive.get(i));cumulative=cumulative.add((BigDecimal)row.get("weight"));
|
||||
row.put("rank",i+1);row.put("share",((BigDecimal)row.get("weight")).divide(denominator,java.math.MathContext.DECIMAL128));
|
||||
row.put("cumulativeShare",cumulative.divide(denominator,java.math.MathContext.DECIMAL128));rows.add(row);
|
||||
}
|
||||
Map<String,Object> combined=new LinkedHashMap<>();combined.put("stage","overall");combined.put("metric","scrapWeight");combined.put("rows",rows);
|
||||
if(compositeRateAvailable) {
|
||||
BigDecimal officialRate=decimal(summary.get("compositeRate"));
|
||||
if(officialRate!=null&&officialRate.signum()>=0)combined.put("compositeRate",officialRate);
|
||||
}
|
||||
combined.put("totalRows",positive.size());combined.put("returnedRows",rows.size());combined.put("truncated",positive.size()>rows.size());
|
||||
combined.put("denominator",denominator);combined.put("completeRanking",true);combined.put("orderPolicy","weight_desc_key_codepoint_asc");rankings.add(combined);
|
||||
}
|
||||
for(Object stage:stages)for(Object field:AnalysisProtocolService.list(task.get("fields"))) {
|
||||
Map<String,Object> one=new LinkedHashMap<>(task);one.put("stages",List.of(stage));one.put("fields",List.of(field));one.put("dimensionStages",Map.of(dimension,List.of(stage)));
|
||||
if(!AnalysisProtocolRankingCoverage.complete(response,one,dimension))continue;
|
||||
Object rawDenominator=AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get(stage)).get(field);
|
||||
BigDecimal denominator=decimal(rawDenominator);
|
||||
if("scrapWeight".equals(field)&&denominator!=null&&denominator.signum()==0) {
|
||||
rankings.add(emptyRanking(stage,denominator));
|
||||
continue;
|
||||
}
|
||||
List<Map<String,Object>> positive=new ArrayList<>();BigDecimal negative=BigDecimal.ZERO;int negativeCount=0;
|
||||
for(Object raw:AnalysisProtocolService.list(response.get("rows"))) {
|
||||
Map<String,Object> row=AnalysisProtocolService.map(raw);Object value=AnalysisProtocolService.map(row.get(stage)).get(field);
|
||||
@@ -36,4 +109,18 @@ final class AnalysisProtocolRankArtifact {
|
||||
while(i<a.length()&&j<b.length()){int x=a.codePointAt(i),y=b.codePointAt(j);if(x!=y)return Integer.compare(x,y);i+=Character.charCount(x);j+=Character.charCount(y);}
|
||||
return Boolean.compare(i<a.length(),j<b.length());
|
||||
}
|
||||
private static int number(Object raw) {
|
||||
if(!(raw instanceof Number value))return 0;
|
||||
try{return new BigDecimal(value.toString()).intValueExact();}catch(RuntimeException invalid){return 0;}
|
||||
}
|
||||
private static BigDecimal decimal(Object raw) {
|
||||
if(!(raw instanceof Number value))return null;
|
||||
try{return new BigDecimal(value.toString());}catch(RuntimeException invalid){return null;}
|
||||
}
|
||||
private static Map<String,Object> emptyRanking(Object stage,BigDecimal denominator) {
|
||||
Map<String,Object> empty=new LinkedHashMap<>();empty.put("stage",stage);empty.put("metric","scrapWeight");empty.put("rows",List.of());
|
||||
empty.put("totalRows",0);empty.put("returnedRows",0);empty.put("truncated",false);empty.put("denominator",denominator);
|
||||
empty.put("completeRanking",true);empty.put("emptyReason","no_positive_scrap");empty.put("orderPolicy","weight_desc_key_codepoint_asc");
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
+87
-4
@@ -8,27 +8,51 @@ final class AnalysisProtocolRankingCoverage {
|
||||
private AnalysisProtocolRankingCoverage() {}
|
||||
|
||||
static boolean complete(Map<String,Object> response,Map<String,Object> task,String dimension) {
|
||||
return complete(response,task,dimension,false);
|
||||
}
|
||||
private static boolean complete(Map<String,Object> response,Map<String,Object> task,String dimension,boolean allowZeroScrap) {
|
||||
List<?> rows=AnalysisProtocolService.list(response.get("rows"));
|
||||
Object raw=AnalysisProtocolService.map(response.get("context")).getOrDefault("rowCount",rows.size());
|
||||
if(!(raw instanceof Number n)||n.doubleValue()!=n.longValue()||n.longValue()!=rows.size()||rows.size()>100000)return false;
|
||||
Map<String,Object> perDimension=AnalysisProtocolService.map(task.get("dimensionStages"));
|
||||
List<?> stages=AnalysisProtocolService.list(perDimension.getOrDefault(dimension,task.get("stages")));
|
||||
if(stages.isEmpty())return false;
|
||||
if(stages.isEmpty()) {
|
||||
List<?> fields=AnalysisProtocolService.list(task.get("fields"));
|
||||
if(!fields.contains("scrapWeight")||!Set.of("rate","scrapWeight").containsAll(fields))return false;
|
||||
Map<String,Object> expanded=new LinkedHashMap<>(task);
|
||||
expanded.put("stages",List.of("black","bright"));expanded.put("fields",List.of("scrapWeight"));
|
||||
expanded.put("dimensionStages",Map.of(dimension,List.of("black","bright")));
|
||||
BigDecimal black=number(AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get("black")).get("scrapWeight"));
|
||||
BigDecimal bright=number(AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get("bright")).get("scrapWeight"));
|
||||
boolean blackAbsent=knownNoSourceStage(response,"black");
|
||||
boolean brightAbsent=knownNoSourceStage(response,"bright");
|
||||
if(black==null&&blackAbsent)black=BigDecimal.ZERO;
|
||||
if(bright==null&&brightAbsent)bright=BigDecimal.ZERO;
|
||||
if(black==null||bright==null||black.signum()<0||bright.signum()<0||blackAbsent&&brightAbsent)return false;
|
||||
if(black.add(bright).signum()>0)return complete(response,expanded,dimension,true);
|
||||
return hasObservedStageRows(response)&&complete(response,expanded,dimension,true);
|
||||
}
|
||||
Set<Object> keys=new HashSet<>();
|
||||
Map<String,Object> summaryCoverage=AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get("coverage"));
|
||||
for(Object value:rows) {
|
||||
Object key=AnalysisProtocolService.map(value).get("key");
|
||||
if(!(key instanceof String text)||text.isBlank()||!keys.add(key))return false;
|
||||
}
|
||||
for(Object stage:stages)for(Object field:AnalysisProtocolService.list(task.get("fields"))) {
|
||||
if(allowZeroScrap&&"scrapWeight".equals(field)&&knownNoSourceStage(response,Objects.toString(stage)))continue;
|
||||
BigDecimal total=number(AnalysisProtocolService.map(AnalysisProtocolService.map(response.get("summary")).get(stage)).get(field));
|
||||
if(total==null||total.signum()<=0)return false;
|
||||
Long stageSourceRows=integer(summaryCoverage.get(stage+"Rows"));
|
||||
if("scrapWeight".equals(field)&&stageSourceRows!=null&&stageSourceRows==0&&total!=null&&total.signum()!=0)return false;
|
||||
boolean observedZeroScrap="scrapWeight".equals(field)&&total!=null&&total.signum()==0&&hasObservedStageRows(response,Objects.toString(stage));
|
||||
if(total==null||total.signum()<0||total.signum()==0&&!allowZeroScrap&&!observedZeroScrap)return false;
|
||||
BigDecimal sum=BigDecimal.ZERO;
|
||||
for(Object value:rows) {
|
||||
Map<String,Object> row=AnalysisProtocolService.map(value);
|
||||
BigDecimal weight=number(AnalysisProtocolService.map(row.get(stage)).get(field));
|
||||
Long rowSourceRows=integer(AnalysisProtocolService.map(row.get("coverage")).get(stage+"Rows"));
|
||||
if("scrapWeight".equals(field)&&rowSourceRows!=null&&rowSourceRows==0&&weight!=null&&weight.signum()!=0)return false;
|
||||
if(weight==null) {
|
||||
Object count=AnalysisProtocolService.map(row.get("coverage")).get(stage+"Rows");
|
||||
if(count instanceof Number c&&c.doubleValue()==0)continue;
|
||||
if(rowSourceRows!=null&&rowSourceRows==0)continue;
|
||||
return false;
|
||||
}
|
||||
// Black formal total already includes the existing signed utilization correction.
|
||||
@@ -39,6 +63,65 @@ final class AnalysisProtocolRankingCoverage {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static boolean knownNoSourceStage(Map<String,Object> response,String stage) {
|
||||
if(!Set.of("black","bright").contains(stage))return false;
|
||||
Map<String,Object> summary=AnalysisProtocolService.map(response.get("summary"));
|
||||
Map<String,Object> summaryCoverage=AnalysisProtocolService.map(summary.get("coverage"));
|
||||
if(!Long.valueOf(0L).equals(integer(summaryCoverage.get(stage+"Rows"))))return false;
|
||||
Object rawMonths=AnalysisProtocolService.map(response.get("coverage")).get("sourceMonths");
|
||||
if(!(rawMonths instanceof List<?> months)||months.isEmpty())return false;
|
||||
String other="black".equals(stage)?"bright":"black";
|
||||
long otherRows=0;
|
||||
for(Object raw:months) {
|
||||
Map<String,Object> month=AnalysisProtocolService.map(raw);
|
||||
Long own=integer(month.get(stage+"Rows")),otherCount=integer(month.get(other+"Rows"));
|
||||
if(own==null||own!=0||otherCount==null||otherCount<0||own+otherCount==0)return false;
|
||||
otherRows+=otherCount;
|
||||
}
|
||||
Long publishedOther=integer(summaryCoverage.get(other+"Rows"));
|
||||
if(publishedOther!=null&&publishedOther!=otherRows)return false;
|
||||
Map<String,Object> stageSummary=AnalysisProtocolService.map(summary.get(stage));
|
||||
if(stageSummary.containsKey("scrapWeight")) {
|
||||
BigDecimal weight=number(stageSummary.get("scrapWeight"));
|
||||
if(weight==null||weight.signum()!=0)return false;
|
||||
}
|
||||
List<?> rows=AnalysisProtocolService.list(response.get("rows"));
|
||||
if(rows.isEmpty())return false;
|
||||
for(Object raw:rows) {
|
||||
Map<String,Object> row=AnalysisProtocolService.map(raw);
|
||||
Long count=integer(AnalysisProtocolService.map(row.get("coverage")).get(stage+"Rows"));
|
||||
if(count==null||count!=0)return false;
|
||||
Map<String,Object> stageRow=AnalysisProtocolService.map(row.get(stage));
|
||||
if(stageRow.containsKey("scrapWeight")) {
|
||||
BigDecimal weight=number(stageRow.get("scrapWeight"));
|
||||
if(weight==null||weight.signum()!=0)return false;
|
||||
}
|
||||
}
|
||||
return otherRows>0;
|
||||
}
|
||||
private static Long integer(Object raw) {
|
||||
if(!(raw instanceof Number number))return null;
|
||||
try{return new BigDecimal(number.toString()).longValueExact();}
|
||||
catch(RuntimeException invalid){return null;}
|
||||
}
|
||||
private static boolean hasObservedStageRows(Map<String,Object> response) {
|
||||
Map<String,Object> summary=AnalysisProtocolService.map(response.get("summary"));
|
||||
Map<String,Object> coverage=AnalysisProtocolService.map(summary.get("coverage"));
|
||||
Long black=nonNegativeCount(coverage.get("blackRows")),bright=nonNegativeCount(coverage.get("brightRows"));
|
||||
return black!=null&&bright!=null&&(black>0||bright>0);
|
||||
}
|
||||
private static boolean hasObservedStageRows(Map<String,Object> response,String stage) {
|
||||
if(!Set.of("black","bright").contains(stage))return false;
|
||||
Map<String,Object> summary=AnalysisProtocolService.map(response.get("summary"));
|
||||
Map<String,Object> coverage=AnalysisProtocolService.map(summary.get("coverage"));
|
||||
Long rows=nonNegativeCount(coverage.get(stage+"Rows"));
|
||||
return rows!=null&&rows>0;
|
||||
}
|
||||
private static Long nonNegativeCount(Object raw) {
|
||||
if(!(raw instanceof Number number))return null;
|
||||
try {long value=new BigDecimal(number.toString()).longValueExact();return value<0?null:value;}
|
||||
catch(RuntimeException invalid){return null;}
|
||||
}
|
||||
static boolean visualsComplete(Map<String,Object> answer,Map<String,Object> task) {
|
||||
if(!"ready".equals(answer.get("visualState")))return false;
|
||||
String type=Objects.toString(task.get("visualType"),"bar");
|
||||
|
||||
@@ -3,6 +3,7 @@ package cn.yunsheng.quality.analysis;
|
||||
import cn.yunsheng.quality.governance.GovernanceAccessService;
|
||||
import cn.yunsheng.quality.identity.AppUserPrincipal;
|
||||
import cn.yunsheng.quality.options.OptionsController;
|
||||
import cn.yunsheng.quality.common.SelectionValues;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.*;
|
||||
@@ -276,7 +277,7 @@ public class AnalysisProtocolService {
|
||||
var queries=new ArrayList<Map<String,Object>>();
|
||||
for(Object raw:list(source.get("queries"))) {
|
||||
var q=map(raw);var one=new LinkedHashMap<String,Object>();
|
||||
for(String key:List.of("queryId","target","parameters","responseHash","nodeId","scopeId","response","task","scope","coverage"))if(q.containsKey(key))one.put(key,q.get(key));
|
||||
for(String key:List.of("queryId","target","parameters","responseHash","nodeId","scopeId","response","task","scope","coverage","rankArtifact"))if(q.containsKey(key))one.put(key,q.get(key));
|
||||
var proof=new LinkedHashMap<String,Object>();for(String key:List.of("turnId","sessionId","attemptId","executionId","revision"))proof.put(key,out.get(key));
|
||||
proof.put("queryId",q.get("queryId"));proof.put("responseHash",q.get("responseHash"));
|
||||
for(String key:List.of("nodeId","scopeId"))if(q.get(key)!=null)proof.put(key,q.get(key));one.put("proof",proof);queries.add(one);
|
||||
@@ -343,6 +344,14 @@ public class AnalysisProtocolService {
|
||||
if(ids.isEmpty()||new HashSet<>(ids).size()!=ids.size()||!known.containsAll(ids)||!name.equals(ref.get("evidenceRef")))throw denied("恢复图表引用了其他执行的查询");
|
||||
refs.add(Map.of("evidenceRef",name,"queryIds",ids));references.add(name);
|
||||
}
|
||||
var rankBindings=new ArrayList<RankingArtifactBinding>();
|
||||
for(var binding:refs)if(list(binding.get("queryIds")).size()==1)for(Object id:list(binding.get("queryIds"))) {
|
||||
var query=list(input.get("queries")).stream().map(AnalysisProtocolService::map)
|
||||
.filter(item->Objects.equals(item.get("queryId"),id)).findFirst().orElse(Map.of());
|
||||
Map<String,Object> artifact=map(query.get("rankArtifact"));
|
||||
if(!artifact.isEmpty())rankBindings.add(new RankingArtifactBinding(Objects.toString(id),Objects.toString(binding.get("evidenceRef")),
|
||||
Objects.toString(query.get("nodeId"),null),Objects.toString(query.get("scopeId"),null),artifact));
|
||||
}
|
||||
var original=AnalysisVisualSnapshotAccess.fromAnswer(answer,fields,AnalysisVisualSnapshotAccess.visibleExtraFilterKeys(jdbc,fields),turn,expected);
|
||||
for(var chart:snapshot.visuals())if(!references.containsAll(list(chart.get("evidenceRefs")))&&original.visuals().stream().noneMatch(old->Objects.equals(canonical(old),canonical(chart))))throw denied("恢复图表缺少原始查询引用");
|
||||
var merged=new LinkedHashMap<String,Map<String,Object>>();
|
||||
@@ -355,11 +364,11 @@ public class AnalysisProtocolService {
|
||||
var candidate=new LinkedHashMap<>(snapshot.asMap());candidate.put("visuals",merged.values().stream().limit(12).toList());
|
||||
if(!merged.isEmpty())candidate.put("visualState","ready");
|
||||
if(task.containsKey("queryPlan")||AnalysisProtocolExploration.nativeAgent(task))for(Object raw:list(input.get("scopes"))) {
|
||||
var scope=map(raw);if(!rawScope(scope)&&!plannedVisualsComplete(candidate,scope,release))chartsComplete=false;
|
||||
var scope=map(raw);if(!rawScope(scope)&&!plannedVisualsComplete(candidate,scope,release,rankBindings))chartsComplete=false;
|
||||
}
|
||||
else {
|
||||
String purpose=!list(task.get("dimensions")).isEmpty()?"ranking":Boolean.TRUE.equals(task.get("trendRequired"))?"trend":"metrics";
|
||||
if(!plannedVisualsComplete(candidate,Map.of("task",task,"purpose",purpose),release))chartsComplete=false;
|
||||
if(!plannedVisualsComplete(candidate,Map.of("task",task,"purpose",purpose),release,rankBindings))chartsComplete=false;
|
||||
}
|
||||
chartsComplete&=gaps.isEmpty();if(!chartsComplete&&gaps.isEmpty())gaps.add("所需图表尚未全部恢复,已保留可用图表和原分析结果");
|
||||
var result=new LinkedHashMap<>(candidate);result.put("chartsComplete",chartsComplete);result.put("queryRefs",refs);result.put("missingEvidence",gaps.stream().distinct().toList());return result;
|
||||
@@ -616,7 +625,16 @@ public class AnalysisProtocolService {
|
||||
binding.current(a.sessionId(),a.turnId(),a.userId(),a.revision(),a.executionId());
|
||||
Map<String,Object> task=read(store.revision(a.sessionId(),a.revision()).taskJson());
|
||||
if(!Objects.equals(canonical(task),canonical(details.get("agentTask")))) throw denied("最终答复没有绑定当前任务");
|
||||
if(Set.of("COMPLETED","PARTIAL").contains(status)) {
|
||||
if(Set.of("COMPLETED","PARTIAL").contains(status)&&!"report".equals(task.get("kind"))
|
||||
&&!hasProtocolEvidence(details)) {
|
||||
status="FAILED";
|
||||
answer.clear();
|
||||
answer.put("answer",noProofFailureReason(details));
|
||||
answer.put("results",List.of());
|
||||
answer.put("releaseId",a.releaseId());
|
||||
answer.put("engine","NAO");
|
||||
answer.put("stopReason","formal_query_evidence_missing");
|
||||
} else if(Set.of("COMPLETED","PARTIAL").contains(status)) {
|
||||
governance.requireBoundAction("QUERY","query.aggregate");
|
||||
if(AnalysisProtocolInspection.KIND.equals(task.get("kind"))) {
|
||||
validateEvidence(a,details);
|
||||
@@ -624,10 +642,12 @@ public class AnalysisProtocolService {
|
||||
answer.clear();answer.putAll(inspection.finish(a,task,ids,actor));
|
||||
if(!Boolean.TRUE.equals(map(answer.get("inspectionCoverage")).get("complete")))status="PARTIAL";
|
||||
} else if("report".equals(task.get("kind"))) {
|
||||
for(String key:List.of("historySources","historySections","historyCoverage","historyEvidence","reportMarkdown","reportHtml","reportTitle","visuals","visualSnapshot","executionCoverage","executionScopes","readingSummary","queryEvidenceRefs"))answer.remove(key);
|
||||
for(String key:List.of("historySources","historySections","historyCoverage","historyEvidence","historyVisualSources","reportMarkdown","reportHtml","reportTitle","visuals","visualSnapshot","executionCoverage","executionScopes","readingSummary","queryEvidenceRefs"))answer.remove(key);
|
||||
var checked=history.finish(a,task,details,actor,historicalQueryValidator(),autonomousSession(a.sessionId()));
|
||||
answer.putAll(checked);answer.put("protocolQueryIds",List.of());answer.put("results",List.of());
|
||||
answer.put("visuals",List.of());answer.put("visualState","not_requested");answer.put("visualSchemaVersion",1);
|
||||
if(!(checked.get("visuals") instanceof List<?>))answer.put("visuals",List.of());
|
||||
if(!(checked.get("visualState") instanceof String))answer.put("visualState","not_requested");
|
||||
if(!checked.containsKey("visualSchemaVersion"))answer.put("visualSchemaVersion",1);
|
||||
boolean complete=Boolean.TRUE.equals(map(checked.get("historyCoverage")).get("complete"));
|
||||
if(!complete){status="PARTIAL";if(autonomousSession(a.sessionId()))appendCoverageNotices(answer,list(map(checked.get("historyCoverage")).get("missingEvidence")));}
|
||||
answer.put("stopReason",complete&&"COMPLETED".equals(status)?"history_report_complete":"history_report_partial");
|
||||
@@ -641,6 +661,16 @@ public class AnalysisProtocolService {
|
||||
else factsComplete&=Boolean.TRUE.equals(checked.get("factsComplete"));
|
||||
Object recordList=checked.remove("recordList");if(recordList!=null)answer.put("recordList",recordList);
|
||||
answer.put("executionCoverage",checked);answer.put("executionScopes",planResolution(a,task,actor).scopes());
|
||||
if(answer.get("answerQuality") instanceof Map<?,?>) {
|
||||
var quality=new LinkedHashMap<>(map(answer.get("answerQuality")));
|
||||
quality.put("dataComplete",Boolean.TRUE.equals(checked.get("factsComplete")));
|
||||
if(Boolean.TRUE.equals(checked.get("factsComplete"))&&!Boolean.TRUE.equals(checked.get("complete")))quality.put("chartsComplete",false);
|
||||
if(!Boolean.TRUE.equals(checked.get("complete"))) {
|
||||
Set<Object> gaps=new LinkedHashSet<>(list(quality.get("missingEvidence")));gaps.addAll(list(checked.get("missingEvidence")));
|
||||
quality.put("missingEvidence",List.copyOf(gaps));
|
||||
}
|
||||
answer.put("answerQuality",quality);
|
||||
}
|
||||
if(AnalysisProtocolExploration.nativeAgent(task)){for(String key:List.of("scopeCoverage","explorationWarnings","conclusionProtocolQueryIds","attemptProtocolQueryIds"))if(checked.containsKey(key))answer.put(key,checked.get(key));}
|
||||
if(Boolean.TRUE.equals(task.get("verifySource"))||Boolean.TRUE.equals(task.get("fullDetails"))) {
|
||||
for(String field:List.of("reportMarkdown","reportHtml","reportTitle"))answer.remove(field);
|
||||
@@ -655,7 +685,12 @@ public class AnalysisProtocolService {
|
||||
} else {
|
||||
validateMetricEvidence(a,task,details);
|
||||
factsComplete&=metricFactsComplete(a,task,details);
|
||||
if(!list(task.get("dimensions")).isEmpty()&&(!completeRankingCoverage(a,task,details)||!AnalysisProtocolRankingCoverage.visualsComplete(answer,task)))status="PARTIAL";
|
||||
if(!list(task.get("dimensions")).isEmpty()) {
|
||||
var rankingScope=new LinkedHashMap<String,Object>();rankingScope.put("purpose","ranking");rankingScope.put("task",task);
|
||||
var rankBindings=rankingArtifactBindings(a,details);
|
||||
boolean visualsRequired=!"none".equals(task.get("visualPreference"));
|
||||
if(!completeRankingCoverage(a,task,details)||visualsRequired&&!plannedVisualsComplete(answer,rankingScope,a.releaseId(),rankBindings))status="PARTIAL";
|
||||
}
|
||||
if(task.get("comparisonPeriod") instanceof Map<?,?>&&!completeComparisonCoverage(a,task,details))status="PARTIAL";
|
||||
}
|
||||
var accepted=list(map(answer.get("executionCoverage")).get("acceptedProtocolQueryIds"));
|
||||
@@ -722,6 +757,38 @@ public class AnalysisProtocolService {
|
||||
if(count==null||count!=1)throw denied("查询证据属于其他任务或已失效");
|
||||
}
|
||||
}
|
||||
private boolean hasProtocolEvidence(Map<String,Object> details) {
|
||||
return details.get("protocolEvidence") instanceof List<?> refs&&!refs.isEmpty();
|
||||
}
|
||||
private String noProofFailureReason(Map<String,Object> details) {
|
||||
Map<String,Object> diagnostic=list(details.get("failureDiagnostics")).stream()
|
||||
.map(AnalysisProtocolService::map).findFirst().orElse(Map.of());
|
||||
String kind=Objects.toString(diagnostic.get("failureKind"),"");
|
||||
return switch(kind) {
|
||||
case "source_coverage" -> "当前已确认范围没有可用的正式来源数据,本轮未提交业务结论。";
|
||||
case "formal_query_rejected" -> "正式查询响应未通过当前范围或覆盖核验,本轮未提交业务结论。";
|
||||
case "transient_http" -> {
|
||||
Long status=safeHttpStatus(diagnostic.get("httpStatus"));
|
||||
boolean classifiedRetryable=Boolean.TRUE.equals(diagnostic.get("retryable"));
|
||||
if(classifiedRetryable&&status!=null&&Set.of(429L,502L,503L,504L).contains(status))
|
||||
yield "正式查询服务暂时不可用(HTTP "+status+"),本轮未提交业务结论,可稍后重试。";
|
||||
yield "正式查询未取得可核验回执,本轮未提交业务结论。";
|
||||
}
|
||||
case "http_error" -> {
|
||||
Long status=safeHttpStatus(diagnostic.get("httpStatus"));
|
||||
yield status==null?"正式查询服务未返回可用结果,本轮未提交业务结论。"
|
||||
:"正式查询服务返回HTTP "+status+",本轮未提交业务结论。";
|
||||
}
|
||||
case "transport_unavailable" -> "正式查询连接暂时不可用,本轮未提交业务结论,可稍后重试。";
|
||||
case "query_rejected" -> "正式查询未通过已确认的范围或参数校验,本轮未提交业务结论。";
|
||||
default -> "本轮未取得可核验的正式查询回执,未提交业务结论。";
|
||||
};
|
||||
}
|
||||
private Long safeHttpStatus(Object raw) {
|
||||
if(!(raw instanceof Number number))return null;
|
||||
try {long value=new java.math.BigDecimal(number.toString()).longValueExact();return value>=400&&value<=599?value:null;}
|
||||
catch(RuntimeException invalid){return null;}
|
||||
}
|
||||
@Transactional
|
||||
public Map<String,Object> queries(String attemptId,AppUserPrincipal actor) {
|
||||
lock(sessionForAttempt(attemptId));Attempt a=require(attemptId,actor,true);
|
||||
@@ -1085,9 +1152,10 @@ public class AnalysisProtocolService {
|
||||
var state=nativeScopeState(a,task,actor);
|
||||
// Older native workers supplied all registered scopes. Keep that conservative compatibility path.
|
||||
if(!details.containsKey("conclusionProtocolQueryIds")) {
|
||||
var result=new LinkedHashMap<>(plannedCoverage(a,task,details,answer,actor,"required".equals(task.get("visualPreference"))));
|
||||
var result=new LinkedHashMap<>(plannedCoverage(a,task,details,answer,actor,true));
|
||||
var gaps=new LinkedHashSet<String>();for(Object gap:list(result.get("missingEvidence")))if(gap instanceof String text)gaps.add(text);gaps.addAll(state.rootGaps());result.put("missingEvidence",List.copyOf(gaps));result.put("complete",gaps.isEmpty());
|
||||
result.put("scopeCoverage",state.coverage());result.put("explorationWarnings",state.warnings());result.put("acceptedProtocolQueryIds",acceptedNativeQueryIds(state));result.put("attemptProtocolQueryIds",state.queries().stream().map(q->map(q.get("proof")).get("queryId")).distinct().toList());return result;
|
||||
var warnings=new LinkedHashSet<String>(state.warnings());for(Object gap:list(result.get("visualGaps")))if(gap instanceof String text)warnings.add(text);
|
||||
result.put("scopeCoverage",state.coverage());result.put("explorationWarnings",List.copyOf(warnings));result.put("acceptedProtocolQueryIds",acceptedNativeQueryIds(state));result.put("attemptProtocolQueryIds",state.queries().stream().map(q->map(q.get("proof")).get("queryId")).distinct().toList());return result;
|
||||
}
|
||||
Object raw=details.get("conclusionProtocolQueryIds");var selected=list(raw);
|
||||
if(!(raw instanceof List<?>)||selected.size()>40||new HashSet<>(selected).size()!=selected.size()||selected.stream().anyMatch(id->!(id instanceof String)))throw denied("最终结论查询引用格式无效");
|
||||
@@ -1111,14 +1179,18 @@ public class AnalysisProtocolService {
|
||||
}
|
||||
var activeIds=active.stream().map(scope->scope.get("scopeId")).collect(java.util.stream.Collectors.toSet());
|
||||
var activeProofs=state.queries().stream().map(q->map(q.get("proof"))).filter(p->activeIds.contains(p.get("scopeId"))).toList();
|
||||
var result=new LinkedHashMap<>(plannedCoverage(a,task,Map.of("protocolEvidence",activeProofs),answer,actor,false,new AnalysisProtocolQueryPlan.Resolution(active,List.of(),List.of(),List.of()),false));
|
||||
var warnings=new LinkedHashSet<String>(state.warnings());
|
||||
boolean required="required".equals(task.get("visualPreference"));boolean requiredReady=false;
|
||||
for(var scope:active)if(!rawScope(scope)) {
|
||||
if(!plannedVisualsComplete(answer,scope,a.releaseId()))warnings.add(Objects.toString(scope.get("label"),"所选范围")+"所需图表尚未完整生成,可独立恢复图表");
|
||||
if(required){var requiredScope=new LinkedHashMap<>(scope);var child=new LinkedHashMap<>(map(scope.get("task")));child.put("visualPreference","required");requiredScope.put("task",child);requiredReady|=plannedVisualsComplete(answer,requiredScope,a.releaseId());}
|
||||
boolean required="required".equals(task.get("visualPreference"));
|
||||
var checkedScopes=new ArrayList<Map<String,Object>>();for(var scope:active) {
|
||||
if(!required||rawScope(scope)){checkedScopes.add(scope);continue;}
|
||||
var requiredScope=new LinkedHashMap<>(scope);var child=new LinkedHashMap<>(map(scope.get("task")));child.put("visualPreference","required");requiredScope.put("task",child);checkedScopes.add(requiredScope);
|
||||
}
|
||||
if(required&&!requiredReady)fatal.add("用户要求的图表尚未完整生成");
|
||||
var activeDetails=new LinkedHashMap<String,Object>();activeDetails.put("protocolEvidence",activeProofs);
|
||||
if(details.containsKey("queryEvidenceBindings"))activeDetails.put("queryEvidenceBindings",details.get("queryEvidenceBindings"));
|
||||
var result=new LinkedHashMap<>(plannedCoverage(a,task,activeDetails,answer,actor,true,new AnalysisProtocolQueryPlan.Resolution(checkedScopes,List.of(),List.of(),List.of()),false));
|
||||
var visualGaps=new LinkedHashSet<String>();for(Object gap:list(result.get("visualGaps")))if(gap instanceof String text)visualGaps.add(text);
|
||||
warnings.addAll(visualGaps);fatal.addAll(visualGaps);
|
||||
if(required&&!visualGaps.isEmpty())fatal.add("用户要求的图表尚未完整生成");
|
||||
result.put("factsComplete",selectedEvidenceComplete&&state.rootGaps().isEmpty()&&state.coverage().stream().filter(c->selectedScopes.contains(c.get("scopeId"))).allMatch(c->Boolean.TRUE.equals(c.get("complete")))&&!selected.isEmpty());
|
||||
result.put("complete",fatal.isEmpty());result.put("missingEvidence",List.copyOf(fatal));result.put("scopeCoverage",state.coverage());result.put("explorationWarnings",List.copyOf(warnings));result.put("conclusionProtocolQueryIds",List.copyOf(selected));result.put("acceptedProtocolQueryIds",acceptedNativeQueryIds(state));result.put("attemptProtocolQueryIds",state.queries().stream().map(q->map(q.get("proof")).get("queryId")).distinct().toList());return result;
|
||||
}
|
||||
@@ -1130,7 +1202,37 @@ public class AnalysisProtocolService {
|
||||
String key=switch(Objects.toString(scope.get("purpose"))){case "ranking"->"weightsComplete";case "defects"->"defectsComplete";case "changes"->"changesComplete";case "source"->"sourceVerificationComplete";case "records"->"recordsPageComplete";default->"metricsComplete";};
|
||||
return Boolean.TRUE.equals(map(query.get("coverage")).get(key));
|
||||
}
|
||||
private record PlannedQuery(String id,String node,String scope,Map<String,String> parameters,Map<String,Object> coverage,String target,Map<String,Object> response){}
|
||||
private record PlannedQuery(String id,String node,String scope,Map<String,String> parameters,Map<String,Object> coverage,String target,Map<String,Object> response,Map<String,Object> rankArtifact){}
|
||||
private record RankingArtifactBinding(String id,String evidenceRef,String node,String scope,Map<String,Object> artifact){}
|
||||
private Map<String,List<String>> queryEvidenceBindings(Object raw) {
|
||||
Map<String,List<String>> result=new LinkedHashMap<>();
|
||||
for(Object item:list(raw)) {
|
||||
Map<String,Object> binding=map(item);Object ref=binding.get("evidenceRef");List<?> rawIds=list(binding.get("queryIds"));
|
||||
if(!(ref instanceof String reference)||!reference.matches("Q[A-F0-9]{8}")||rawIds.isEmpty()||rawIds.size()>40
|
||||
||rawIds.stream().anyMatch(id->!(id instanceof String text)||text.isBlank())
|
||||
||new HashSet<>(rawIds).size()!=rawIds.size()||result.containsKey(reference))continue;
|
||||
result.put(reference,rawIds.stream().map(Object::toString).toList());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private List<RankingArtifactBinding> rankingArtifactBindings(List<PlannedQuery> queries,Object rawBindings) {
|
||||
var refs=queryEvidenceBindings(rawBindings);var result=new ArrayList<RankingArtifactBinding>();
|
||||
for(var query:queries)if(Boolean.TRUE.equals(query.coverage().get("weightsComplete"))&&!query.rankArtifact().isEmpty())
|
||||
for(var entry:refs.entrySet())if(entry.getValue().size()==1&&Objects.equals(entry.getValue().getFirst(),query.id()))
|
||||
result.add(new RankingArtifactBinding(query.id(),entry.getKey(),query.node(),query.scope(),query.rankArtifact()));
|
||||
return List.copyOf(result);
|
||||
}
|
||||
private List<RankingArtifactBinding> rankingArtifactBindings(Attempt a,Map<String,Object> details) {
|
||||
var queries=new ArrayList<PlannedQuery>();
|
||||
for(Object raw:list(details.get("protocolEvidence"))) {
|
||||
Map<String,Object> proof=map(raw);Object queryId=proof.get("queryId");
|
||||
var rows=jdbc.query("SELECT protocol_node_id,protocol_scope_id,coverage_json,rank_artifact_json FROM analysis_protocol_query WHERE query_id=? AND attempt_id=? AND execution_id=? FOR SHARE",
|
||||
(rs,n)->new PlannedQuery(Objects.toString(queryId),rs.getString(1),rs.getString(2),Map.of(),read(rs.getString(3)),"GET /api/metrics",Map.of(),read(rs.getString(4))),
|
||||
queryId,a.attemptId(),a.executionId());
|
||||
if(rows.size()==1)queries.add(rows.getFirst());
|
||||
}
|
||||
return rankingArtifactBindings(queries,details.get("queryEvidenceBindings"));
|
||||
}
|
||||
private Map<String,Object> plannedCoverage(Attempt a,Map<String,Object> task,Map<String,Object> details,Map<String,Object> answer,AppUserPrincipal actor) {
|
||||
return plannedCoverage(a,task,details,answer,actor,true);
|
||||
}
|
||||
@@ -1145,9 +1247,9 @@ public class AnalysisProtocolService {
|
||||
List<PlannedQuery> verified=new ArrayList<>();
|
||||
for(Object item:list(details.get("protocolEvidence"))) {
|
||||
Map<String,Object> ref=map(item);
|
||||
var queries=jdbc.query("SELECT protocol_node_id,protocol_scope_id,parameters_json,coverage_json,query_target,response_json FROM analysis_protocol_query WHERE query_id=? AND attempt_id=? AND execution_id=? FOR SHARE",(rs,n)->{
|
||||
var queries=jdbc.query("SELECT protocol_node_id,protocol_scope_id,parameters_json,coverage_json,query_target,response_json,rank_artifact_json FROM analysis_protocol_query WHERE query_id=? AND attempt_id=? AND execution_id=? FOR SHARE",(rs,n)->{
|
||||
Map<String,String> parameters=new LinkedHashMap<>();read(rs.getString(3)).forEach((k,v)->parameters.put(k,Objects.toString(v)));
|
||||
return new PlannedQuery(Objects.toString(ref.get("queryId")),rs.getString(1),rs.getString(2),canonicalQuery(parameters),read(rs.getString(4)),rs.getString(5),read(rs.getString(6)));
|
||||
return new PlannedQuery(Objects.toString(ref.get("queryId")),rs.getString(1),rs.getString(2),canonicalQuery(parameters),read(rs.getString(4)),rs.getString(5),read(rs.getString(6)),read(rs.getString(7)));
|
||||
},ref.get("queryId"),a.attemptId(),a.executionId());
|
||||
if(queries.size()!=1)throw denied("分步任务没有对应的正式节点查询证据");
|
||||
PlannedQuery query=queries.getFirst();
|
||||
@@ -1157,6 +1259,7 @@ public class AnalysisProtocolService {
|
||||
if(!matches)throw denied("节点证据不属于当前合法的查询范围或期间");verified.add(query);
|
||||
}
|
||||
List<AnalysisRecordSnapshot.Input> recordInputs=new ArrayList<>();List<Map<String,Object>> sourceChecks=new ArrayList<>();
|
||||
List<RankingArtifactBinding> rankBindings=rankingArtifactBindings(verified,details.get("queryEvidenceBindings"));
|
||||
for(Map<String,Object> scope:resolution.scopes()) {
|
||||
String label=Objects.toString(scope.get("label"),"所选范围");boolean ranking="ranking".equals(scope.get("purpose"));
|
||||
if("changes".equals(scope.get("purpose"))) {
|
||||
@@ -1167,7 +1270,7 @@ public class AnalysisProtocolService {
|
||||
missing.add(label+"变化排名尚未取得完整同范围正式依据");
|
||||
for(var query:candidates)for(Object gap:list(query.coverage().get("missingEvidence")))if(gap instanceof String text)missing.add(label+":"+text);
|
||||
}
|
||||
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId()))visualMissing.add(label+"所需变化图表尚未完整生成");
|
||||
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId(),rankBindings))visualMissing.add(label+"所需变化图表尚未完整生成");
|
||||
continue;
|
||||
}
|
||||
if("defects".equals(scope.get("purpose"))) {
|
||||
@@ -1179,7 +1282,7 @@ public class AnalysisProtocolService {
|
||||
for(var query:candidates)for(Object gap:list(query.coverage().get("missingEvidence")))if(gap instanceof String text)missing.add(label+":"+text);
|
||||
}
|
||||
}
|
||||
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId()))visualMissing.add(label+"所需缺陷图表尚未完整生成");
|
||||
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId(),rankBindings))visualMissing.add(label+"所需缺陷图表尚未完整生成");
|
||||
continue;
|
||||
}
|
||||
if(rawScope(scope)) {
|
||||
@@ -1211,7 +1314,7 @@ public class AnalysisProtocolService {
|
||||
for(PlannedQuery query:candidates)for(Object gap:list(query.coverage().get("missingEvidence")))if(gap instanceof String text)missing.add(label+":"+text);
|
||||
}
|
||||
}
|
||||
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId()))visualMissing.add(label+"所需图表尚未完整生成");
|
||||
if(checkVisuals&&!plannedVisualsComplete(answer,scope,a.releaseId(),rankBindings))visualMissing.add(label+"所需图表尚未完整生成");
|
||||
}
|
||||
if(resolution.scopes().isEmpty())missing.add("没有可核验的实际查询范围");
|
||||
Map<String,Object> result=new LinkedHashMap<>();
|
||||
@@ -1224,6 +1327,7 @@ public class AnalysisProtocolService {
|
||||
}
|
||||
result.put("factsComplete",missing.isEmpty());missing.addAll(visualMissing);
|
||||
result.put("complete",missing.isEmpty());result.put("missingEvidence",missing.stream().distinct().toList());result.put("scopeCount",resolution.scopes().size());
|
||||
result.put("visualGaps",visualMissing.stream().distinct().toList());
|
||||
if(!sourceChecks.isEmpty())result.put("sourceChecks",sourceChecks);
|
||||
result.put("skipped",resolution.skipped());
|
||||
result.put("verifiedQueryCount",verified.stream().map(PlannedQuery::id).distinct().count());return result;
|
||||
@@ -1249,8 +1353,19 @@ public class AnalysisProtocolService {
|
||||
return String.join("\n\n",lines);
|
||||
}
|
||||
private boolean plannedVisualsComplete(Map<String,Object> answer,Map<String,Object> scope,String release) {
|
||||
return plannedVisualsComplete(answer,scope,release,List.of());
|
||||
}
|
||||
private boolean plannedVisualsComplete(Map<String,Object> answer,Map<String,Object> scope,String release,List<RankingArtifactBinding> rankBindings) {
|
||||
Map<String,Object> child=map(scope.get("task"));String purpose=Objects.toString(scope.get("purpose"));
|
||||
boolean required=!"none".equals(child.get("visualPreference"))&&("ranking".equals(purpose)||"trend".equals(purpose)||"required".equals(child.get("visualPreference"))||child.get("visualType")!=null);
|
||||
boolean metricScope="metrics".equals(purpose)||"metrics".equals(child.get("kind"));
|
||||
boolean singleObjectMetricRanking=singleObjectMetricRanking(scope,child,purpose);
|
||||
boolean optionalAutoScalarMetric=metricScope&&"auto".equals(child.get("visualPreference"))
|
||||
&&!Boolean.TRUE.equals(child.get("comparisonRequired"))&&!Boolean.TRUE.equals(child.get("trendRequired"))
|
||||
&&list(child.get("fields")).size()==1&&list(child.get("stages")).size()<=1;
|
||||
boolean required=!"none".equals(child.get("visualPreference"))&&("ranking".equals(purpose)&&!singleObjectMetricRanking||"trend".equals(purpose)
|
||||
||"required".equals(child.get("visualPreference"))
|
||||
||metricScope&&Boolean.TRUE.equals(child.get("comparisonRequired"))
|
||||
||child.get("visualType")!=null&&!optionalAutoScalarMetric&&!singleObjectMetricRanking);
|
||||
if("changes".equals(purpose)&&!"none".equals(child.get("visualPreference"))) {
|
||||
var query=map(child.get("changeQuery"));
|
||||
boolean related=list(answer.get("visuals")).stream().map(AnalysisProtocolService::map).anyMatch(chart->Objects.equals(query.get("dimension"),chart.get("dimension"))
|
||||
@@ -1260,12 +1375,15 @@ public class AnalysisProtocolService {
|
||||
required="required".equals(child.get("visualPreference"))||related;
|
||||
}
|
||||
if(!required)return true;
|
||||
if("ranking".equals(purpose)&&!rankingHasVisualDataOrUnknown(scope,release,rankBindings))return true;
|
||||
if(!"ready".equals(answer.get("visualState")))return false;
|
||||
List<Map<String,Object>> charts=list(answer.get("visuals")).stream().map(AnalysisProtocolService::map).filter(chart->{
|
||||
Map<String,Object> actual=map(chart.get("scope"));Map<String,Object> business=new LinkedHashMap<>();
|
||||
for(String key:FILTERS)if(actual.containsKey(key))business.put(key,actual.get(key));
|
||||
boolean sameScope="changes".equals(purpose)?AnalysisProtocolChanges.sameFilters(business,map(child.get("filters")))
|
||||
:Objects.equals(canonical(AnalysisProtocolQueryPlan.identityFilters(business)),canonical(AnalysisProtocolQueryPlan.identityFilters(map(child.get("filters")))));
|
||||
// Snapshot normalization omits cleared empty rules. Compare their
|
||||
// business meaning without changing saved scope IDs or accepting a
|
||||
// missing non-empty restriction.
|
||||
boolean sameScope=AnalysisProtocolChanges.sameFilters(business,map(child.get("filters")));
|
||||
if(!release.equals(actual.get("releaseId"))||!sameScope)return false;
|
||||
if(!chartPeriodMatches(map(chart.get("period")),map(child.get("period")),"trend".equals(purpose)))return false;
|
||||
return !Boolean.TRUE.equals(child.get("comparisonRequired"))||chartPeriodMatches(map(chart.get("comparisonPeriod")),map(child.get("comparisonPeriod")),false);
|
||||
@@ -1286,20 +1404,80 @@ public class AnalysisProtocolService {
|
||||
}
|
||||
List<?> stages=list(child.get("stages")).isEmpty()?List.of("overall"):list(child.get("stages"));
|
||||
for(Object field:list(child.get("fields")))for(Object rawStage:stages) {
|
||||
// Composite rate is a companion column on the same scrap-ranked
|
||||
// population; it does not require a second independently sorted chart.
|
||||
if("ranking".equals(purpose)&&list(child.get("stages")).isEmpty()&&"rate".equals(field))continue;
|
||||
String stage=rawStage.toString();Set<String> metrics=switch(field.toString()) {
|
||||
case "rate"->Set.of(stage.equals("overall")?"composite_rate":stage+"_rate");
|
||||
case "scrapRate"->Set.of(stage+"_scrap_rate");case "totalWeight"->Set.of(stage+"_total_weight");
|
||||
case "scrapWeight"->Set.of(stage+"_scrap_weight","current_scrap_weight");default->Set.of();
|
||||
};
|
||||
if("ranking".equals(purpose)&&"scrapWeight".equals(field)
|
||||
&&!rankingTargetHasVisualDataOrUnknown(scope,release,rankBindings,stage))continue;
|
||||
boolean found=charts.stream().anyMatch(chart->metrics.contains(chart.get("metric"))
|
||||
&&(stage.equals("overall")?chart.get("stage")==null:stage.equals(chart.get("stage")))
|
||||
&&("ranking".equals(purpose)?"all_scope_weight".equals(map(chart.get("coverage")).get("shareBasis"))&&list(child.get("dimensions")).contains(chart.get("dimension")):
|
||||
"trend".equals(purpose)?Set.of("line","area","heatmap").contains(chart.get("type")):true)
|
||||
&&(child.get("visualType")==null||Objects.equals(child.get("visualType"),chart.get("type"))));
|
||||
&&(child.get("visualType")==null||Objects.equals(child.get("visualType"),chart.get("type")))
|
||||
&&(!"ranking".equals(purpose)||rankingChartBound(chart,scope,release,rankBindings)));
|
||||
if(!found)return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean singleObjectMetricRanking(Map<String,Object> scope,Map<String,Object> child,String purpose) {
|
||||
if(!"ranking".equals(purpose)||!"metrics".equals(child.get("kind"))
|
||||
||!"auto".equals(child.get("visualPreference"))
|
||||
||Boolean.TRUE.equals(child.get("comparisonRequired"))||Boolean.TRUE.equals(child.get("trendRequired"))
|
||||
||child.get("comparisonPeriod") instanceof Map<?,?>
|
||||
||!list(child.get("stages")).isEmpty()
|
||||
||!new HashSet<>(list(child.get("fields"))).equals(Set.of("rate","scrapWeight"))
|
||||
||!Objects.equals(scope.get("limit"),1))return false;
|
||||
List<?> dimensions=list(child.get("dimensions"));
|
||||
if(dimensions.size()!=1||!(dimensions.getFirst() instanceof String dimension)
|
||||
||!Set.of("product","department","group").contains(dimension))return false;
|
||||
Object selected=map(child.get("filters")).get(dimension);
|
||||
if(!(selected instanceof String wire)||wire.isBlank())return false;
|
||||
try {if(SelectionValues.parse(wire).size()!=1)return false;}catch(RuntimeException invalid){return false;}
|
||||
return list(map(child.get("dimensionStages")).get(dimension)).isEmpty();
|
||||
}
|
||||
private boolean rankingHasVisualDataOrUnknown(Map<String,Object> scope,String release,List<RankingArtifactBinding> bindings) {
|
||||
Map<String,Object> child=map(scope.get("task"));List<?> stages=list(child.get("stages")).isEmpty()?List.of("overall"):list(child.get("stages"));
|
||||
boolean hasVisualTarget=false;
|
||||
for(Object field:list(child.get("fields")))for(Object stage:stages) {
|
||||
if(list(child.get("stages")).isEmpty()&&"rate".equals(field))continue;
|
||||
hasVisualTarget=true;
|
||||
if(!"scrapWeight".equals(field)||rankingTargetHasVisualDataOrUnknown(scope,release,bindings,Objects.toString(stage)))return true;
|
||||
}
|
||||
return !hasVisualTarget;
|
||||
}
|
||||
private boolean rankingTargetHasVisualDataOrUnknown(Map<String,Object> scope,String release,
|
||||
List<RankingArtifactBinding> bindings,String stage) {
|
||||
Map<String,Object> child=map(scope.get("task"));List<?> dimensions=list(child.get("dimensions"));
|
||||
if(dimensions.isEmpty())return true;
|
||||
for(Object dimension:dimensions) {
|
||||
var candidates=bindings.stream().filter(binding->bindingMatchesScope(binding,scope))
|
||||
.filter(binding->Objects.equals(dimension,binding.artifact().get("dimension"))).toList();
|
||||
if(candidates.isEmpty()||candidates.stream().anyMatch(binding->
|
||||
!AnalysisVisualSnapshotAccess.rankingArtifactProvesEmptyScrap(binding.artifact(),child,release,Objects.toString(dimension),stage)))return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private boolean bindingMatchesScope(RankingArtifactBinding binding,Map<String,Object> scope) {
|
||||
if(scope.get("nodeId")!=null||scope.get("scopeId")!=null)
|
||||
return Objects.equals(scope.get("nodeId"),binding.node())&&Objects.equals(scope.get("scopeId"),binding.scope());
|
||||
return binding.node()==null&&binding.scope()==null;
|
||||
}
|
||||
private boolean rankingChartBound(Map<String,Object> chart,Map<String,Object> scope,String release,List<RankingArtifactBinding> bindings) {
|
||||
List<?> refs=list(chart.get("evidenceRefs"));if(refs.size()!=1||!(refs.getFirst() instanceof String ref))return false;
|
||||
Map<String,Object> child=map(scope.get("task"));int limit=scope.get("limit") instanceof Number n?n.intValue():child.get("limit") instanceof Number n?n.intValue():10;
|
||||
for(var binding:bindings) {
|
||||
if(!Objects.equals(ref,binding.evidenceRef()))continue;
|
||||
if(!bindingMatchesScope(binding,scope))continue;
|
||||
if(AnalysisVisualSnapshotAccess.rankingChartMatchesArtifact(chart,binding.artifact(),child,release,limit,ref))return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private boolean chartPeriodMatches(Map<String,Object> actual,Map<String,Object> expected,boolean trend) {
|
||||
if(actual.isEmpty()||expected.isEmpty())return false;
|
||||
Map<String,Object> plain=new LinkedHashMap<>(actual);plain.remove("label");
|
||||
|
||||
@@ -59,6 +59,7 @@ public class AnalysisTurnService {
|
||||
private static final int PLAN_REVISIONS_MAX = 8;
|
||||
private static final int DEADLINE_SECONDS = 240;
|
||||
private static final int NAO_DEADLINE_SECONDS = Math.toIntExact(AnalysisRuntimePolicy.WATCHDOG_TIMEOUT.toSeconds());
|
||||
private static final int NAO_TOOL_CALLS_MAX = 1000;
|
||||
private static final int NATIVE_HISTORY_QUERY_LIMIT = 12;
|
||||
private static final int NATIVE_HISTORY_ENTRY_LIMIT = 8;
|
||||
private static final int NATIVE_HISTORY_CONTEXT_LIMIT = 16_000;
|
||||
@@ -345,8 +346,14 @@ public class AnalysisTurnService {
|
||||
|
||||
private Map<String, Object> visibleTurnPayload(Turn turn, AppUserPrincipal actor, Map<String, Object> context) {
|
||||
Map<String, Object> answer = read(turn.answerJson());
|
||||
if(protocol!=null&&protocol.ownsTurn(turn.id())&&"report".equals(mapValue(mapValue(readNullable(turn.planJson(),Map.of())).get("task")).get("kind"))
|
||||
&&Set.of("COMPLETED","PARTIAL").contains(turn.status()))protocol.authorizeReportSnapshot(turn.id(),actor);
|
||||
Map<String,Object> plan=readNullable(turn.planJson(),Map.of());
|
||||
Map<String,Object> task=mapValue(plan.get("task"));
|
||||
boolean authorizedHistoryReport=false;
|
||||
if(protocol!=null&&protocol.ownsTurn(turn.id())&&"report".equals(task.get("kind"))
|
||||
&&Set.of("COMPLETED","PARTIAL").contains(turn.status())) {
|
||||
protocol.authorizeReportSnapshot(turn.id(),actor);
|
||||
authorizedHistoryReport="confirmed_history_report".equals(answer.get("completionMethod"));
|
||||
}
|
||||
if(protocol!=null&&protocol.ownsTurn(turn.id())&&AnalysisProtocolInspection.KIND.equals(mapValue(mapValue(readNullable(turn.planJson(),Map.of())).get("task")).get("kind"))
|
||||
&&Set.of("COMPLETED","PARTIAL").contains(turn.status()))protocol.authorizeInspectionSnapshot(turn.id(),actor);
|
||||
Map<String, Object> budget = readNullable(turn.budgetJson(), Map.of());
|
||||
@@ -361,10 +368,10 @@ public class AnalysisTurnService {
|
||||
try { governance.requireEvidence(actor); result.put("evidence", readAny(turn.evidenceJson(), List.of())); }
|
||||
catch (ResponseStatusException denied) { result.put("evidence", List.of()); result.put("evidenceAvailable", false); }
|
||||
result.put("answer", answer.getOrDefault("answer", "")); result.put("results", filterResultList(answer.get("results"), actor));
|
||||
result.putAll(visibleVisuals(answer, turn, actor, context));
|
||||
result.putAll(visibleVisuals(answer, turn, actor, context,authorizedHistoryReport));
|
||||
for (String field : List.of("engine", "explanationEngine", "explanationWarning", "warning",
|
||||
"businessResultsSentToModel", "releaseId", "task", "analysisChecks", "agentDecisions", "completion",
|
||||
"reportMarkdown", "reportHtml", "reportTitle", "nativeChatId", "toolCallCount", "protocol", "queryEvidenceRefs", "actualModel", "phaseTimingsMs", "executionMode", "readingSummary", "scopeCoverage", "explorationWarnings", "conclusionProtocolQueryIds", "attemptProtocolQueryIds", "memberList", "recordList", "historySources", "historySections", "historyCoverage", "historyEvidence", "inspectionCoverage", "controlledInspection"))
|
||||
"reportMarkdown", "reportHtml", "reportTitle", "nativeChatId", "toolCallCount", "protocol", "queryEvidenceRefs", "actualModel", "phaseTimingsMs", "executionMode", "completionMethod", "readingSummary", "answerQuality", "scopeCoverage", "explorationWarnings", "conclusionProtocolQueryIds", "attemptProtocolQueryIds", "memberList", "recordList", "historySources", "historySections", "historyCoverage", "historyEvidence", "historyVisualSources", "inspectionCoverage", "controlledInspection"))
|
||||
if (answer.containsKey(field)) result.put(field, answer.get(field));
|
||||
result.put("stopReason", turn.stopReason()); result.put("budget", budget);
|
||||
result.putAll(jdbc.queryForObject("SELECT UNIX_TIMESTAMP(created_at)*1000,UNIX_TIMESTAMP(started_at)*1000,UNIX_TIMESTAMP(finished_at)*1000 FROM analysis_turn WHERE id=? AND user_id=?",
|
||||
@@ -935,6 +942,7 @@ public class AnalysisTurnService {
|
||||
String attemptId=attempt.attemptId();
|
||||
try {
|
||||
if(attempt.input().containsKey("controlledInspection")) {asSecurityActor(actor,()->{runProtocolInspection(attempt,actor,session);return null;});return;}
|
||||
initializeNativeBudget(turn.id(),turn.budgetJson());
|
||||
NaoAnalysisRunner.Result result=naoRunner.run(turn.id(),session.id(),actor,turn.question(),session.releaseId(),protocol.runtimeContext(attempt,actor),
|
||||
progress->{
|
||||
if(progress.isEmpty()||protocol.isStopped(attemptId))return;
|
||||
@@ -975,13 +983,7 @@ public class AnalysisTurnService {
|
||||
Map<String, Object> plan = new LinkedHashMap<>();
|
||||
Map<String, Object> submittedPlan = readNullable(turn.planJson(), Map.of());
|
||||
Map<String, Object> context = nativeContext(submittedPlan, session.releaseId());
|
||||
Map<String, Object> nativeBudget = readNullable(turn.budgetJson(), Map.of());
|
||||
nativeBudget.put("toolCallsMax", 1000);
|
||||
nativeBudget.put("planRevisionsMax", 0);
|
||||
nativeBudget.put("maxOutputTokens", AnalysisRuntimePolicy.MAX_OUTPUT_TOKENS);
|
||||
nativeBudget.put("deadlineAt", Instant.now().plusSeconds(NAO_DEADLINE_SECONDS).toString());
|
||||
jdbc.update("UPDATE analysis_turn SET budget_json=CAST(? AS JSON) WHERE id=? AND status='RUNNING'",
|
||||
write(nativeBudget), turnId);
|
||||
initializeNativeBudget(turnId,turn.budgetJson());
|
||||
plan.put("status", "NATIVE_NAO");
|
||||
plan.put("nativeRuntime", true);
|
||||
plan.put("engine", "NAO");
|
||||
@@ -1074,6 +1076,21 @@ public class AnalysisTurnService {
|
||||
}
|
||||
}
|
||||
|
||||
void initializeNativeBudget(String turnId,String currentBudgetJson) {
|
||||
Map<String,Object> nativeBudget=readNullable(currentBudgetJson,Map.of());
|
||||
nativeBudget.put("toolCallsMax",NAO_TOOL_CALLS_MAX);
|
||||
nativeBudget.put("toolCallsUsed",0);
|
||||
nativeBudget.put("toolCallsRemaining",NAO_TOOL_CALLS_MAX);
|
||||
nativeBudget.put("planRevisionsMax",0);
|
||||
nativeBudget.put("planRevisionsUsed",0);
|
||||
nativeBudget.put("maxOutputTokens",AnalysisRuntimePolicy.MAX_OUTPUT_TOKENS);
|
||||
nativeBudget.put("turnTimeoutSeconds",AnalysisRuntimePolicy.TURN_TIMEOUT.toSeconds());
|
||||
nativeBudget.put("requestTimeoutSeconds",AnalysisRuntimePolicy.REQUEST_TIMEOUT.toSeconds());
|
||||
nativeBudget.put("deadlineAt",Instant.now().plus(AnalysisRuntimePolicy.WATCHDOG_TIMEOUT).toString());
|
||||
jdbc.update("UPDATE analysis_turn SET budget_json=CAST(? AS JSON) WHERE id=? AND status='RUNNING'",
|
||||
write(nativeBudget),turnId);
|
||||
}
|
||||
|
||||
private Map<String, Object> nativeContext(Map<String, Object> persistedPlan, String releaseId) {
|
||||
Map<String, Object> frozen = mapValue(persistedPlan.get("frozenContext"));
|
||||
if (frozen.isEmpty()) frozen = mapValue(persistedPlan.get("context"));
|
||||
@@ -1603,6 +1620,14 @@ public class AnalysisTurnService {
|
||||
if (details.get("timeline") instanceof List<?>) answer.put("timeline", NaoAnalysisRunner.safeTimeline(details.get("timeline")));
|
||||
if(AnalysisProtocolExploration.nativeAgent(protocolTask))answer.put("executionMode","native_agent");
|
||||
var summary=boundedReadingSummary(details.get("readingSummary"),answer.get("queryEvidenceRefs"));if(!summary.isEmpty())answer.put("readingSummary",summary);
|
||||
if(details.get("answerQuality") instanceof Map<?,?> quality) {
|
||||
var safe=new LinkedHashMap<String,Object>();safe.put("version",1);
|
||||
for(String field:List.of("dataComplete","narrativeComplete","chartsComplete"))
|
||||
if(quality.get(field) instanceof Boolean value)safe.put(field,value);
|
||||
if(quality.get("missingEvidence") instanceof List<?> gaps)safe.put("missingEvidence",gaps.stream()
|
||||
.filter(value->value instanceof String gap&&!gap.isBlank()&&gap.length()<=1000).distinct().limit(20).toList());
|
||||
answer.put("answerQuality",safe);
|
||||
}
|
||||
Object count = details.get("toolCount");
|
||||
if (count instanceof Number) answer.put("toolCallCount", count);
|
||||
if(details.get("actualModel") instanceof String name&&name.matches("[A-Za-z0-9_./:@+\\-]{1,160}"))answer.put("actualModel",name);
|
||||
@@ -2751,9 +2776,15 @@ public class AnalysisTurnService {
|
||||
}
|
||||
private Map<String, Object> visibleVisuals(Map<String, Object> answer, Turn turn, AppUserPrincipal actor,
|
||||
Map<String, Object> expectedScope) {
|
||||
return visibleVisuals(answer,turn,actor,expectedScope,false);
|
||||
}
|
||||
|
||||
private Map<String, Object> visibleVisuals(Map<String, Object> answer, Turn turn, AppUserPrincipal actor,
|
||||
Map<String, Object> expectedScope,boolean authorizedHistoryReport) {
|
||||
if (!visualsEnabled) return AnalysisVisualSnapshotAccess.empty("not_requested").asMap();
|
||||
Set<String> allowedFields = governance.allowedFields(actor);
|
||||
Set<String> visibleFilters = AnalysisVisualSnapshotAccess.visibleExtraFilterKeys(jdbc, allowedFields);
|
||||
if(authorizedHistoryReport)return visibleHistoryVisuals(answer,turn,allowedFields,visibleFilters);
|
||||
Map<String, Object> scope = expectedScope;
|
||||
if (isNativePlan(readNullable(turn.planJson(), Map.of()))) {
|
||||
String releaseId = clean(expectedScope == null ? null : expectedScope.get("releaseId"));
|
||||
@@ -2762,6 +2793,66 @@ public class AnalysisTurnService {
|
||||
}
|
||||
return AnalysisVisualSnapshotAccess.fromAnswer(answer, allowedFields, visibleFilters, turn.id(), cleanContext(scope)).asMap();
|
||||
}
|
||||
|
||||
private Map<String,Object> visibleHistoryVisuals(Map<String,Object> answer,Turn turn,Set<String> allowedFields,
|
||||
Set<String> visibleFilters) {
|
||||
if(!"confirmed_history_report".equals(answer.get("completionMethod")))
|
||||
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
if(!"ready".equals(answer.get("visualState")))
|
||||
return AnalysisVisualSnapshotAccess.fromAnswer(answer,allowedFields,visibleFilters,turn.id(),Map.of()).asMap();
|
||||
if(!(answer.get("visuals") instanceof List<?> charts)||charts.isEmpty()
|
||||
||!(answer.get("historyVisualSources") instanceof List<?> lineages)||lineages.size()!=charts.size())
|
||||
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
|
||||
Map<String,Map<String,Object>> sourceByVisualId=new LinkedHashMap<>();
|
||||
Set<String> signedSourceTurns=new HashSet<>();
|
||||
for(Object raw:objectList(answer.get("historySources"))) {
|
||||
Map<String,Object> source=mapValue(raw);String id=clean(source.get("turnId"));
|
||||
if(Boolean.TRUE.equals(source.get("available"))&&id!=null)signedSourceTurns.add(id);
|
||||
}
|
||||
for(Object raw:lineages) {
|
||||
Map<String,Object> lineage=mapValue(raw);
|
||||
if(!lineage.keySet().containsAll(Set.of("visualId","sourceTurnId","scope","period","comparisonPeriod","dimension","stage","metric","evidenceRefs")))
|
||||
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
String visualId=clean(lineage.get("visualId"));
|
||||
String sourceTurnId=clean(lineage.get("sourceTurnId"));
|
||||
if(visualId==null||sourceTurnId==null||!signedSourceTurns.contains(sourceTurnId)
|
||||
||sourceByVisualId.putIfAbsent(visualId,lineage)!=null)
|
||||
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
}
|
||||
Set<String> reportRefs=new HashSet<>();
|
||||
for(Object ref:objectList(answer.get("queryEvidenceRefs")))if(ref instanceof String text)reportRefs.add(text);
|
||||
List<Map<String,Object>> checkedCharts=new ArrayList<>();Set<String> checkedIds=new HashSet<>();
|
||||
for(Object raw:charts) {
|
||||
Map<String,Object> chart=mapValue(raw);String visualId=clean(chart.get("id"));
|
||||
Map<String,Object> lineage=sourceByVisualId.get(visualId);
|
||||
if(lineage==null||!checkedIds.add(visualId))return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
if(!Objects.equals(turn.id(),chart.get("originTurnId"))
|
||||
||!sameCanonical(chart.get("scope"),lineage.get("scope"))
|
||||
||!sameCanonical(chart.get("period"),lineage.get("period"))
|
||||
||!sameCanonical(chart.get("comparisonPeriod"),lineage.get("comparisonPeriod"))
|
||||
||!sameCanonical(chart.get("dimension"),lineage.get("dimension"))
|
||||
||!sameCanonical(chart.get("stage"),lineage.get("stage"))
|
||||
||!sameCanonical(chart.get("metric"),lineage.get("metric"))
|
||||
||!sameCanonical(chart.get("evidenceRefs"),lineage.get("evidenceRefs")))
|
||||
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
List<?> refs=chart.get("evidenceRefs") instanceof List<?> values?values:List.of();
|
||||
if(refs.isEmpty()||!reportRefs.containsAll(refs))return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
Map<String,Object> sourceScope=mapValue(lineage.get("scope"));
|
||||
if(sourceScope.isEmpty())return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
Map<String,Object> rebound=new LinkedHashMap<>(chart);rebound.put("originTurnId",turn.id());
|
||||
Map<String,Object> oneChart=new LinkedHashMap<>(answer);oneChart.put("visuals",List.of(rebound));
|
||||
var checked=AnalysisVisualSnapshotAccess.fromAnswer(oneChart,allowedFields,visibleFilters,turn.id(),sourceScope);
|
||||
if(!"ready".equals(checked.state())||checked.visuals().size()!=1)
|
||||
return AnalysisVisualSnapshotAccess.empty("failed").asMap();
|
||||
checkedCharts.add(checked.visuals().getFirst());
|
||||
}
|
||||
return new AnalysisVisualSnapshotAccess.Snapshot(AnalysisVisualSnapshotAccess.SCHEMA_VERSION,"ready",List.copyOf(checkedCharts)).asMap();
|
||||
}
|
||||
|
||||
private boolean sameCanonical(Object left,Object right) {
|
||||
return Objects.equals(AnalysisProtocolService.canonical(left),AnalysisProtocolService.canonical(right));
|
||||
}
|
||||
private Map<String,Object> stringMap(Map<?,?> map) { Map<String,Object> out=new LinkedHashMap<>();map.forEach((k,v)->out.put(String.valueOf(k),v));return out; }
|
||||
private String write(Object value) { return json.writeValueAsString(value); }
|
||||
private String sha256(String value) { try{return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)));}catch(Exception e){throw new IllegalStateException(e);} }
|
||||
|
||||
+149
-8
@@ -96,6 +96,149 @@ public final class AnalysisVisualSnapshotAccess {
|
||||
allowedFields, visibleExtraFilterKeys, turnId, expectedScope);
|
||||
}
|
||||
|
||||
/** Binds a normalized ranking chart to the authoritative rank artifact and its real query reference. */
|
||||
static boolean rankingChartMatchesArtifact(Map<String,Object> chart, Map<String,Object> artifact,
|
||||
Map<String,Object> task, String release, int limit,
|
||||
String expectedEvidenceRef) {
|
||||
try {
|
||||
if (chart == null || artifact == null || task == null || expectedEvidenceRef == null
|
||||
|| !List.of(expectedEvidenceRef).equals(chart.get("evidenceRefs"))
|
||||
|| !Objects.equals(release, artifact.get("releaseId"))
|
||||
|| !Objects.equals(chart.get("dimension"), artifact.get("dimension"))
|
||||
|| !AnalysisProtocolService.list(task.get("dimensions")).contains(artifact.get("dimension"))
|
||||
|| !AnalysisProtocolChanges.sameFilters(stringMap(artifact.get("filters")), stringMap(task.get("filters")))
|
||||
|| !rankingPeriodMatches(artifact.get("period"), task.get("period"))
|
||||
|| !rankingPeriodMatches(chart.get("period"), task.get("period"))) return false;
|
||||
|
||||
String visualMetric = chart.get("metric") instanceof String value ? value : "";
|
||||
String stage;
|
||||
String field;
|
||||
if ("current_scrap_weight".equals(visualMetric)) {
|
||||
stage = "overall"; field = "scrapWeight";
|
||||
if (chart.get("stage") != null) return false;
|
||||
} else if (visualMetric.startsWith("black_") || visualMetric.startsWith("bright_")) {
|
||||
stage = visualMetric.startsWith("black_") ? "black" : "bright";
|
||||
if (!Objects.equals(stage, chart.get("stage"))) return false;
|
||||
field = visualMetric.endsWith("_scrap_weight") ? "scrapWeight"
|
||||
: visualMetric.endsWith("_total_weight") ? "totalWeight" : "";
|
||||
} else return false;
|
||||
if (field.isEmpty() || !AnalysisProtocolService.list(task.get("fields")).contains(field)
|
||||
|| !AnalysisProtocolService.list(task.get("stages")).isEmpty()
|
||||
&& !AnalysisProtocolService.list(task.get("stages")).contains(stage)) return false;
|
||||
|
||||
Map<String,Object> ranking = AnalysisProtocolService.list(artifact.get("rankings")).stream()
|
||||
.map(AnalysisProtocolService::map)
|
||||
.filter(item -> Objects.equals(stage,item.get("stage")) && Objects.equals(field,item.get("metric"))
|
||||
&& Boolean.TRUE.equals(item.get("completeRanking")))
|
||||
.findFirst().orElse(null);
|
||||
if (ranking == null || !"weight_desc_key_codepoint_asc".equals(ranking.get("orderPolicy"))) return false;
|
||||
Long totalRows = longInteger(ranking.get("totalRows"));
|
||||
BigDecimal denominator = decimal(ranking.get("denominator"));
|
||||
List<?> rows = ranking.get("rows") instanceof List<?> values ? values : null;
|
||||
if (totalRows == null || totalRows < 0 || rows == null || rows.size() != Math.min(10L,totalRows)
|
||||
|| denominator == null || denominator.signum() <= 0 || limit < 1 || limit > 10) return false;
|
||||
List<?> categories = chart.get("categories") instanceof List<?> values ? values : null;
|
||||
List<?> series = chart.get("series") instanceof List<?> values ? values : null;
|
||||
if (categories == null || series == null || series.size() != 1) return false;
|
||||
Map<String,Object> oneSeries = AnalysisProtocolService.map(series.getFirst());
|
||||
List<?> values = oneSeries.get("values") instanceof List<?> points ? points : null;
|
||||
int expectedRows = (int)Math.min(limit,totalRows);
|
||||
if (categories.size() != expectedRows || values == null || values.size() != expectedRows
|
||||
|| !Objects.equals(visualMetric,oneSeries.get("key"))) return false;
|
||||
Map<String,Object> coverage = stringMap(chart.get("coverage"));
|
||||
if (!Objects.equals("all_scope_weight",coverage.get("shareBasis"))
|
||||
|| !integerEquals(coverage.get("totalRows"),Math.toIntExact(totalRows))
|
||||
|| !integerEquals(coverage.get("returnedRows"),expectedRows)
|
||||
|| !Objects.equals(coverage.get("truncated"),expectedRows < totalRows)
|
||||
|| !sameDecimal(coverage.get("denominator"),denominator)) return false;
|
||||
|
||||
BigDecimal previousWeight = null;
|
||||
String previousKey = null;
|
||||
for (int index = 0; index < expectedRows; index++) {
|
||||
Map<String,Object> row = AnalysisProtocolService.map(rows.get(index));
|
||||
Map<String,Object> category = AnalysisProtocolService.map(categories.get(index));
|
||||
BigDecimal weight = decimal(row.get("weight"));
|
||||
String key = row.get("key") instanceof String text ? text : null;
|
||||
String label = row.get("label") instanceof String text ? text : null;
|
||||
if (integerEquals(row.get("rank"),index+1) == false || weight == null || weight.signum() <= 0
|
||||
|| key == null || key.isBlank() || label == null || label.isBlank()
|
||||
|| !Objects.equals(key,category.get("key")) || !Objects.equals(label,category.get("label"))
|
||||
|| !sameDecimal(values.get(index),weight)
|
||||
|| previousWeight != null && (previousWeight.compareTo(weight) < 0
|
||||
|| previousWeight.compareTo(weight) == 0 && codePointCompare(previousKey,key) > 0)) return false;
|
||||
previousWeight = weight; previousKey = key;
|
||||
}
|
||||
return true;
|
||||
} catch (RuntimeException invalid) { return false; }
|
||||
}
|
||||
|
||||
/** Accepts only a server-signed empty scrap ranking as proof that no chartable data exists. */
|
||||
static boolean rankingArtifactProvesEmptyScrap(Map<String,Object> artifact,Map<String,Object> task,
|
||||
String release,String dimension,String stage) {
|
||||
try {
|
||||
if(artifact==null||task==null||!Objects.equals(release,artifact.get("releaseId"))
|
||||
||!Objects.equals(dimension,artifact.get("dimension"))
|
||||
||!AnalysisProtocolService.list(task.get("dimensions")).contains(dimension)
|
||||
||!AnalysisProtocolService.list(task.get("fields")).contains("scrapWeight"))return false;
|
||||
List<?> taskStages=AnalysisProtocolService.list(task.get("stages"));
|
||||
if(taskStages.isEmpty()? !"overall".equals(stage) : !taskStages.contains(stage)||"overall".equals(stage))return false;
|
||||
if(!AnalysisProtocolChanges.sameFilters(stringMap(artifact.get("filters")),stringMap(task.get("filters")))
|
||||
||!rankingPeriodMatches(artifact.get("period"),task.get("period")))return false;
|
||||
Map<String,Object> ranking=AnalysisProtocolService.list(artifact.get("rankings")).stream()
|
||||
.map(AnalysisProtocolService::map)
|
||||
.filter(item->Objects.equals(stage,item.get("stage"))&&"scrapWeight".equals(item.get("metric")))
|
||||
.findFirst().orElse(null);
|
||||
if(ranking==null||!Boolean.TRUE.equals(ranking.get("completeRanking"))
|
||||
||!"weight_desc_key_codepoint_asc".equals(ranking.get("orderPolicy"))
|
||||
||!"no_positive_scrap".equals(ranking.get("emptyReason"))
|
||||
||!integerEquals(ranking.get("totalRows"),0)
|
||||
||!integerEquals(ranking.get("returnedRows"),0)
|
||||
||!Boolean.FALSE.equals(ranking.get("truncated")))return false;
|
||||
List<?> rows=ranking.get("rows") instanceof List<?> values?values:null;
|
||||
BigDecimal denominator=decimal(ranking.get("denominator"));
|
||||
return rows!=null&&rows.isEmpty()&&denominator!=null&&denominator.signum()==0;
|
||||
} catch(RuntimeException invalid) { return false; }
|
||||
}
|
||||
|
||||
private static boolean rankingPeriodMatches(Object actualRaw,Object expectedRaw) {
|
||||
Map<String,Object> actual = stringMap(actualRaw), expected = stringMap(expectedRaw);
|
||||
actual.remove("label"); actual.remove("granularity");
|
||||
if (actual.containsKey("start") || actual.containsKey("end")) {
|
||||
if (!"month".equals(stringMap(actualRaw).get("granularity")) || !(actual.get("start") instanceof String start)
|
||||
|| !(actual.get("end") instanceof String end) || !start.matches("[0-9]{4}-[0-9]{2}")
|
||||
|| !end.matches("[0-9]{4}-[0-9]{2}")) return false;
|
||||
Long year = number(expected.get("year")); if (year == null) return false;
|
||||
int first = expected.containsKey("monthStart") ? Math.toIntExact(number(expected.get("monthStart")))
|
||||
: expected.containsKey("month") ? Math.toIntExact(number(expected.get("month")))
|
||||
: expected.containsKey("quarter") ? (Math.toIntExact(number(expected.get("quarter")))-1)*3+1 : 1;
|
||||
int last = expected.containsKey("monthEnd") ? Math.toIntExact(number(expected.get("monthEnd")))
|
||||
: expected.containsKey("month") ? first : expected.containsKey("quarter") ? first+2 : 12;
|
||||
return start.equals(String.format(java.util.Locale.ROOT,"%04d-%02d",year,first))
|
||||
&& end.equals(String.format(java.util.Locale.ROOT,"%04d-%02d",year,last));
|
||||
}
|
||||
return Objects.equals(AnalysisProtocolService.canonical(actual),AnalysisProtocolService.canonical(expected));
|
||||
}
|
||||
|
||||
private static Long number(Object raw) {
|
||||
if (!(raw instanceof Number value)) return null;
|
||||
try { return Long.valueOf(value.toString()); }
|
||||
catch (NumberFormatException invalid) { return null; }
|
||||
}
|
||||
|
||||
private static boolean sameDecimal(Object left,Object right) {
|
||||
BigDecimal a=decimal(left),b=decimal(right);
|
||||
return a!=null&&b!=null&&a.compareTo(b)==0;
|
||||
}
|
||||
|
||||
private static int codePointCompare(String left,String right) {
|
||||
int i=0,j=0;
|
||||
while(i<left.length()&&j<right.length()) {
|
||||
int a=left.codePointAt(i),b=right.codePointAt(j);if(a!=b)return Integer.compare(a,b);
|
||||
i+=Character.charCount(a);j+=Character.charCount(b);
|
||||
}
|
||||
return Boolean.compare(i<left.length(),j<right.length());
|
||||
}
|
||||
|
||||
/** Current enabled filter fields whose source columns are both visible to this identity. */
|
||||
public static Set<String> visibleExtraFilterKeys(JdbcTemplate jdbc, Set<String> allowedFields) {
|
||||
Set<String> allowed = allowedFields == null ? Set.of() : allowedFields;
|
||||
@@ -174,7 +317,7 @@ public final class AnalysisVisualSnapshotAccess {
|
||||
|| !validWaterfallChart(type, metric, unit, semantics, categories, series, coverage, period, comparisonPeriod)) return null;
|
||||
if ("all_scope_weight".equals(coverage.get("shareBasis"))
|
||||
&& (!Set.of("product","department","group").contains(dimension)
|
||||
|| !Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight").contains(metric))) return null;
|
||||
|| !Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight","current_scrap_weight").contains(metric))) 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");
|
||||
@@ -240,7 +383,7 @@ public final class AnalysisVisualSnapshotAccess {
|
||||
BigDecimal decimal = decimal(point);
|
||||
if (decimal == null || decimal.precision() > 64 || Math.abs(decimal.scale()) > 64) return null;
|
||||
if (Set.of("composite_rate", "black_rate", "bright_rate").contains(metric)
|
||||
&& (decimal.signum() < 0 || decimal.compareTo(BigDecimal.ONE) > 0)) return null;
|
||||
&& decimal.signum() < 0) return null;
|
||||
if (Set.of("black_scrap_rate", "bright_scrap_rate").contains(metric) && decimal.signum() < 0) return null;
|
||||
if (Set.of("black_good_weight", "black_scrap_weight", "black_total_weight",
|
||||
"black_responsibility_scrap", "black_material_scrap", "black_external_scrap",
|
||||
@@ -249,8 +392,6 @@ public final class AnalysisVisualSnapshotAccess {
|
||||
"bright_processing_scrap", "bright_diffusion_scrap", "current_scrap_weight",
|
||||
"source_defect_weight").contains(metric) && decimal.signum() < 0
|
||||
&& !("line".equals(type)&&"black_total_weight".equals(metric))) 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);
|
||||
}
|
||||
}
|
||||
@@ -470,8 +611,8 @@ public final class AnalysisVisualSnapshotAccess {
|
||||
Map<String, Object> comparisonPeriod) {
|
||||
if (!Set.of("donut", "pareto").contains(type)) return true;
|
||||
if ("pareto".equals(type) && "all_scope_weight".equals(coverage.get("shareBasis"))) {
|
||||
if (!Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight").contains(metric)
|
||||
|| !"kg".equals(unit) || semantics.stage() == null || comparisonPeriod != null
|
||||
if (!Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight","current_scrap_weight").contains(metric)
|
||||
|| !"kg".equals(unit) || semantics.stage() == null&&!"current_scrap_weight".equals(metric) || comparisonPeriod != null
|
||||
|| categories.isEmpty() || categories.size() > 10 || series.size() != 1
|
||||
|| !integerEquals(coverage.get("returnedRows"),categories.size())) return false;
|
||||
BigDecimal denominator = decimal(coverage.get("denominator")), sum = BigDecimal.ZERO, previous = null;
|
||||
@@ -528,7 +669,7 @@ public final class AnalysisVisualSnapshotAccess {
|
||||
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;
|
||||
if (points[index] == null) return false;
|
||||
}
|
||||
BigDecimal tolerance = new BigDecimal("0.000001");
|
||||
return points[0].add(points[1]).subtract(points[2]).abs().compareTo(tolerance) <= 0;
|
||||
@@ -552,7 +693,7 @@ public final class AnalysisVisualSnapshotAccess {
|
||||
MetricSemantics semantics, Map<String, Object> scope,
|
||||
Map<String, Object> period) {
|
||||
if (section == null && dimension == null) return true; // schema-1 snapshots created before chapter metadata
|
||||
if (Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight").contains(metric)
|
||||
if (Set.of("black_total_weight","bright_total_weight","black_scrap_weight","bright_scrap_weight","current_scrap_weight").contains(metric)
|
||||
&& Set.of("bar","column","pareto").contains(type)
|
||||
&& dimension != null && Set.of("department","group","product").contains(dimension))
|
||||
return section == null || section.equals(dimension);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package cn.yunsheng.quality.analysis;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** Upgrade the current immutable release once; older releases retain the exact source fallback. */
|
||||
@Component
|
||||
public class FineDefectRollupBackfill implements ApplicationRunner {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FineDefectRollupBackfill.class);
|
||||
private final JdbcTemplate jdbc;
|
||||
private final FineDefectRollups rollups;
|
||||
|
||||
public FineDefectRollupBackfill(JdbcTemplate jdbc, FineDefectRollups rollups) { this.jdbc = jdbc; this.rollups = rollups; }
|
||||
|
||||
@Override public void run(ApplicationArguments args) {
|
||||
var missing = jdbc.query("""
|
||||
SELECT DISTINCT b.id FROM current_release c JOIN release_member rm ON rm.release_id=c.release_id
|
||||
JOIN transform_build b ON b.id=rm.transform_build_id
|
||||
LEFT JOIN fine_defect_rollup_build rb ON rb.build_id=b.id
|
||||
WHERE c.dataset_id='quality' AND b.status='COMPLETED' AND rb.build_id IS NULL ORDER BY b.id
|
||||
""", (rs, row) -> rs.getString(1));
|
||||
long start = System.nanoTime();
|
||||
for (String build : missing) rollups.refreshBuild(build);
|
||||
if (!missing.isEmpty()) LOG.info("Fine defect rollup backfill: builds={}, elapsedMs={}", missing.size(), (System.nanoTime() - start) / 1_000_000L);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package cn.yunsheng.quality.analysis;
|
||||
|
||||
import cn.yunsheng.quality.metrics.MetricsRollupService.GroupKey;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.ResultSetExtractor;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
import static cn.yunsheng.quality.analysis.AnalysisFineDefectsService.*;
|
||||
|
||||
/** Mergeable source statistics, frozen in the same transaction as each transform build. */
|
||||
@Service
|
||||
public class FineDefectRollups {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final JsonMapper json;
|
||||
|
||||
public FineDefectRollups(JdbcTemplate jdbc, JsonMapper json) { this.jdbc = jdbc; this.json = json; }
|
||||
|
||||
@Transactional
|
||||
public void refreshBuild(String build) {
|
||||
jdbc.update("DELETE FROM fine_defect_rollup_build WHERE build_id=?", build);
|
||||
jdbc.update("DELETE FROM fine_defect_rollup WHERE build_id=?", build);
|
||||
long rows = 0;
|
||||
for (String stage : List.of("black", "bright")) {
|
||||
var groups = aggregate(build, stage, "", List.of());
|
||||
if (!representable(groups)) return; // Unusual precision keeps the exact source-query fallback.
|
||||
insert(build, groups);
|
||||
rows += groups.values().stream().mapToLong(a -> a.rollupStats().getFirst().rows()).sum();
|
||||
}
|
||||
refreshOrganizations(build, "", List.of());
|
||||
jdbc.update("INSERT INTO fine_defect_rollup_build(build_id,source_row_count) VALUES(?,?)", build, rows);
|
||||
}
|
||||
|
||||
public void copyBuild(String parent, String child) {
|
||||
Integer ready = jdbc.queryForObject("SELECT COUNT(*) FROM fine_defect_rollup_build WHERE build_id=?", Integer.class, parent);
|
||||
if (ready == null || ready != 1) { refreshBuild(child); return; }
|
||||
jdbc.update("""
|
||||
INSERT INTO fine_defect_rollup(build_id,stage,month_key,department,group_name,project_group,product,grain,
|
||||
stat_kind,field_key,weight_sum,present_rows,missing_rows,invalid_rows,negative_rows,source_row_count)
|
||||
SELECT ?,stage,month_key,department,group_name,project_group,product,grain,
|
||||
stat_kind,field_key,weight_sum,present_rows,missing_rows,invalid_rows,negative_rows,source_row_count
|
||||
FROM fine_defect_rollup WHERE build_id=?
|
||||
""", child, parent);
|
||||
jdbc.update("INSERT INTO fine_defect_rollup_build(build_id,source_row_count) SELECT ?,source_row_count FROM fine_defect_rollup_build WHERE build_id=?", child, parent);
|
||||
}
|
||||
|
||||
public void refreshGroups(String build, Set<GroupKey> keys) {
|
||||
for (String stage : List.of("black", "bright")) {
|
||||
var selected = keys.stream().filter(k -> stage.equals(k.stage())).toList();
|
||||
if (selected.isEmpty()) continue;
|
||||
String predicate = String.join(" OR ", java.util.Collections.nCopies(selected.size(),
|
||||
"(f.month_key<=>? AND f.department COLLATE utf8mb4_0900_as_cs<=>? AND f.group_name COLLATE utf8mb4_0900_as_cs<=>? AND f.project_group COLLATE utf8mb4_0900_as_cs<=>? AND f.product COLLATE utf8mb4_0900_as_cs<=>?)"));
|
||||
List<Object> args = new ArrayList<>();
|
||||
for (var key : selected) { args.add(key.month()); args.add(key.department()); args.add(key.group()); args.add(key.project()); args.add(key.product()); }
|
||||
List<Object> deletedArgs = new ArrayList<>(List.of(build, stage)); deletedArgs.addAll(args);
|
||||
jdbc.update("DELETE f FROM fine_defect_rollup f WHERE f.build_id=? AND f.stage=? AND f.grain='detail' AND (" + predicate + ")", deletedArgs.toArray());
|
||||
var groups = aggregate(build, stage, " AND (" + predicate + ")", args);
|
||||
if (!representable(groups)) { jdbc.update("DELETE FROM fine_defect_rollup_build WHERE build_id=?", build); return; }
|
||||
insert(build, groups);
|
||||
}
|
||||
var organizations = keys.stream().map(k -> new GroupKey(k.stage(), k.month(), k.department(), k.group(), null, null)).distinct().toList();
|
||||
if (!organizations.isEmpty()) {
|
||||
String predicate = String.join(" OR ", java.util.Collections.nCopies(organizations.size(),
|
||||
"(f.stage=? AND f.month_key<=>? AND f.department COLLATE utf8mb4_0900_as_cs<=>? AND f.group_name COLLATE utf8mb4_0900_as_cs<=>?)"));
|
||||
List<Object> args = new ArrayList<>();
|
||||
for (var key : organizations) { args.add(key.stage()); args.add(key.month()); args.add(key.department()); args.add(key.group()); }
|
||||
refreshOrganizations(build, " AND (" + predicate + ")", args);
|
||||
}
|
||||
}
|
||||
|
||||
private void refreshOrganizations(String build, String predicate, List<Object> scopeArgs) {
|
||||
List<Object> args = new ArrayList<>(List.of(build)); args.addAll(scopeArgs);
|
||||
jdbc.update("DELETE f FROM fine_defect_rollup f WHERE f.build_id=? AND f.grain='organization'" + predicate, args.toArray());
|
||||
jdbc.update("""
|
||||
INSERT INTO fine_defect_rollup(build_id,stage,month_key,department,group_name,project_group,product,grain,
|
||||
stat_kind,field_key,weight_sum,present_rows,missing_rows,invalid_rows,negative_rows,source_row_count)
|
||||
SELECT f.build_id,f.stage,f.month_key,f.department,f.group_name,NULL,NULL,'organization',
|
||||
f.stat_kind,f.field_key,SUM(f.weight_sum),SUM(f.present_rows),SUM(f.missing_rows),
|
||||
SUM(f.invalid_rows),SUM(f.negative_rows),SUM(f.source_row_count)
|
||||
FROM fine_defect_rollup f WHERE f.build_id=? AND f.grain='detail'
|
||||
""" + predicate + " GROUP BY f.build_id,f.stage,f.month_key,f.department,f.group_name,f.stat_kind,f.field_key", args.toArray());
|
||||
}
|
||||
|
||||
boolean read(Aggregator aggregate, Request request, Set<String> allowed, String predicate, List<Object> scopeArgs) {
|
||||
Integer missing = jdbc.queryForObject("""
|
||||
SELECT COUNT(*) FROM release_member rm LEFT JOIN fine_defect_rollup_build rb ON rb.build_id=rm.transform_build_id
|
||||
WHERE rm.release_id=? AND rb.build_id IS NULL
|
||||
""", Integer.class, scopeArgs.getFirst());
|
||||
if (missing == null || missing > 0) return false;
|
||||
List<Object> args = new ArrayList<>(scopeArgs);
|
||||
args.add(request.stage());
|
||||
// Scope predicates are generated by the shared authorization helper, never supplied as SQL by a caller.
|
||||
boolean organization = !predicate.contains("f.product") && !predicate.contains("f.project_group");
|
||||
args.add(organization ? "organization" : "detail");
|
||||
List<String> selected = new ArrayList<>();
|
||||
for (var stat : aggregate.rollupStats()) {
|
||||
selected.add("(f.stat_kind=? AND f.field_key=?)"); args.add(stat.kind()); args.add(stat.key());
|
||||
}
|
||||
String sql = """
|
||||
SELECT f.stat_kind,f.field_key,SUM(f.weight_sum) AS weight_sum,
|
||||
SUM(f.present_rows) AS present_rows,SUM(f.missing_rows) AS missing_rows,
|
||||
SUM(f.invalid_rows) AS invalid_rows,SUM(f.negative_rows) AS negative_rows,
|
||||
SUM(f.source_row_count) AS source_row_count
|
||||
FROM release_member rm
|
||||
JOIN transform_build b ON b.id=rm.transform_build_id AND b.source_revision_id=rm.source_revision_id
|
||||
JOIN source_revision sr ON sr.id=rm.source_revision_id
|
||||
JOIN fine_defect_rollup f
|
||||
""" + (organization ? " FORCE INDEX (ix_fine_rollup_build_month) " : " ")
|
||||
+ " ON f.build_id=rm.transform_build_id " + predicate
|
||||
+ " AND f.stage=? AND f.grain=? AND (" + String.join(" OR ", selected) + ") GROUP BY f.stat_kind,f.field_key";
|
||||
jdbc.query(sql, (RowCallbackHandler) rs -> aggregate.mergeRollup(new Aggregator.RollupStat(
|
||||
rs.getString("stat_kind"), rs.getString("field_key"), rs.getBigDecimal("weight_sum"),
|
||||
rs.getLong("present_rows"), rs.getLong("missing_rows"), rs.getLong("invalid_rows"),
|
||||
rs.getLong("negative_rows"), rs.getLong("source_row_count")), allowed), args.toArray());
|
||||
return true;
|
||||
}
|
||||
|
||||
private Map<GroupKey, Aggregator> aggregate(String build, String stage, String predicate, List<Object> scopeArgs) {
|
||||
Request request = new Request(build, "top", null, null, null, null, null, null, null, null,
|
||||
true, stage, "defect", null, null, 100, Map.of());
|
||||
var fields = fields(stage); var categories = categories(stage);
|
||||
Set<String> components = new LinkedHashSet<>();
|
||||
for (var category : categories) if (fields.stream().anyMatch(f -> f.category().equals(category.name()))) components.add(category.componentCode());
|
||||
List<String> rawKeys = new ArrayList<>(fields.stream().map(DefectField::key).toList());
|
||||
categories.stream().map(c -> ExpectedSourceDetailPolicy.totalField(c.componentCode())).filter(java.util.Objects::nonNull).forEach(rawKeys::add);
|
||||
String sql = "SELECT f.month_key,f.department,f.group_name,f.project_group,f.product," + jsonObject("f.raw_values", rawKeys)
|
||||
+ " AS raw_fields," + jsonObject("f.components", new ArrayList<>(components)) + " AS formal_components FROM fact_" + stage + " f WHERE f.build_id=?" + predicate;
|
||||
List<Object> args = new ArrayList<>(List.of(build)); args.addAll(scopeArgs);
|
||||
Map<GroupKey, Aggregator> groups = new LinkedHashMap<>();
|
||||
jdbc.query(connection -> {
|
||||
var statement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
statement.setFetchSize(Integer.MIN_VALUE); new ArgumentPreparedStatementSetter(args.toArray()).setValues(statement); return statement;
|
||||
}, (ResultSetExtractor<Void>) rs -> {
|
||||
while (rs.next()) {
|
||||
GroupKey key = new GroupKey(stage, rs.getString("month_key"), rs.getString("department"), rs.getString("group_name"), rs.getString("project_group"), rs.getString("product"));
|
||||
groups.computeIfAbsent(key, ignored -> new Aggregator(request, fields, categories, components, json))
|
||||
.add(readObject(json, rs.getString("raw_fields")), readObject(json, rs.getString("formal_components")), key.product(), key.group());
|
||||
}
|
||||
return null;
|
||||
});
|
||||
return groups;
|
||||
}
|
||||
|
||||
private static boolean representable(Map<GroupKey, Aggregator> groups) {
|
||||
Map<String,BigDecimal> sums = new LinkedHashMap<>();
|
||||
if (groups.values().stream().flatMap(a -> a.rollupStats().stream()).anyMatch(s -> !representable(s.sum()))) return false;
|
||||
groups.values().forEach(a -> a.rollupStats().forEach(s -> sums.merge(s.kind() + ":" + s.key(), s.sum(), BigDecimal::add)));
|
||||
return sums.values().stream().allMatch(FineDefectRollups::representable);
|
||||
}
|
||||
|
||||
private static boolean representable(BigDecimal value) {
|
||||
BigDecimal sum = value.stripTrailingZeros(); return sum.scale() <= 20 && sum.precision() - sum.scale() <= 45;
|
||||
}
|
||||
|
||||
private void insert(String build, Map<GroupKey, Aggregator> groups) {
|
||||
List<Object[]> batch = new ArrayList<>();
|
||||
groups.forEach((key, aggregate) -> aggregate.rollupStats().forEach(s -> batch.add(new Object[] {
|
||||
build,key.stage(),key.month(),key.department(),key.group(),key.project(),key.product(),
|
||||
s.kind(),s.key(),s.sum(),s.present(),s.missing(),s.invalid(),s.negative(),s.rows()})));
|
||||
for (int offset = 0; offset < batch.size(); offset += 500) {
|
||||
var selected = batch.subList(offset, Math.min(batch.size(), offset + 500));
|
||||
List<Object> values = new ArrayList<>(); selected.forEach(row -> java.util.Collections.addAll(values, row));
|
||||
jdbc.update("""
|
||||
INSERT INTO fine_defect_rollup(build_id,stage,month_key,department,group_name,project_group,product,grain,
|
||||
stat_kind,field_key,weight_sum,present_rows,missing_rows,invalid_rows,negative_rows,source_row_count)
|
||||
VALUES
|
||||
""" + String.join(",", java.util.Collections.nCopies(selected.size(), "(?,?,?,?,?,?,?,'detail',?,?,?,?,?,?,?,?)")), values.toArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,8 +79,15 @@ public class ApiExceptionHandler {
|
||||
ProblemDetail unexpected(Exception exception, HttpServletRequest request) {
|
||||
// Record only classes and frames: exception messages may contain request
|
||||
// values, database parameters, or upstream credentials.
|
||||
LOG.error("Unexpected API failure: {} frames={}", exception.getClass().getName(),
|
||||
LOG.error("Unexpected API failure: {} {} {} frames={}", request.getMethod(), request.getRequestURI(), exception.getClass().getName(),
|
||||
java.util.Arrays.stream(exception.getStackTrace()).limit(14).toList());
|
||||
Throwable cause = exception.getCause();
|
||||
var seen = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<Throwable, Boolean>());
|
||||
seen.add(exception);
|
||||
for (int depth = 0; cause != null && depth < 4 && seen.add(cause); depth++, cause = cause.getCause()) {
|
||||
LOG.error("API failure cause: {} frames={}", cause.getClass().getName(),
|
||||
java.util.Arrays.stream(cause.getStackTrace()).limit(8).toList());
|
||||
}
|
||||
ProblemDetail detail = ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "服务处理失败");
|
||||
detail.setProperty("path", request.getRequestURI());
|
||||
detail.setProperty("timestamp", Instant.now().toString());
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package cn.yunsheng.quality.governance;
|
||||
|
||||
import cn.yunsheng.quality.identity.AppUserPrincipal;
|
||||
import cn.yunsheng.quality.common.ImmutableQueryCache;
|
||||
import java.util.*;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/** Original rows for the clicked business-group inventory key, never a recalculation. */
|
||||
@Service
|
||||
public class ClassificationSourceService {
|
||||
private final GovernanceSupport g;
|
||||
private final CandidateAcceptanceService access;
|
||||
private final GovernanceAccessService fields;
|
||||
private record Key(String release,String stage,List<ClassificationPreviewService.Condition> conditions) {
|
||||
Key {conditions=List.copyOf(conditions);}
|
||||
}
|
||||
private final ImmutableQueryCache<Key,List<String>> indexes=new ImmutableQueryCache<>(32);
|
||||
public ClassificationSourceService(GovernanceSupport g,CandidateAcceptanceService access,GovernanceAccessService fields) {
|
||||
this.g=g;this.access=access;this.fields=fields;
|
||||
}
|
||||
public record Request(String stage,List<ClassificationPreviewService.Condition> conditions,Integer page,Integer pageSize,String releaseId) {}
|
||||
public Map<String,Object> list(Request request,AppUserPrincipal actor) {
|
||||
if(request==null||!Set.of("black","bright").contains(Objects.toString(request.stage(),"")))throw GovernanceSupport.error(HttpStatus.BAD_REQUEST,"来源阶段无效");
|
||||
ClassificationPreviewService.validate(new ClassificationPreviewService.Request(request.stage(),"原始数据",request.conditions()));
|
||||
int page=request.page()==null?1:request.page(),size=request.pageSize()==null?15:request.pageSize();
|
||||
if(page<1||size<1||size>100)throw GovernanceSupport.error(HttpStatus.BAD_REQUEST,"分页无效");
|
||||
access.requireSourceDetail(actor);
|
||||
var current=g.rows("SELECT release_id FROM current_release WHERE dataset_id='quality'");
|
||||
if(current.isEmpty())return Map.of("rows",List.of(),"total",0,"page",page,"pageSize",size);
|
||||
String release=request.releaseId()==null?String.valueOf(current.getFirst().get("releaseId")):request.releaseId();
|
||||
if(g.rows("SELECT id FROM data_release WHERE id=? AND status IN ('PUBLISHED','ACTIVE')",release).isEmpty())throw GovernanceSupport.error(HttpStatus.NOT_FOUND,"正式数据版本不存在");
|
||||
access.requireRead(release,actor);
|
||||
Map<String,String> expressions=Map.of("@department","f.department","@product","f.product","PDT代码","JSON_VALUE(f.components,'$.sourcePdtCode')","销售片区","JSON_VALUE(f.components,'$.sourceSalesRegion')");
|
||||
List<Object> args=new ArrayList<>();args.add(release);
|
||||
StringBuilder where=new StringBuilder(" WHERE rm.release_id=? AND (f.group_name IS NULL OR TRIM(f.group_name)='')");
|
||||
for(var condition:request.conditions()) {
|
||||
String expression=expressions.get(condition.field());
|
||||
if(condition.operator().equals("IS_BLANK"))where.append(" AND (").append(expression).append(" IS NULL OR REGEXP_LIKE(").append(expression).append(",'^[[:space:]]*$'))");
|
||||
else {where.append(" AND CAST(").append(expression).append(" AS BINARY)=CAST(? AS BINARY)");args.add(condition.value());}
|
||||
}
|
||||
String from=" FROM release_member rm JOIN fact_"+request.stage()+" f ON f.build_id=rm.transform_build_id JOIN source_revision sr ON sr.id=rm.source_revision_id";
|
||||
var ids=indexes.get(new Key(release,request.stage(),request.conditions()),()->g.jdbc.queryForList("SELECT f.id"+from+where+" ORDER BY f.month_key,sr.source_file,f.sheet_name,f.physical_row_no,f.id",String.class,args.toArray()));
|
||||
int start=(int)Math.min(ids.size(),(long)(page-1)*size);var selected=ids.subList(start,Math.min(ids.size(),start+size));
|
||||
Set<String> allowed=fields.allowedFields(actor);
|
||||
List<Map<String,Object>> rows=new ArrayList<>();
|
||||
if(!selected.isEmpty()) {
|
||||
List<Object> rowArgs=new ArrayList<>(args);rowArgs.addAll(selected);
|
||||
var fetched=g.rows("SELECT f.id,sr.source_file,f.sheet_name AS sheet,f.physical_row_no AS `row_number`,f.month_key AS month,f.department,f.product,f.raw_values"+from+where+" AND f.id IN ("+String.join(",",Collections.nCopies(selected.size(),"?"))+")",rowArgs.toArray());
|
||||
Map<String,Map<String,Object>> byId=new HashMap<>();
|
||||
for(var row:fetched) {
|
||||
Object decoded=g.decode(Objects.toString(row.remove("rawValues"),"{}"));Map<String,Object> visible=new LinkedHashMap<>();
|
||||
if(decoded instanceof Map<?,?> raw)raw.forEach((key,value)->{if(allowed.contains("*")||allowed.contains(String.valueOf(key)))visible.put(String.valueOf(key),value);});
|
||||
row.put("raw",visible);row.put("stage",request.stage());byId.put(String.valueOf(row.get("id")),row);
|
||||
}
|
||||
for(String id:selected)if(byId.containsKey(id))rows.add(byId.get(id));
|
||||
}
|
||||
return Map.of("rows",rows,"total",ids.size(),"page",page,"pageSize",size,"releaseId",release);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,9 @@ public class MappingIssueController {
|
||||
private final MappingIssueService service;
|
||||
private final UnclassifiedSourceService unclassified;
|
||||
private final ClassificationPreviewService preview;
|
||||
public MappingIssueController(MappingIssueService service, UnclassifiedSourceService unclassified,ClassificationPreviewService preview){this.service=service;this.unclassified=unclassified;this.preview=preview;}
|
||||
private final ClassificationSourceService sources;
|
||||
public MappingIssueController(MappingIssueService service, UnclassifiedSourceService unclassified,ClassificationPreviewService preview,ClassificationSourceService sources){this.service=service;this.unclassified=unclassified;this.preview=preview;this.sources=sources;}
|
||||
@PostMapping("/classification-sources") public Map<String,Object> classificationSources(@RequestBody ClassificationSourceService.Request request,@AuthenticationPrincipal AppUserPrincipal actor){return sources.list(request,actor);}
|
||||
@PostMapping("/classification-preview") public Map<String,Object> classificationPreview(@RequestBody ClassificationPreviewService.Request request,@AuthenticationPrincipal AppUserPrincipal actor){return preview.preview(request,actor);}
|
||||
@GetMapping("/classification-metadata") public Map<String,Object> classificationMetadata(){return cn.yunsheng.quality.ingestion.ConfigurableMatchRules.metadata();}
|
||||
@GetMapping("/unclassified") public Map<String,Object> unclassified(@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="50") int pageSize,@AuthenticationPrincipal AppUserPrincipal actor){return unclassified.list(page,pageSize,actor);}
|
||||
|
||||
+12
-2
@@ -89,20 +89,30 @@ public final class UnclassifiedMappingRebuild {
|
||||
changes.add(new Change(stage,text(row.get("build_id")),text(row.get("sheet_name")),((Number)row.get("physical_row_no")).intValue(),text(row.get("month_key")),text(row.get("department")),text(row.get("product")),oldGroup,oldProject,group,project,groupChanged,projectChanged,components));
|
||||
}
|
||||
}
|
||||
var changesByBuild=new LinkedHashMap<String,List<Change>>();
|
||||
for(var change:changes)changesByBuild.computeIfAbsent(change.parent(),ignored->new ArrayList<>()).add(change);
|
||||
var changedMonths=changes.stream().map(Change::month).distinct().sorted().toList();
|
||||
String release=UUID.randomUUID().toString(),run=UUID.randomUUID().toString();
|
||||
int releaseInserted=jdbc.update("INSERT INTO data_release(id,name,status,parent_release_id,expected_current_release_id,rule_version,mapping_version,target_version_id,complete,validation,created_by) SELECT ?,?,'CANDIDATE',id,id,rule_version,?,target_version_id,TRUE,JSON_SET(COALESCE(validation,JSON_OBJECT()),'$.recalculationScope','UNCLASSIFIED','$.scannedRows',?,'$.classifiedRows',?,'$.excelFilesParsed',0),? FROM data_release WHERE id=? AND rule_version=?",release,"未分类归类 "+operation.substring(0,8),mapping,scanned,changes.size(),actor.getId().toString(),parent,rule);
|
||||
if(releaseInserted!=1)throw new IllegalStateException("当前数据版本已变化,未分类归类未提交");
|
||||
jdbc.update("INSERT INTO import_run(id,idempotency_key,status,stage,requested_sources,expected_current_release_id,created_by,processed,total,result_release_id,expected_mapping_version,expected_rule_version,workflow_operation_id,validation,started_at,completed_at) VALUES(?,?,'COMPLETED','CLASSIFICATION_READY',JSON_ARRAY(),?,?,?,?,?,?,?,?,JSON_OBJECT('recalculationScope','UNCLASSIFIED','excelFilesParsed',0),CURRENT_TIMESTAMP(6),CURRENT_TIMESTAMP(6))",run,"UNCLASSIFIED:"+operation,parent,actor.getId().toString(),changes.size(),scanned,release,mapping,rule,operation);
|
||||
var members=jdbc.queryForList("SELECT m.source_document_id,m.source_revision_id,m.transform_build_id FROM release_member m WHERE m.release_id=?",parent);
|
||||
jdbc.update("UPDATE data_release SET validation=JSON_SET(validation,'$.classificationSnapshotReuse',1,'$.classificationChangedMonths',CAST(? AS JSON),'$.classificationCopiedSources',?,'$.classificationReusedSources',?) WHERE id=?",encode(changedMonths),changesByBuild.size(),members.size()-changesByBuild.size(),release);
|
||||
var changedOrigins=new ArrayList<cn.yunsheng.quality.governance.MappingIssueService.SourceRow>();
|
||||
for(var member:members){
|
||||
String old=text(member.get("transform_build_id")),build=UUID.randomUUID().toString();
|
||||
String old=text(member.get("transform_build_id"));
|
||||
var selectedChanges=changesByBuild.get(old);
|
||||
if(selectedChanges==null){
|
||||
jdbc.update("INSERT INTO release_member(release_id,source_document_id,source_revision_id,transform_build_id) VALUES(?,?,?,?)",release,member.get("source_document_id"),member.get("source_revision_id"),old);
|
||||
continue;
|
||||
}
|
||||
String build=UUID.randomUUID().toString();
|
||||
int inserted=jdbc.update("INSERT INTO transform_build(id,source_revision_id,build_key,parser_version,rule_version,mapping_version,status,row_count,rejected_count,component_summary,error_summary,started_at,completed_at,parser_config,parser_config_hash) SELECT ?,source_revision_id,?,parser_version,rule_version,?,'COMPLETED',row_count,rejected_count,JSON_SET(COALESCE(component_summary,JSON_OBJECT()),'$.classificationParentBuildId',id),error_summary,CURRENT_TIMESTAMP(6),CURRENT_TIMESTAMP(6),parser_config,parser_config_hash FROM transform_build WHERE id=? AND status='COMPLETED' AND rule_version=?",build,hash("UNCLASSIFIED\n"+operation+"\n"+old+"\n"+mapping),mapping,old,rule);if(inserted!=1)throw new IllegalStateException("来源快照已变化");
|
||||
// 只复制已冻结结果,不解析文件、不重算已分类行的数值或维度。
|
||||
for(String stage:List.of("black","bright"))jdbc.update("INSERT INTO fact_"+stage+"(build_id,source_revision_id,sheet_name,physical_row_no,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,raw_values,components) SELECT ?,source_revision_id,sheet_name,physical_row_no,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,raw_values,components FROM fact_"+stage+" WHERE build_id=?",build,old);
|
||||
jdbc.update("INSERT INTO defect_component(build_id,stage,sheet_name,physical_row_no,defect_name,weight,source_field) SELECT ?,stage,sheet_name,physical_row_no,defect_name,weight,source_field FROM defect_component WHERE build_id=?",build,old);
|
||||
rollups.copyBuild(old,build);var keys=new LinkedHashSet<MetricsRollupService.GroupKey>();
|
||||
for(var change:changes)if(change.parent().equals(old)){
|
||||
for(var change:selectedChanges){
|
||||
int updated=jdbc.update("UPDATE fact_"+change.stage()+" SET group_name=?,project_group=?,components=CAST(? AS JSON) WHERE build_id=? AND sheet_name=? AND physical_row_no=? AND (group_name<=>?) AND (project_group<=>?) AND "+(change.groupChanged()?"(group_name IS NULL OR TRIM(group_name)='')":"(project_group IS NULL OR TRIM(project_group)='')"),change.group(),change.project(),encode(change.components()),build,change.sheet(),change.row(),change.oldGroup(),change.oldProject());if(updated!=1)throw new IllegalStateException("未分类行已变化,归类未提交");
|
||||
changedOrigins.add(new cn.yunsheng.quality.governance.MappingIssueService.SourceRow(change.stage(),build,change.sheet(),change.row()));
|
||||
keys.add(new MetricsRollupService.GroupKey(change.stage(),change.month(),change.department(),change.oldGroup(),change.oldProject(),change.product()));keys.add(new MetricsRollupService.GroupKey(change.stage(),change.month(),change.department(),change.group(),change.project(),change.product()));
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package cn.yunsheng.quality.ingestion;
|
||||
|
||||
import java.util.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** Certifies immutable source reuse only for a bound, unclassified-only workflow. */
|
||||
public final class UnclassifiedSnapshotReuse {
|
||||
private UnclassifiedSnapshotReuse() {}
|
||||
public record Proof(Set<String> buildIds,List<String> changedMonths) {}
|
||||
|
||||
public static Proof verify(JdbcTemplate jdbc,JsonMapper json,String releaseId) {
|
||||
var rows=jdbc.queryForList("""
|
||||
SELECT c.parent_release_id,c.expected_current_release_id,c.rule_version,c.target_version_id,
|
||||
p.status parent_status,p.rule_version parent_rule,p.target_version_id parent_target,
|
||||
CAST(c.validation AS CHAR) validation,CAST(old_map.definition AS CHAR) before_definition,
|
||||
CAST(new_map.definition AS CHAR) after_definition,
|
||||
(SELECT COUNT(*) FROM import_run i JOIN data_workflow_operation w ON w.id=i.workflow_operation_id
|
||||
WHERE i.result_release_id=c.id AND i.expected_current_release_id=p.id
|
||||
AND i.expected_mapping_version=c.mapping_version AND i.expected_rule_version=c.rule_version
|
||||
AND i.status='COMPLETED' AND i.stage='CLASSIFICATION_READY'
|
||||
AND i.idempotency_key=CONCAT('UNCLASSIFIED:',w.id)
|
||||
AND w.operation_type='APPLY_MAPPING' AND w.expected_current_release_id=p.id
|
||||
AND w.mapping_version_id=c.mapping_version AND w.created_by=c.created_by
|
||||
AND i.created_by=c.created_by) bound_receipts
|
||||
FROM data_release c JOIN data_release p ON p.id=c.parent_release_id
|
||||
JOIN mapping_version old_map ON old_map.id=p.mapping_version
|
||||
JOIN mapping_version new_map ON new_map.id=c.mapping_version WHERE c.id=?
|
||||
""",releaseId);
|
||||
if(rows.size()!=1)return null;
|
||||
var row=rows.getFirst();var validation=decode(json,row.get("validation"));
|
||||
if(!Objects.equals(validation.get("classificationSnapshotReuse"),1)
|
||||
||!"UNCLASSIFIED".equals(validation.get("recalculationScope"))
|
||||
||!Set.of("PUBLISHED","ACTIVE").contains(row.get("parent_status"))
|
||||
||!Objects.equals(row.get("parent_release_id"),row.get("expected_current_release_id"))
|
||||
||!Objects.equals(row.get("rule_version"),row.get("parent_rule"))
|
||||
||!Objects.equals(row.get("target_version_id"),row.get("parent_target"))
|
||||
||!(((Number)row.get("bound_receipts")).intValue()==1))return null;
|
||||
try {
|
||||
if(UnclassifiedMappingRebuild.changedRules(decode(json,row.get("before_definition")),decode(json,row.get("after_definition")))==null)return null;
|
||||
}catch(IllegalArgumentException invalidChange){return null;}
|
||||
if(!(validation.get("classificationChangedMonths") instanceof List<?> rawMonths))return null;
|
||||
var months=new ArrayList<String>();
|
||||
for(Object value:rawMonths){if(!(value instanceof String month)||!month.matches("[0-9]{4}-(0[1-9]|1[0-2])")||months.contains(month))return null;months.add(month);}
|
||||
String parent=Objects.toString(row.get("parent_release_id"));
|
||||
// No source can be added, removed, swapped, or rebound to another revision.
|
||||
Integer invalid=jdbc.queryForObject("""
|
||||
SELECT
|
||||
(SELECT COUNT(*) FROM release_member n LEFT JOIN release_member p
|
||||
ON p.release_id=? AND p.source_document_id=n.source_document_id
|
||||
LEFT JOIN transform_build b ON b.id=n.transform_build_id
|
||||
WHERE n.release_id=? AND (p.source_document_id IS NULL OR n.source_revision_id<>p.source_revision_id
|
||||
OR (n.transform_build_id<>p.transform_build_id AND
|
||||
COALESCE(JSON_VALUE(b.component_summary,'$.classificationParentBuildId'),'')<>p.transform_build_id)))
|
||||
+(SELECT COUNT(*) FROM release_member p WHERE p.release_id=? AND NOT EXISTS(
|
||||
SELECT 1 FROM release_member n WHERE n.release_id=? AND n.source_document_id=p.source_document_id))
|
||||
""",Integer.class,parent,releaseId,parent,releaseId);
|
||||
if(invalid==null||invalid!=0)return null;
|
||||
var reused=new LinkedHashSet<String>(jdbc.query("""
|
||||
SELECT n.transform_build_id FROM release_member n JOIN release_member p
|
||||
ON p.release_id=? AND p.source_document_id=n.source_document_id
|
||||
AND p.source_revision_id=n.source_revision_id AND p.transform_build_id=n.transform_build_id
|
||||
WHERE n.release_id=?
|
||||
""",(rs,index)->rs.getString(1),parent,releaseId));
|
||||
Integer copied=jdbc.queryForObject("SELECT COUNT(*) FROM release_member n JOIN release_member p ON p.release_id=? AND p.source_document_id=n.source_document_id WHERE n.release_id=? AND n.transform_build_id<>p.transform_build_id",Integer.class,parent,releaseId);
|
||||
if(copied==null||copied==0&&!months.isEmpty()||copied>0&&months.isEmpty())return null;
|
||||
return new Proof(Set.copyOf(reused),List.copyOf(months));
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String,Object> decode(JsonMapper json,Object value){return value==null?Map.of():json.readValue(String.valueOf(value),Map.class);}
|
||||
}
|
||||
@@ -21,8 +21,12 @@ public class MetricsRollupService {
|
||||
"'bright_diffusion_scrap'," + sumComponent("bright_diffusion_scrap") + ")";
|
||||
|
||||
private final JdbcTemplate jdbc;
|
||||
private final cn.yunsheng.quality.analysis.FineDefectRollups fineDefects;
|
||||
|
||||
public MetricsRollupService(JdbcTemplate jdbc) { this.jdbc = jdbc; }
|
||||
public MetricsRollupService(JdbcTemplate jdbc) {
|
||||
this.jdbc = jdbc;
|
||||
this.fineDefects = new cn.yunsheng.quality.analysis.FineDefectRollups(jdbc, tools.jackson.databind.json.JsonMapper.builder().build());
|
||||
}
|
||||
|
||||
public record GroupKey(String stage,String month,String department,String group,String project,String product) {}
|
||||
|
||||
@@ -30,6 +34,7 @@ public class MetricsRollupService {
|
||||
public void copyBuild(String parent,String child) {
|
||||
jdbc.update("INSERT INTO metric_rollup(build_id,stage,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,components,source_row_count,excluded_blank_rows,unmapped_rows,project_unconfigured_rows) SELECT ?,stage,month_key,department,group_name,project_group,product,qualified_weight,scrap_weight,total_weight,components,source_row_count,excluded_blank_rows,unmapped_rows,project_unconfigured_rows FROM metric_rollup WHERE build_id=?",child,parent);
|
||||
if(jdbc.update("INSERT INTO metric_rollup_build(build_id,source_row_count) SELECT ?,source_row_count FROM metric_rollup_build WHERE build_id=?",child,parent)!=1) throw new IllegalStateException("旧版本缺少已冻结的指标汇总");
|
||||
fineDefects.copyBuild(parent, child);
|
||||
}
|
||||
|
||||
public void refreshGroups(String build,java.util.Set<GroupKey> keys) {
|
||||
@@ -43,6 +48,7 @@ public class MetricsRollupService {
|
||||
var insertArgs=new java.util.ArrayList<Object>(List.of(stage,build));insertArgs.addAll(values);
|
||||
jdbc.update(rollupInsertSql("fact_"+stage).replace("WHERE f.build_id=?","WHERE f.build_id=? AND ("+inserted+")"),insertArgs.toArray());
|
||||
}
|
||||
fineDefects.refreshGroups(build, keys);
|
||||
}
|
||||
|
||||
/** Idempotently refreshes a completed build in the caller's transaction. */
|
||||
@@ -63,6 +69,7 @@ public class MetricsRollupService {
|
||||
(SELECT COUNT(*) FROM fact_black WHERE build_id=?) +
|
||||
(SELECT COUNT(*) FROM fact_bright WHERE build_id=?), CURRENT_TIMESTAMP(6)
|
||||
""", buildId, buildId, buildId);
|
||||
fineDefects.refreshBuild(buildId);
|
||||
}
|
||||
|
||||
static String rollupInsertSql(String table) {
|
||||
|
||||
@@ -182,7 +182,15 @@ public class ReleaseService {
|
||||
if(coverage.overlapCount(releaseId)>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"来源快照或声明范围相互覆盖,不能重复计数发布");
|
||||
Integer invalidDefinitions=jdbc.queryForObject("SELECT COUNT(*) FROM data_release r LEFT JOIN rule_version rules ON rules.id=r.rule_version LEFT JOIN mapping_version maps ON maps.id=r.mapping_version LEFT JOIN target_version targets ON targets.id=r.target_version_id WHERE r.id=? AND (rules.id IS NULL OR maps.id IS NULL OR(r.target_version_id IS NOT NULL AND(targets.id IS NULL OR targets.status NOT IN('ACTIVE','SUPERSEDED','VALIDATED'))))",Integer.class,releaseId);
|
||||
if(invalidDefinitions!=null&&invalidDefinitions>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"规则、映射或目标冻结依赖不可执行");
|
||||
Integer invalid=jdbc.queryForObject("SELECT COUNT(*) FROM release_member rm JOIN transform_build b ON b.id=rm.transform_build_id JOIN data_release r ON r.id=rm.release_id WHERE rm.release_id=? AND (b.status<>'COMPLETED' OR b.rejected_count>0 OR b.row_count=0 OR b.rule_version<>r.rule_version OR b.mapping_version<>r.mapping_version OR b.source_revision_id<>rm.source_revision_id)",Integer.class,releaseId);
|
||||
var reuse=cn.yunsheng.quality.ingestion.UnclassifiedSnapshotReuse.verify(jdbc,mapper,releaseId);
|
||||
String buildCheck="SELECT COUNT(*) FROM release_member rm JOIN transform_build b ON b.id=rm.transform_build_id JOIN data_release r ON r.id=rm.release_id WHERE rm.release_id=? AND (b.status<>'COMPLETED' OR b.rejected_count>0 OR b.row_count=0 OR b.rule_version<>r.rule_version OR b.mapping_version<>r.mapping_version OR b.source_revision_id<>rm.source_revision_id)";
|
||||
var buildArgs=new ArrayList<Object>(List.of(releaseId));
|
||||
if(reuse!=null&&!reuse.buildIds().isEmpty()){
|
||||
var reused=reuse.buildIds().stream().sorted().toList();
|
||||
buildCheck=buildCheck.replace("b.mapping_version<>r.mapping_version","(b.mapping_version<>r.mapping_version AND b.id NOT IN ("+String.join(",",java.util.Collections.nCopies(reused.size(),"?"))+"))");
|
||||
buildArgs.addAll(reused);
|
||||
}
|
||||
Integer invalid=jdbc.queryForObject(buildCheck,Integer.class,buildArgs.toArray());
|
||||
if(invalid!=null&&invalid>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"版本转换产物或冻结依赖不完整");
|
||||
Integer missing=jdbc.queryForObject("SELECT COUNT(*) FROM release_protocol_dependency dp JOIN source_protocol p ON p.id=dp.protocol_id WHERE dp.release_id=? AND p.applicable=TRUE AND p.expected=TRUE AND NOT EXISTS(SELECT 1 FROM release_member rm JOIN source_document d ON d.id=rm.source_document_id WHERE rm.release_id=? AND "+cn.yunsheng.quality.governance.SourceIdentity.canonicalSlotSql("d.source_slot")+"="+cn.yunsheng.quality.governance.SourceIdentity.canonicalSlotSql("p.source_slot")+") AND NOT EXISTS(SELECT 1 FROM release_source_exclusion e JOIN source_document d ON d.id=e.source_document_id WHERE e.release_id=? AND "+cn.yunsheng.quality.governance.SourceIdentity.canonicalSlotSql("d.source_slot")+"="+cn.yunsheng.quality.governance.SourceIdentity.canonicalSlotSql("p.source_slot")+")",Integer.class,releaseId,releaseId,releaseId);
|
||||
if(missing!=null&&missing>0)throw new ResponseStatusException(HttpStatus.CONFLICT,"协议要求的来源分片尚未到齐");
|
||||
|
||||
@@ -551,14 +551,22 @@ public class DataWorkflowService {
|
||||
|
||||
/** Execute the candidate's own formulas before changing any active definition or pointer. */
|
||||
private void validateCandidateMetrics(String releaseId,AppUserPrincipal actor) {
|
||||
var reuse=cn.yunsheng.quality.ingestion.UnclassifiedSnapshotReuse.verify(jdbc,json,releaseId);
|
||||
// A zero-hit classification changes no fact or formula. Other classification
|
||||
// updates need only the affected months and their enclosing quarter/year.
|
||||
if(reuse!=null&&reuse.changedMonths().isEmpty())return;
|
||||
Set<CandidatePeriod> periods=new LinkedHashSet<>();
|
||||
for(Map<String,Object> row:jdbc.queryForList("SELECT DISTINCT d.source_year,d.source_month FROM release_member m JOIN source_document d ON d.id=m.source_document_id WHERE m.release_id=? ORDER BY d.source_year,d.source_month",releaseId)) {
|
||||
int year=((Number)row.get("source_year")).intValue(),month=((Number)row.get("source_month")).intValue();
|
||||
if(reuse!=null&&!reuse.changedMonths().contains(String.format(java.util.Locale.ROOT,"%04d-%02d",year,month)))continue;
|
||||
periods.add(new CandidatePeriod(year,month,null));
|
||||
periods.add(new CandidatePeriod(year,null,(month-1)/3+1));
|
||||
periods.add(new CandidatePeriod(year,null,null));
|
||||
}
|
||||
if(periods.isEmpty())periods.add(new CandidatePeriod(java.time.LocalDate.now(java.time.ZoneId.of("Asia/Shanghai")).getYear(),null,null));
|
||||
if(periods.isEmpty()){
|
||||
if(reuse!=null)throw conflict("未分类更新期间与来源不一致");
|
||||
periods.add(new CandidatePeriod(java.time.LocalDate.now(java.time.ZoneId.of("Asia/Shanghai")).getYear(),null,null));
|
||||
}
|
||||
try {
|
||||
for(CandidatePeriod period:periods)for(String dimension:List.of("overall","department","group","product","projectGroup"))
|
||||
metrics.calculateCandidateForGovernance(new MetricsService.MetricsQuery(period.year(),period.month(),period.quarter(),
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
CREATE TABLE fine_defect_rollup_build (
|
||||
build_id CHAR(36) COLLATE utf8mb4_0900_ai_ci NOT NULL PRIMARY KEY,
|
||||
source_row_count BIGINT NOT NULL,
|
||||
refreshed_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
|
||||
CONSTRAINT fk_fine_rollup_build FOREIGN KEY (build_id) REFERENCES transform_build(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_cs;
|
||||
|
||||
CREATE TABLE fine_defect_rollup (
|
||||
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
build_id CHAR(36) COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
||||
stage VARCHAR(16) NOT NULL,
|
||||
month_key CHAR(7) NOT NULL,
|
||||
department VARCHAR(190) NULL,
|
||||
group_name VARCHAR(190) NULL,
|
||||
project_group VARCHAR(190) NULL,
|
||||
product VARCHAR(190) NULL,
|
||||
grain VARCHAR(16) NOT NULL,
|
||||
stat_kind VARCHAR(16) NOT NULL,
|
||||
field_key VARCHAR(100) NOT NULL,
|
||||
weight_sum DECIMAL(65,20) NOT NULL,
|
||||
present_rows BIGINT NOT NULL,
|
||||
missing_rows BIGINT NOT NULL,
|
||||
invalid_rows BIGINT NOT NULL,
|
||||
negative_rows BIGINT NOT NULL,
|
||||
source_row_count BIGINT NOT NULL,
|
||||
KEY ix_fine_rollup_build_month (build_id, stage, grain, month_key),
|
||||
KEY ix_fine_rollup_dimensions (build_id, stage, department(100), group_name(100), product(100), project_group(100)),
|
||||
CONSTRAINT fk_fine_rollup_fact_build FOREIGN KEY (build_id) REFERENCES transform_build(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_cs;
|
||||
Reference in New Issue
Block a user