9 Commits
37 changed files with 1486 additions and 121 deletions
+9
View File
@@ -164,3 +164,12 @@
- 计算设置移除“配置说明”折叠区和说明编辑框,仅保留隐藏字段以沿用既有保存数据;公式维护和保存入口不变。 - 计算设置移除“配置说明”折叠区和说明编辑框,仅保留隐藏字段以沿用既有保存数据;公式维护和保存入口不变。
- 2026-11-03:本轮指标与目标、完整结果公式、中文字段编辑、条件优先布局、重复账号与配置说明精简,已按用户授权发布为 `20261103-082040Z-rules-workspace`。分母策略的条件隐藏已撤销,线上保持始终显示;正式计算、后端与业务数据未改动。68项检查和线上桌面/窄屏验收通过。 - 2026-11-03:本轮指标与目标、完整结果公式、中文字段编辑、条件优先布局、重复账号与配置说明精简,已按用户授权发布为 `20261103-082040Z-rules-workspace`。分母策略的条件隐藏已撤销,线上保持始终显示;正式计算、后端与业务数据未改动。68项检查和线上桌面/窄屏验收通过。
## 2026-11-13:不良率分类钻取
- 点击有细项的分类曲线进入该分类,保留总废率淡虚线、细项曲线及“全部分类”返回;不良率曲线和横轴取消月份筛选,其他趋势保留原交互。
- 表格局部展开当前阶段的分类和细项,另一阶段收拢为关键指标,Excel 跟随当前列;返回恢复原字段和重量/废率选择。
- 年份和产品变化保留分类,阶段变化退出。加载或失败不混用旧范围行与新细项,完成前禁止导出。
- 缺失与零区分,无细项类别不可钻取;覆盖不一致时仅给出简短提示。
- 2026-11-13:不良率钻取“全部分类 › 当前分类”移至趋势标题旁,不再在筛选项与图例之间单独占一行;所有查询页面共用。
+8
View File
@@ -50,6 +50,14 @@ Linux 容器部署、自动重启、空间回收、备份保留及容量告警
DSH CLI 固定为 `0.2.2-rc.1`,依赖与官方 Python SDK 校验记录在 `runtime/dsh/`,要求 Node 24.9+。可运行 `python3 scripts/setup_dsh_runtime.py --node /path/to/node24` 安装项目内运行环境,`--check` 检查真实启动且不调用模型。`scripts/build_dsh_application.py` 构建独立应用,正式运行包使用其中的 `scripts/quality_analysis_runtime.py`;根目录同名源码仍有双引擎兼容逻辑,不能据此推断线上支持切换。当前系统隔离与正式服务在 macOS 验证;Linux/Windows 等效隔离未验收时明确拒绝 DSH 启动。详见[正式接入说明](docs/DeepSeekHarness正式接入.md)。 DSH CLI 固定为 `0.2.2-rc.1`,依赖与官方 Python SDK 校验记录在 `runtime/dsh/`,要求 Node 24.9+。可运行 `python3 scripts/setup_dsh_runtime.py --node /path/to/node24` 安装项目内运行环境,`--check` 检查真实启动且不调用模型。`scripts/build_dsh_application.py` 构建独立应用,正式运行包使用其中的 `scripts/quality_analysis_runtime.py`;根目录同名源码仍有双引擎兼容逻辑,不能据此推断线上支持切换。当前系统隔离与正式服务在 macOS 验证;Linux/Windows 等效隔离未验收时明确拒绝 DSH 启动。详见[正式接入说明](docs/DeepSeekHarness正式接入.md)。
## 不良率趋势钻取
总体、产业部、产品、黑片和亮片查询默认进入年度。切换年份保留当前产品层级;明确指定期间的链接继续使用该期间。
在不良率趋势图中点击有细项的分类曲线,可查看该分类下各细项的废率趋势;分类总废率以虚线保留。点击“全部分类”返回。切换年份或产品保留分类,切换黑片/亮片退出分类。不良率图不再通过点击曲线或横轴筛选月份,合格率与产量图保留原交互。
底部表格同步展开当前分类的细项,重量/废率切换与 Excel 导出跟随当前显示;产品和期间行维度保持不变。细项使用正式阶段同期总重量作分母,月、季、年均先汇总分量再计算;没有来源或分母时显示缺失,真实零显示零。细项与正式分类总量不一致时提示覆盖不足,不补造分类。
## 旧版本时间迁移 ## 旧版本时间迁移
连接与 MySQL 会话现在统一使用 UTC,界面仍按本地时区显示。V25 只校正旧代码按 UTC 墙上时间写入的截止/有效期、报告调度及报告租约列(前者由 Java 写入,租约使用 SQL UTC_TIMESTAMP),不改 SQL CURRENT_TIMESTAMP 自动生成的创建时间,也不修改业务日期或指标。若旧库存在此类值,迁移要求先备份并核实旧会话相对 UTC 的偏移,然后设置 `MYSQL_LEGACY_TIMESTAMP_OFFSET_SECONDS`(旧会话 UTC 为 `0`,旧会话中国标准时间为 `28800`)。本机启动可在私有 local-config 中记录 `legacyTimestampOffsetSeconds`;不能仅凭电脑当前时区猜测。全新空库不需要该参数;迁移标记和历史值修改在同一事务提交,防止提交确认丢失后重复平移。还原旧备份时应重新核实其原运行时区。 连接与 MySQL 会话现在统一使用 UTC,界面仍按本地时区显示。V25 只校正旧代码按 UTC 墙上时间写入的截止/有效期、报告调度及报告租约列(前者由 Java 写入,租约使用 SQL UTC_TIMESTAMP),不改 SQL CURRENT_TIMESTAMP 自动生成的创建时间,也不修改业务日期或指标。若旧库存在此类值,迁移要求先备份并核实旧会话相对 UTC 的偏移,然后设置 `MYSQL_LEGACY_TIMESTAMP_OFFSET_SECONDS`(旧会话 UTC 为 `0`,旧会话中国标准时间为 `28800`)。本机启动可在私有 local-config 中记录 `legacyTimestampOffsetSeconds`;不能仅凭电脑当前时区猜测。全新空库不需要该参数;迁移标记和历史值修改在同一事务提交,防止提交确认丢失后重复平移。还原旧备份时应重新核实其原运行时区。
@@ -155,6 +155,75 @@ public class AnalysisFineDefectsService {
return withScope((complement == null ? aggregate : complement).response(request, release), scopeContext); return withScope((complement == null ? aggregate : complement).response(request, release), scopeContext);
} }
/** One authorized scan (or rollup query) for every period/object and all visible children. */
@Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ)
public Map<String, Object> grouped(Map<String, Object> input, AppUserPrincipal actor, String dimension, String granularity) {
Set<String> allowed = governance.allowedFields(actor);
Request request = parse(input, allowed, true);
String bucket = switch (dimension) {
case "period" -> switch (granularity) {
case "month" -> "f.month_key";
case "quarter" -> "CONCAT(LEFT(f.month_key,4),'-Q',FLOOR((CAST(RIGHT(f.month_key,2) AS UNSIGNED)-1)/3)+1)";
case "year" -> "LEFT(f.month_key,4)";
default -> throw bad("趋势粒度无效");
};
case "overall" -> "'overall'";
case "department" -> "COALESCE(f.department,'__UNASSIGNED__') COLLATE utf8mb4_0900_as_cs";
case "group" -> "COALESCE(f.group_name,'__UNASSIGNED__') COLLATE utf8mb4_0900_as_cs";
case "product" -> "COALESCE(f.product,'__UNASSIGNED__') COLLATE utf8mb4_0900_as_cs";
case "projectGroup" -> "COALESCE(f.project_group,'__UNASSIGNED__') COLLATE utf8mb4_0900_as_cs";
default -> throw bad("细分维度无效");
};
if (request.category() == null) throw bad("必须指定不良分类");
String releaseId = releases.resolvePublishedRelease(request.releaseId());
Map<String, Object> release = releaseContext(releaseId);
if (hasExtendedScope(input) && sourceColumns == null) throw bad("扩展缺陷查询范围当前不可用");
StringBuilder predicate = new StringBuilder(" WHERE rm.release_id = ?");
List<Object> args = new ArrayList<>(List.of(releaseId));
appendScope(predicate,args,request,input,releaseId,actor);
if (request.productSearch() != null) {
predicate.append(" AND LOCATE(LOWER(?),LOWER(COALESCE(f.product,'')))>0"); args.add(request.productSearch());
}
List<DefectField> fields = applicableFields(request,allowed);
List<Category> categories = categories(request.stage());
Set<String> components = new LinkedHashSet<>();
for (Category category : categories) if (fields.stream().anyMatch(f -> f.category().equals(category.name()))
&& isAllowed(category.componentCode(),allowed)) components.add(category.componentCode());
java.util.function.Supplier<Aggregator> create = () -> new Aggregator(request,fields,categories,components,json);
Map<String,Aggregator> groups = new LinkedHashMap<>();
if (!fields.isEmpty()) {
boolean read = rollups != null && request.extraFilters().isEmpty()
&& (!(input.get("columnFilters") instanceof List<?> filters) || filters.isEmpty())
&& rollups.readGroups(groups,create,request,allowed,predicate.toString(),args,bucket,dimension);
if (!read) {
List<String> rawKeys = new ArrayList<>(fields.stream().map(DefectField::key).toList());
categories.stream().map(c -> ExpectedSourceDetailPolicy.totalField(c.componentCode()))
.filter(java.util.Objects::nonNull).filter(k -> isAllowed(k,allowed)).forEach(rawKeys::add);
String sql = "SELECT " + bucket + " AS bucket_key," + jsonObject("f.raw_values",rawKeys)
+ " AS raw_fields," + jsonObject("f.components",new ArrayList<>(components))
+ " AS formal_components,f.group_name AS business_group 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 fact_" + request.stage()
+ " f ON f.build_id=rm.transform_build_id AND f.source_revision_id=rm.source_revision_id " + predicate;
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()) groups.computeIfAbsent(rs.getString("bucket_key"),key -> create.get())
.add(readObject(rs.getString("raw_fields")),readObject(rs.getString("formal_components")),null,rs.getString("business_group"));
return null;
});
}
}
Map<String,Object> result = new LinkedHashMap<>();
groups.forEach((key,value) -> {
String responseKey=dimension.equals("department")&&!key.equals("__UNASSIGNED__")&&!key.isBlank()?DepartmentLabels.display(key):key;
if(result.containsKey(responseKey))throw bad("产业部名称映射冲突,暂不能计算细分废率");
result.put(responseKey,value.response(request,release));
});
return mapOf("releaseId",releaseId,"fields",fields.stream().map(f -> mapOf("key",f.key(),"label",f.label(),"category",f.category())).toList(),"groups",result);
}
/** Exact counters allow subtracting a small excluded set without rescanning all retained raw JSON. */ /** Exact counters allow subtracting a small excluded set without rescanning all retained raw JSON. */
private Aggregator exclusionComplement(Request request, Map<String,Object> input, String releaseId, AppUserPrincipal actor, private Aggregator exclusionComplement(Request request, Map<String,Object> input, String releaseId, AppUserPrincipal actor,
Set<String> allowed, List<DefectField> fields, List<Category> categories, Set<String> components, Set<String> allowed, List<DefectField> fields, List<Category> categories, Set<String> components,
@@ -229,6 +298,10 @@ public class AnalysisFineDefectsService {
} }
static Request parse(Map<String, Object> input, Set<String> allowedFields) { static Request parse(Map<String, Object> input, Set<String> allowedFields) {
return parse(input, allowedFields, false);
}
static Request parse(Map<String, Object> input, Set<String> allowedFields, boolean grouped) {
if (input == null) throw bad("请求体不能为空"); if (input == null) throw bad("请求体不能为空");
for (String key : input.keySet()) if (!REQUEST_KEYS.contains(key)) throw bad("未知请求属性:" + key); for (String key : input.keySet()) if (!REQUEST_KEYS.contains(key)) throw bad("未知请求属性:" + key);
if (hasExtendedScope(input)) SourceColumnQueries.normalizedScope(input); if (hasExtendedScope(input)) SourceColumnQueries.normalizedScope(input);
@@ -243,7 +316,7 @@ public class AnalysisFineDefectsService {
if (month != null && (month < 1 || month > 12)) throw bad("月份无效"); if (month != null && (month < 1 || month > 12)) throw bad("月份无效");
if (quarter != null && (quarter < 1 || quarter > 4)) throw bad("季度无效"); if (quarter != null && (quarter < 1 || quarter > 4)) throw bad("季度无效");
if (month != null && quarter != null) throw bad("月份与季度筛选不可同时指定"); if (month != null && quarter != null) throw bad("月份与季度筛选不可同时指定");
if (mode.equals("top") && year == null) throw bad("top 查询必须指定 year"); if (mode.equals("top") && year == null && !grouped) throw bad("top 查询必须指定 year");
String stage = optionalText(input, "stage"); String stage = optionalText(input, "stage");
if (stage != null && !Set.of("black", "bright").contains(stage)) throw bad("stage 必须是 black 或 bright"); if (stage != null && !Set.of("black", "bright").contains(stage)) throw bad("stage 必须是 black 或 bright");
if (mode.equals("top") && stage == null) throw bad("top 查询必须指定 stage"); if (mode.equals("top") && stage == null) throw bad("top 查询必须指定 stage");
@@ -0,0 +1,79 @@
package cn.yunsheng.quality.analysis;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.metrics.MetricsResponseAccess;
import cn.yunsheng.quality.metrics.MetricsService;
import cn.yunsheng.quality.metrics.MetricsService.MetricStage;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
/** Batched source children joined to the official, authorized period/object denominators. */
@RestController
@RequestMapping("/api/analysis/defects/drilldown")
public class DefectDrilldownController {
private final AnalysisFineDefectsService fine;
private final MetricsService metrics;
private final MetricsResponseAccess metricAccess;
private final GovernanceAccessService governance;
public DefectDrilldownController(AnalysisFineDefectsService fine,MetricsService metrics,MetricsResponseAccess metricAccess,GovernanceAccessService governance){this.fine=fine;this.metrics=metrics;this.metricAccess=metricAccess;this.governance=governance;}
@PostMapping
@PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'DATA_ADMIN', 'USER')")
public Map<String,Object> query(@RequestBody Map<String,Object> input,@AuthenticationPrincipal AppUserPrincipal actor) {
governance.requireDetail(actor);governance.requireBoundAction("QUERY","query.detail");
String dimension=String.valueOf(input.getOrDefault("dimension","period"));
String granularity=String.valueOf(input.getOrDefault("trendGranularity","month"));
if(!Set.of("period","overall","department","group","product","projectGroup").contains(dimension))throw bad("细分维度无效");
if(!Set.of("month","quarter","year").contains(granularity))throw bad("趋势粒度无效");
Map<String,Object> scope=new LinkedHashMap<>(input);scope.remove("trendGranularity");scope.remove("view");scope.put("dimension","defect");scope.put("mode","top");scope.put("limit",100);
// Reuse the source parser to reject unknown filters and validate the selected category/stage.
AnalysisFineDefectsService.Request request=AnalysisFineDefectsService.parse(scope,governance.allowedFields(actor),true);
if(request.category()==null)throw bad("必须指定不良分类");
var query=new MetricsService.MetricsQuery(number(scope,"year"),number(scope,"month"),number(scope,"quarter"),request.department(),request.group(),request.product(),request.projectGroup(),dimension.equals("period")?"overall":dimension,request.includeUnassigned(),request.releaseId(),granularity,AnalysisExplorationTools.exclusions(scope.get("exclusions")))
.withProductSearch(request.productSearch()).withExtraFilters(scope.get("extraFilters")).withColumnFilters(scope.get("columnFilters")).withMonthRange(number(scope,"monthStart"),number(scope,"monthEnd")).withStage(request.stage());
var paired=metrics.calculatePairedSnapshot(query,actor,dimension.equals("period"),() -> fine.grouped(scope,actor,dimension,granularity));
// Refresh the visible fields after the snapshot; a concurrent policy revocation cannot release old fields.
governance.requireDetail(actor);governance.requireBoundAction("QUERY","query.detail");
Set<String> currentAllowed=governance.allowedFields(actor);
Map<String,MetricStage> formal=new LinkedHashMap<>();
if(dimension.equals("period")) {
var snapshot=metricAccess.visibleTrend(paired.trend(),actor);
if(!Objects.equals(snapshot.release().get("id"),request.releaseId()))throw bad("查询数据版本不一致");
snapshot.trend().forEach(row->formal.put(row.period(),request.stage().equals("black")?row.black():row.bright()));
} else {
var snapshot=metricAccess.visible(paired.metrics(),actor);
if(!Objects.equals(snapshot.release().get("id"),request.releaseId()))throw bad("查询数据版本不一致");
if(dimension.equals("overall"))formal.put("overall",request.stage().equals("black")?snapshot.summary().black():snapshot.summary().bright());
else snapshot.rows().forEach(row->formal.put(normalizeKey(row.key()),request.stage().equals("black")?row.black():row.bright()));
}
Map<String,Object> raw=paired.source();
if(!Objects.equals(raw.get("releaseId"),request.releaseId()))throw bad("查询数据版本不一致");
Map<?,?> sourceGroups=(Map<?,?>)raw.get("groups");
List<Map<String,Object>> groups=new ArrayList<>();
formal.forEach((key,stage)->{
Map<?,?> source=sourceGroups.get(key) instanceof Map<?,?> m?m:Map.of();
List<?> rows=source.get("rows") instanceof List<?> list?list:List.of();
List<Map<String,Object>> children=new ArrayList<>();
for(Object item:rows)if(item instanceof Map<?,?> child && (currentAllowed.contains("*")||currentAllowed.contains(String.valueOf(child.get("key"))))) {
BigDecimal weight=child.get("weight") instanceof BigDecimal value?value:null;
var value=new LinkedHashMap<String,Object>();value.put("key",child.get("key"));value.put("weight",weight);value.put("rate",ratio(weight,stage.totalWeight()));children.add(value);
}
BigDecimal parentWeight=stage.defects().stream().filter(d->d.name().equals(request.category())).map(MetricsService.DefectRow::weight).filter(Objects::nonNull).findFirst().orElse(null);
List<?> reconciliation=source.get("categoryReconciliation") instanceof List<?> list?list:List.of();
String status=reconciliation.stream().filter(Map.class::isInstance).map(Map.class::cast).filter(m->request.category().equals(m.get("category"))).map(m->String.valueOf(m.get("reconciliationStatus"))).findFirst().orElse("UNAVAILABLE");
Map<String,Object> group=new LinkedHashMap<>();group.put("key",key);group.put("totalWeight",stage.totalWeight());group.put("parentWeight",parentWeight);group.put("parentRate",ratio(parentWeight,stage.totalWeight()));group.put("children",children);group.put("reconciliationStatus",status);groups.add(group);
});
return Map.of("releaseId",request.releaseId(),"stage",request.stage(),"category",request.category(),"dimension",dimension,"granularity",granularity,"fields",((List<?>)raw.get("fields")).stream().filter(item -> item instanceof Map<?,?> field && (currentAllowed.contains("*")||currentAllowed.contains(String.valueOf(field.get("key"))))).toList(),"groups",groups);
}
private static String normalizeKey(String key){return key==null?"__UNASSIGNED__":key;}
private static Integer number(Map<String,Object> input,String key){Object value=input.get(key);if(value==null)return null;if(!(value instanceof Number n)||n.doubleValue()!=n.intValue())throw bad("期间必须为整数");return n.intValue();}
private static BigDecimal ratio(BigDecimal weight,BigDecimal total){return weight==null||total==null||total.signum()<=0?null:weight.divide(total,12,RoundingMode.HALF_UP);}
private static ResponseStatusException bad(String message){return new ResponseStatusException(HttpStatus.BAD_REQUEST,message);}
}
@@ -0,0 +1,107 @@
package cn.yunsheng.quality.analysis;
import static cn.yunsheng.quality.analysis.AnalysisFineDefectsService.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import tools.jackson.databind.json.JsonMapper;
/** Category payloads retain the exact counters and original authorization dimensions. */
final class FineDefectCompactRollups {
private static final int FORMAT = 1;
private final JdbcTemplate jdbc;
private final JsonMapper json;
FineDefectCompactRollups(JdbcTemplate jdbc, JsonMapper json) { this.jdbc=jdbc; this.json=json; }
void invalidate(String build) {
jdbc.update("DELETE FROM fine_defect_compact_build WHERE build_id=?",build);
}
void refreshBuild(String build) {
invalidate(build);
jdbc.update("DELETE FROM fine_defect_compact WHERE build_id=?",build);
Integer ready=jdbc.queryForObject("SELECT COUNT(*) FROM fine_defect_rollup_build WHERE build_id=?",Integer.class,build);
if(ready==null||ready!=1)return;
insert(build,"",List.of());
jdbc.update("INSERT INTO fine_defect_compact_build(build_id,format_version) VALUES(?,?)",build,FORMAT);
}
void refreshGroups(String build,String stage,String predicate,List<Object> scopeArgs) {
List<Object> args=new ArrayList<>(List.of(build,stage));args.addAll(scopeArgs);
jdbc.update("DELETE f FROM fine_defect_compact f WHERE f.build_id=? AND f.stage=? AND ("+predicate+")",args.toArray());
List<Object> filters=new ArrayList<>(List.of(stage));filters.addAll(scopeArgs);
insert(build," AND f.stage=? AND ("+predicate+")",filters);
// A partial update cannot make a previously incomplete build ready.
}
private void insert(String build,String predicate,List<Object> scopeArgs) {
List<Object> args=new ArrayList<>(List.of(build));args.addAll(scopeArgs);
String category=categoryExpression();
jdbc.update("""
INSERT INTO fine_defect_compact(build_id,stage,month_key,department,group_name,project_group,
product,category,source_row_count,stats,payload_bytes)
SELECT build_id,stage,month_key,department,group_name,project_group,product,category,
source_row_count,stats,OCTET_LENGTH(CAST(stats AS CHAR)) FROM (
SELECT f.build_id,f.stage,ANY_VALUE(f.month_key) AS month_key,
ANY_VALUE(f.department) AS department,ANY_VALUE(f.group_name) AS group_name,
ANY_VALUE(f.project_group) AS project_group,ANY_VALUE(f.product) AS product,
"""+category+" AS category,"+"""
MAX(f.source_row_count) AS source_row_count,
JSON_ARRAYAGG(JSON_ARRAY(f.stat_kind,f.field_key,CAST(f.weight_sum AS CHAR),
f.present_rows,f.missing_rows,f.invalid_rows,f.negative_rows)) AS stats
FROM fine_defect_rollup f WHERE f.build_id=? AND f.grain='detail'
"""+predicate+"""
GROUP BY f.build_id,f.stage,CAST(f.month_key AS BINARY),CAST(f.department AS BINARY),
CAST(f.group_name AS BINARY),CAST(f.project_group AS BINARY),CAST(f.product AS BINARY),category
) packed WHERE category IS NOT NULL
""",args.toArray());
}
private static String categoryExpression() {
StringBuilder source=new StringBuilder("CASE f.field_key"),formal=new StringBuilder("CASE f.field_key");
for(String stage:List.of("black","bright")) {
for(var field:fields(stage))source.append(" WHEN ").append(literal(field.key())).append(" THEN ").append(literal(field.category()));
for(var category:categories(stage))if(category.available())formal.append(" WHEN ").append(literal(category.componentCode())).append(" THEN ").append(literal(category.name()));
}
return "CASE f.stat_kind WHEN 'source' THEN "+source+" END WHEN 'formal' THEN "+formal+" END WHEN 'expected' THEN f.field_key END";
}
private static String literal(String value) { return "'"+value.replace("'","''")+"'"; }
boolean read(Map<String,Aggregator> groups,Supplier<Aggregator> create,Request request,Set<String> allowed,
String predicate,List<Object> scopeArgs,String bucket) {
Integer missing=jdbc.queryForObject("""
SELECT COUNT(*) FROM release_member rm
LEFT JOIN fine_defect_compact_build cb ON cb.build_id=rm.transform_build_id AND cb.format_version=?
WHERE rm.release_id=? AND cb.build_id IS NULL
""",Integer.class,FORMAT,scopeArgs.getFirst());
if(missing==null||missing>0)return false;
List<Object> args=new ArrayList<>(scopeArgs);args.add(request.stage());args.add(request.category());
String from=" 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_compact f ON f.build_id=rm.transform_build_id "
+predicate+" AND f.stage=? AND f.category=?";
// Bound each JSON result before aggregation. Oversized buckets use the existing tall-stat query.
Integer oversized=jdbc.queryForObject("SELECT COUNT(*) FROM (SELECT "+bucket+" AS bucket_key"+from
+" GROUP BY bucket_key HAVING SUM(f.payload_bytes)+4*COUNT(*)>LEAST(4194304,@@max_allowed_packet DIV 4)) oversized",Integer.class,args.toArray());
if(oversized!=null&&oversized>0)return false;
jdbc.query("SELECT "+bucket+" AS bucket_key,JSON_ARRAYAGG(f.stats) AS parts,SUM(f.source_row_count) AS source_row_count"
+from+" GROUP BY bucket_key",(RowCallbackHandler) rs -> {
Aggregator aggregate=groups.computeIfAbsent(rs.getString("bucket_key"),key -> create.get());
long rows=rs.getLong("source_row_count");
// SQL groups with the same collation as formal metrics. Decimal strings avoid float conversion.
for(var part:json.readTree(rs.getString("parts")))for(var stat:part) {
aggregate.mergeRollup(new Aggregator.RollupStat(stat.get(0).asString(),stat.get(1).asString(),
new BigDecimal(stat.get(2).asString()),stat.get(3).longValue(),stat.get(4).longValue(),
stat.get(5).longValue(),stat.get(6).longValue(),rows),allowed);
}
},args.toArray());
return true;
}
}
@@ -25,6 +25,15 @@ public class FineDefectRollupBackfill implements ApplicationRunner {
""", (rs, row) -> rs.getString(1)); """, (rs, row) -> rs.getString(1));
long start = System.nanoTime(); long start = System.nanoTime();
for (String build : missing) rollups.refreshBuild(build); for (String build : missing) rollups.refreshBuild(build);
var compactMissing=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
JOIN fine_defect_rollup_build rb ON rb.build_id=b.id
LEFT JOIN fine_defect_compact_build cb ON cb.build_id=b.id AND cb.format_version=1
WHERE c.dataset_id='quality' AND b.status='COMPLETED' AND cb.build_id IS NULL ORDER BY b.id
""",(rs,row) -> rs.getString(1));
for(String build:compactMissing)rollups.refreshCompactBuild(build);
if(!compactMissing.isEmpty())LOG.info("Compact fine defect backfill: builds={}, elapsedMs={}",compactMissing.size(),(System.nanoTime()-start)/1_000_000L);
if (!missing.isEmpty()) LOG.info("Fine defect rollup backfill: builds={}, elapsedMs={}", missing.size(), (System.nanoTime() - start) / 1_000_000L); if (!missing.isEmpty()) LOG.info("Fine defect rollup backfill: builds={}, elapsedMs={}", missing.size(), (System.nanoTime() - start) / 1_000_000L);
} }
} }
@@ -23,11 +23,13 @@ import static cn.yunsheng.quality.analysis.AnalysisFineDefectsService.*;
public class FineDefectRollups { public class FineDefectRollups {
private final JdbcTemplate jdbc; private final JdbcTemplate jdbc;
private final JsonMapper json; private final JsonMapper json;
private final FineDefectCompactRollups compact;
public FineDefectRollups(JdbcTemplate jdbc, JsonMapper json) { this.jdbc = jdbc; this.json = json; } public FineDefectRollups(JdbcTemplate jdbc, JsonMapper json) { this.jdbc = jdbc; this.json = json; this.compact = new FineDefectCompactRollups(jdbc,json); }
@Transactional @Transactional
public void refreshBuild(String build) { public void refreshBuild(String build) {
compact.invalidate(build);
jdbc.update("DELETE FROM fine_defect_rollup_build WHERE build_id=?", build); jdbc.update("DELETE FROM fine_defect_rollup_build WHERE build_id=?", build);
jdbc.update("DELETE FROM fine_defect_rollup WHERE build_id=?", build); jdbc.update("DELETE FROM fine_defect_rollup WHERE build_id=?", build);
long rows = 0; long rows = 0;
@@ -39,8 +41,10 @@ public class FineDefectRollups {
} }
refreshOrganizations(build, "", List.of()); refreshOrganizations(build, "", List.of());
jdbc.update("INSERT INTO fine_defect_rollup_build(build_id,source_row_count) VALUES(?,?)", build, rows); jdbc.update("INSERT INTO fine_defect_rollup_build(build_id,source_row_count) VALUES(?,?)", build, rows);
compact.refreshBuild(build);
} }
@Transactional
public void copyBuild(String parent, String child) { public void copyBuild(String parent, String child) {
Integer ready = jdbc.queryForObject("SELECT COUNT(*) FROM fine_defect_rollup_build WHERE build_id=?", Integer.class, parent); 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; } if (ready == null || ready != 1) { refreshBuild(child); return; }
@@ -52,8 +56,10 @@ public class FineDefectRollups {
FROM fine_defect_rollup WHERE build_id=? FROM fine_defect_rollup WHERE build_id=?
""", child, parent); """, 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); 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);
compact.refreshBuild(child);
} }
@Transactional
public void refreshGroups(String build, Set<GroupKey> keys) { public void refreshGroups(String build, Set<GroupKey> keys) {
for (String stage : List.of("black", "bright")) { for (String stage : List.of("black", "bright")) {
var selected = keys.stream().filter(k -> stage.equals(k.stage())).toList(); var selected = keys.stream().filter(k -> stage.equals(k.stage())).toList();
@@ -65,8 +71,9 @@ public class FineDefectRollups {
List<Object> deletedArgs = new ArrayList<>(List.of(build, stage)); deletedArgs.addAll(args); 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()); 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); var groups = aggregate(build, stage, " AND (" + predicate + ")", args);
if (!representable(groups)) { jdbc.update("DELETE FROM fine_defect_rollup_build WHERE build_id=?", build); return; } if (!representable(groups)) { jdbc.update("DELETE FROM fine_defect_rollup_build WHERE build_id=?", build); compact.invalidate(build); return; }
insert(build, groups); insert(build, groups);
compact.refreshGroups(build,stage,predicate,args);
} }
var organizations = keys.stream().map(k -> new GroupKey(k.stage(), k.month(), k.department(), k.group(), null, null)).distinct().toList(); var organizations = keys.stream().map(k -> new GroupKey(k.stage(), k.month(), k.department(), k.group(), null, null)).distinct().toList();
if (!organizations.isEmpty()) { if (!organizations.isEmpty()) {
@@ -78,6 +85,9 @@ public class FineDefectRollups {
} }
} }
@Transactional
public void refreshCompactBuild(String build) { compact.refreshBuild(build); }
private void refreshOrganizations(String build, String predicate, List<Object> scopeArgs) { private void refreshOrganizations(String build, String predicate, List<Object> scopeArgs) {
List<Object> args = new ArrayList<>(List.of(build)); args.addAll(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("DELETE f FROM fine_defect_rollup f WHERE f.build_id=? AND f.grain='organization'" + predicate, args.toArray());
@@ -125,6 +135,21 @@ public class FineDefectRollups {
return true; return true;
} }
boolean readGroups(Map<String,Aggregator> groups, java.util.function.Supplier<Aggregator> create,
Request request, Set<String> allowed, String predicate, List<Object> scopeArgs,
String bucket, String dimension) {
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;
if(dimension.equals("product")&&compact.read(groups,create,request,allowed,predicate,scopeArgs,bucket))return true;
boolean organization=!predicate.contains("f.product")&&!predicate.contains("f.project_group")&&!List.of("product","projectGroup").contains(dimension);
List<Object> args=new ArrayList<>(scopeArgs);args.add(request.stage());args.add(organization?"organization":"detail");
List<String> selected=new ArrayList<>();
for(var stat:create.get().rollupStats()){selected.add("(f.stat_kind=? AND f.field_key=?)");args.add(stat.kind());args.add(stat.key());}
String sql="SELECT "+bucket+" AS bucket_key,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 ON f.build_id=rm.transform_build_id "+predicate+" AND f.stage=? AND f.grain=? AND ("+String.join(" OR ",selected)+") GROUP BY bucket_key,f.stat_kind,f.field_key";
jdbc.query(sql,(RowCallbackHandler) rs -> groups.computeIfAbsent(rs.getString("bucket_key"),key -> create.get()).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) { 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, Request request = new Request(build, "top", null, null, null, null, null, null, null, null,
true, stage, "defect", null, null, 100, Map.of()); true, stage, "defect", null, null, 100, Map.of());
@@ -31,12 +31,14 @@ public class MetricsRollupService {
public record GroupKey(String stage,String month,String department,String group,String project,String product) {} public record GroupKey(String stage,String month,String department,String group,String project,String product) {}
/** 归类维护沿用旧汇总;仅重建归属发生变化的分组,重量计算结果不变。 */ /** 归类维护沿用旧汇总;仅重建归属发生变化的分组,重量计算结果不变。 */
@Transactional
public void copyBuild(String parent,String child) { 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); 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("旧版本缺少已冻结的指标汇总"); 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); fineDefects.copyBuild(parent, child);
} }
@Transactional
public void refreshGroups(String build,java.util.Set<GroupKey> keys) { public void refreshGroups(String build,java.util.Set<GroupKey> keys) {
for(String stage:List.of("black","bright")) { for(String stage:List.of("black","bright")) {
var selected=keys.stream().filter(k->stage.equals(k.stage())).toList();if(selected.isEmpty())continue; var selected=keys.stream().filter(k->stage.equals(k.stage())).toList();if(selected.isEmpty())continue;
@@ -84,6 +84,23 @@ public class MetricsService {
return withConsistentSnapshot(() -> calculateTrendForReleaseSnapshot(query, actor, release)); return withConsistentSnapshot(() -> calculateTrendForReleaseSnapshot(query, actor, release));
} }
public record PairedSnapshot<T>(MetricSnapshot metrics, MetricTrendSnapshot trend, T source) {}
/** Authorize before opening the read snapshot, so service-binding locks do not span source scans. */
public <T> PairedSnapshot<T> calculatePairedSnapshot(MetricsQuery query, AppUserPrincipal actor,
boolean trendOnly, Supplier<T> source) {
if (actor == null) throw new ResponseStatusException(HttpStatus.FORBIDDEN,"指标查询必须绑定当前有效用户");
governance.requireUserActivePolicy(actor);
governance.requireBoundAction("QUERY","query.aggregate");
validate(query);
String releaseId=query.releaseId()==null||query.releaseId().isBlank()?currentReleaseId():query.releaseId();
if(releaseId==null)throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"必须绑定正式数据版本");
ReleaseInfo release=release(releaseId,actor);
return withConsistentSnapshot(() -> trendOnly
? new PairedSnapshot<T>(null,calculateTrendForReleaseSnapshot(query,actor,release),source.get())
: new PairedSnapshot<T>(calculateForReleaseSnapshot(query,actor,release),null,source.get()));
}
private MetricTrendSnapshot calculateTrendForReleaseSnapshot(MetricsQuery query, AppUserPrincipal actor, private MetricTrendSnapshot calculateTrendForReleaseSnapshot(MetricsQuery query, AppUserPrincipal actor,
ReleaseInfo release) { ReleaseInfo release) {
boolean hasUnrolledBuilds = hasUnrolledBuilds(release.id()); boolean hasUnrolledBuilds = hasUnrolledBuilds(release.id());
@@ -92,9 +92,12 @@ public class RecordController {
public Map<String,Object> records(String stage,Integer year,Integer month,Integer quarter,String department,String group,String product,String productSearch,String projectGroup,boolean includeUnassigned,String releaseId,int page,int pageSize,String field,String search,String sort,String direction,List<String> columns,String extraFilters,AppUserPrincipal actor) { public Map<String,Object> records(String stage,Integer year,Integer month,Integer quarter,String department,String group,String product,String productSearch,String projectGroup,boolean includeUnassigned,String releaseId,int page,int pageSize,String field,String search,String sort,String direction,List<String> columns,String extraFilters,AppUserPrincipal actor) {
return recordsFiltered(stage,year,month,quarter,department,group,product,productSearch,projectGroup,includeUnassigned,releaseId,page,pageSize,field,search,sort,direction,columns,extraFilters,null,actor); return recordsFiltered(stage,year,month,quarter,department,group,product,productSearch,projectGroup,includeUnassigned,releaseId,page,pageSize,field,search,sort,direction,columns,extraFilters,null,actor);
} }
public Map<String,Object> recordsFiltered(String stage,Integer year,Integer month,Integer quarter,String department,String group,String product,String productSearch,String projectGroup,boolean includeUnassigned,String releaseId,int page,int pageSize,String field,String search,String sort,String direction,List<String> columns,String extraFilters,String columnFilters,AppUserPrincipal actor) {
return recordsRangeFiltered(stage,year,month,quarter,department,group,product,productSearch,projectGroup,includeUnassigned,releaseId,page,pageSize,field,search,sort,direction,columns,extraFilters,columnFilters,null,null,actor);
}
@GetMapping @GetMapping
@PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'DATA_ADMIN', 'USER')") @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'DATA_ADMIN', 'USER')")
public Map<String, Object> recordsFiltered(@RequestParam String stage, public Map<String, Object> recordsRangeFiltered(@RequestParam String stage,
@RequestParam(required = false) Integer year, @RequestParam(required = false) Integer year,
@RequestParam(required = false) Integer month, @RequestParam(required = false) Integer month,
@RequestParam(required = false) Integer quarter, @RequestParam(required = false) Integer quarter,
@@ -114,11 +117,13 @@ public class RecordController {
@RequestParam(required = false) List<String> columns, @RequestParam(required = false) List<String> columns,
@RequestParam(required=false) String extraFilters, @RequestParam(required=false) String extraFilters,
@RequestParam(required=false) String columnFilters, @RequestParam(required=false) String columnFilters,
@RequestParam(required=false) Integer monthStart,
@RequestParam(required=false) Integer monthEnd,
@AuthenticationPrincipal AppUserPrincipal actor) { @AuthenticationPrincipal AppUserPrincipal actor) {
governance.requireDetail(actor); governance.requireDetail(actor);
governance.requireBoundAction("QUERY", "query.detail"); governance.requireBoundAction("QUERY", "query.detail");
if (!List.of("black", "bright").contains(stage)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "stage 必须是 black 或 bright"); if (!List.of("black", "bright").contains(stage)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "stage 必须是 black 或 bright");
validatePeriodFilters(year, month, quarter); validatePeriodFilters(year, month, quarter, monthStart, monthEnd);
if (page < 1 || pageSize < 1 || pageSize > 200) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "分页参数无效"); if (page < 1 || pageSize < 1 || pageSize > 200) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "分页参数无效");
if (productSearch != null && productSearch.length() > SelectionValues.MAX_VALUE_LENGTH) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "产品搜索文本过长"); if (productSearch != null && productSearch.length() > SelectionValues.MAX_VALUE_LENGTH) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "产品搜索文本过长");
List<String> selectedColumns = validateColumns(columns, actor); List<String> selectedColumns = validateColumns(columns, actor);
@@ -135,7 +140,7 @@ public class RecordController {
StringBuilder predicate = new StringBuilder(" WHERE rm.release_id = ?"); StringBuilder predicate = new StringBuilder(" WHERE rm.release_id = ?");
List<Object> args = new ArrayList<>(); List<Object> args = new ArrayList<>();
args.add(selectedRelease); args.add(selectedRelease);
appendPeriodFilters(predicate, args, year, month, quarter); appendPeriodFilters(predicate, args, year, month, quarter, monthStart, monthEnd);
SelectionValues.appendPredicate(predicate, args, "f.department", department, DepartmentLabels::queryValue); SelectionValues.appendPredicate(predicate, args, "f.department", department, DepartmentLabels::queryValue);
SelectionValues.appendPredicate(predicate, args, "f.group_name", group, value -> value); SelectionValues.appendPredicate(predicate, args, "f.group_name", group, value -> value);
SelectionValues.appendPredicate(predicate, args, "f.product", product, value -> value); SelectionValues.appendPredicate(predicate, args, "f.product", product, value -> value);
@@ -213,9 +218,12 @@ public class RecordController {
public ResponseEntity<StreamingResponseBody> export(Integer year,Integer month,Integer quarter,String department,String group,String product,String productSearch,String projectGroup,boolean includeUnassigned,String releaseId,String extraFilters,AppUserPrincipal actor) { public ResponseEntity<StreamingResponseBody> export(Integer year,Integer month,Integer quarter,String department,String group,String product,String productSearch,String projectGroup,boolean includeUnassigned,String releaseId,String extraFilters,AppUserPrincipal actor) {
return exportFiltered(year,month,quarter,department,group,product,productSearch,projectGroup,includeUnassigned,releaseId,extraFilters,null,null,actor); return exportFiltered(year,month,quarter,department,group,product,productSearch,projectGroup,includeUnassigned,releaseId,extraFilters,null,null,actor);
} }
public ResponseEntity<StreamingResponseBody> exportFiltered(Integer year,Integer month,Integer quarter,String department,String group,String product,String productSearch,String projectGroup,boolean includeUnassigned,String releaseId,String extraFilters,String stage,String columnFilters,AppUserPrincipal actor) {
return exportRangeFiltered(year,month,quarter,department,group,product,productSearch,projectGroup,includeUnassigned,releaseId,extraFilters,stage,columnFilters,null,null,actor);
}
@GetMapping("/export") @GetMapping("/export")
@PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'DATA_ADMIN', 'USER')") @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'DATA_ADMIN', 'USER')")
public ResponseEntity<StreamingResponseBody> exportFiltered(@RequestParam(required = false) Integer year, public ResponseEntity<StreamingResponseBody> exportRangeFiltered(@RequestParam(required = false) Integer year,
@RequestParam(required = false) Integer month, @RequestParam(required = false) Integer month,
@RequestParam(required = false) Integer quarter, @RequestParam(required = false) Integer quarter,
@RequestParam(required = false) String department, @RequestParam(required = false) String department,
@@ -228,12 +236,14 @@ public class RecordController {
@RequestParam(required=false) String extraFilters, @RequestParam(required=false) String extraFilters,
@RequestParam(required=false) String stage, @RequestParam(required=false) String stage,
@RequestParam(required=false) String columnFilters, @RequestParam(required=false) String columnFilters,
@RequestParam(required=false) Integer monthStart,
@RequestParam(required=false) Integer monthEnd,
@AuthenticationPrincipal AppUserPrincipal actor) { @AuthenticationPrincipal AppUserPrincipal actor) {
governance.requireDetail(actor); governance.requireDetail(actor);
governance.requireBoundAction("QUERY", "query.detail"); governance.requireBoundAction("QUERY", "query.detail");
if (productSearch != null && productSearch.length() > SelectionValues.MAX_VALUE_LENGTH) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "产品搜索文本过长"); if (productSearch != null && productSearch.length() > SelectionValues.MAX_VALUE_LENGTH) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "产品搜索文本过长");
Path file = stage==null&&columnFilters==null ? workbookExport.export(year,month,quarter,department,group,product,projectGroup,includeUnassigned,releaseId,actor,QueryFilterFields.parse(extraFilters),productSearch) : workbookExport.exportFiltered(year, month, quarter, department, group, product, projectGroup, Path file = workbookExport.exportRangeFiltered(year, month, quarter, department, group, product, projectGroup,
includeUnassigned, releaseId, actor, QueryFilterFields.parse(extraFilters), productSearch,stage,cn.yunsheng.quality.filters.SourceColumnQueries.parse(columnFilters)); includeUnassigned, releaseId, actor, QueryFilterFields.parse(extraFilters), productSearch,stage,cn.yunsheng.quality.filters.SourceColumnQueries.parse(columnFilters),monthStart,monthEnd);
StreamingResponseBody body = output -> { StreamingResponseBody body = output -> {
try (InputStream input = Files.newInputStream(file)) { try (InputStream input = Files.newInputStream(file)) {
input.transferTo(output); input.transferTo(output);
@@ -250,7 +260,11 @@ public class RecordController {
} }
static void appendPeriodFilters(StringBuilder predicate, List<Object> args, Integer year, Integer month, Integer quarter) { static void appendPeriodFilters(StringBuilder predicate, List<Object> args, Integer year, Integer month, Integer quarter) {
validatePeriodFilters(year, month, quarter); appendPeriodFilters(predicate,args,year,month,quarter,null,null);
}
static void appendPeriodFilters(StringBuilder predicate, List<Object> args, Integer year, Integer month, Integer quarter, Integer monthStart, Integer monthEnd) {
validatePeriodFilters(year, month, quarter, monthStart, monthEnd);
if (year != null) { predicate.append(" AND f.month_key LIKE ?"); args.add(String.format("%04d-%%", year)); } if (year != null) { predicate.append(" AND f.month_key LIKE ?"); args.add(String.format("%04d-%%", year)); }
if (month != null) { predicate.append(" AND RIGHT(f.month_key, 2) = ?"); args.add(String.format("%02d", month)); } if (month != null) { predicate.append(" AND RIGHT(f.month_key, 2) = ?"); args.add(String.format("%02d", month)); }
if (quarter != null) { if (quarter != null) {
@@ -259,13 +273,25 @@ public class RecordController {
args.add(firstMonth); args.add(firstMonth);
args.add(firstMonth + 2); args.add(firstMonth + 2);
} }
if (monthStart != null) {
predicate.append(" AND CAST(RIGHT(f.month_key, 2) AS UNSIGNED) BETWEEN ? AND ?");
args.add(monthStart); args.add(monthEnd);
}
} }
static void validatePeriodFilters(Integer year, Integer month, Integer quarter) { static void validatePeriodFilters(Integer year, Integer month, Integer quarter) {
validatePeriodFilters(year,month,quarter,null,null);
}
static void validatePeriodFilters(Integer year, Integer month, Integer quarter, Integer monthStart, Integer monthEnd) {
if (year != null && (year < 2000 || year > 2100)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "年份无效"); if (year != null && (year < 2000 || year > 2100)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "年份无效");
if (month != null && (month < 1 || month > 12)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "月份无效"); if (month != null && (month < 1 || month > 12)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "月份无效");
if (quarter != null && (quarter < 1 || quarter > 4)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "季度无效"); if (quarter != null && (quarter < 1 || quarter > 4)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "季度无效");
if (month != null && quarter != null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "月份与季度筛选不可同时指定"); if (month != null && quarter != null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "月份与季度筛选不可同时指定");
if ((monthStart == null) != (monthEnd == null)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "monthStart 和 monthEnd 必须成对提供");
if (monthStart != null && (year == null || month != null || quarter != null
|| monthStart < 1 || monthStart > 12 || monthEnd < 1 || monthEnd > 12 || monthStart > monthEnd))
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "月份范围无效,且不能与 month 或 quarter 并用");
} }
private Map<String, Object> mapRow(ResultSet rs, String stage, Set<String> allowedFields, List<String> selectedColumns) throws SQLException { private Map<String, Object> mapRow(ResultSet rs, String stage, Set<String> allowedFields, List<String> selectedColumns) throws SQLException {
@@ -73,10 +73,13 @@ public class RecordsWorkbookExportService {
return exportFiltered(year,month,quarter,department,group,product,projectGroup,includeUnassigned,releaseId,actor,extraFilters,productSearch,null,List.of()); return exportFiltered(year,month,quarter,department,group,product,projectGroup,includeUnassigned,releaseId,actor,extraFilters,productSearch,null,List.of());
} }
public Path exportFiltered(Integer year,Integer month,Integer quarter,String department,String group,String product,String projectGroup,boolean includeUnassigned,String releaseId,AppUserPrincipal actor,Map<String,List<String>> extraFilters,String productSearch,String selectedStage,List<Map<String,Object>> columnFilters) { public Path exportFiltered(Integer year,Integer month,Integer quarter,String department,String group,String product,String projectGroup,boolean includeUnassigned,String releaseId,AppUserPrincipal actor,Map<String,List<String>> extraFilters,String productSearch,String selectedStage,List<Map<String,Object>> columnFilters) {
return exportRangeFiltered(year,month,quarter,department,group,product,projectGroup,includeUnassigned,releaseId,actor,extraFilters,productSearch,selectedStage,columnFilters,null,null);
}
public Path exportRangeFiltered(Integer year,Integer month,Integer quarter,String department,String group,String product,String projectGroup,boolean includeUnassigned,String releaseId,AppUserPrincipal actor,Map<String,List<String>> extraFilters,String productSearch,String selectedStage,List<Map<String,Object>> columnFilters,Integer monthStart,Integer monthEnd) {
if(selectedStage!=null&&!List.of("black","bright").contains(selectedStage))throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"stage 必须是 black 或 bright"); if(selectedStage!=null&&!List.of("black","bright").contains(selectedStage))throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"stage 必须是 black 或 bright");
columnFilters=cn.yunsheng.quality.filters.SourceColumnQueries.parse(columnFilters); columnFilters=cn.yunsheng.quality.filters.SourceColumnQueries.parse(columnFilters);
if(selectedStage!=null)for(var rule:columnFilters)if(!List.of(selectedStage).containsAll((List<?>)rule.get("stages")))throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"来源字段筛选必须属于当前阶段"); if(selectedStage!=null)for(var rule:columnFilters)if(!List.of(selectedStage).containsAll((List<?>)rule.get("stages")))throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"来源字段筛选必须属于当前阶段");
RecordController.validatePeriodFilters(year, month, quarter); RecordController.validatePeriodFilters(year, month, quarter, monthStart, monthEnd);
if (productSearch != null && productSearch.length() > SelectionValues.MAX_VALUE_LENGTH) if (productSearch != null && productSearch.length() > SelectionValues.MAX_VALUE_LENGTH)
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "产品搜索文本过长"); throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "产品搜索文本过长");
String selectedRelease = releases.resolvePublishedRelease(releaseId); String selectedRelease = releases.resolvePublishedRelease(releaseId);
@@ -89,9 +92,9 @@ public class RecordsWorkbookExportService {
workbook.setCompressTempFiles(true); workbook.setCompressTempFiles(true);
CellStyle headerStyle = createHeaderStyle(workbook); CellStyle headerStyle = createHeaderStyle(workbook);
if(selectedStage==null||selectedStage.equals("black"))createStageSheet(workbook, headerStyle, "黑片", "fact_black", selectedRelease, year, month, quarter, if(selectedStage==null||selectedStage.equals("black"))createStageSheet(workbook, headerStyle, "黑片", "fact_black", selectedRelease, year, month, quarter,
department, group, product, projectGroup, includeUnassigned, allowedFields, actor, extraFilters, productSearch,columnFilters); department, group, product, projectGroup, includeUnassigned, allowedFields, actor, extraFilters, productSearch,columnFilters,monthStart,monthEnd);
if(selectedStage==null||selectedStage.equals("bright"))createStageSheet(workbook, headerStyle, "亮片", "fact_bright", selectedRelease, year, month, quarter, if(selectedStage==null||selectedStage.equals("bright"))createStageSheet(workbook, headerStyle, "亮片", "fact_bright", selectedRelease, year, month, quarter,
department, group, product, projectGroup, includeUnassigned, allowedFields, actor, extraFilters, productSearch,columnFilters); department, group, product, projectGroup, includeUnassigned, allowedFields, actor, extraFilters, productSearch,columnFilters,monthStart,monthEnd);
try (var stream = Files.newOutputStream(output)) { try (var stream = Files.newOutputStream(output)) {
workbook.write(stream); workbook.write(stream);
} }
@@ -112,12 +115,12 @@ public class RecordsWorkbookExportService {
String releaseId, Integer year, Integer month, Integer quarter, String department, String releaseId, Integer year, Integer month, Integer quarter, String department,
String group, String product, String projectGroup, boolean includeUnassigned, String group, String product, String projectGroup, boolean includeUnassigned,
Set<String> allowedFields, AppUserPrincipal actor, Map<String,List<String>> extraFilters, Set<String> allowedFields, AppUserPrincipal actor, Map<String,List<String>> extraFilters,
String productSearch,List<Map<String,Object>> columnFilters) throws IOException { String productSearch,List<Map<String,Object>> columnFilters,Integer monthStart,Integer monthEnd) throws IOException {
Sheet sheet = workbook.createSheet(sheetName); Sheet sheet = workbook.createSheet(sheetName);
LinkedHashSet<String> rawFields = new LinkedHashSet<>(); LinkedHashSet<String> rawFields = new LinkedHashSet<>();
if (releaseId != null) { if (releaseId != null) {
QueryScope rawScope = queryScope(fact, releaseId, year, month, quarter, department, group, product, QueryScope rawScope = queryScope(fact, releaseId, year, month, quarter, department, group, product,
projectGroup, includeUnassigned, "f.raw_values", actor, extraFilters, productSearch,columnFilters); projectGroup, includeUnassigned, "f.raw_values", actor, extraFilters, productSearch,columnFilters,monthStart,monthEnd);
scan(rawScope, rs -> rawFields.addAll(visibleRaw(readJson(rs.getString(1)), allowedFields).keySet())); scan(rawScope, rs -> rawFields.addAll(visibleRaw(readJson(rs.getString(1)), allowedFields).keySet()));
} }
boolean showOrderType=SalesOrderTypes.allowed(allowedFields); boolean showOrderType=SalesOrderTypes.allowed(allowedFields);
@@ -141,7 +144,7 @@ public class RecordsWorkbookExportService {
QueryScope rowScope = queryScope(fact, releaseId, year, month, quarter, department, group, product, QueryScope rowScope = queryScope(fact, releaseId, year, month, quarter, department, group, product,
projectGroup, includeUnassigned, projectGroup, includeUnassigned,
"sr.source_file, f.sheet_name, f.physical_row_no, sr.id AS source_revision_id, f.month_key, " + "sr.source_file, f.sheet_name, f.physical_row_no, sr.id AS source_revision_id, f.month_key, " +
"f.department, f.group_name, f.project_group, f.product, f.raw_values", actor, extraFilters, productSearch,columnFilters); "f.department, f.group_name, f.project_group, f.product, f.raw_values", actor, extraFilters, productSearch,columnFilters,monthStart,monthEnd);
int[] rowIndex = {1}; int[] rowIndex = {1};
scan(rowScope, rs -> writeRow(sheet, rowIndex, rs, rawFields, allowedFields,showOrderType)); scan(rowScope, rs -> writeRow(sheet, rowIndex, rs, rawFields, allowedFields,showOrderType));
} }
@@ -149,11 +152,11 @@ public class RecordsWorkbookExportService {
private QueryScope queryScope(String fact, String releaseId, Integer year, Integer month, Integer quarter, private QueryScope queryScope(String fact, String releaseId, Integer year, Integer month, Integer quarter,
String department, String group, String product, String projectGroup, String department, String group, String product, String projectGroup,
boolean includeUnassigned, String select, AppUserPrincipal actor, Map<String,List<String>> extraFilters, boolean includeUnassigned, String select, AppUserPrincipal actor, Map<String,List<String>> extraFilters,
String productSearch,List<Map<String,Object>> columnFilters) { String productSearch,List<Map<String,Object>> columnFilters,Integer monthStart,Integer monthEnd) {
StringBuilder predicate = new StringBuilder(" WHERE rm.release_id = ?"); StringBuilder predicate = new StringBuilder(" WHERE rm.release_id = ?");
List<Object> args = new ArrayList<>(); List<Object> args = new ArrayList<>();
args.add(releaseId); args.add(releaseId);
RecordController.appendPeriodFilters(predicate, args, year, month, quarter); RecordController.appendPeriodFilters(predicate, args, year, month, quarter, monthStart, monthEnd);
SelectionValues.appendPredicate(predicate, args, "f.department", department, DepartmentLabels::queryValue); SelectionValues.appendPredicate(predicate, args, "f.department", department, DepartmentLabels::queryValue);
SelectionValues.appendPredicate(predicate, args, "f.group_name", group, value -> value); SelectionValues.appendPredicate(predicate, args, "f.group_name", group, value -> value);
SelectionValues.appendPredicate(predicate, args, "f.product", product, value -> value); SelectionValues.appendPredicate(predicate, args, "f.product", product, value -> value);
@@ -0,0 +1,23 @@
-- Query-oriented category payloads over the existing exact source statistics.
CREATE TABLE fine_defect_compact_build (
build_id CHAR(36) COLLATE utf8mb4_0900_ai_ci NOT NULL PRIMARY KEY,
format_version INT NOT NULL,
CONSTRAINT fk_fine_compact_build FOREIGN KEY (build_id) REFERENCES transform_build(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_cs;
CREATE TABLE fine_defect_compact (
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,
category VARCHAR(32) NOT NULL,
source_row_count BIGINT NOT NULL,
stats JSON NOT NULL,
payload_bytes INT NOT NULL,
KEY ix_fine_compact_scope (build_id,stage,category,month_key),
CONSTRAINT fk_fine_compact_fact_build FOREIGN KEY (build_id) REFERENCES transform_build(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_cs;
@@ -160,7 +160,7 @@ class AnalysisChangesServiceTest {
void rejectsOutOfRangeLimitsBeforeQueryingMetrics() { void rejectsOutOfRangeLimitsBeforeQueryingMetrics() {
authorize(Set.of("*")); authorize(Set.of("*"));
assertThrows(org.springframework.web.server.ResponseStatusException.class, assertThrows(org.springframework.web.server.ResponseStatusException.class,
() -> service.query(request("product", "rate_drop", 11), actor)); () -> service.query(request("product", "rate_drop", 101), actor));
verifyNoInteractions(metrics); verifyNoInteractions(metrics);
} }
@@ -0,0 +1,44 @@
package cn.yunsheng.quality.analysis;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.metrics.MetricsService;
import cn.yunsheng.quality.metrics.MetricsResponseAccess;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
class DefectDrilldownControllerTest {
@Test void policyChangesDuringSnapshotCannotReleaseRemovedSourceFields() {
var metrics=mock(MetricsService.class);var fine=mock(AnalysisFineDefectsService.class);var access=mock(GovernanceAccessService.class);
var actor=new AppUserPrincipal(UUID.randomUUID(),"test","","测试",true,false,List.of("SYSTEM_ADMIN"),List.of("*"),List.of("*"),List.of());
when(access.allowedFields(actor)).thenReturn(Set.of("*"));when(access.policy(actor.getId().toString())).thenReturn(Map.of("detailAllowed",true));
var stage=new MetricsService.MetricStage(new BigDecimal("90"),BigDecimal.TEN,new BigDecimal("100"),new BigDecimal(".9"),List.of(new MetricsService.DefectRow("加工废",BigDecimal.TEN,BigDecimal.ONE)),BigDecimal.ZERO,new BigDecimal(".9"));
var row=new MetricsService.TrendMetricRow("2026-01","2026-01","1月",null,null,null,null,null,stage,stage,Map.of(),null);
var snapshot=new MetricsService.MetricTrendSnapshot(Map.of("id","release"),Map.of(),List.of(row),Map.of());
var raw=Map.<String,Object>of("releaseId","release","fields",List.of(Map.of("key","加工缺角重量","label","加工缺角重量")),"groups",Map.of("2026-01",Map.of("rows",List.of(Map.of("key","加工缺角重量","weight",BigDecimal.ONE)))));
when(fine.grouped(anyMap(),eq(actor),eq("period"),eq("month"))).thenReturn(raw);
when(metrics.calculatePairedSnapshot(any(),eq(actor),eq(true),any())).thenAnswer(call->{
Object result=((Supplier<?>)call.getArgument(3)).get();
when(access.allowedFields(actor)).thenReturn(Set.of("bright_total_weight","bright_processing_scrap"));
return new MetricsService.PairedSnapshot<>(null,snapshot,result);
});
var controller=new DefectDrilldownController(fine,metrics,new MetricsResponseAccess(access),access);
var response=controller.query(Map.of("releaseId","release","stage","bright","category","加工废","dimension","period","year",2026),actor);
assertTrue(((List<?>)response.get("fields")).isEmpty());
var group=(Map<?,?>)((List<?>)response.get("groups")).getFirst();
assertTrue(((List<?>)group.get("children")).isEmpty());assertEquals(new BigDecimal("100"),group.get("totalWeight"));
verify(access,times(2)).requireDetail(actor);
}
@Test void deniedDetailOrInvalidDimensionCannotStartEitherScan() {
var metrics=mock(MetricsService.class);var fine=mock(AnalysisFineDefectsService.class);var access=mock(GovernanceAccessService.class);var actor=mock(AppUserPrincipal.class);
var controller=new DefectDrilldownController(fine,metrics,new MetricsResponseAccess(access),access);
assertThrows(ResponseStatusException.class,()->controller.query(Map.of("dimension","sql-injection"),actor));
doThrow(new ResponseStatusException(HttpStatus.FORBIDDEN)).when(access).requireDetail(actor);
assertThrows(ResponseStatusException.class,()->controller.query(Map.of(),actor));verifyNoInteractions(metrics,fine);
}
}
@@ -43,7 +43,7 @@ class FineDefectRollupsMySqlTest {
jdbc.execute("CREATE TABLE data_release(id CHAR(36) PRIMARY KEY,name VARCHAR(100),status VARCHAR(32),published_at TIMESTAMP NULL,rule_version VARCHAR(100),mapping_version VARCHAR(100))"); jdbc.execute("CREATE TABLE data_release(id CHAR(36) PRIMARY KEY,name VARCHAR(100),status VARCHAR(32),published_at TIMESTAMP NULL,rule_version VARCHAR(100),mapping_version VARCHAR(100))");
jdbc.execute("CREATE TABLE release_member(release_id CHAR(36),source_revision_id CHAR(36),transform_build_id CHAR(36))"); jdbc.execute("CREATE TABLE release_member(release_id CHAR(36),source_revision_id CHAR(36),transform_build_id CHAR(36))");
for (String stage : List.of("black", "bright")) jdbc.execute("CREATE TABLE fact_" + stage + "(id BIGINT AUTO_INCREMENT PRIMARY KEY,build_id CHAR(36),source_revision_id CHAR(36),month_key CHAR(7),department VARCHAR(190),group_name VARCHAR(190),project_group VARCHAR(190),product VARCHAR(190),qualified_weight DECIMAL(38,8),scrap_weight DECIMAL(38,8),total_weight DECIMAL(38,8),raw_values JSON,components JSON,KEY ix_fact_" + stage + "_month_dims(build_id,month_key))"); for (String stage : List.of("black", "bright")) jdbc.execute("CREATE TABLE fact_" + stage + "(id BIGINT AUTO_INCREMENT PRIMARY KEY,build_id CHAR(36),source_revision_id CHAR(36),month_key CHAR(7),department VARCHAR(190),group_name VARCHAR(190),project_group VARCHAR(190),product VARCHAR(190),qualified_weight DECIMAL(38,8),scrap_weight DECIMAL(38,8),total_weight DECIMAL(38,8),raw_values JSON,components JSON,KEY ix_fact_" + stage + "_month_dims(build_id,month_key))");
for (String migration : List.of("V16__metric_rollups.sql", "V36__fine_defect_rollups.sql", "V39__sales_order_types.sql", "V40__sales_order_fact_indexes.sql", "V41__sales_order_metric_rollups.sql")) { for (String migration : List.of("V16__metric_rollups.sql", "V36__fine_defect_rollups.sql", "V39__sales_order_types.sql", "V40__sales_order_fact_indexes.sql", "V41__sales_order_metric_rollups.sql", "V42__fine_defect_compact_rollups.sql")) {
try (var stream = getClass().getResourceAsStream("/db/migration/" + migration)) { try (var stream = getClass().getResourceAsStream("/db/migration/" + migration)) {
for (String sql : new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8).split(";")) if (!sql.isBlank()) jdbc.execute(sql); for (String sql : new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8).split(";")) if (!sql.isBlank()) jdbc.execute(sql);
} }
@@ -97,6 +97,63 @@ class FineDefectRollupsMySqlTest {
verify(jdbc, never()).query(any(PreparedStatementCreator.class), any(ResultSetExtractor.class)); verify(jdbc, never()).query(any(PreparedStatementCreator.class), any(ResultSetExtractor.class));
} }
@Test void groupedDrilldownMatchesRawAcrossPeriodsObjectsAndRestrictedFieldsWithoutFactScans() {
for(String dimension:List.of("period","overall","department","group","product","projectGroup")) {
for(String granularity:List.of("month","quarter","year")) {
var input=request("black",Map.of("category","责废"));
var expected=source.grouped(input,actor,dimension,granularity);clearInvocations(jdbc);
assertEquals(canonical(expected),canonical(fast.grouped(input,actor,dimension,granularity)),dimension+granularity);
verify(jdbc,never()).query(any(PreparedStatementCreator.class),any(ResultSetExtractor.class));
}
}
when(governance.allowedFields(actor)).thenReturn(Set.of("责废尺寸重量"));
var input=request("black",Map.of("category","责废"));input.remove("year");
assertEquals(canonical(source.grouped(input,actor,"period","year")),canonical(fast.grouped(input,actor,"period","year")));
}
@Test void productDrilldownUsesCompactStatisticsWithoutChangingScopedOrRestrictedResults() {
var input=request("black",Map.of("category","责废"));
var expected=source.grouped(input,actor,"product","month");clearInvocations(jdbc);
assertEquals(canonical(expected),canonical(fast.grouped(input,actor,"product","month")));
var queries=org.mockito.ArgumentCaptor.forClass(String.class);
verify(jdbc,atLeastOnce()).query(queries.capture(),any(RowCallbackHandler.class),any(Object[].class));
assertTrue(queries.getAllValues().stream().anyMatch(sql -> sql.contains("JOIN fine_defect_compact f")));
for(var scope:List.<Map<String,Object>>of(Map.of("department","A"),Map.of("group","G"),Map.of("includeUnassigned",false),Map.of("productSearch","p0"))) {
var scoped=request("black",scope);scoped.put("category","责废");
assertEquals(canonical(source.grouped(scoped,actor,"product","month")),canonical(fast.grouped(scoped,actor,"product","month")));
}
var viewer=new AppUserPrincipal(UUID.randomUUID(),"scoped-viewer","","Viewer",true,false,List.of("USER"),List.of("A"),List.of("G"),List.of());
when(governance.allowedFields(viewer)).thenReturn(Set.of("责废尺寸重量"));
assertEquals(canonical(source.grouped(input,viewer,"product","month")),canonical(fast.grouped(input,viewer,"product","month")));
}
@Test void compactBackfillPreservesUnicodeEquivalentDimensionGroupsAndExactRowCounts() {
seed("black","2026-01","A","G","Café",Map.of("责废尺寸重量","0.100000000000000001"),Map.of("black_responsibility_scrap",1));
seed("black","2026-01","A","G","Cafe\u0301",Map.of("责废尺寸重量","0.200000000000000002"),Map.of("black_responsibility_scrap",1));
metrics.refreshBuild(build);
jdbc.update("DELETE FROM fine_defect_compact_build WHERE build_id=?",build);
jdbc.update("DELETE FROM fine_defect_compact WHERE build_id=?",build);
var input=request("black",Map.of("category","责废","productSearch","caf"));
var expected=fast.grouped(input,actor,"product","month"); // Existing SQL-collation tall path.
rollups.refreshCompactBuild(build);
var actual=fast.grouped(input,actor,"product","month");
var groups=(Map<?,?>)actual.get("groups");assertEquals(1,groups.size());
// SQL may choose either equivalent spelling as the group label.
assertEquals(canonical(new ArrayList<>(((Map<?,?>)expected.get("groups")).values())),canonical(new ArrayList<>(groups.values())));
assertEquals(2L,((Map<?,?>)groups.values().iterator().next()).get("sourceRows"));
}
@Test void missingVersionedCompactOrOversizedBucketFallsBackToExactTallStatistics() {
var input=request("black",Map.of("category","责废"));
var expected=source.grouped(input,actor,"product","month");
jdbc.update("UPDATE fine_defect_compact SET payload_bytes=5000000 WHERE build_id=?",build);
assertEquals(canonical(expected),canonical(fast.grouped(input,actor,"product","month")));
jdbc.update("UPDATE fine_defect_compact_build SET format_version=2 WHERE build_id=?",build);
assertEquals(canonical(expected),canonical(fast.grouped(input,actor,"product","month")));
jdbc.update("DELETE FROM fine_defect_compact_build WHERE build_id=?",build);
assertEquals(canonical(expected),canonical(fast.grouped(input,actor,"product","month")));
}
@Test void exactWeightsCountsReconciliationAndAllStandardScopesMatchTheSourceWithoutScanningFacts() { @Test void exactWeightsCountsReconciliationAndAllStandardScopesMatchTheSourceWithoutScanningFacts() {
for (String stage : List.of("black", "bright")) { for (String stage : List.of("black", "bright")) {
for (Map<String,Object> scope : List.<Map<String,Object>>of(Map.of(), Map.of("month", 1), Map.of("quarter", 1), for (Map<String,Object> scope : List.<Map<String,Object>>of(Map.of(), Map.of("month", 1), Map.of("quarter", 1),
@@ -137,6 +194,9 @@ class FineDefectRollupsMySqlTest {
equivalent(request("black", Map.of("group", "NEW")), actor); equivalent(request("black", Map.of("group", "NEW")), actor);
equivalent(request("black", Map.of("group", "G")), actor); equivalent(request("black", Map.of("group", "G")), actor);
equivalent(request("bright", Map.of()), actor); equivalent(request("bright", Map.of()), actor);
var detail=request("black",Map.of("category","责废"));
assertEquals(canonical(source.grouped(detail,actor,"product","month")),canonical(fast.grouped(detail,actor,"product","month")));
assertEquals(1,jdbc.queryForObject("SELECT COUNT(*) FROM fine_defect_compact_build WHERE build_id=? AND format_version=1",Integer.class,child));
} }
@Test void excludingASmallTypeSubtractsExactCountsAndWeightsAndLargeExclusionsKeepSourceFallback() { @Test void excludingASmallTypeSubtractsExactCountsAndWeightsAndLargeExclusionsKeepSourceFallback() {
@@ -170,7 +230,7 @@ class FineDefectRollupsMySqlTest {
String origin = System.getenv("QUALITY_FINE_ROLLUP_SOURCE_DATABASE"); String origin = System.getenv("QUALITY_FINE_ROLLUP_SOURCE_DATABASE");
assertTrue(origin.matches("[a-zA-Z0-9_]+") && !origin.equals(database)); assertTrue(origin.matches("[a-zA-Z0-9_]+") && !origin.equals(database));
String current = jdbc.queryForObject("SELECT release_id FROM " + origin + ".current_release WHERE dataset_id='quality'", String.class); String current = jdbc.queryForObject("SELECT release_id FROM " + origin + ".current_release WHERE dataset_id='quality'", String.class);
for (String table : List.of("fine_defect_rollup", "fine_defect_rollup_build", "metric_rollup", "metric_rollup_build", "fact_black", "fact_bright", "release_member", "transform_build", "source_revision")) jdbc.update("DELETE FROM " + table); for (String table : List.of("fine_defect_compact", "fine_defect_compact_build", "fine_defect_rollup", "fine_defect_rollup_build", "metric_rollup", "metric_rollup_build", "fact_black", "fact_bright", "release_member", "transform_build", "source_revision")) jdbc.update("DELETE FROM " + table);
jdbc.update("INSERT INTO transform_build SELECT b.id,b.source_revision_id FROM " + origin + ".transform_build b JOIN " + origin + ".release_member rm ON rm.transform_build_id=b.id WHERE rm.release_id=?", current); jdbc.update("INSERT INTO transform_build SELECT b.id,b.source_revision_id FROM " + origin + ".transform_build b JOIN " + origin + ".release_member rm ON rm.transform_build_id=b.id WHERE rm.release_id=?", current);
jdbc.update("INSERT INTO source_revision SELECT DISTINCT source_revision_id FROM transform_build"); jdbc.update("INSERT INTO source_revision SELECT DISTINCT source_revision_id FROM transform_build");
jdbc.update("INSERT INTO release_member SELECT ?,source_revision_id,transform_build_id FROM " + origin + ".release_member WHERE release_id=?", release, current); jdbc.update("INSERT INTO release_member SELECT ?,source_revision_id,transform_build_id FROM " + origin + ".release_member WHERE release_id=?", release, current);
@@ -284,6 +284,112 @@ class SourceColumnQueriesMySqlTest {
} }
} }
@Test void continuousRangeDetailsAndBothExcelSheetsExcludeOutsideMonthsAndUnauthorizedRows() throws Exception {
var releases=mock(cn.yunsheng.quality.release.ReleaseService.class);
when(releases.resolvePublishedRelease(RELEASE)).thenReturn(RELEASE);
var mapper=JsonMapper.builder().build();
for(String stage:List.of("black","bright")) {
jdbc.execute("ALTER TABLE fact_"+stage+" ADD qualified_weight DECIMAL(28,10),ADD scrap_weight DECIMAL(28,10),ADD total_weight DECIMAL(28,10),ADD components JSON,ADD sheet_name VARCHAR(80),ADD physical_row_no INT");
jdbc.update("UPDATE fact_"+stage+" SET qualified_weight=90,scrap_weight=10,total_weight=100,components=JSON_OBJECT(),sheet_name='fixture',physical_row_no=id");
String revision=stage.equals("black")?BLACK_REVISION:BRIGHT_REVISION;
for(String month:List.of("2026-03","2026-09","2026-10","2025-03")) {
insert(stage,"build-"+stage,revision,"部门A","组A","{\"委托单\":\"range-row\"}");
jdbc.update("UPDATE fact_"+stage+" SET month_key=?,components=JSON_OBJECT(),sheet_name='fixture',physical_row_no=id ORDER BY id DESC LIMIT 1",month);
insert(stage,"build-"+stage,revision,"部门B","组B","{\"委托单\":\"hidden-range-row\"}");
jdbc.update("UPDATE fact_"+stage+" SET month_key=? ORDER BY id DESC LIMIT 1",month);
}
}
var exporter=new cn.yunsheng.quality.records.RecordsWorkbookExportService(jdbc,releases,governance,mapper);
var records=new cn.yunsheng.quality.records.RecordController(jdbc,releases,mapper,governance,exporter);
for(String stage:List.of("black","bright")) {
var detail=records.recordsRangeFiltered(stage,2026,null,null,null,null,"P-1",null,null,true,RELEASE,1,50,null,null,null,"asc",null,null,null,3,9,actor);
assertEquals(2L,((Number)detail.get("total")).longValue());
var rows=(List<Map<String,Object>>)detail.get("rows");assertEquals(2,rows.size());
assertEquals(Set.of("2026-03","2026-09"),rows.stream().map(row->row.get("month")).collect(java.util.stream.Collectors.toSet()));
assertTrue(rows.stream().allMatch(row->"部门A".equals(row.get("department"))));
}
var file=exporter.exportRangeFiltered(2026,null,null,null,null,"P-1",null,true,RELEASE,actor,Map.of(),null,null,List.of(),3,9);
try(var book=org.apache.poi.ss.usermodel.WorkbookFactory.create(file.toFile())) {
assertEquals(2,book.getNumberOfSheets());
for(var sheet:book) {
assertEquals(2,sheet.getLastRowNum());
var months=new java.util.HashSet<String>();
for(int row=1;row<=sheet.getLastRowNum();row++) {
months.add(sheet.getRow(row).getCell(4).getStringCellValue());
assertEquals("部门A",sheet.getRow(row).getCell(5).getStringCellValue());
assertEquals("P-1",sheet.getRow(row).getCell(8).getStringCellValue());
}
assertEquals(Set.of("2026-03","2026-09"),months);
}
} finally {java.nio.file.Files.deleteIfExists(file);}
}
@Test void defectDrilldownUsesOfficialDenominatorsAcrossPeriodsObjectsAndCurrentPermissions() throws Exception {
when(governance.allowedFields(actor)).thenReturn(Set.of("*"));
when(governance.policy(actor.getId().toString())).thenReturn(Map.of("detailAllowed",true));
jdbc.execute("CREATE TABLE data_release(id CHAR(36) PRIMARY KEY,name VARCHAR(80),status VARCHAR(24),rule_version VARCHAR(80))");
jdbc.update("INSERT INTO data_release VALUES (?, 'fixture','PUBLISHED',NULL)",RELEASE);
jdbc.execute("ALTER TABLE data_release ADD published_at TIMESTAMP NULL,ADD mapping_version VARCHAR(100)");
jdbc.execute("CREATE TABLE transform_build(id CHAR(36),source_revision_id CHAR(36))");
jdbc.update("INSERT INTO transform_build VALUES ('build-bright',?)",BRIGHT_REVISION);
jdbc.update("INSERT INTO transform_build VALUES ('build-black',?)",BLACK_REVISION);
jdbc.execute("CREATE TABLE metric_rollup_build(build_id CHAR(36))");
for(String stage:List.of("black","bright")) {
jdbc.execute("DELETE FROM fact_"+stage);
jdbc.execute("ALTER TABLE fact_"+stage+" ADD qualified_weight DECIMAL(28,10),ADD scrap_weight DECIMAL(28,10),ADD total_weight DECIMAL(28,10),ADD components JSON,ADD sheet_name VARCHAR(80),ADD physical_row_no INT");
}
jdbc.execute("CREATE TABLE metric_rollup LIKE fact_black");
jdbc.execute("ALTER TABLE metric_rollup ADD stage VARCHAR(10),ADD source_row_count BIGINT,ADD excluded_blank_rows BIGINT,ADD unmapped_rows BIGINT,ADD project_unconfigured_rows BIGINT");
String[] months={"2026-01","2026-02","2026-03","2026-04","2025-01"};
int[] total={100,200,100,0,100};int[] child={2,6,0,0,7};int[] parent={10,20,5,0,10};
for(int i=0;i<months.length;i++) {
var raw=new java.util.LinkedHashMap<String,Object>();
for(String key:List.of("加工缺角重量","加工磕边重量","加工烂毛重量","加工尺寸超差重量","加工刀丝歪角重量","加工磁化方向错重量","加工隐裂重量","加工错重量","加工其他重量"))raw.put(key,0);
raw.put("加工缺角重量",i==2?null:child[i]);raw.put("加工磕边重量",parent[i]-child[i]);
insert("bright","build-bright",BRIGHT_REVISION,"部门A","组A",JsonMapper.builder().build().writeValueAsString(raw));
jdbc.update("UPDATE fact_bright SET month_key=?,qualified_weight=?,scrap_weight=?,total_weight=?,components=?,sheet_name='fixture',physical_row_no=id ORDER BY id DESC LIMIT 1",months[i],total[i]-parent[i],parent[i],total[i],"{\"bright_processing_scrap\":"+parent[i]+"}");
}
insert("bright","build-bright",BRIGHT_REVISION,"部门B","组B","{\"加工缺角重量\":999}");
jdbc.update("UPDATE fact_bright SET qualified_weight=1,scrap_weight=999,total_weight=1000,components=JSON_OBJECT('bright_processing_scrap',999) ORDER BY id DESC LIMIT 1");
var releases=mock(cn.yunsheng.quality.release.ReleaseService.class);when(releases.resolvePublishedRelease(RELEASE)).thenReturn(RELEASE);
var mapper=JsonMapper.builder().build();var fine=new cn.yunsheng.quality.analysis.AnalysisFineDefectsService(jdbc,releases,governance,mapper);fine.setSourceColumns(queries);
var metrics=new cn.yunsheng.quality.metrics.MetricsService(jdbc,governance);
var controller=new cn.yunsheng.quality.analysis.DefectDrilldownController(fine,metrics,new cn.yunsheng.quality.metrics.MetricsResponseAccess(governance),governance);
var input=new java.util.LinkedHashMap<String,Object>(Map.of("releaseId",RELEASE,"year",2026,"stage","bright","category","加工废","dimension","period","trendGranularity","month"));
var monthly=controller.query(input,actor);var groups=(List<Map<String,Object>>)monthly.get("groups");assertEquals(4,groups.size());
java.util.function.Function<Map<String,Object>,Object> rate=g->((List<Map<String,Object>>)g.get("children")).stream().filter(c->c.get("key").equals("加工缺角重量")).findFirst().orElseThrow().get("rate");
assertEquals(0,new java.math.BigDecimal("0.02").compareTo((java.math.BigDecimal)rate.apply(groups.get(0))));
assertEquals(0,new java.math.BigDecimal("0.03").compareTo((java.math.BigDecimal)rate.apply(groups.get(1))));
assertNull(rate.apply(groups.get(2)));assertNull(rate.apply(groups.get(3)));
input.put("trendGranularity","quarter");var quarter=(List<Map<String,Object>>)controller.query(input,actor).get("groups");
assertEquals(0,new java.math.BigDecimal("0.02").compareTo((java.math.BigDecimal)rate.apply(quarter.get(0)))); // 8 / 400, never average monthly percentages.
input.put("dimension","product");var products=(List<Map<String,Object>>)controller.query(input,actor).get("groups");assertEquals(1,products.size());assertEquals("P-1",products.getFirst().get("key"));
input.put("dimension","period");input.put("trendGranularity","year");input.remove("year");assertEquals(2,((List<?>)controller.query(input,actor).get("groups")).size());
for(String product:new String[]{"",null}) {
insert("bright","build-bright",BRIGHT_REVISION,"部门A","组A","{\"加工缺角重量\":4}");
jdbc.update("UPDATE fact_bright SET product=?,qualified_weight=?,scrap_weight=4,total_weight=?,components=JSON_OBJECT('bright_processing_scrap',4) ORDER BY id DESC LIMIT 1",product,product==null?396:96,product==null?400:100);
}
input.put("year",2026);input.put("dimension","product");
var unassigned=(List<Map<String,Object>>)controller.query(input,actor).get("groups");
var emptyGroup=unassigned.stream().filter(g->"".equals(g.get("key"))).findFirst().orElseThrow();
var nullGroup=unassigned.stream().filter(g->"__UNASSIGNED__".equals(g.get("key"))).findFirst().orElseThrow();
assertEquals(0,new java.math.BigDecimal("0.04").compareTo((java.math.BigDecimal)rate.apply(emptyGroup)));
assertEquals(0,new java.math.BigDecimal("0.01").compareTo((java.math.BigDecimal)rate.apply(nullGroup)));
jdbc.update("UPDATE fact_bright SET department='质量管理部' WHERE department='部门A'");
var allActor=new AppUserPrincipal(UUID.randomUUID(),"alias-test","","测试",true,false,List.of("SYSTEM_ADMIN"),List.of("*"),List.of("*"),List.of());
when(governance.allowedFields(allActor)).thenReturn(Set.of("*"));when(governance.policy(allActor.getId().toString())).thenReturn(Map.of("detailAllowed",true));
input.put("dimension","department");
var departments=(List<Map<String,Object>>)controller.query(input,allActor).get("groups");
var quality=departments.stream().filter(g->"质量中心".equals(g.get("key"))).findFirst().orElseThrow();
assertFalse(((List<?>)quality.get("children")).isEmpty());
jdbc.update("UPDATE fact_bright SET department='部门A' WHERE department='质量管理部'");
input.put("dimension","period");
when(governance.allowedFields(actor)).thenReturn(Set.of("加工缺角重量"));
var restricted=(List<Map<String,Object>>)controller.query(input,actor).get("groups");assertTrue(restricted.stream().allMatch(g->g.get("totalWeight")==null&&g.get("parentWeight")==null&&rate.apply(g)==null));
doThrow(new ResponseStatusException(org.springframework.http.HttpStatus.FORBIDDEN)).when(governance).requireDetail(actor);
assertThrows(ResponseStatusException.class,()->controller.query(input,actor));
}
private void schema() { private void schema() {
jdbc.execute("CREATE TABLE release_member(release_id CHAR(36),source_document_id CHAR(36),source_revision_id CHAR(36),transform_build_id CHAR(36),PRIMARY KEY(release_id,source_document_id))"); jdbc.execute("CREATE TABLE release_member(release_id CHAR(36),source_document_id CHAR(36),source_revision_id CHAR(36),transform_build_id CHAR(36),PRIMARY KEY(release_id,source_document_id))");
jdbc.execute("CREATE TABLE source_revision(id CHAR(36) PRIMARY KEY,source_file VARCHAR(190))"); jdbc.execute("CREATE TABLE source_revision(id CHAR(36) PRIMARY KEY,source_file VARCHAR(190))");
@@ -2,6 +2,7 @@ package cn.yunsheng.quality.metrics;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains; import static org.mockito.ArgumentMatchers.contains;
@@ -66,6 +67,28 @@ class MetricsServiceSnapshotTest {
assertTrue(!TransactionSynchronizationManager.isActualTransactionActive()); assertTrue(!TransactionSynchronizationManager.isActualTransactionActive());
} }
@Test
void pairedFormalAndSourceReadsShareOneSnapshotAndSupplierFailureRollsItBack() {
RecordingTransactionManager manager=new RecordingTransactionManager();
JdbcTemplate jdbc=mock(JdbcTemplate.class);GovernanceAccessService governance=mock(GovernanceAccessService.class);
MetricsService service=new MetricsService(jdbc,governance,manager);
when(jdbc.queryForObject(contains("metric_rollup_build"),eq(Integer.class),any(Object[].class))).thenAnswer(call->{assertTrue(TransactionSynchronizationManager.isActualTransactionActive());return 0;});
when(jdbc.query(anyString(),any(RowMapper.class),any(Object[].class))).thenAnswer(call->{
assertTrue(!TransactionSynchronizationManager.isActualTransactionActive());
RowMapper<?> mapper=call.getArgument(1);ResultSet rs=mock(ResultSet.class);
when(rs.getString(1)).thenReturn("release-1");when(rs.getString(2)).thenReturn("current");when(rs.getString(3)).thenReturn("ACTIVE");return List.of(mapper.mapRow(rs,0));
});
doAnswer(call->{assertTrue(TransactionSynchronizationManager.isActualTransactionActive());return null;}).when(jdbc).query(anyString(),any(RowCallbackHandler.class),any(Object[].class));
var actor=new AppUserPrincipal(UUID.randomUUID(),"reader","unused","Reader",true,false,List.of("SYSTEM_ADMIN"),List.of("*"),List.of("*"),List.of());
var query=new MetricsService.MetricsQuery(Integer.valueOf(2026),null,null,null,null,null,null,"overall",true,"release-1");
var paired=service.calculatePairedSnapshot(query,actor,false,()->{
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());assertEquals(1,manager.beginCount);return "source";
});
assertEquals("source",paired.source());assertEquals("release-1",paired.metrics().release().get("id"));assertTrue(!TransactionSynchronizationManager.isActualTransactionActive());
assertThrows(IllegalStateException.class,()->service.calculatePairedSnapshot(query,actor,false,()->{assertTrue(TransactionSynchronizationManager.isActualTransactionActive());throw new IllegalStateException("fixture");}));
assertEquals(2,manager.beginCount);assertEquals(1,manager.rollbackCount);assertTrue(!TransactionSynchronizationManager.isActualTransactionActive());
}
@Test @Test
void governanceSnapshotStartsRepeatableReadOrJoinsTheExistingWriteTransaction() { void governanceSnapshotStartsRepeatableReadOrJoinsTheExistingWriteTransaction() {
RecordingTransactionManager transactionManager = new RecordingTransactionManager(); RecordingTransactionManager transactionManager = new RecordingTransactionManager();
@@ -98,6 +121,7 @@ class MetricsServiceSnapshotTest {
private int isolationLevel; private int isolationLevel;
private int propagationBehavior; private int propagationBehavior;
private int beginCount; private int beginCount;
private int rollbackCount;
private boolean active; private boolean active;
@Override protected Object doGetTransaction() { return this; } @Override protected Object doGetTransaction() { return this; }
@@ -113,6 +137,6 @@ class MetricsServiceSnapshotTest {
@Override protected void doCommit(DefaultTransactionStatus status) { active = false; } @Override protected void doCommit(DefaultTransactionStatus status) { active = false; }
@Override protected void doRollback(DefaultTransactionStatus status) { active = false; } @Override protected void doRollback(DefaultTransactionStatus status) { active = false; rollbackCount++; }
} }
} }
@@ -0,0 +1,81 @@
package cn.yunsheng.quality.records;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import cn.yunsheng.quality.governance.GovernanceAccessService;
import cn.yunsheng.quality.identity.AppUserPrincipal;
import cn.yunsheng.quality.release.ReleaseService;
import java.sql.PreparedStatement;
import java.util.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.MethodParameter;
import org.springframework.jdbc.core.*;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.*;
import tools.jackson.databind.json.JsonMapper;
class RecordRangeHttpTest {
final JdbcTemplate jdbc=mock(JdbcTemplate.class);
final GovernanceAccessService access=mock(GovernanceAccessService.class);
final AppUserPrincipal actor=mock(AppUserPrincipal.class);
final List<String> queries=new ArrayList<>();
final List<List<Object>> bindings=new ArrayList<>();
MockMvc mvc;
@BeforeEach void setup() {
var releases=mock(ReleaseService.class);
when(releases.resolvePublishedRelease("release")).thenReturn("release");
when(actor.getRoleCodes()).thenReturn(List.of("USER"));
when(actor.getDepartments()).thenReturn(List.of("部门A"));
when(actor.getGroups()).thenReturn(List.of("组A"));
when(access.allowedFields(actor)).thenReturn(Set.of("原始字段"));
when(jdbc.queryForObject(startsWith("SELECT COUNT(*)"),eq(Long.class),any(Object[].class))).thenAnswer(call->{
queries.add(call.getArgument(0));bindings.add(Arrays.asList(Arrays.copyOfRange(call.getArguments(),2,call.getArguments().length)));return 0L;
});
doAnswer(call->{
queries.add(call.getArgument(0));var values=new ArrayList<Object>();
PreparedStatement statement=mock(PreparedStatement.class);
doAnswer(bind->{values.add(bind.getArgument(1));return null;}).when(statement).setObject(anyInt(),any());
((PreparedStatementSetter)call.getArgument(1)).setValues(statement);bindings.add(values);return null;
}).when(jdbc).query(anyString(),any(PreparedStatementSetter.class),any(RowCallbackHandler.class));
var mapper=JsonMapper.builder().build();
var exporter=new RecordsWorkbookExportService(jdbc,releases,access,mapper);
var controller=new RecordController(jdbc,releases,mapper,access,exporter);
mvc=MockMvcBuilders.standaloneSetup(controller).setCustomArgumentResolvers(new HandlerMethodArgumentResolver(){
public boolean supportsParameter(MethodParameter p){return p.getParameterType()==AppUserPrincipal.class;}
public Object resolveArgument(MethodParameter p,ModelAndViewContainer m,NativeWebRequest r,WebDataBinderFactory b){return actor;}
}).build();
}
@Test void listAndBothExportSheetsApplyTheSameRangeAndLiveDataScope() throws Exception {
mvc.perform(get("/api/records").param("stage","black").param("year","2026").param("monthStart","3").param("monthEnd","9").param("product","产品A").param("releaseId","release")).andExpect(status().isOk());
var result=mvc.perform(get("/api/records/export").param("year","2026").param("monthStart","3").param("monthEnd","9").param("product","产品A").param("releaseId","release")).andReturn();
if(result.getRequest().isAsyncStarted())mvc.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch(result)).andExpect(status().isOk());
assertEquals(5,queries.size());
for(int i=0;i<queries.size();i++) {
assertTrue(queries.get(i).contains("BETWEEN ? AND ?"),queries.get(i));
assertEquals(List.of("release","2026-%",3,9,"产品A","部门A","组A"),bindings.get(i));
}
verify(access,times(2)).requireDetail(actor);verify(access,times(2)).requireBoundAction("QUERY","query.detail");
}
@Test void malformedRangesAreRejectedByListAndExportBeforeAnyRowsAreRead() throws Exception {
for(String endpoint:List.of("/api/records","/api/records/export")) {
for(String[] bounds:List.of(new String[]{"3",""},new String[]{"9","3"},new String[]{"0","9"},new String[]{"1","13"})) {
var request=get(endpoint).param("stage","black").param("year","2026").param("monthStart",bounds[0]).param("releaseId","release");
if(!bounds[1].isEmpty())request.param("monthEnd",bounds[1]);
mvc.perform(request).andExpect(status().isBadRequest());
}
mvc.perform(get(endpoint).param("stage","black").param("monthStart","1").param("monthEnd","9").param("releaseId","release")).andExpect(status().isBadRequest());
mvc.perform(get(endpoint).param("stage","black").param("year","2026").param("month","3").param("monthStart","1").param("monthEnd","9").param("releaseId","release")).andExpect(status().isBadRequest());
}
assertTrue(queries.isEmpty());
}
}
+1 -1
View File
@@ -24,7 +24,7 @@ from pymysql.cursors import DictCursor
LABEL = 'com.lyunlab.quality-center-retention' LABEL = 'com.lyunlab.quality-center-retention'
SUPPORT = Path.home() / 'Library/Application Support/QualityCenterService' SUPPORT = Path.home() / 'Library/Application Support/QualityCenterService'
TERMINAL = ('COMPLETED', 'FAILED', 'CANCELLED', 'SUCCEEDED', 'STOPPED', 'REJECTED', 'CLOSED', 'INTERRUPTED', 'INVALIDATED') TERMINAL = ('COMPLETED', 'FAILED', 'CANCELLED', 'SUCCEEDED', 'STOPPED', 'REJECTED', 'CLOSED', 'INTERRUPTED', 'INVALIDATED')
BUILD_CHILDREN = ('defect_component', 'fine_defect_rollup', 'metric_rollup', 'sales_order_metric_rollup', BUILD_CHILDREN = ('fine_defect_compact', 'fine_defect_compact_build', 'defect_component', 'fine_defect_rollup', 'metric_rollup', 'sales_order_metric_rollup',
'fine_defect_rollup_build', 'metric_rollup_build', 'sales_order_metric_rollup_build', 'fine_defect_rollup_build', 'metric_rollup_build', 'sales_order_metric_rollup_build',
'fact_black', 'fact_bright') 'fact_black', 'fact_bright')
OWNED_RELEASE = ('release_member', 'release_protocol_dependency', 'release_source_exclusion', OWNED_RELEASE = ('release_member', 'release_protocol_dependency', 'release_source_exclusion',
+41 -1
View File
@@ -2,7 +2,47 @@
核实日期:2026年11月12日(上海时区)。这是可更新的接续记录,不替代实际代码与运行状态。 核实日期:2026年11月12日(上海时区)。这是可更新的接续记录,不替代实际代码与运行状态。
## 当前任务
## 2026-11-13:Gitea 同步交付
本次将已发布的来源明细与期间范围修复、换年保持产品层级、查询默认年度、不良率曲线细分、表格及 Excel 联动、趋势与表格独立加载、标题旁钻取路径,以及紧凑预汇总提速整合同步至 `lyun/quality_analyse` 的 `main`。保留各次发布的提交记录,线上版本为2026.11.13.4,本次仓库同步不重复部署。
功能源码与已验收交付目录逐文件一致;已有构建、隔离数据库、维护检查及线上一致性与性能验证继续有效。本节为最新仓库交付状态,下方实施记录中的“未推送”保留为当时时点。私有配置、业务数据、构建包和验收产物继续留在本机。
## 2026-11-13:细分表格预汇总优化
定位:年度全产品加工废细项需对约31万条细项统计重新分组,源查询约10秒,线上完整接口约11.76秒。继承柏拉图预汇总方式,新增按构建、阶段、月份、完整组织维度、产品和分类保存的紧凑统计;重量用十进制字符串,保留缺失/非法/负值及行数。查询继续使用原授权条件、正式版本绑定和正式分母,SQL保留原排序规则的产品桶语义。
生命周期:V42只新增两张派生汇总表;构建、复制与局部更新同事务维护。启动时仅补当前正式版本,可从既有统计生成,不重导原表。未完成、版本不符、扩展筛选或大桶超过预算时沿用原查询。保留清理识别新依赖;本轮不解除旧自动维护兼容保护。
验证:974项后端构建测试(812执行通过、162按条件跳过),专项隔离库/控制器/快照26项(25通过、1跳过),维护23项通过。隔离副本同范围核心SQL从9978ms降至1471ms,全部金额与计数一致,最大单桶13.9KB,自有库已清理。只读核对当前版本汇总,不等于私有原表全量回归。已发布2026.11.13.4,功能提交 `a4eabeb`,当前52个构建的派生统计就绪,业务版本指针和MySQL进程保持。线上同范围完整接口11757ms降至3262ms,6715个产品完整返回结果的规范化校验值一致;实际点击趋势约1.33秒、表格约4秒就绪,导出可用,标题路径仍在上方,无页面错误。本地独立分支已提交,未推送Gitea;验收记录位于 `output/20261113-defect-drilldown/compact-live-performance.json`。
## 2026-11-13:钻取路径移至趋势标题旁
“全部分类 › 当前分类”已移到月/季/年度变化趋势标题旁,筛选项和图例间不再单独占行;产品汇总与黑片/亮片页共用。12项相关前端测试与生产构建通过,已发布2026.11.13.3,功能提交 `79edb3f`。线上确认路径位于标题行,返回功能保持。只更新前端;本地提交,未推送Gitea。截图:`output/20261113-defect-drilldown/live-heading.png`。
## 2026-11-13:不良率钻取显示提速
定位:趋势请求约0.7秒,年度全产品细项约12秒(6715个产品)。原页面通过 Promise.all 等待两者,导致趋势被整张产品细项表拖慢;趋势对象变化也会重复请求未变化的表格。SQL 分析确认细项数据使用与柏拉图相同的预汇总来源,按产品分组产生约9.4万条统计结果;本轮未改计算口径或数据库结构。
实现:拆分趋势和表格的请求范围、取消、加载与错误状态。趋势返回即展示;底表独立更新并保留导出门禁。只切趋势对象时复用仍属于当前范围的表格结果。新增回归覆盖慢表不阻塞趋势、表失败不隐藏趋势和请求不重复。
验证与发布:相关39项前端测试及生产构建通过。2026.11.13.2 已发布,功能提交 `f8f7335`,仅更新前端,后端和数据库不变。线上原生点击加工废曲线后约1.15秒就绪(接口1.07秒),此时底表仍在独立更新;本地同范围约0.8秒。换趋势对象不再重复读取未变化的表格,数据口径和权限检查保留。独立交付分支仅本地提交,未推送 Gitea;验收证据仍在 `output/20261113-defect-drilldown/`。全产品表格的批量分组耗时仍存在,不将趋势提速等同于表格查询已提速。
## 2026-11-13:不良率钻取与查询修复
实现:分类曲线钻取细项趋势,保留分类总废率虚线;表格和 Excel 同步展开。分类切换、返回、换年/换产品及阶段切换遵循 README 中的交互约定。不良率图取消月份点击筛选。细项与正式指标在同一可重复读快照中计算,复用正式分母和来源权限,返回前再次校验权限;支持批量预汇总和原始来源回退。
同时交付:产品月份钻取来源明细上下文、季度/年度/连续月范围及 Excel、未分类对象、换年保留产品层级、五个查询页默认和重置年度;保留已上线的 Excel 格式。
验证:完整构建后端971项(812执行通过、159按条件跳过),前端相关61项通过,类型检查及生产构建通过;专项隔离 MySQL 27项(26通过、1跳过)。全新合成应用中验证实际曲线点击、换年/换产品保持分类、黑片/亮片、无细项类别、表格返回与 Excel 数值百分比;自有数据库正常清理,业务当前版本指针不变。没有执行私有原表全量回归。
发布状态:2026.11.13.1 已上线并通过线上验收,功能提交 `63bf8c1`,前端版本记录与该提交对应。本机后端使用已验收的不可变 JAR,MySQL进程保持,数据库迁移文件与旧运行包完全一致;DSH配置沿用。线上实际点击曲线、表格细项、换年保留产品及分类、产品月份黑片/亮片来源明细均通过,未认证写入被拒绝,无浏览器错误。全产品年度细项首次请求约15秒,单产品及月趋势更快。私有验收材料位于 `output/20261113-defect-drilldown/`,不入库。独立交付分支仅本地提交,未推送 Gitea。
## 前一阶段修复记录(已包含在本次发布)
目标:修正主目录过时说明,补充轻量项目规则与统一进度入口,并按后续授权同步当前文档到 Gitea。 目标:修正主目录过时说明,补充轻量项目规则与统一进度入口,并按后续授权同步当前文档到 Gitea。
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
"""Synthetic, disposable application fixture for authenticated browser acceptance."""
import json
import re
import time
import uuid
from pathlib import Path
from isolated_runtime import isolated_api
from benchmark_scale import sql
ROOT=Path(__file__).resolve().parents[1]
OUT=ROOT/'output/20261113-defect-drilldown'
def literal(value):
if value is None:return 'NULL'
return "CONVERT(0x"+str(value).encode().hex()+" USING utf8mb4)"
def seed(database):
release=str(uuid.uuid4())
sql("INSERT INTO data_release(id,name,status,rule_version,mapping_version,complete) VALUES ("+','.join(map(literal,[release,'合成钻取验收','PUBLISHED','pq-dax-v1','observed-2026-v2']))+",TRUE);",database)
service=(ROOT/'backend/src/main/java/cn/yunsheng/quality/analysis/AnalysisFineDefectsService.java').read_text()
for stage in ['black','bright']:
document,revision,build=[str(uuid.uuid4()) for _ in range(3)]
sql("INSERT INTO source_document(id,source_slot,stage,source_year,source_month,source_file) VALUES("+','.join(map(literal,[document,'fixture-'+stage,stage]))+",2026,1,'synthetic.xlsx');",database)
sql("INSERT INTO source_revision(id,source_document_id,sha256,source_file,archive_path,file_size,parser_version,status) VALUES("+','.join(map(literal,[revision,document,'0'*64,'synthetic.xlsx','synthetic-only',0,'fixture','COMPLETE']))+");",database)
sql("INSERT INTO transform_build(id,source_revision_id,build_key,parser_version,rule_version,mapping_version,status) VALUES("+','.join(map(literal,[build,revision,uuid.uuid4().hex*2,'fixture','pq-dax-v1','observed-2026-v2','COMPLETE']))+");",database)
sql("INSERT INTO release_member(release_id,source_document_id,source_revision_id,transform_build_id) VALUES("+','.join(map(literal,[release,document,revision,build]))+");",database)
fieldblock=service.split('private static final List<DefectField> '+('BLACK_FIELDS' if stage=='black' else 'BRIGHT_FIELDS'))[1].split(');',1)[0]
fields=re.findall(r'field\("([^"]+)"',fieldblock)
row=0
for year,month in [(2025,1),(2026,1),(2026,2),(2026,3)]:
for product in ['合成产品A','合成产品B']:
row+=1;total=1000 if product.endswith('A') else 2000
child=10*month;parent=100*month
raw={key:0 for key in fields}
if stage=='bright':raw.update({'加工缺角重量':child,'加工磕边重量':parent-child})
else:raw.update({'责废缺角重量':child,'责废尺寸重量':parent-child})
codes=['black_responsibility_scrap','black_material_scrap','black_external_scrap'] if stage=='black' else ['bright_processing_scrap','bright_plating_scrap','bright_material_scrap','bright_diffusion_scrap','bright_headquarters_scrap']
components={key:0 for key in codes};components[codes[0]]=parent
values=[build,revision,'合成数据',row,f'{year}-{month:02d}','合成部门','合成业务组',product,total-parent,parent,total,json.dumps(raw,ensure_ascii=False),json.dumps(components)]
sql('INSERT INTO fact_'+stage+'(build_id,source_revision_id,sheet_name,physical_row_no,month_key,department,group_name,product,qualified_weight,scrap_weight,total_weight,raw_values,components) VALUES('+','.join(map(literal,values))+');',database)
sql("UPDATE current_release SET release_id="+literal(release)+",pointer_version=pointer_version+1 WHERE dataset_id='quality';",database)
return release
def main():
OUT.mkdir(parents=True,exist_ok=True)
stop=OUT/'stop-fixture'
if stop.exists():stop.unlink()
with isolated_api(port=5287,fresh=True,evidence_dir=OUT/'isolated') as (api,database,evidence):
release=seed(database)
context={'releaseId':release,'year':2026,'stage':'bright','category':'加工废','dimension':'period','trendGranularity':'month'}
status,data,_=api.call('/api/analysis/defects/drilldown',context)
assert status==200,(status,data)
assert len(data['fields'])==9,data
january=next(g for g in data['groups'] if g['key']=='2026-01')
assert abs(january['totalWeight']-3000)<1e-8
assert abs(next(c['rate'] for c in january['children'] if c['key']=='加工缺角重量')-20/3000)<1e-10
(OUT/'fixture-ready.json').write_text(json.dumps({'ready':True,'port':5287,'synthetic':True,'apiVerified':True,'releaseId':release},ensure_ascii=False,indent=2))
print('SYNTHETIC_FIXTURE_READY',flush=True)
deadline=time.monotonic()+1800
while not stop.exists() and time.monotonic()<deadline:time.sleep(1)
if not stop.exists():raise RuntimeError('Fixture acceptance timed out')
print('SYNTHETIC_FIXTURE_CLEANED',flush=True)
if __name__=='__main__':main()
+77 -30
View File
@@ -33,6 +33,7 @@ import { ArrowLeftOutlined, DownloadOutlined, InfoCircleOutlined, ReloadOutlined
import { ApiError, api, decodeQueryValueList, encodeQueryValueList } from './api'; import { ApiError, api, decodeQueryValueList, encodeQueryValueList } from './api';
import type { import type {
DefectMetric, DefectMetric,
DefectDrilldownResponse,
FineDefectTopResponse, FineDefectTopResponse,
MetricRow, MetricRow,
MetricsResponse, MetricsResponse,
@@ -59,6 +60,8 @@ import './query-motion.css';
import { ExtraQueryFilters } from './ExtraQueryFilters'; import { ExtraQueryFilters } from './ExtraQueryFilters';
import { freezeSourceRecordsQuery } from './sourceRecordsContext'; import { freezeSourceRecordsQuery } from './sourceRecordsContext';
import { downloadMetricWorkbook } from './metricWorkbook'; import { downloadMetricWorkbook } from './metricWorkbook';
import { useDefectDrilldown } from './useDefectDrilldown';
import { defectCell, defectGroup, defectRateLabel as fineDefectRateLabel } from './defectDrilldown';
echarts.use([BarChart, LineChart, GraphicComponent, GridComponent, LegendComponent, MarkLineComponent, TooltipComponent, SVGRenderer]); echarts.use([BarChart, LineChart, GraphicComponent, GridComponent, LegendComponent, MarkLineComponent, TooltipComponent, SVGRenderer]);
@@ -152,7 +155,7 @@ function monthNumber(value: number | string): number | null {
if (typeof value === 'number' && value >= 1 && value <= 12) return value; if (typeof value === 'number' && value >= 1 && value <= 12) return value;
const raw = String(value).trim(); const raw = String(value).trim();
const yearMonth = raw.match(/^\d{4}[-/]\s*(\d{1,2})$/); const yearMonth = raw.match(/^\d{4}[-/]\s*(\d{1,2})$/);
const match = yearMonth || raw.match(/\d{1,2}/); const match = yearMonth || raw.match(/^(\d{1,2})(?:月)?$/);
if (!match) return null; if (!match) return null;
const result = Number(match[1] ?? match[0]); const result = Number(match[1] ?? match[0]);
return result >= 1 && result <= 12 ? result : null; return result >= 1 && result <= 12 ? result : null;
@@ -298,7 +301,7 @@ export default function QueryPage({ page, onUnauthorized }: {
const automaticPeriod = useRef(page !== 'overall' && !initialQuery.year && !initialQuery.month && !initialQuery.quarter); const automaticPeriod = useRef(page !== 'overall' && !initialQuery.year && !initialQuery.month && !initialQuery.quarter);
const defaultMonth = previousCompleteMonth(); const defaultMonth = previousCompleteMonth();
const defaultQuarter = previousCompleteQuarter(); const defaultQuarter = previousCompleteQuarter();
const [period, setPeriod] = useState<PeriodKind>(initialQuery.month ? 'month' : initialQuery.quarter ? 'quarter' : initialQuery.year || page === 'overall' ? 'year' : 'month'); const [period, setPeriod] = useState<PeriodKind>(initialQuery.month ? 'month' : initialQuery.quarter ? 'quarter' : 'year');
const [year, setYear] = useState(initialQuery.year || (period === 'month' ? defaultMonth.year : period === 'quarter' ? defaultQuarter.year : new Date().getFullYear())); const [year, setYear] = useState(initialQuery.year || (period === 'month' ? defaultMonth.year : period === 'quarter' ? defaultQuarter.year : new Date().getFullYear()));
const [month, setMonth] = useState(initialQuery.month || defaultMonth.month); const [month, setMonth] = useState(initialQuery.month || defaultMonth.month);
const [quarter, setQuarter] = useState(initialQuery.quarter || defaultQuarter.quarter); const [quarter, setQuarter] = useState(initialQuery.quarter || defaultQuarter.quarter);
@@ -364,7 +367,7 @@ export default function QueryPage({ page, onUnauthorized }: {
setOptions(result); setOptions(result);
if (automaticPeriod.current) { if (automaticPeriod.current) {
const available = defaultQueryMonth(result); const available = defaultQueryMonth(result);
setYear(available.year); setMonth(available.month); setMonth(available.month);
automaticPeriod.current = false; automaticPeriod.current = false;
} }
setOptionsError(''); setOptionsError('');
@@ -405,7 +408,7 @@ export default function QueryPage({ page, onUnauthorized }: {
}, [exactProductSearch, product]); }, [exactProductSearch, product]);
const queryParams = useMemo(() => ({ ...toApiParams({ year, period, month, quarter, department, group, product: productFilterForQuery, productSearch: view === 'product' ? appliedSearch : undefined, projectGroup, extraFilters, view, includeUnassigned, trendGranularity }), releaseId: initialQuery.releaseId || currentReleaseId || undefined }), [year, period, month, quarter, department, group, productFilterForQuery, appliedSearch, projectGroup, extraFilters, view, includeUnassigned, trendGranularity, initialQuery.releaseId, currentReleaseId]); const queryParams = useMemo(() => ({ ...toApiParams({ year, period, month, quarter, department, group, product: productFilterForQuery, productSearch: view === 'product' ? appliedSearch : undefined, projectGroup, extraFilters, view, includeUnassigned, trendGranularity }), releaseId: initialQuery.releaseId || currentReleaseId || undefined }), [year, period, month, quarter, department, group, productFilterForQuery, appliedSearch, projectGroup, extraFilters, view, includeUnassigned, trendGranularity, initialQuery.releaseId, currentReleaseId]);
const queryKey = useMemo(() => JSON.stringify(queryParams), [queryParams]); const queryKey = useMemo(() => JSON.stringify(queryParams), [queryParams]);
const drilldownContextKey = JSON.stringify({ view, year, department, group, product, projectGroup, extraFilters, includeUnassigned }); const drilldownContextKey = JSON.stringify({ view, department, group, product, projectGroup, extraFilters, includeUnassigned });
useEffect(() => { useEffect(() => {
setDrilledProduct(''); setDrilledProduct('');
}, [drilldownContextKey]); }, [drilldownContextKey]);
@@ -576,7 +579,7 @@ export default function QueryPage({ page, onUnauthorized }: {
view: drilledProduct ? 'overall' as const : view, view: drilledProduct ? 'overall' as const : view,
layoutView: drilledProduct ? 'product' as const : view, layoutView: drilledProduct ? 'product' as const : view,
period: drilledProduct ? 'year' as const : period, period: drilledProduct ? 'year' as const : period,
year, month, quarter, drilledProduct, paginationScope: JSON.stringify({query:queryParams,drilledProduct,view}), year, month, quarter, drilledProduct, granularity: trendGranularity, paginationScope: JSON.stringify({query:queryParams,drilledProduct,view}),
} : null); } : null);
const tableRows = retainedTable?.rows || []; const tableRows = retainedTable?.rows || [];
const tablePeriod = retainedTable?.period || period; const tablePeriod = retainedTable?.period || period;
@@ -584,6 +587,14 @@ export default function QueryPage({ page, onUnauthorized }: {
const tableMonth = retainedTable?.month || month; const tableMonth = retainedTable?.month || month;
const tableQuarter = retainedTable?.quarter || quarter; const tableQuarter = retainedTable?.quarter || quarter;
const tableProduct = retainedTable?.drilledProduct || ''; const tableProduct = retainedTable?.drilledProduct || '';
const tableProductLabel = tableProduct === '__UNASSIGNED__' ? '未分类' : tableProduct;
const defectDrill = useDefectDrilldown({active,mode:trendMode,stage:trendStage,
query:{...defectQueryParams,releaseId:summaryMetricsDataRelease()},trendQuery:trendParams,
tableQuery:drilledProduct?trendParams:queryParams,
tableDimension:tableView==='overall'&&tablePeriod==='year'?'period':tableView,
tableGranularity:drilledProduct?trendGranularity:'month',refreshKey:JSON.stringify([refreshKey,queryRevision]),onUnauthorized,display:defectDisplay,onDisplay:setDefectDisplay});
function summaryMetricsDataRelease() { return (drilledProduct?trendObjectMetrics?.data:currentMetricsData)?.release?.id || currentMetricsData?.release?.id || queryParams.releaseId; }
const captureTrendAnchor = useCallback(() => { const captureTrendAnchor = useCallback(() => {
trendAnchorTop.current = trendSection.current?.getBoundingClientRect().top ?? null; trendAnchorTop.current = trendSection.current?.getBoundingClientRect().top ?? null;
@@ -649,7 +660,8 @@ export default function QueryPage({ page, onUnauthorized }: {
const openRecords = useCallback((row: MetricRow | null, stage: Stage, selectedMonth?: number, sourceQuery?: QueryParams, sourceData?: MetricsResponse | null) => { const openRecords = useCallback((row: MetricRow | null, stage: Stage, selectedMonth?: number, sourceQuery?: QueryParams, sourceData?: MetricsResponse | null) => {
const source = sourceData === undefined ? summaryMetricsData : sourceData; const source = sourceData === undefined ? summaryMetricsData : sourceData;
if (!source?.release?.id) { message.info('指标数据尚未就绪,请重新查询后打开来源明细。'); return; } if (!source?.release?.id) { message.info('指标数据尚未就绪,请重新查询后打开来源明细。'); return; }
setRecordQuerySnapshot(freezeSourceRecordsQuery(sourceQuery || (drilledProduct ? defectQueryParams : queryParams), source.release.id, view, row, selectedMonth)); // Product drilldown rows represent months; the product is already bound in the query.
setRecordQuerySnapshot(freezeSourceRecordsQuery(sourceQuery || (drilledProduct ? defectQueryParams : queryParams), source.release.id, drilledProduct ? 'overall' : view, row, selectedMonth));
setSelectedRecord(row); setRecordStage(stage); setRecordMonth(selectedMonth); setRecordsOpen(true); setSelectedRecord(row); setRecordStage(stage); setRecordMonth(selectedMonth); setRecordsOpen(true);
}, [summaryMetricsData, drilledProduct, defectQueryParams, queryParams, view, message]); }, [summaryMetricsData, drilledProduct, defectQueryParams, queryParams, view, message]);
const summaryMetricsError = drilledProduct ? trendObjectMetricsError : currentMetricsError; const summaryMetricsError = drilledProduct ? trendObjectMetricsError : currentMetricsError;
@@ -678,7 +690,7 @@ export default function QueryPage({ page, onUnauthorized }: {
const trendOptions = useMemo(() => { const trendOptions = useMemo(() => {
const base = view === 'department' ? options?.departments?.filter(name => !RETIRED_DEPARTMENTS.has(name)) : view === 'group' ? allowedGroups : view === 'product' const base = view === 'department' ? options?.departments?.filter(name => !RETIRED_DEPARTMENTS.has(name)) : view === 'group' ? allowedGroups : view === 'product'
? department.length || group.length ? organizationPathOptions.products : options?.products : []; ? department.length || group.length ? organizationPathOptions.products : options?.products : [];
return (base || []).map(value => ({ label: value, value })); return [...(base || []).map(value => ({ label: value, value })), ...(view === 'product' ? [{ label: '未分类', value: '__UNASSIGNED__' }] : [])];
}, [view, options, allowedGroups, department, group, organizationPathOptions]); }, [view, options, allowedGroups, department, group, organizationPathOptions]);
useEffect(() => { useEffect(() => {
if (options && trendObject && !trendOptions.some(item => item.value === trendObject)) { if (options && trendObject && !trendOptions.some(item => item.value === trendObject)) {
@@ -738,7 +750,7 @@ export default function QueryPage({ page, onUnauthorized }: {
<ExtraQueryFilters key={queryRevision} releaseId={queryParams.releaseId} projectGroup={projectGroup} projectGroups={options?.projectGroupStatus === 'READY' ? options.projectGroups : undefined} onProjectGroup={setProjectGroup} value={extraFilters} onChange={setExtraFilters} onUnauthorized={onUnauthorized} /> <ExtraQueryFilters key={queryRevision} releaseId={queryParams.releaseId} projectGroup={projectGroup} projectGroups={options?.projectGroupStatus === 'READY' ? options.projectGroups : undefined} onProjectGroup={setProjectGroup} value={extraFilters} onChange={setExtraFilters} onUnauthorized={onUnauthorized} />
<div className="query-toolbar-tail"><span className={`query-status ${queryBusy || summaryBusy ? 'busy' : currentMetricsError || summaryMetricsError ? 'failed' : ''}`}><i />{queryBusy || summaryBusy ? '正在查询' : currentMetricsError || summaryMetricsError ? '查询失败' : '查询完成'}</span> <div className="query-toolbar-tail"><span className={`query-status ${queryBusy || summaryBusy ? 'busy' : currentMetricsError || summaryMetricsError ? 'failed' : ''}`}><i />{queryBusy || summaryBusy ? '正在查询' : currentMetricsError || summaryMetricsError ? '查询失败' : '查询完成'}</span>
<QuerySnapshotButton title={drilledProduct ? `${drilledProduct} · 产品质量查询` : page === 'overall' ? '总体合格率' : `${scopeName}质量查询`} queryKey={JSON.stringify([queryParams,trendMode,trendObject,trendStage,trendGranularity,rateBasis,fullFields,defectDisplay,queryRevision])} disabled={queryBusy || summaryBusy || !summaryMetricsData} onUnauthorized={onUnauthorized} /> <QuerySnapshotButton title={drilledProduct ? `${drilledProduct} · 产品质量查询` : page === 'overall' ? '总体合格率' : `${scopeName}质量查询`} queryKey={JSON.stringify([queryParams,trendMode,trendObject,trendStage,trendGranularity,rateBasis,fullFields,defectDisplay,queryRevision])} disabled={queryBusy || summaryBusy || !summaryMetricsData} onUnauthorized={onUnauthorized} />
<Button type="default" onClick={() => { automaticPeriod.current = false; const previous = defaultQueryMonth(options); const previousQuarter = previousCompleteQuarter(); setViewOverride(); setDrilledProduct(''); setDepartment([]); setGroup([]); setProduct([]); setProjectGroup([]); setExtraFilters({}); setSearch(''); setAppliedSearch(''); setTrendObject(''); setYear(page === 'overall' ? new Date().getFullYear() : previous.year); setMonth(previous.month); setQuarter(previousQuarter.quarter); setPeriod(page === 'overall' ? 'year' : 'month'); setTrendGranularity('month'); }}>重置</Button> <Button type="default" onClick={() => { automaticPeriod.current = false; const previous = defaultQueryMonth(options); const previousQuarter = previousCompleteQuarter(); setViewOverride(); setDrilledProduct(''); setDepartment([]); setGroup([]); setProduct([]); setProjectGroup([]); setExtraFilters({}); setSearch(''); setAppliedSearch(''); setTrendObject(''); setYear(new Date().getFullYear()); setMonth(previous.month); setQuarter(previousQuarter.quarter); setPeriod('year'); setTrendGranularity('month'); }}>重置</Button>
</div> </div>
{organizationSummary.length > 0 && <div className="organization-breadcrumb" aria-label="当前组织路径"> {organizationSummary.length > 0 && <div className="organization-breadcrumb" aria-label="当前组织路径">
<span>组织路径</span><Button type="link" onClick={() => { setDepartment([]); setGroup([]); setProduct([]); }}>全部组织</Button> <span>组织路径</span><Button type="link" onClick={() => { setDepartment([]); setGroup([]); setProduct([]); }}>全部组织</Button>
@@ -762,38 +774,40 @@ export default function QueryPage({ page, onUnauthorized }: {
<> <>
<div className="analysis-stack"> <div className="analysis-stack">
<QueryTrendPanel panelRef={trendSection} id={active ? 'monthly-trend' : undefined} <QueryTrendPanel headingControls={defectDrill.controls} panelRef={trendSection} id={active ? 'monthly-trend' : undefined}
mode={trendMode} onMode={setTrendMode} granularity={trendGranularity} onGranularity={setTrendGranularity} mode={trendMode} onMode={setTrendMode} granularity={trendGranularity} onGranularity={setTrendGranularity}
defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay} fullFields={trendFullFields} onFullFields={setTrendFullFields} defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay} fullFields={trendFullFields} onFullFields={setTrendFullFields}
object={trendObject} onObject={setSynchronizedTrendObject} object={trendObject} onObject={setSynchronizedTrendObject}
objects={trendOptions} scopeName={scopeName} objects={trendOptions} scopeName={scopeName}
stageSwitch={<Segmented value={trendStage} options={[{ label: '亮片', value: 'bright' }, { label: '黑片', value: 'black' }]} onChange={value => setTrendStage(value as Stage)} aria-label="趋势阶段" />}> stageSwitch={<Segmented value={trendStage} options={[{ label: '亮片', value: 'bright' }, { label: '黑片', value: 'black' }]} onChange={value => setTrendStage(value as Stage)} aria-label="趋势阶段" />}>
{currentTrendError && <Alert type="warning" showIcon message={retainedTrend ? "趋势更新失败,当前显示上次结果" : "趋势读取失败"} description={retainedTrend ? `${currentTrendError};当前范围:${loadedQueryScope(retainedTrend.query, decodeQueryValueList)}` : currentTrendError} action={<Button onClick={() => setTrendRevision(value => value + 1)}>重试</Button>} />} {currentTrendError && <Alert type="warning" showIcon message={retainedTrend ? "趋势更新失败,当前显示上次结果" : "趋势读取失败"} description={retainedTrend ? `${currentTrendError};当前范围:${loadedQueryScope(retainedTrend.query, decodeQueryValueList)}` : currentTrendError} action={<Button onClick={() => setTrendRevision(value => value + 1)}>重试</Button>} />}
<div className="trend-chart-shell" aria-busy={trendRefreshing} inert={trendRefreshing || trendDataKey !== trendKey}> <div className="trend-chart-shell" aria-busy={trendRefreshing} inert={trendRefreshing || trendDataKey !== trendKey}>
{trendMode === 'data' {trendMode === 'data'
? <TrendTable rows={renderedTrendData} defectDisplay={defectDisplay} rateBasis={rateBasis} fullFields={trendFullFields} onMonth={selectTrendPeriod} /> ? <TrendTable rows={renderedTrendData} defectDisplay={defectDisplay} rateBasis={rateBasis} fullFields={trendFullFields} onMonth={selectTrendPeriod} />
: <TrendChart rows={renderedTrendData} mode={trendMode} stage={trendStage} rateBasis={rateBasis} targetRate={renderedTrendTarget} selectedPeriod={selectedTrendPeriod} onMonth={selectTrendPeriod} />} : <TrendChart defectDrilldown={trendDataKey===trendKey&&!trendLoading&&!currentTrendError?defectDrill.trend:undefined} drillableCategories={defectDrill.categories} onDefect={defectDrill.choose} rows={renderedTrendData} mode={trendMode} stage={trendStage} rateBasis={rateBasis} targetRate={renderedTrendTarget} selectedPeriod={selectedTrendPeriod} onMonth={selectTrendPeriod} />}
{showTrendLoading && <span className="trend-refresh-indicator"><Spin size="small" />{retainedTrend ? `正在更新 · 当前显示上次筛选的${loadedQueryScope(retainedTrend.query, decodeQueryValueList)}` : '正在读取趋势'}</span>} {showTrendLoading && <span className="trend-refresh-indicator"><Spin size="small" />{retainedTrend ? `正在更新 · 当前显示上次筛选的${loadedQueryScope(retainedTrend.query, decodeQueryValueList)}` : '正在读取趋势'}</span>}
</div> </div>
</QueryTrendPanel> </QueryTrendPanel>
<QueryTablePanel <QueryTablePanel
heading={<SectionHeading title={tableProduct ? '产品月度质量数据' : page === 'overall' ? `${tablePeriod === 'year' ? '年度月度' : tablePeriod === 'quarter' ? '季度' : '月度'}汇总` : `${scopeName}质量汇总`} subtitle={tableProduct ? `${retainedTable?.year || year} 年 · ${tableProduct} · 重量 / kg 合格率 / %` : '重量 / kg 合格率 / %'} />} busy={queryBusy || !tableReady} heading={<SectionHeading title={tableProduct ? `产品${retainedTable?.granularity === 'quarter' ? '季度' : retainedTable?.granularity === 'year' ? '年度' : '月度'}质量数据` : page === 'overall' ? `${tablePeriod === 'year' ? '年度月度' : tablePeriod === 'quarter' ? '季度' : '月度'}汇总` : `${scopeName}质量汇总`} subtitle={tableProduct ? `${retainedTable?.year || year} 年 · ${tableProductLabel} · 重量 / kg 合格率 / %` : '重量 / kg 合格率 / %'} />} busy={queryBusy || !tableReady || defectDrill.tablePending}
search={drilledProduct || search} onSearchChange={setSearch} onSearch={() => applySearch()} productView={view === 'product'} searchPlaceholder={view === 'group' ? '搜索业务分组' : undefined} searchDisabled={Boolean(drilledProduct)} search={drilledProduct ? tableProductLabel : search} onSearchChange={setSearch} onSearch={() => applySearch()} productView={view === 'product'} searchPlaceholder={view === 'group' ? '搜索业务分组' : undefined} searchDisabled={Boolean(drilledProduct)}
fullFields={fullFields} onFullFields={setFullFields} defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay} fullFields={fullFields} onFullFields={setFullFields} defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay}
returnControls={<>{!drilledProduct && (view !== baseView || view === 'group' || view === 'product' && (group.length > 0 || department.length > 0)) && <Button className="query-return-button" icon={<ArrowLeftOutlined />} onClick={() => { setSearch(''); setAppliedSearch(''); setTrendObject(''); if (view === 'product') { setProduct([]); setGroup([]); setViewOverride('group'); } else if (view === 'group') { setGroup([]); setDepartment([]); setViewOverride('department'); } else { setViewOverride(); } }}>返回上一级</Button>} returnControls={<>{!drilledProduct && (view !== baseView || view === 'group' || view === 'product' && (group.length > 0 || department.length > 0)) && <Button className="query-return-button" icon={<ArrowLeftOutlined />} onClick={() => { setSearch(''); setAppliedSearch(''); setTrendObject(''); if (view === 'product') { setProduct([]); setGroup([]); setViewOverride('group'); } else if (view === 'group') { setGroup([]); setDepartment([]); setViewOverride('department'); } else { setViewOverride(); } }}>返回上一级</Button>}
{drilledProduct && <Button className="query-return-button" icon={<ArrowLeftOutlined />} onClick={() => { setDrilledProduct(''); setTrendObject(''); setTrendMode('production'); }}>返回上一级</Button>}</>} {drilledProduct && <Button className="query-return-button" icon={<ArrowLeftOutlined />} onClick={() => { setDrilledProduct(''); setTrendObject(''); if(trendMode!=='defects')setTrendMode('production'); }}>返回上一级</Button>}</>}
exporting={summaryExporting} exportDisabled={queryBusy || summaryBusy || !tableReady || !summaryMetricsData || Boolean(drilledProduct && (trendLoading || currentTrendError)) || !(drilledProduct ? drilledTrendData : visibleRows).length} exporting={summaryExporting} exportDisabled={!!defectDrill.category && (!defectDrill.table || defectDrill.pending || !!defectDrill.error) || queryBusy || summaryBusy || !tableReady || !summaryMetricsData || Boolean(drilledProduct && (trendLoading || currentTrendError)) || !(drilledProduct ? drilledTrendData : visibleRows).length}
onExport={async () => { onExport={async () => {
setSummaryExporting(true); setSummaryExporting(true);
try { await summaryExport.current?.exportExcel(); } try { await summaryExport.current?.exportExcel(); }
catch (error) { message.error(`导出失败:${chooseError(error)}`); } catch (error) { message.error(`导出失败:${chooseError(error)}`); }
finally { setSummaryExporting(false); } finally { setSummaryExporting(false); }
}}> }}>
<SmoothTableFrame busy={queryBusy || !tableReady} blocked={queryBusy || summaryBusy || !tableReady}> {defectDrill.tableControls}
<SmoothTableFrame busy={queryBusy || !tableReady || defectDrill.tablePending} blocked={queryBusy || summaryBusy || !tableReady || defectDrill.tablePending}>
<MetricTable <MetricTable
ref={summaryExport} ref={summaryExport}
defectDrilldown={tableReady&&!queryBusy?defectDrill.table:undefined}
paginationScope={retainedTable?.paginationScope} paginationScope={retainedTable?.paginationScope}
exportFilename={`${drilledProduct || scopeName}质量汇总-${year}${drilledProduct || period === 'year' ? '' : period === 'month' ? `-${String(month).padStart(2, '0')}` : `-Q${quarter}`}-${fullFields ? '完整字段' : '关键指标'}`} exportFilename={`${drilledProduct || scopeName}质量汇总-${year}${drilledProduct || period === 'year' ? '' : period === 'month' ? `-${String(month).padStart(2, '0')}` : `-Q${quarter}`}-${fullFields ? '完整字段' : '关键指标'}`}
rows={tableRows} rows={tableRows}
@@ -815,12 +829,12 @@ export default function QueryPage({ page, onUnauthorized }: {
else if (view === 'department' && row.department) { setSearch(''); setAppliedSearch(''); setTrendObject(''); setGroup([]); setProduct([]); setDepartment([row.department]); setViewOverride('group'); } else if (view === 'department' && row.department) { setSearch(''); setAppliedSearch(''); setTrendObject(''); setGroup([]); setProduct([]); setDepartment([row.department]); setViewOverride('group'); }
else if (view === 'group' && row.group) { setSearch(''); setAppliedSearch(''); setTrendObject(''); setProduct([]); setGroup([row.group]); setViewOverride('product'); } else if (view === 'group' && row.group) { setSearch(''); setAppliedSearch(''); setTrendObject(''); setProduct([]); setGroup([row.group]); setViewOverride('product'); }
else if (view === 'product') { else if (view === 'product') {
const selectedProduct = row.product || row.label; const selectedProduct = row.key === '__UNASSIGNED__' || !row.product && !row.key?.trim() ? '__UNASSIGNED__' : row.product || row.label;
if (!selectedProduct) return; if (!selectedProduct) return;
setDrilledProduct(selectedProduct); setDrilledProduct(selectedProduct);
setTrendObject(selectedProduct); setTrendObject(selectedProduct);
setTrendGranularity('month'); setTrendGranularity('month');
setTrendMode('production'); if(trendMode!=='defects')setTrendMode('production');
} }
}} }}
/> />
@@ -1081,9 +1095,9 @@ const totalWeightTitle = (label: string) => <span className="query-total-weight-
const rateBasisTitle = (label: string, basis: QueryRateBasis, kind: 'bright' | 'composite', onBasis?: () => void) => <span className="query-total-weight-title query-rate-rule-title"><span className="query-rate-label">{onBasis ? <button className="header-link" onClick={onBasis}>{label}</button> : label}{basis !== 'original' && <small className="query-rate-subtitle">(去本部报废)</small>}</span>{basis !== 'original' && <Tooltip trigger={['hover', 'focus']} title={<div>{kind === 'composite' && <>综合合格率=黑片合格率×亮片合格率。<br /></>}原口径:亮片合格重量÷亮片总重量。<br />去本部报废:(亮片合格重量+本部报废)÷亮片总重量,同时从亮片报废重量中扣除本部报废,总重量不变。<br />当前:去本部报废。计算结果以已发布数据版本及生效规则为准。</div>}><InfoCircleOutlined tabIndex={0} role="img" aria-label={`${kind === 'composite' ? '综合合格率' : '亮片合格率'}计算说明`} /></Tooltip>}</span>; const rateBasisTitle = (label: string, basis: QueryRateBasis, kind: 'bright' | 'composite', onBasis?: () => void) => <span className="query-total-weight-title query-rate-rule-title"><span className="query-rate-label">{onBasis ? <button className="header-link" onClick={onBasis}>{label}</button> : label}{basis !== 'original' && <small className="query-rate-subtitle">(去本部报废)</small>}</span>{basis !== 'original' && <Tooltip trigger={['hover', 'focus']} title={<div>{kind === 'composite' && <>综合合格率=黑片合格率×亮片合格率。<br /></>}原口径:亮片合格重量÷亮片总重量。<br />去本部报废:(亮片合格重量+本部报废)÷亮片总重量,同时从亮片报废重量中扣除本部报废,总重量不变。<br />当前:去本部报废。计算结果以已发布数据版本及生效规则为准。</div>}><InfoCircleOutlined tabIndex={0} role="img" aria-label={`${kind === 'composite' ? '综合合格率' : '亮片合格率'}计算说明`} /></Tooltip>}</span>;
export function MetricTable({ export function MetricTable({
ref, exportFilename, rows, view, layoutView = view, pageSizeOverride, paginationScope, period, month, quarter, fullFields, rateBasis, onDetail, onBasis, onDrilldown, onlyStage, defectDisplay = 'weight', emptyText = '当前条件下暂无数据', ref, exportFilename, rows, view, defectDrilldown, layoutView = view, pageSizeOverride, paginationScope, period, month, quarter, fullFields, rateBasis, onDetail, onBasis, onDrilldown, onlyStage, defectDisplay = 'weight', emptyText = '当前条件下暂无数据',
}: { }: {
ref?: React.Ref<{ exportExcel: () => Promise<void> }>; exportFilename: string; ref?: React.Ref<{ exportExcel: () => Promise<void> }>; exportFilename: string; defectDrilldown?: DefectDrilldownResponse;
rows: MetricRow[]; view: ViewKind; layoutView?: ViewKind; pageSizeOverride?: number; paginationScope?: string; period: PeriodKind; month: number; quarter: number; fullFields: boolean; rows: MetricRow[]; view: ViewKind; layoutView?: ViewKind; pageSizeOverride?: number; paginationScope?: string; period: PeriodKind; month: number; quarter: number; fullFields: boolean;
rateBasis: 'original' | 'withoutHeadquartersScrap'; rateBasis: 'original' | 'withoutHeadquartersScrap';
onDetail: (row: MetricRow | null, stage: Stage, month?: number) => void; onBasis: () => void; onDrilldown: (row: MetricRow) => void; onDetail: (row: MetricRow | null, stage: Stage, month?: number) => void; onBasis: () => void; onDrilldown: (row: MetricRow) => void;
@@ -1091,7 +1105,9 @@ export function MetricTable({
}) { }) {
const compact = useCompactTable(); const compact = useCompactTable();
const isOverall = view === 'overall'; const isOverall = view === 'overall';
const title = (row: MetricRow) => isOverall && period === 'year' const title = (row: MetricRow) => isOverall && period === 'year' && ['quarter', 'year'].includes((row as TrendMetricRow).granularity || '')
? (row as TrendMetricRow).periodLabel || row.label
: isOverall && period === 'year'
? (monthNumber((row as TrendMetricRow).month) ? MONTHS[monthNumber((row as TrendMetricRow).month)! - 1] : row.label) ? (monthNumber((row as TrendMetricRow).month) ? MONTHS[monthNumber((row as TrendMetricRow).month)! - 1] : row.label)
: isOverall && period === 'month' ? MONTHS[month - 1] : isOverall && period === 'month' ? MONTHS[month - 1]
: isOverall && period === 'quarter' ? `第${quarter}季度` : objectLabel(row, view); : isOverall && period === 'quarter' ? `第${quarter}季度` : objectLabel(row, view);
@@ -1123,15 +1139,25 @@ export function MetricTable({
totalWeight: '总重量', qualifiedWeight: '合格重量', scrapWeight: '报废重量', totalWeight: '总重量', qualifiedWeight: '合格重量', scrapWeight: '报废重量',
headquartersScrapWeight: '本部报废', rate: '合格率', headquartersScrapWeight: '本部报废', rate: '合格率',
}; };
const stageColumns = (stage: Stage): ColumnsType<MetricRow> => metricStageColumnPlan(rows, stage, fullFields, rateBasis).map(column => { const stageColumns = (stage: Stage): ColumnsType<MetricRow> => {
const base: ColumnsType<MetricRow> = metricStageColumnPlan(rows, stage, defectDrilldown ? false : fullFields, rateBasis).map(column => {
if (column.kind === 'defect') return defectColumns(stage, [column.name])[0]; if (column.kind === 'defect') return defectColumns(stage, [column.name])[0];
const metricColumn = numeric(fieldLabels[column.field], stage, column.field); const metricColumn = numeric(fieldLabels[column.field], stage, column.field);
return stage === 'bright' && column.field === 'rate' return stage === 'bright' && column.field === 'rate'
? { ...metricColumn, render: (_: unknown, row: MetricRow) => <Button type="link" className="table-value" tabIndex={-1} style={{ userSelect: 'text', WebkitUserSelect: 'text' }}><AnimatedMetricText value={rateText(visibleStageRate(row, 'bright', rateBasis))} /></Button> } ? { ...metricColumn, render: (_: unknown, row: MetricRow) => <Button type="link" className="table-value" tabIndex={-1} style={{ userSelect: 'text', WebkitUserSelect: 'text' }}><AnimatedMetricText value={rateText(visibleStageRate(row, 'bright', rateBasis))} /></Button> }
: metricColumn; : metricColumn;
}); });
if(defectDrilldown?.stage===stage) {
const data=defectDrilldown;
const fields=[{key:'parent',label:data.category},...data.fields];
const columns=fields.map(field=>({key:`${stage}-fine-${field.key}`,title:defectDisplay==='rate'?fineDefectRateLabel(field.label):field.label.replace(/重量$/,''),exportRate:defectDisplay==='rate',align:'center' as const,width:140,
render:(_:unknown,row:MetricRow)=>{const value=defectCell(data,row,field.key,defectDisplay);return defectDisplay==='rate'?rateText(value):numberText(value);}}));
base.splice(base.length-1,0,...columns);
}
return base;
};
const allColumns: ColumnsType<MetricRow> = [ const allColumns: ColumnsType<MetricRow> = [
{ title: isOverall ? period === 'year' ? '月份' : '统计期间' : view === 'group' ? '产业部 / 业务分组' : view === 'product' ? '产品规格' : view === 'projectGroup' ? '项目组' : view === 'department' ? '产业部' : '对象', key: 'object', dataIndex: 'label', fixed: 'left', width: compact ? isOverall ? 100 : 180 : isOverall ? 120 : layoutView === 'product' ? (fullFields ? 240 : 300) : layoutView === 'group' ? (fullFields ? 220 : 260) : (fullFields ? 180 : 220), render: (_: unknown, row) => <span className="object-cell">{view === 'department' || view === 'group' || view === 'product' ? <Button type="link" className="object-drilldown" style={{ userSelect: 'text', WebkitUserSelect: 'text' }} onClick={() => { if (window.getSelection()?.isCollapsed !== false) onDrilldown(row); }}>{title(row)} <span>›</span></Button> : <strong>{title(row)}</strong>}{view === 'group' && row.department && <small>{row.department}</small>}</span> }, { title: isOverall ? period === 'year' && !rows.some(row => ['quarter', 'year'].includes((row as TrendMetricRow).granularity || '')) ? '月份' : '统计期间' : view === 'group' ? '产业部 / 业务分组' : view === 'product' ? '产品规格' : view === 'projectGroup' ? '项目组' : view === 'department' ? '产业部' : '对象', key: 'object', dataIndex: 'label', fixed: 'left', width: compact ? isOverall ? 100 : 180 : isOverall ? 120 : layoutView === 'product' ? (fullFields ? 240 : 300) : layoutView === 'group' ? (fullFields ? 220 : 260) : (fullFields ? 180 : 220), render: (_: unknown, row) => <span className="object-cell">{view === 'department' || view === 'group' || view === 'product' ? <Button type="link" className="object-drilldown" style={{ userSelect: 'text', WebkitUserSelect: 'text' }} onClick={() => { if (window.getSelection()?.isCollapsed !== false) onDrilldown(row); }}>{title(row)} <span>›</span></Button> : <strong>{title(row)}</strong>}{view === 'group' && row.department && <small>{row.department}</small>}</span> },
{ key: 'stage-black', title: <span className="stage-heading black"><i />黑片</span>, children: stageColumns('black') }, { key: 'stage-black', title: <span className="stage-heading black"><i />黑片</span>, children: stageColumns('black') },
{ key: 'stage-bright', title: <span className="stage-heading bright"><i />亮片</span>, children: stageColumns('bright') }, { key: 'stage-bright', title: <span className="stage-heading bright"><i />亮片</span>, children: stageColumns('bright') },
{ title: rateBasisTitle('综合合格率', rateBasis, 'composite', onBasis), key: 'compositeRate', dataIndex: 'compositeRate', align: 'center', fixed: compact ? undefined : 'right', width: 120, className: 'combined-column', render: (_: number | null, row: MetricRow) => <button className="combined-value" tabIndex={-1} style={{ userSelect: 'text', WebkitUserSelect: 'text' }}><AnimatedMetricText value={rateText(visibleCompositeRate(row, rateBasis))} /></button> }, { title: rateBasisTitle('综合合格率', rateBasis, 'composite', onBasis), key: 'compositeRate', dataIndex: 'compositeRate', align: 'center', fixed: compact ? undefined : 'right', width: 120, className: 'combined-column', render: (_: number | null, row: MetricRow) => <button className="combined-value" tabIndex={-1} style={{ userSelect: 'text', WebkitUserSelect: 'text' }}><AnimatedMetricText value={rateText(visibleCompositeRate(row, rateBasis))} /></button> },
@@ -1143,6 +1169,7 @@ export function MetricTable({
if (key === 'object') return [title(row), view === 'group' ? row.department : null].filter(Boolean).join(' / '); if (key === 'object') return [title(row), view === 'group' ? row.department : null].filter(Boolean).join(' / ');
if (key === 'compositeRate') return visibleCompositeRate(row, rateBasis); if (key === 'compositeRate') return visibleCompositeRate(row, rateBasis);
const stage: Stage = key.startsWith('black-') ? 'black' : 'bright'; const stage: Stage = key.startsWith('black-') ? 'black' : 'bright';
if (defectDrilldown && key.startsWith(`${stage}-fine-`)) return defectCell(defectDrilldown,row,key.slice(`${stage}-fine-`.length),defectDisplay);
if (key.startsWith(`${stage}-defect-`)) { if (key.startsWith(`${stage}-defect-`)) {
const weight=defectList(row, stage).find(item => item.name === key.slice(`${stage}-defect-`.length))?.weight ?? null; const weight=defectList(row, stage).find(item => item.name === key.slice(`${stage}-defect-`.length))?.weight ?? null;
return defectDisplay === 'rate' ? defectWeightRate(weight,stageValue(row,stage,'totalWeight')) : weight; return defectDisplay === 'rate' ? defectWeightRate(weight,stageValue(row,stage,'totalWeight')) : weight;
@@ -1152,7 +1179,7 @@ export function MetricTable({
return field === 'rate' ? visibleStageRate(row, stage, stage === 'bright' ? rateBasis : 'original') : stageValue(row, stage, field, rateBasis); return field === 'rate' ? visibleStageRate(row, stage, stage === 'bright' ? rateBasis : 'original') : stageValue(row, stage, field, rateBasis);
}), }),
})); }));
const dataSource = rows.map((row, index) => ({ ...row, key: row.key || `${title(row)}-${index}` })); const dataSource = rows.map((row, index) => ({ ...row, sourceMetricKey: row.key, key: row.key || `${title(row)}-${index}` }));
const [currentPage, setCurrentPage] = useState(1); const [currentPage, setCurrentPage] = useState(1);
const [pageSize, setPageSize] = useState(SUMMARY_PAGE_SIZE); const [pageSize, setPageSize] = useState(SUMMARY_PAGE_SIZE);
const effectivePage = pageSizeOverride == null ? Math.min(currentPage, Math.max(1, Math.ceil(rows.length / pageSize))) : 1; const effectivePage = pageSizeOverride == null ? Math.min(currentPage, Math.max(1, Math.ceil(rows.length / pageSize))) : 1;
@@ -1184,7 +1211,7 @@ function trendBarWidth(chartWidth: number, count: number): number {
return Math.max(12, Math.min(64, Math.floor(((chartWidth - 132) / Math.max(count, 1)) * 0.8))); return Math.max(12, Math.min(64, Math.floor(((chartWidth - 132) / Math.max(count, 1)) * 0.8)));
} }
export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedPeriod, onMonth, onlyStage }: { rows: TrendMetricRow[]; mode: 'production' | 'defects'; stage: Stage; rateBasis: 'original' | 'withoutHeadquartersScrap'; targetRate: number | null; selectedPeriod?: string; onMonth: (value: number | string) => void; onlyStage?: Stage }) { export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedPeriod, onMonth, onlyStage, defectDrilldown, drillableCategories = [], onDefect }: { defectDrilldown?:DefectDrilldownResponse; drillableCategories?:string[]; onDefect?:(category:string)=>void; rows: TrendMetricRow[]; mode: 'production' | 'defects'; stage: Stage; rateBasis: 'original' | 'withoutHeadquartersScrap'; targetRate: number | null; selectedPeriod?: string; onMonth: (value: number | string) => void; onlyStage?: Stage }) {
const { active } = usePageCache(); const { active } = usePageCache();
const container = useRef<HTMLDivElement>(null); const container = useRef<HTMLDivElement>(null);
const [snapshotRequested, setSnapshotRequested] = useState(false); const [snapshotRequested, setSnapshotRequested] = useState(false);
@@ -1197,6 +1224,8 @@ export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedP
const visible = useResourceVisibility(container, active) || snapshotRequested; const visible = useResourceVisibility(container, active) || snapshotRequested;
const chart = useRef<ReturnType<typeof echarts.init> | null>(null); const chart = useRef<ReturnType<typeof echarts.init> | null>(null);
const onMonthRef = useRef(onMonth); const onMonthRef = useRef(onMonth);
const drillRef = useRef({onDefect,drillableCategories,drilled:!!defectDrilldown});
drillRef.current={onDefect,drillableCategories,drilled:!!defectDrilldown};
const rowsRef = useRef(rows); const rowsRef = useRef(rows);
const modeRef = useRef(mode); const modeRef = useRef(mode);
const stageRef = useRef(stage); const stageRef = useRef(stage);
@@ -1214,7 +1243,7 @@ export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedP
const refreshBarMasks = useCallback(() => { const refreshBarMasks = useCallback(() => {
const instance = chart.current; const instance = chart.current;
if (!instance || modeRef.current !== 'production') { setBarMasks([]); return; } if (!instance || modeRef.current !== 'production') { setBarMasks(previous => previous.length ? [] : previous); return; }
const zero = Number(instance.convertToPixel({ yAxisIndex: 0 }, 0)); const zero = Number(instance.convertToPixel({ yAxisIndex: 0 }, 0));
const width = trendBarWidth(instance.getWidth(), rowsRef.current.length); const width = trendBarWidth(instance.getWidth(), rowsRef.current.length);
const next = rowsRef.current.flatMap(row => { const next = rowsRef.current.flatMap(row => {
@@ -1249,6 +1278,11 @@ export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedP
selectionReady.current = false; selectionReady.current = false;
setBarMasks([]); setBarMasks([]);
instance.on('click', params => { instance.on('click', params => {
if(modeRef.current==='defects') {
const drill=drillRef.current;
if(params.componentType==='series'&&!drill.drilled&&drill.drillableCategories.includes(String(params.seriesName)))drill.onDefect?.(String(params.seriesName));
return;
}
if (params.componentType !== 'series' && params.componentType !== 'xAxis') return; if (params.componentType !== 'series' && params.componentType !== 'xAxis') return;
const currentRows = rowsRef.current; const currentRows = rowsRef.current;
const index = params.dataIndex ?? currentRows.findIndex(row => (row.periodLabel || String(row.month)) === String(params.value ?? params.name)); const index = params.dataIndex ?? currentRows.findIndex(row => (row.periodLabel || String(row.month)) === String(params.value ?? params.name));
@@ -1304,7 +1338,14 @@ export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedP
return [escapeTooltipText(title), ...items.map(item => { return [escapeTooltipText(title), ...items.map(item => {
const name = String(item.seriesName || ''); const name = String(item.seriesName || '');
const formatted = item.value == null ? '—' : onlyRates || name.includes('率') ? `${Number(item.value).toFixed(2)}%` : `${numberText(item.value)} kg`; const formatted = item.value == null ? '—' : onlyRates || name.includes('率') ? `${Number(item.value).toFixed(2)}%` : `${numberText(item.value)} kg`;
return `${item.marker || ''}${escapeTooltipText(name)}:${formatted}`; let weight='';
if(defectDrilldown&&onlyRates&&item.dataIndex!==undefined) {
const group=defectGroup(defectDrilldown,rows[item.dataIndex]);
const field=defectDrilldown.fields.find(field=>fineDefectRateLabel(field.label)===name);
const value=field?group?.children.find(child=>child.key===field.key)?.weight:group?.parentWeight;
weight=`(${numberText(value)} kg)`;
}
return `${item.marker || ''}${escapeTooltipText(name)}:${formatted}${weight}`;
})].join('<br/>'); })].join('<br/>');
}; };
const base: echarts.EChartsCoreOption = { const base: echarts.EChartsCoreOption = {
@@ -1317,7 +1358,7 @@ export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedP
grid: { left: 65, right: mode === 'production' ? 67 : 32, top: 70, bottom: 38 }, grid: { left: 65, right: mode === 'production' ? 67 : 32, top: 70, bottom: 38 },
legend: { type: 'scroll', top: 0, left: 9, right: 9, itemWidth: 14, itemHeight: 7, itemGap: 20, textStyle: { color: ink, fontFamily, fontSize, fontWeight: 500 } }, legend: { type: 'scroll', top: 0, left: 9, right: 9, itemWidth: 14, itemHeight: 7, itemGap: 20, textStyle: { color: ink, fontFamily, fontSize, fontWeight: 500 } },
tooltip: { trigger: 'axis', backgroundColor: surface, borderColor: line, borderWidth: 1, padding: [10, 13], textStyle: { color: ink, fontFamily, fontSize: tooltipFontSize }, transitionDuration: 0, axisPointer: { type: 'line', lineStyle: { color: blue, width: 1, type: 'dashed' } }, formatter: (params: unknown) => formatTooltip(params, mode === 'defects') }, tooltip: { trigger: 'axis', backgroundColor: surface, borderColor: line, borderWidth: 1, padding: [10, 13], textStyle: { color: ink, fontFamily, fontSize: tooltipFontSize }, transitionDuration: 0, axisPointer: { type: 'line', lineStyle: { color: blue, width: 1, type: 'dashed' } }, formatter: (params: unknown) => formatTooltip(params, mode === 'defects') },
xAxis: { type: 'category', data: labels, triggerEvent: true, axisLine: { lineStyle: { color: line } }, axisLabel: { color: ink, fontFamily, fontSize, margin: 12 }, axisTick: { show: false } }, xAxis: { type: 'category', data: labels, triggerEvent: mode === 'production', axisLine: { lineStyle: { color: line } }, axisLabel: { color: ink, fontFamily, fontSize, margin: 12 }, axisTick: { show: false } },
}; };
let option: echarts.EChartsCoreOption; let option: echarts.EChartsCoreOption;
const pointStyle = { smooth: 0.15, smoothMonotone: 'x' as const, z: 10, symbol: 'circle', symbolSize: 5, showSymbol: true, emphasis: { focus: 'none' as const, scale: 1.35 }, connectNulls: false }; const pointStyle = { smooth: 0.15, smoothMonotone: 'x' as const, z: 10, symbol: 'circle', symbolSize: 5, showSymbol: true, emphasis: { focus: 'none' as const, scale: 1.35 }, connectNulls: false };
@@ -1355,16 +1396,22 @@ export function TrendChart({ rows, mode, stage, rateBasis, targetRate, selectedP
option = { option = {
...base, ...base,
yAxis: [{ type: 'value', name: '不良率 / %', nameGap: 18, nameTextStyle: { color: ink, align: 'left', fontSize }, axisLabel: { color: ink, formatter: '{value}%', fontFamily: numericFamily, fontSize }, axisLine: { show: false }, axisTick: { show: false }, splitLine: { lineStyle: { color: line, type: 'dashed' } } }], yAxis: [{ type: 'value', name: '不良率 / %', nameGap: 18, nameTextStyle: { color: ink, align: 'left', fontSize }, axisLabel: { color: ink, formatter: '{value}%', fontFamily: numericFamily, fontSize }, axisLine: { show: false }, axisTick: { show: false }, splitLine: { lineStyle: { color: line, type: 'dashed' } } }],
series: names.map((name, index) => ({ id: `defect-${stage}-${name}`, name, type: 'line' as const, ...pointStyle, lineStyle: { width: 2, color: palette[index % palette.length] }, itemStyle: { color: palette[index % palette.length] }, data: rows.map(row => { const defect = row[stage].defects.find(item => item.name === name); return rateNumber(defectWeightRate(defect?.weight, row[stage].totalWeight)); }) })), series: names.map((name, index) => ({ id: `defect-${stage}-${name}`, name, type: 'line' as const, triggerLineEvent:true, cursor:drillableCategories.includes(name)?'pointer':'default', ...pointStyle, lineStyle: { width: 2, color: palette[index % palette.length] }, itemStyle: { color: palette[index % palette.length] }, data: rows.map(row => { const defect = row[stage].defects.find(item => item.name === name); return rateNumber(defectWeightRate(defect?.weight, row[stage].totalWeight)); }) })),
}; };
} }
if(mode==='defects'&&defectDrilldown) {
const data=defectDrilldown;
const palette=[blue,teal,'#bf7d58','#9278bf','#598eae','#9c8850','#5b9c86','#ba6f83','#6b7ca5'];
option={...option,series:[{id:'fine-parent',name:fineDefectRateLabel(data.category),type:'line',...pointStyle,symbol:'none',lineStyle:{width:1.5,type:'dashed',color:ink,opacity:.5},data:rows.map(row=>rateNumber(defectGroup(data,row)?.parentRate))},
...data.fields.map((field,index)=>({id:`fine-${field.key}`,name:fineDefectRateLabel(field.label),type:'line' as const,...pointStyle,lineStyle:{width:2,color:palette[index%palette.length]},itemStyle:{color:palette[index%palette.length]},data:rows.map(row=>rateNumber(defectGroup(data,row)?.children.find(child=>child.key===field.key)?.rate))}))]};
}
if(onlyStage && mode === 'production') { if(onlyStage && mode === 'production') {
const stageOption=option as {series?:Array<{id?:string}>;legend?:unknown}; const stageOption=option as {series?:Array<{id?:string}>;legend?:unknown};
stageOption.series=stageOption.series?.filter(item=>item.id==='qualified-weight'||item.id==='scrap-weight'||item.id===`${onlyStage}-rate`); stageOption.series=stageOption.series?.filter(item=>item.id==='qualified-weight'||item.id==='scrap-weight'||item.id===`${onlyStage}-rate`);
} }
instance.setOption(option, { replaceMerge: ['series', 'yAxis'], lazyUpdate: false }); instance.setOption(option, { replaceMerge: ['series', 'yAxis'], lazyUpdate: false });
refreshBarMasks(); refreshBarMasks();
}, [rows, mode, stage, rateBasis, targetRate, themeRevision, refreshBarMasks, onlyStage, selectedPeriod, active, visible]); }, [rows, mode, stage, rateBasis, targetRate, themeRevision, refreshBarMasks, onlyStage, selectedPeriod, active, visible, defectDrilldown, drillableCategories.join('\0')]);
const dimmedPeriod = selectedPeriod === lineSelectedPeriod ? undefined : selectedPeriod; const dimmedPeriod = selectedPeriod === lineSelectedPeriod ? undefined : selectedPeriod;
+3 -3
View File
@@ -7,18 +7,18 @@ type Granularity = 'month' | 'quarter' | 'year';
type TrendMode = 'production' | 'defects' | 'data'; type TrendMode = 'production' | 'defects' | 'data';
type DefectDisplay = 'weight' | 'rate'; type DefectDisplay = 'weight' | 'rate';
export function QueryTrendPanel({ panelRef, id, mode, onMode, granularity, onGranularity, defectDisplay, onDefectDisplay, fullFields, onFullFields, object, onObject, objects, scopeName, stageSwitch, children }: { export function QueryTrendPanel({ panelRef, id, mode, onMode, granularity, onGranularity, defectDisplay, onDefectDisplay, fullFields, onFullFields, object, onObject, objects, scopeName, stageSwitch, headingControls, children }: {
panelRef?: Ref<HTMLElement>; id?: string; mode: TrendMode; onMode: (value: TrendMode) => void; panelRef?: Ref<HTMLElement>; id?: string; mode: TrendMode; onMode: (value: TrendMode) => void;
granularity: Granularity; onGranularity: (value: Granularity) => void; granularity: Granularity; onGranularity: (value: Granularity) => void;
defectDisplay: DefectDisplay; onDefectDisplay: (value: DefectDisplay) => void; defectDisplay: DefectDisplay; onDefectDisplay: (value: DefectDisplay) => void;
fullFields: boolean; onFullFields: (value: boolean) => void; fullFields: boolean; onFullFields: (value: boolean) => void;
object?: string; onObject: (value?: string) => void; objects: Array<{ label: string; value: string }>; object?: string; onObject: (value?: string) => void; objects: Array<{ label: string; value: string }>;
scopeName: string; stageSwitch?: ReactNode; children: ReactNode; scopeName: string; stageSwitch?: ReactNode; headingControls?: ReactNode; children: ReactNode;
}) { }) {
const periodName = granularity === 'month' ? '月度' : granularity === 'quarter' ? '季度' : '年度'; const periodName = granularity === 'month' ? '月度' : granularity === 'quarter' ? '季度' : '年度';
return <section className="panel section-panel trend-panel" data-snapshot-module="trend" ref={panelRef} id={id}> return <section className="panel section-panel trend-panel" data-snapshot-module="trend" ref={panelRef} id={id}>
<div className="section-heading-row trend-heading-row"> <div className="section-heading-row trend-heading-row">
<div className="section-heading"><h2>{periodName}变化趋势</h2></div> <div className="section-heading trend-heading-content"><h2>{periodName}变化趋势</h2>{headingControls}</div>
<Segmented value={mode} options={[{ label: '合格率与产量', value: 'production' }, { label: '不良率', value: 'defects' }, { label: `${periodName}数据`, value: 'data' }]} onChange={onMode} aria-label="趋势视图" /> <Segmented value={mode} options={[{ label: '合格率与产量', value: 'production' }, { label: '不良率', value: 'defects' }, { label: `${periodName}数据`, value: 'data' }]} onChange={onMode} aria-label="趋势视图" />
</div> </div>
<div className="trend-controls"> <div className="trend-controls">
+22 -16
View File
@@ -11,6 +11,8 @@ import { StageSourceFilters } from './StageSourceFilters';
import { SalesOrderTypeSelect } from './ExtraQueryFilters'; import { SalesOrderTypeSelect } from './ExtraQueryFilters';
import { salesOrderTypeSelection } from './salesOrderTypes'; import { salesOrderTypeSelection } from './salesOrderTypes';
import { stageProductQuery, sortStageRowsByTotalWeight } from './stageProductQuery'; import { stageProductQuery, sortStageRowsByTotalWeight } from './stageProductQuery';
import { freezeSourceRecordsQuery } from './sourceRecordsContext';
import { useDefectDrilldown } from './useDefectDrilldown';
import { usePageCache } from './pageCache'; import { usePageCache } from './pageCache';
import { useDelayedIndicator } from './useDelayedBusy'; import { useDelayedIndicator } from './useDelayedBusy';
import { defaultQueryMonth, previousCompleteMonth, previousCompleteQuarter } from './periodDefaults'; import { defaultQueryMonth, previousCompleteMonth, previousCompleteQuarter } from './periodDefaults';
@@ -28,8 +30,8 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
const {message}=App.useApp();const {active,initialSearch,refreshKey,onLocationChange,currentReleaseId}=usePageCache(); const {message}=App.useApp();const {active,initialSearch,refreshKey,onLocationChange,currentReleaseId}=usePageCache();
const [initial]=useState(()=>queryFromLocation(initialSearch));const defaults=previousCompleteMonth(); const [initial]=useState(()=>queryFromLocation(initialSearch));const defaults=previousCompleteMonth();
const automaticPeriod=useRef(!initial.year&&!initial.month&&!initial.quarter); const automaticPeriod=useRef(!initial.year&&!initial.month&&!initial.quarter);
const [period,setPeriod]=useState<PeriodKind>(initial.quarter?'quarter':initial.month?'month':initial.year?'year':'month'); const [period,setPeriod]=useState<PeriodKind>(initial.quarter?'quarter':initial.month?'month':'year');
const [year,setYear]=useState(initial.year||defaults.year);const [month,setMonth]=useState(initial.month||defaults.month);const [quarter,setQuarter]=useState(initial.quarter||previousCompleteQuarter().quarter); const [year,setYear]=useState(initial.year||(period==='month'?defaults.year:period==='quarter'?previousCompleteQuarter().year:new Date().getFullYear()));const [month,setMonth]=useState(initial.month||defaults.month);const [quarter,setQuarter]=useState(initial.quarter||previousCompleteQuarter().quarter);
const [department,setDepartment]=useState(()=>decodeQueryValueList(initial.department));const [group,setGroup]=useState(()=>decodeQueryValueList(initial.group));const [product,setProduct]=useState(()=>decodeQueryValueList(initial.product));const [projectGroup,setProjectGroup]=useState(()=>decodeQueryValueList(initial.projectGroup)); const [department,setDepartment]=useState(()=>decodeQueryValueList(initial.department));const [group,setGroup]=useState(()=>decodeQueryValueList(initial.group));const [product,setProduct]=useState(()=>decodeQueryValueList(initial.product));const [projectGroup,setProjectGroup]=useState(()=>decodeQueryValueList(initial.projectGroup));
const [sourceFilters,setSourceFilters]=useState<SourceColumnFilter[]>(initial.columnFilters||[]);const [search,setSearch]=useState(initial.productSearch||'');const [appliedSearch,setAppliedSearch]=useState(initial.productSearch||'');const includeUnassigned=true; const [sourceFilters,setSourceFilters]=useState<SourceColumnFilter[]>(initial.columnFilters||[]);const [search,setSearch]=useState(initial.productSearch||'');const [appliedSearch,setAppliedSearch]=useState(initial.productSearch||'');const includeUnassigned=true;
const [moreFiltersOpen,setMoreFiltersOpen]=useState(false); const [moreFiltersOpen,setMoreFiltersOpen]=useState(false);
@@ -49,7 +51,7 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
const loadSupport=useCallback(async()=>{ const loadSupport=useCallback(async()=>{
const generation=++supportGeneration.current;supportController.current?.abort();const controller=new AbortController();supportController.current=controller; const generation=++supportGeneration.current;supportController.current?.abort();const controller=new AbortController();supportController.current=controller;
setSupportLoading(true);setSupportError(''); setSupportLoading(true);setSupportError('');
try{const [choices,source,filterFields]=await Promise.all([api.options(controller.signal),api.sourceColumnCatalog(initial.releaseId||currentReleaseId||undefined,controller.signal),api.filterFields(false,controller.signal)]);if(generation!==supportGeneration.current)return;if(automaticPeriod.current){const available=defaultQueryMonth(choices);setYear(available.year);setMonth(available.month);automaticPeriod.current=false;}setOptions(choices);setCatalog(source);setExtraFields(filterFields.filter(field=>field.key==='salesOrderType'));} try{const [choices,source,filterFields]=await Promise.all([api.options(controller.signal),api.sourceColumnCatalog(initial.releaseId||currentReleaseId||undefined,controller.signal),api.filterFields(false,controller.signal)]);if(generation!==supportGeneration.current)return;if(automaticPeriod.current){const available=defaultQueryMonth(choices);setMonth(available.month);automaticPeriod.current=false;}setOptions(choices);setCatalog(source);setExtraFields(filterFields.filter(field=>field.key==='salesOrderType'));}
catch(error){if(generation===supportGeneration.current&&!onUnauthorized(error))setSupportError(error instanceof Error?error.message:'查询条件读取失败');} catch(error){if(generation===supportGeneration.current&&!onUnauthorized(error))setSupportError(error instanceof Error?error.message:'查询条件读取失败');}
finally{if(generation===supportGeneration.current)setSupportLoading(false);} finally{if(generation===supportGeneration.current)setSupportLoading(false);}
},[onUnauthorized,initial.releaseId,currentReleaseId]); },[onUnauthorized,initial.releaseId,currentReleaseId]);
@@ -57,7 +59,7 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
const fields=useMemo(()=>catalog?.sourceFields.filter(field=>field.type==='source_value'&&field.stages.includes(stage))||[],[catalog,stage]); const fields=useMemo(()=>catalog?.sourceFields.filter(field=>field.type==='source_value'&&field.stages.includes(stage))||[],[catalog,stage]);
const exactProductSearch=useMemo(()=>(options?.products||[]).find(name=>name.trim().toLocaleLowerCase()===appliedSearch.trim().toLocaleLowerCase())||'', [options,appliedSearch]); const exactProductSearch=useMemo(()=>(options?.products||[]).find(name=>name.trim().toLocaleLowerCase()===appliedSearch.trim().toLocaleLowerCase())||'', [options,appliedSearch]);
const queryProducts=useMemo(()=>drilledProduct?[drilledProduct]:exactProductSearch?product.length&&!product.includes(exactProductSearch)?['__codex_no_product_search_match__']:[exactProductSearch]:product,[drilledProduct,exactProductSearch,product]); const queryProducts=useMemo(()=>drilledProduct?[drilledProduct]:exactProductSearch?product.length&&!product.includes(exactProductSearch)?['__codex_no_product_search_match__']:[exactProductSearch]:product,[drilledProduct,exactProductSearch,product]);
useEffect(()=>{setDrilledProduct('');},[year,department,group,product,projectGroup,sourceFilters,extraFilters]); useEffect(()=>{setDrilledProduct('');},[department,group,product,projectGroup,sourceFilters,extraFilters]);
const draft=useMemo(()=>stageProductQuery({year,...(period==='month'?{month}:period==='quarter'?{quarter}:{}),department:encodeQueryValueList(department),group:encodeQueryValueList(group),product:encodeQueryValueList(queryProducts),projectGroup:encodeQueryValueList(projectGroup),productSearch:drilledProduct?undefined:appliedSearch.trim()||undefined,includeUnassigned,extraFilters,view:'product',trendGranularity:'month',releaseId:initial.releaseId||currentReleaseId||catalog?.releaseId},stage,sourceFilters),[year,period,month,quarter,department,group,queryProducts,drilledProduct,projectGroup,appliedSearch,includeUnassigned,catalog,stage,sourceFilters,extraFilters,initial.releaseId,currentReleaseId]); const draft=useMemo(()=>stageProductQuery({year,...(period==='month'?{month}:period==='quarter'?{quarter}:{}),department:encodeQueryValueList(department),group:encodeQueryValueList(group),product:encodeQueryValueList(queryProducts),projectGroup:encodeQueryValueList(projectGroup),productSearch:drilledProduct?undefined:appliedSearch.trim()||undefined,includeUnassigned,extraFilters,view:'product',trendGranularity:'month',releaseId:initial.releaseId||currentReleaseId||catalog?.releaseId},stage,sourceFilters),[year,period,month,quarter,department,group,queryProducts,drilledProduct,projectGroup,appliedSearch,includeUnassigned,catalog,stage,sourceFilters,extraFilters,initial.releaseId,currentReleaseId]);
// Apply organizational/source filters directly, like the other query pages. Product text search keeps its explicit submit. // Apply organizational/source filters directly, like the other query pages. Product text search keeps its explicit submit.
useEffect(()=>{if(active&&catalog&&!supportLoading&&!supportError)setApplied(previous=>JSON.stringify(previous)===JSON.stringify(draft)?previous:draft);},[active,catalog,draft,supportLoading,supportError]); useEffect(()=>{if(active&&catalog&&!supportLoading&&!supportError)setApplied(previous=>JSON.stringify(previous)===JSON.stringify(draft)?previous:draft);},[active,catalog,draft,supportLoading,supportError]);
@@ -82,8 +84,8 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
const queryFailed=!!(error||supportError); const queryFailed=!!(error||supportError);
const submit=()=>{automaticPeriod.current=false;setAppliedSearch(search.trim());setDrilledProduct('');setRevision(v=>v+1);}; const submit=()=>{automaticPeriod.current=false;setAppliedSearch(search.trim());setDrilledProduct('');setRevision(v=>v+1);};
const setTrendObject=(value?:string)=>{const selected=value||'';setDrilledProduct('');setSearch(selected);setAppliedSearch(selected);}; const setTrendObject=(value?:string)=>{const selected=value||'';setDrilledProduct('');setSearch(selected);setAppliedSearch(selected);};
const returnToProducts=()=>{setDrilledProduct('');setSearch(drillParentSearch.current);setAppliedSearch(drillParentSearch.current);setTrendMode('production');}; const returnToProducts=()=>{setDrilledProduct('');setSearch(drillParentSearch.current);setAppliedSearch(drillParentSearch.current);if(trendMode!=='defects')setTrendMode('production');};
const reset=()=>{automaticPeriod.current=false;const previous=defaultQueryMonth(options);setYear(previous.year);setMonth(previous.month);setQuarter(previousCompleteQuarter().quarter);setPeriod('month');setTrendGranularity('month');setDepartment([]);setGroup([]);setProduct([]);setProjectGroup([]);setSourceFilters([]);setExtraFilters({});setSearch('');setAppliedSearch('');setDrilledProduct('');setApplied(stageProductQuery({year:previous.year,month:previous.month,includeUnassigned,view:'product',trendGranularity:'month',releaseId:catalog?.releaseId},stage,[]));setRevision(v=>v+1);}; const reset=()=>{automaticPeriod.current=false;const previous=defaultQueryMonth(options);const currentYear=new Date().getFullYear();setYear(currentYear);setMonth(previous.month);setQuarter(previousCompleteQuarter().quarter);setPeriod('year');setTrendGranularity('month');setDepartment([]);setGroup([]);setProduct([]);setProjectGroup([]);setSourceFilters([]);setExtraFilters({});setSearch('');setAppliedSearch('');setDrilledProduct('');setApplied(stageProductQuery({year:currentYear,includeUnassigned,view:'product',trendGranularity:'month',releaseId:catalog?.releaseId},stage,[]));setRevision(v=>v+1);};
const groups=(options?.groups||[]).filter(name=>!department.length||department.includes(options?.groupDepartments?.[name]||'')); const groups=(options?.groups||[]).filter(name=>!department.length||department.includes(options?.groupDepartments?.[name]||''));
const paths=options?.organizationPaths||[];const products=department.length||group.length?[...new Set(paths.filter(path=>(!department.length||department.includes(path.department||''))&&(!group.length||group.includes(path.group||''))).map(path=>path.product).filter((v):v is string=>!!v))]:options?.products||[]; const paths=options?.organizationPaths||[];const products=department.length||group.length?[...new Set(paths.filter(path=>(!department.length||department.includes(path.department||''))&&(!group.length||group.includes(path.group||''))).map(path=>path.product).filter((v):v is string=>!!v))]:options?.products||[];
const trendOptions=useMemo(()=>[...products.map(value=>({label:value,value})),...(drilledProduct==='__UNASSIGNED__'?[{label:'未分类',value:drilledProduct}]:[])],[products,drilledProduct]); const trendOptions=useMemo(()=>[...products.map(value=>({label:value,value})),...(drilledProduct==='__UNASSIGNED__'?[{label:'未分类',value:drilledProduct}]:[])],[products,drilledProduct]);
@@ -98,9 +100,12 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
const trendRefreshing=!trendError&&(trendLoading||trendResponse?.key!==trendKey); const trendRefreshing=!trendError&&(trendLoading||trendResponse?.key!==trendKey);
const showTrendLoading=useDelayedIndicator(trendRefreshing); const showTrendLoading=useDelayedIndicator(trendRefreshing);
const tableReady=!!current&&(!drilledProduct||(trendResponse?.key===trendKey&&!trendLoading&&!trendError)); const tableReady=!!current&&(!drilledProduct||(trendResponse?.key===trendKey&&!trendLoading&&!trendError));
const table=useRetainedResult(tableReady&&applied?{rows:drilledProduct?visibleTrend:sortedRows,query:applied,drilledProduct,period:drilledProduct?'year' as const:applied.month?'month' as const:applied.quarter?'quarter' as const:'year' as const}:null); const table=useRetainedResult(tableReady&&applied?{rows:drilledProduct?visibleTrend:sortedRows,query:applied,drilledProduct,granularity:trendGranularity,period:drilledProduct?'year' as const:applied.month?'month' as const:applied.quarter?'quarter' as const:'year' as const}:null);
const defectDrill=useDefectDrilldown({active,mode:trendMode,stage,query:{...(applied||draft),releaseId:current?.release?.id||applied?.releaseId},trendQuery:trendQuery||draft,
tableQuery:drilledProduct?(trendQuery||draft):(applied||draft),tableDimension:drilledProduct?'period':'product',tableGranularity:trendGranularity,
refreshKey:JSON.stringify([refreshKey,revision]),onUnauthorized,display:defectDisplay,onDisplay:setDefectDisplay});
const summary=displayed?.data.summary[stage];const stageRate=stage==='bright'&&rateBasis==='withoutHeadquartersScrap'?summary?.rateWithoutHeadquartersScrap:summary?.rate; const summary=displayed?.data.summary[stage];const stageRate=stage==='bright'&&rateBasis==='withoutHeadquartersScrap'?summary?.rateWithoutHeadquartersScrap:summary?.rate;
const openDetail=(row:MetricRow|null,_stage:Stage,selectedMonth?:number)=>{if(!applied||!current?.release)return;setRecordVisible(true);setRecords({row,query:{...applied,releaseId:current.release.id,...(row&&selectedMonth==null?{product:row.product||'__UNASSIGNED__'}:{}),...(selectedMonth?{month:selectedMonth,quarter:undefined}: {})}});}; const openDetail=(row:MetricRow|null,_stage:Stage,selectedMonth?:number)=>{if(!applied||!current?.release)return;setRecordVisible(true);setRecords({row,query:freezeSourceRecordsQuery(applied,current.release.id,drilledProduct?'overall':'product',row,selectedMonth)});};
const runExport=async()=>{setExporting(true);try{await exporter.current?.exportExcel();}catch(e){message.error(e instanceof Error?e.message:'导出失败');}finally{setExporting(false);}}; const runExport=async()=>{setExporting(true);try{await exporter.current?.exportExcel();}catch(e){message.error(e instanceof Error?e.message:'导出失败');}finally{setExporting(false);}};
const formulaKey=stage==='black'?'blackRate':rateBasis==='original'?'brightRate':'brightRateWithoutHeadquartersScrap'; const formulaKey=stage==='black'?'blackRate':rateBasis==='original'?'brightRate':'brightRateWithoutHeadquartersScrap';
return <div className="query-page stage-product-page"> return <div className="query-page stage-product-page">
@@ -133,30 +138,31 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
<MetricCard label="总重量" value={`${weight(summary?.totalWeight)} kg`} onClick={()=>openDetail(null,stage)}/> <MetricCard label="总重量" value={`${weight(summary?.totalWeight)} kg`} onClick={()=>openDetail(null,stage)}/>
</section> </section>
<div className="analysis-stack"> <div className="analysis-stack">
<QueryTrendPanel mode={trendMode} onMode={setTrendMode} granularity={trendGranularity} onGranularity={setTrendGranularity} <QueryTrendPanel headingControls={defectDrill.controls} mode={trendMode} onMode={setTrendMode} granularity={trendGranularity} onGranularity={setTrendGranularity}
defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay} fullFields={trendFullFields} onFullFields={setTrendFullFields} defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay} fullFields={trendFullFields} onFullFields={setTrendFullFields}
object={trendObject} onObject={setTrendObject} objects={trendOptions} scopeName="产品"> object={trendObject} onObject={setTrendObject} objects={trendOptions} scopeName="产品">
{trendError&&<Alert type="warning" showIcon message={trendResponse?"趋势更新失败,当前显示上次结果":"趋势读取失败"} description={trendResponse?`${trendError};当前范围:${loadedQueryScope(trendResponse.query,decodeQueryValueList)}`:trendError} action={<Button onClick={()=>setTrendRevision(v=>v+1)}>重试</Button>}/>} {trendError&&<Alert type="warning" showIcon message={trendResponse?"趋势更新失败,当前显示上次结果":"趋势读取失败"} description={trendResponse?`${trendError};当前范围:${loadedQueryScope(trendResponse.query,decodeQueryValueList)}`:trendError} action={<Button onClick={()=>setTrendRevision(v=>v+1)}>重试</Button>}/>}
<div className="trend-chart-shell" aria-busy={trendRefreshing} inert={trendRefreshing||!!trendError}> <div className="trend-chart-shell" aria-busy={trendRefreshing} inert={trendRefreshing||!!trendError}>
{trendMode==='data' {trendMode==='data'
? <TrendTable rows={displayedTrend} defectDisplay={defectDisplay} rateBasis={rateBasis} onlyStage={stage} fullFields={trendFullFields} onMonth={selectTrendPeriod}/> ? <TrendTable rows={displayedTrend} defectDisplay={defectDisplay} rateBasis={rateBasis} onlyStage={stage} fullFields={trendFullFields} onMonth={selectTrendPeriod}/>
: <TrendChart rows={displayedTrend} mode={trendMode} stage={stage} onlyStage={stage} rateBasis={rateBasis} targetRate={null} selectedPeriod={selectedTrendPeriod} onMonth={selectTrendPeriod}/>} : <TrendChart defectDrilldown={trendResponse?.key===trendKey&&!trendLoading&&!trendError?defectDrill.trend:undefined} drillableCategories={defectDrill.categories} onDefect={defectDrill.choose} rows={displayedTrend} mode={trendMode} stage={stage} onlyStage={stage} rateBasis={rateBasis} targetRate={null} selectedPeriod={selectedTrendPeriod} onMonth={selectTrendPeriod}/>}
{showTrendLoading&&<span className="trend-refresh-indicator"><Spin size="small"/>{trendResponse?`正在更新 · 当前显示上次筛选的${loadedQueryScope(trendResponse.query,decodeQueryValueList)}`:'正在读取趋势'}</span>} {showTrendLoading&&<span className="trend-refresh-indicator"><Spin size="small"/>{trendResponse?`正在更新 · 当前显示上次筛选的${loadedQueryScope(trendResponse.query,decodeQueryValueList)}`:'正在读取趋势'}</span>}
</div> </div>
</QueryTrendPanel> </QueryTrendPanel>
<QueryTablePanel heading={<SectionHeading title={table?.drilledProduct?'产品月度质量数据':`${label}产品质量汇总`} subtitle={table?.drilledProduct?`${table.query.year} 年 · ${table.drilledProduct==='__UNASSIGNED__'?'未分类':table.drilledProduct} · 重量 / kg 合格率 / %`:'重量 / kg 合格率 / %'}/>} <QueryTablePanel heading={<SectionHeading title={table?.drilledProduct?`产品${table.granularity==='quarter'?'季度':table.granularity==='year'?'年度':'月度'}质量数据`:`${label}产品质量汇总`} subtitle={table?.drilledProduct?`${table.query.year} 年 · ${table.drilledProduct==='__UNASSIGNED__'?'未分类':table.drilledProduct} · 重量 / kg 合格率 / %`:'重量 / kg 合格率 / %'}/>}
busy={queryBusy||!tableReady} search={drilledLabel||search} onSearchChange={setSearch} onSearch={submit} searchDisabled={!!drilledProduct} searchUnavailable={!catalog||!!supportError} busy={queryBusy||!tableReady||defectDrill.tablePending} search={drilledLabel||search} onSearchChange={setSearch} onSearch={submit} searchDisabled={!!drilledProduct} searchUnavailable={!catalog||!!supportError}
fullFields={fullFields} onFullFields={setFullFields} defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay} fullFields={fullFields} onFullFields={setFullFields} defectDisplay={defectDisplay} onDefectDisplay={setDefectDisplay}
returnControls={drilledProduct&&<Button className="query-return-button" icon={<ArrowLeftOutlined/>} onClick={returnToProducts}>返回上一级</Button>} returnControls={drilledProduct&&<Button className="query-return-button" icon={<ArrowLeftOutlined/>} onClick={returnToProducts}>返回上一级</Button>}
exporting={exporting} exportDisabled={!current||loading||!tableReady||!table?.rows.length} onExport={()=>void runExport()}> exporting={exporting} exportDisabled={!!defectDrill.category&&(!defectDrill.table||defectDrill.pending||!!defectDrill.error)||!current||loading||!tableReady||!table?.rows.length} onExport={()=>void runExport()}>
<SmoothTableFrame busy={queryBusy||!tableReady} blocked={queryBusy||!tableReady}> {defectDrill.tableControls}
<MetricTable ref={exporter} exportFilename={`${table?.drilledProduct||label}产品汇总-${table?.query.year||year}-${fullFields?'完整字段':'关键指标'}`} <SmoothTableFrame busy={queryBusy||!tableReady||defectDrill.tablePending} blocked={queryBusy||!tableReady||defectDrill.tablePending}>
<MetricTable defectDrilldown={tableReady&&!queryBusy?defectDrill.table:undefined} ref={exporter} exportFilename={`${table?.drilledProduct||label}产品汇总-${table?.query.year||year}-${fullFields?'完整字段':'关键指标'}`}
paginationScope={JSON.stringify({query:table?.query,drilledProduct:table?.drilledProduct})} paginationScope={JSON.stringify({query:table?.query,drilledProduct:table?.drilledProduct})}
rows={table?.rows||[]} view={table?.drilledProduct?'overall':'product'} layoutView="product" pageSizeOverride={table?.drilledProduct?12:undefined} rows={table?.rows||[]} view={table?.drilledProduct?'overall':'product'} layoutView="product" pageSizeOverride={table?.drilledProduct?12:undefined}
period={table?.period||period} month={table?.query.month||month} quarter={table?.query.quarter||quarter} period={table?.period||period} month={table?.query.month||month} quarter={table?.query.quarter||quarter}
fullFields={fullFields} defectDisplay={defectDisplay} rateBasis={rateBasis} onlyStage={stage} onDetail={openDetail} onBasis={()=>setBasisOpen(true)} fullFields={fullFields} defectDisplay={defectDisplay} rateBasis={rateBasis} onlyStage={stage} onDetail={openDetail} onBasis={()=>setBasisOpen(true)}
emptyText={queryBusy?'正在更新数据…':error?'查询失败,暂无可展示结果':'当前条件下暂无数据'} emptyText={queryBusy?'正在更新数据…':error?'查询失败,暂无可展示结果':'当前条件下暂无数据'}
onDrilldown={row=>{if(drilledProduct)return;const selected=row.product||'__UNASSIGNED__';drillParentSearch.current=appliedSearch;setDrilledProduct(selected);setTrendGranularity('month');setTrendMode('production');}}/> onDrilldown={row=>{if(drilledProduct)return;const selected=row.product||'__UNASSIGNED__';drillParentSearch.current=appliedSearch;setDrilledProduct(selected);setTrendGranularity('month');if(trendMode!=='defects')setTrendMode('production');}}/>
</SmoothTableFrame> </SmoothTableFrame>
</QueryTablePanel> </QueryTablePanel>
<section className="panel section-panel defects-panel" data-snapshot-module="defects" aria-busy={loading||!current}> <section className="panel section-panel defects-panel" data-snapshot-module="defects" aria-busy={loading||!current}>
+8
View File
@@ -17,6 +17,8 @@ import type {
SourceColumnField, SourceColumnField,
Collection, Collection,
FineDefectTopResponse, FineDefectTopResponse,
FineDefectCatalogResponse,
DefectDrilldownResponse,
MetricsResponse, MetricsResponse,
TrendResponse, TrendResponse,
OptionsResponse, OptionsResponse,
@@ -272,6 +274,12 @@ export const api = {
fineDefectTop(params: Record<string, unknown>, signal?: AbortSignal) { fineDefectTop(params: Record<string, unknown>, signal?: AbortSignal) {
return queryPost<FineDefectTopResponse>('/api/analysis/defects', params, signal); return queryPost<FineDefectTopResponse>('/api/analysis/defects', params, signal);
}, },
fineDefectCatalog(params: Record<string, unknown>, signal?: AbortSignal) {
return queryPost<FineDefectCatalogResponse>('/api/analysis/defects', params, signal);
},
defectDrilldown(params: Record<string, unknown>, signal?: AbortSignal) {
return queryPost<DefectDrilldownResponse>('/api/analysis/defects/drilldown', params, signal);
},
records(params: RecordsParams, signal?: AbortSignal) { records(params: RecordsParams, signal?: AbortSignal) {
return request<RecordsResponse>(`/api/records?${toQuery(params)}`, { signal }); return request<RecordsResponse>(`/api/records?${toQuery(params)}`, { signal });
}, },
+19
View File
@@ -0,0 +1,19 @@
import type { DefectDrilldownResponse, MetricRow, QueryParams, TrendMetricRow } from './types';
export const defectRateLabel = (label: string) => label.endsWith('重量') ? `${label.slice(0,-2)}废率` : `${label}率`;
export function defectScope(query: QueryParams): Record<string, unknown> {
const {view: _view,trendGranularity: _granularity,...scope}=query;
return scope;
}
export function defectGroup(data: DefectDrilldownResponse, row: MetricRow) {
const period=row as TrendMetricRow;
const sourceKey=(row as MetricRow & {sourceMetricKey?:string}).sourceMetricKey??row.key;
const key=data.dimension==='period'?String(period.period??period.month):data.dimension==='overall'?'overall':sourceKey??'__UNASSIGNED__';
return data.groups.find(group=>group.key===key);
}
export function defectCell(data: DefectDrilldownResponse, row: MetricRow, field: string, display: 'weight'|'rate'): number|null {
const group=defectGroup(data,row);
if(field==='parent')return (display==='rate'?group?.parentRate:group?.parentWeight)??null;
const child=group?.children.find(value=>value.key===field);
return (display==='rate'?child?.rate:child?.weight)??null;
}
+113 -17
View File
@@ -1,12 +1,13 @@
import { isValidElement, type Key } from 'react'; import { isValidElement, type Key } from 'react';
import { getStoredWorkspaceThemeId, getWorkspaceTheme, type WorkspaceThemeId } from './ui-themes.ts';
type ExportColumn = { exportRate?: boolean; key?: Key; title?: unknown; children?: readonly ExportColumn[] }; type ExportColumn = { exportRate?: boolean; key?: Key; title?: unknown; width?: string | number; children?: readonly ExportColumn[] };
type ExportValue = string | number | null; type ExportValue = string | number | null;
function nodeText(node: unknown): string { function nodeText(node: unknown): string {
if (typeof node === 'string' || typeof node === 'number') return String(node); if (typeof node === 'string' || typeof node === 'number') return String(node);
if (Array.isArray(node)) return node.map(nodeText).join(''); if (Array.isArray(node)) return node.map(nodeText).join('');
if (isValidElement<{ children?: unknown }>(node)) return nodeText(node.props.children); if (isValidElement<{ children?: unknown }>(node)) return `${node.type === 'small' ? '\n' : ''}${nodeText(node.props.children)}`;
return ''; return '';
} }
@@ -22,24 +23,119 @@ export function workbookColumns(columns: readonly ExportColumn[], parents: strin
}); });
} }
export async function createMetricWorkbook<T>(columns: readonly ExportColumn[], rows: readonly T[], value: (key: string, row: T) => ExportValue) { function dataColumns(columns: readonly ExportColumn[]): ExportColumn[] {
return columns.flatMap(column => {
if (column.children) {
const children = dataColumns(column.children);
return children.length ? [{ ...column, children }] : [];
}
return String(column.key ?? '') && column.key !== 'actions' ? [column] : [];
});
}
function mix(foreground: string, background: string, proportion: number): string {
return 'FF' + [1, 3, 5].map(offset => Math.round(
parseInt(foreground.slice(offset, offset + 2), 16) * proportion
+ parseInt(background.slice(offset, offset + 2), 16) * (1 - proportion),
).toString(16).padStart(2, '0')).join('').toUpperCase();
}
// Mirrors the query table's theme tokens and semantic colors. Dark always exports as light.
export function metricWorkbookPalette(themeId: WorkspaceThemeId = 'light') {
const theme = getWorkspaceTheme(themeId === 'dark' ? 'light' : themeId);
return {
ink: mix(theme.ink, theme.surface, 1), surface: mix(theme.surface, theme.surface, 1),
grid: mix(theme.metricGrid, theme.surface, 1), headGrid: mix(theme.metricGridHead, theme.surface, 1),
head: mix(theme.surface, theme.background, .55), alternate: mix(theme.surface, theme.soft, .97),
black: 'FF20252B', bright: mix(theme.primary, theme.surface, 1), combined: mix(theme.teal, theme.surface, 1),
blackHead: mix('#20252B', theme.surface, .07), brightHead: mix(theme.primary, theme.surface, .10),
combinedFill: mix(theme.teal, theme.surface, .05),
};
}
export async function createMetricWorkbook<T>(columns: readonly ExportColumn[], rows: readonly T[], value: (key: string, row: T) => ExportValue, themeId: WorkspaceThemeId = getStoredWorkspaceThemeId()) {
const { default: ExcelJS } = await import('exceljs'); const { default: ExcelJS } = await import('exceljs');
const workbook = new ExcelJS.Workbook(); const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet('质量汇总', { views: [{ state: 'frozen', xSplit: 1, ySplit: 1 }] }); workbook.creator = '质量数据分析平台';
const fields = workbookColumns(columns); const tree = dataColumns(columns);
sheet.columns = fields.map(field => ({ header: field.header, key: field.key, width: field.key === 'object' ? 38 : Math.max(18, Math.min(field.header.length * 2, 48)) })); const fields = workbookColumns(tree);
for (const row of rows) sheet.addRow(fields.map(field => { const depth = (items: readonly ExportColumn[]): number => Math.max(1, ...items.map(column => column.children ? 1 + depth(column.children) : 1));
const result = value(field.key, row); const headerRows = depth(tree);
return typeof result === 'number' && !Number.isFinite(result) ? null : result; const palette = metricWorkbookPalette(themeId);
})); const sheet = workbook.addWorksheet('质量汇总', {
fields.forEach((field, index) => { views: [{ state: 'frozen', xSplit: fields.length ? 1 : 0, ySplit: headerRows, showGridLines: false }],
if (field.key !== 'object') sheet.getColumn(index + 1).numFmt = field.rate ? '0.00%' : '#,##0.0'; properties: { defaultRowHeight: 26.25 },
pageSetup: { orientation: 'landscape', paperSize: 9, fitToPage: true, fitToWidth: 1, fitToHeight: 0 },
}); });
sheet.getRow(1).font = { bold: true, color: { argb: 'FFFFFFFF' } }; if (!fields.length) return workbook;
sheet.getRow(1).fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF6379DF' } }; const border = (argb: string) => ({
sheet.getRow(1).alignment = { vertical: 'middle', wrapText: true }; top: { style: 'thin' as const, color: { argb } }, bottom: { style: 'thin' as const, color: { argb } },
sheet.getRow(1).height = 36; left: { style: 'thin' as const, color: { argb } }, right: { style: 'thin' as const, color: { argb } },
sheet.autoFilter = { from: { row: 1, column: 1 }, to: { row: Math.max(1, rows.length + 1), column: fields.length } }; });
const solid = (argb: string) => ({ type: 'pattern' as const, pattern: 'solid' as const, fgColor: { argb } });
let leafIndex = 0;
const writeHeaders = (items: readonly ExportColumn[], level: number) => {
for (const column of items) {
const start = leafIndex + 1;
const key = String(column.key ?? '');
const isGroup = !!column.children;
if (column.children) writeHeaders(column.children, level + 1);
else {
leafIndex++;
const pixels = Number(column.width) || (key === 'object' ? 220 : 112);
sheet.getColumn(start).width = Math.max(14, Math.min(60, (pixels - 5) / 7));
sheet.getColumn(start).key = key;
sheet.getColumn(start).numFmt = key === 'object' ? '@' : fields[start - 1].rate ? '0.00%' : '#,##0.0';
}
const lastRow = isGroup ? level : headerRows;
const end = leafIndex;
const black = key === 'stage-black' || key === 'black-rate';
const bright = key === 'stage-bright' || key === 'bright-rate';
const combined = key === 'compositeRate';
const fill = combined ? palette.combinedFill : isGroup && black ? palette.blackHead : isGroup && bright ? palette.brightHead : palette.head;
const color = combined ? palette.combined : black ? palette.black : bright ? palette.bright : palette.ink;
for (let r = level; r <= lastRow; r++) for (let c = start; c <= end; c++) {
const cell = sheet.getCell(r, c);
cell.fill = solid(fill);
cell.border = border(palette.headGrid);
cell.font = { name: '微软雅黑', size: 10.5, bold: true, color: { argb: color } };
cell.alignment = { horizontal: 'center', vertical: 'middle', wrapText: true };
}
if (lastRow > level || end > start) sheet.mergeCells(level, start, lastRow, end);
const label = nodeText(column.title).trim();
sheet.getCell(level, start).value = !isGroup && key !== 'object' && !fields[start - 1].rate ? `${label}\n(kg)` : label;
}
};
writeHeaders(tree, 1);
for (let i = 1; i <= headerRows; i++) {
let lines = 1;
sheet.getRow(i).eachCell(cell => { if (typeof cell.value === 'string') lines = Math.max(lines, cell.value.split('\n').length); });
sheet.getRow(i).height = Math.max(27, lines * 16 + 6);
}
rows.forEach((row, index) => {
const exported = sheet.getRow(headerRows + index + 1);
let lines = 1;
fields.forEach((field, column) => {
const result = value(field.key, row);
const cell = exported.getCell(column + 1);
cell.value = typeof result === 'number' && !Number.isFinite(result) ? null : result;
const combined = field.key === 'compositeRate';
const color = combined ? palette.combined : field.key === 'black-rate' ? palette.black : field.key === 'bright-rate' ? palette.bright : palette.ink;
cell.font = { name: field.key === 'object' ? '微软雅黑' : 'Arial', size: combined ? 11.25 : field.key === 'object' ? 12 : 9.75, bold: field.key === 'object' || field.key.endsWith('-rate') || combined, color: { argb: color } };
cell.fill = solid(combined ? palette.combinedFill : index % 2 ? palette.alternate : palette.surface);
cell.border = border(palette.grid);
cell.alignment = { horizontal: 'center', vertical: 'middle', wrapText: true };
if (typeof result === 'string') {
const width = sheet.getColumn(column + 1).width! - 2;
const textLines = result.split(/\r?\n/).reduce((total, line) => total + Math.max(1, Math.ceil(Array.from(line).reduce((length, char) => length + (/[^\x00-\xff]/.test(char) ? 2 : 1), 0) / width)), 0);
lines = Math.max(lines, textLines);
}
});
exported.height = Math.max(26.25, lines * 16 + 8);
});
sheet.autoFilter = { from: { row: headerRows, column: 1 }, to: { row: headerRows + rows.length, column: fields.length } };
sheet.pageSetup.printTitlesRow = `1:${headerRows}`;
sheet.pageSetup.printArea = `A1:${sheet.getColumn(fields.length).letter}${headerRows + rows.length}`;
return workbook; return workbook;
} }
+7
View File
@@ -53,3 +53,10 @@
.smooth-table-frame { min-width: 0; overflow: clip; transition: height 520ms cubic-bezier(.22,.61,.36,1); } .smooth-table-frame { min-width: 0; overflow: clip; transition: height 520ms cubic-bezier(.22,.61,.36,1); }
.animated-metric-text { font-variant-numeric: tabular-nums lining-nums; } .animated-metric-text { font-variant-numeric: tabular-nums lining-nums; }
@media (prefers-reduced-motion: reduce) { .smooth-table-frame { transition: none; } } @media (prefers-reduced-motion: reduce) { .smooth-table-frame { transition: none; } }
.defect-drilldown-controls { display:flex;align-items:center;flex-wrap:wrap;gap:8px;margin:0;color:var(--ink);font-size:13px;flex:1; }
.defect-drilldown-controls > .ant-btn {padding-inline:0;}
.defect-drilldown-notice {color:var(--muted-ink);font-size:12px;margin-left:auto;}
.trend-heading-content {flex:1;align-items:center;flex-wrap:wrap;gap:6px 16px;}
.trend-heading-content h2 {flex-shrink:0;}
+13 -3
View File
@@ -1,14 +1,24 @@
import type { MetricRow, QueryParams, ViewKind } from './types'; import type { MetricRow, QueryParams, TrendMetricRow, ViewKind } from './types';
/** Freeze the clicked metric's filters and release together for every detail/export request. */ /** Freeze the clicked metric's filters and release together for every detail/export request. */
export function freezeSourceRecordsQuery(query: QueryParams, releaseId: string, view: ViewKind, row: MetricRow | null, month?: number): QueryParams { export function freezeSourceRecordsQuery(query: QueryParams, releaseId: string, view: ViewKind, row: MetricRow | null, month?: number): QueryParams {
if (!releaseId) throw new Error('指标数据尚未就绪,请重新查询后打开来源明细。'); if (!releaseId) throw new Error('指标数据尚未就绪,请重新查询后打开来源明细。');
const frozen = JSON.parse(JSON.stringify(query)) as QueryParams; const frozen = JSON.parse(JSON.stringify(query)) as QueryParams;
frozen.releaseId = releaseId; frozen.releaseId = releaseId;
if (month) { frozen.month = month; delete frozen.quarter; } const periodRow = row as TrendMetricRow | null;
const period = view === 'overall' ? String(periodRow?.period ?? periodRow?.month ?? '') : '';
const match = period.match(/^(\d{4})(?:-(?:Q([1-4])|(0?[1-9]|1[0-2])))?$/);
if (match || month) {
delete frozen.month; delete frozen.quarter; delete frozen.monthStart; delete frozen.monthEnd;
if (match) {
frozen.year = Number(match[1]);
if (match[2]) frozen.quarter = Number(match[2]);
if (match[3]) frozen.month = Number(match[3]);
} else if (month) frozen.month = month;
}
if (row && view !== 'overall') { if (row && view !== 'overall') {
const dimension = view === 'projectGroup' ? 'projectGroup' : view; const dimension = view === 'projectGroup' ? 'projectGroup' : view;
frozen[dimension] = row.key === '__UNASSIGNED__' ? '__UNASSIGNED__' : row[dimension] || row.key; frozen[dimension] = row.key === '__UNASSIGNED__' || !row[dimension] && !row.key?.trim() ? '__UNASSIGNED__' : row[dimension] || row.key;
} }
return frozen; return frozen;
} }
+11
View File
@@ -62,6 +62,17 @@ export interface FineDefectTopResponse {
notice?: string; notice?: string;
} }
export interface FineDefectCatalogResponse {
categories: Array<{stage:Stage;category:string;available:boolean;reason?:string}>;
fields: Array<{stage:Stage;key:string;label:string;category:string;available?:boolean}>;
}
export interface DefectDrilldownResponse {
releaseId:string;stage:Stage;category:string;dimension:string;granularity:string;
fields:Array<{key:string;label:string;category:string}>;
groups:Array<{key:string;totalWeight:number|null;parentWeight:number|null;parentRate:number|null;reconciliationStatus:string;
children:Array<{key:string;weight:number|null;rate:number|null}>}>;
}
export interface StageMetric { export interface StageMetric {
qualifiedWeight: number | null; qualifiedWeight: number | null;
scrapWeight: number | null; scrapWeight: number | null;
+64
View File
@@ -0,0 +1,64 @@
import { useEffect, useMemo, useRef, useState } from 'react';
import { Alert, Button, Spin } from 'antd';
import { api } from './api';
import { defectScope } from './defectDrilldown';
import type { DefectDrilldownResponse, FineDefectCatalogResponse, QueryParams, Stage } from './types';
export function useDefectDrilldown({active,mode,stage,query,trendQuery,tableQuery,tableDimension,tableGranularity,onUnauthorized,display,onDisplay,refreshKey=0}:{
refreshKey?:string|number;active:boolean;mode:string;stage:Stage;query:QueryParams;trendQuery:QueryParams;tableQuery:QueryParams;
tableDimension:string;tableGranularity:string;onUnauthorized:(error:unknown)=>boolean;display:'weight'|'rate';onDisplay:(display:'weight'|'rate')=>void;
}) {
const [selection,setSelection]=useState<{stage:Stage;category:string}|null>(null);
const [catalogFailure,setCatalogFailure]=useState('');
const [catalog,setCatalog]=useState<FineDefectCatalogResponse|null>(null);
const [revision,setRevision]=useState(0);
const previousDisplay=useRef(display);
const enabled=active&&mode==='defects';
const category=mode==='defects'&&selection?.stage===stage?selection.category:null;
const catalogKey=JSON.stringify([enabled,stage,defectScope(query),revision,refreshKey]);
const common={stage,category,releaseId:query.releaseId};
const trend=useDefectPart(enabled&&!!category&&!!query.releaseId,{...defectScope(trendQuery),...common,dimension:'period',trendGranularity:trendQuery.trendGranularity||'month'},[revision,refreshKey],onUnauthorized);
const table=useDefectPart(enabled&&!!category&&!!query.releaseId,{...defectScope(tableQuery),...common,dimension:tableDimension,trendGranularity:tableGranularity},[revision,refreshKey],onUnauthorized);
useEffect(()=>{
if(!enabled||!query.releaseId)return;
const controller=new AbortController();setCatalog(null);setCatalogFailure('');
api.fineDefectCatalog({...defectScope(query),stage,mode:'catalog'},controller.signal)
.then(value=>{if(!controller.signal.aborted)setCatalog(value);})
.catch(error=>{if(!controller.signal.aborted&&!onUnauthorized(error))setCatalogFailure(error instanceof Error?error.message:'细分目录读取失败');});
return()=>controller.abort();
},[catalogKey,onUnauthorized]);
useEffect(()=>{
if(selection&&(selection.stage!==stage||mode!=='defects')){setSelection(null);onDisplay(previousDisplay.current);}
},[stage,mode]);
const error=trend.error||table.error;
const pending=trend.pending||table.pending;
const categories=useMemo(()=>(catalog?.categories||[]).filter(item=>item.stage===stage&&item.available&&catalog?.fields.some(field=>field.stage===stage&&field.category===item.category&&field.available!==false)).map(item=>item.category),[catalog,stage]);
const choose=(next:string)=>{if(!categories.includes(next))return;if(!category)previousDisplay.current=display;setSelection({stage,category:next});onDisplay('rate');};
const clear=()=>{setSelection(null);onDisplay(previousDisplay.current);};
const incomplete=[trend.data,table.data].some(data=>data?.groups.some(group=>group.reconciliationStatus!=='RECONCILED'));
const retry=<Button size="small" onClick={()=>setRevision(v=>v+1)}>重试</Button>;
const controls=category?<div className="defect-drilldown-controls"><Button type="link" onClick={clear}>全部分类</Button><span>› {category}</span>{trend.pending&&<span><Spin size="small"/> 正在读取细分</span>}{trend.error&&<Alert type="error" showIcon message={trend.error} action={retry}/>} {incomplete&&<span className="defect-drilldown-notice">细分来源未完全覆盖分类总量</span>}</div>:catalogFailure&&enabled?<Alert type="error" showIcon message="细分目录读取失败" description={catalogFailure} action={<Button size="small" onClick={()=>setRevision(v=>v+1)}>重试</Button>}/>:null;
const tableControls=table.error?<Alert type="error" showIcon message={table.error} action={retry}/>:null;
return {category,categories,choose,clear,controls,tableControls,pending,error,trend:trend.data,table:table.data,trendPending:trend.pending,tablePending:table.pending,trendError:trend.error,tableError:table.error};
}
/** Keep the small trend independent from the potentially large object table. */
function useDefectPart(enabled:boolean,params:Record<string,unknown>,refresh:unknown[],onUnauthorized:(error:unknown)=>boolean) {
const key=JSON.stringify([params,refresh]);
const [result,setResult]=useState<{key:string;data?:DefectDrilldownResponse;error?:string}|null>(null);
useEffect(()=>{
if(!enabled){setResult(null);return;}
const controller=new AbortController();
api.defectDrilldown(params,controller.signal).then(data=>{
if(controller.signal.aborted)return;
if(data.releaseId!==params.releaseId||data.stage!==params.stage||data.category!==params.category)throw new Error('细分查询范围已变化,请重试。');
setResult({key,data});
}).catch(error=>{
if(!controller.signal.aborted&&!onUnauthorized(error))setResult({key,error:error instanceof Error?error.message:'细分查询失败'});
});
return()=>controller.abort();
},[enabled,key,onUnauthorized]);
const current=enabled&&result?.key===key?result:null;
return {data:current?.data,error:current?.error||'',pending:enabled&&!current};
}
@@ -0,0 +1,92 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import ts from 'typescript';
import React from 'react';
import { defectScope,defectCell,defectRateLabel } from '../src/defectDrilldown.ts';
const read=name=>fs.readFileSync(new URL(`../src/${name}`,import.meta.url),'utf8');
const transpile=code=>ts.transpileModule(code,{compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText;
const deferred=()=>{let resolve,reject;const promise=new Promise((r,j)=>{resolve=r;reject=j;});return {promise,resolve,reject};};
const tick=()=>new Promise(resolve=>setImmediate(resolve));
function fixture() {
const slots=[],effects=[],requests=[],catalog=[];let index=0,dirty=false,queue=[],output;
const props={active:true,mode:'defects',stage:'bright',query:{year:2026,view:'product',releaseId:'r',product:'P'},trendQuery:{year:2026,view:'product',releaseId:'r',product:'P',trendGranularity:'month'},tableQuery:{year:2026,view:'product',releaseId:'r',product:'P'},tableDimension:'product',tableGranularity:'month',onUnauthorized:()=>false,refreshKey:0,display:'weight',onDisplay:v=>props.display=v};
const hooks={
useState(initial){const n=index++;if(!(n in slots))slots[n]=initial;return [slots[n],v=>{const next=typeof v==='function'?v(slots[n]):v;if(!Object.is(slots[n],next)){slots[n]=next;dirty=true;}}];},
useRef(initial){const n=index++;return slots[n]??(slots[n]={current:initial});},
useMemo(fn){index++;return fn();},
useEffect(fn,deps){const n=index++,old=effects[n];if(!old||deps.some((d,i)=>!Object.is(d,old.deps[i])))queue.push(()=>{old?.cleanup?.();effects[n]={deps,cleanup:fn()};});},
};
const api={fineDefectCatalog(params,signal){const d=deferred();catalog.push({...d,params,signal});return d.promise;},defectDrilldown(params,signal){const d=deferred();requests.push({...d,params,signal});return d.promise;}};
const code=read('useDefectDrilldown.tsx').slice(read('useDefectDrilldown.tsx').indexOf('export function')).replace('export function','function');
const hook=new Function('React','hooks','api','defectScope',transpile(`const {useState,useRef,useEffect,useMemo}=hooks;const Alert='Alert',Button='Button',Spin='Spin';${code};return useDefectDrilldown;`))(React,hooks,api,defectScope);
return {props,requests,catalog,render(){let count=0;do{dirty=false;index=0;queue=[];output=hook(props);queue.forEach(fn=>fn());assert.ok(++count<15);}while(dirty);return output;},unmount(){effects.forEach(e=>e?.cleanup?.());}};
}
const catalogData={categories:[{stage:'bright',category:'加工废',available:true},{stage:'bright',category:'外废',available:false}],fields:[{stage:'bright',key:'加工缺角重量',label:'加工缺角重量',category:'加工废',available:true}]};
const response={releaseId:'r',stage:'bright',category:'加工废',fields:[],groups:[]};
test('drilldown batches trend and object queries, ignores late old-year replies, keeps the category across years and restores display on return',async()=>{
const f=fixture();f.render();f.catalog[0].resolve(catalogData);await tick();let ui=f.render();
ui.choose('外废');assert.equal(f.render().category,null);
ui.choose('加工废');ui=f.render();assert.equal(f.props.display,'rate');assert.equal(ui.pending,true);assert.equal(f.requests.length,2);
assert.equal(f.requests[0].params.dimension,'period');assert.equal(f.requests[1].params.dimension,'product');
f.props.query={...f.props.query,year:2025};f.props.trendQuery={...f.props.trendQuery,year:2025};f.props.tableQuery={...f.props.tableQuery,year:2025};
ui=f.render();assert.equal(ui.category,'加工废');assert.equal(f.requests[0].signal.aborted,true);
f.requests[0].resolve(response);f.requests[1].resolve(response);await tick();assert.equal(f.render().table,undefined);
f.requests[2].resolve({...response,dimension:'period'});f.requests[3].resolve({...response,dimension:'product'});await tick();ui=f.render();assert.equal(ui.pending,false);assert.equal(ui.table.dimension,'product');
f.props.refreshKey=1;ui=f.render();assert.equal(ui.pending,true);assert.equal(ui.table,undefined);assert.equal(f.requests.length,6);
ui.clear();assert.equal(f.render().category,null);assert.equal(f.props.display,'weight');f.unmount();
});
test('current failures expose retry without presenting old-scope data as the selected result; stage switch exits drilldown',async()=>{
const f=fixture();f.render();f.catalog[0].resolve(catalogData);await tick();f.render().choose('加工废');f.render();
f.requests[0].reject(Error('offline'));await tick();let ui=f.render();assert.equal(ui.error,'offline');assert.equal(ui.table,undefined);
f.props.stage='black';assert.equal(f.render().category,null);assert.equal(f.props.display,'weight');f.unmount();
});
test('defect curves and x-axis never select a month; only available category curves drill, and production clicks keep month selection',()=>{
const source=read('QueryPage.tsx'),start=source.indexOf(" instance.on('click', params => {",source.indexOf('export function TrendChart'));
const code=source.slice(start,source.indexOf(' const observer =',start));
const clicked=[],months=[];let handler;
const modeRef={current:'defects'},drillRef={current:{drilled:false,drillableCategories:['加工废'],onDefect:v=>clicked.push(v)}};
new Function('instance','modeRef','drillRef','rowsRef','onMonthRef','setLineSelectedPeriod',transpile(code))({on:(_,fn)=>handler=fn},modeRef,drillRef,{current:[{month:'2026-02'}]},{current:v=>months.push(v)},()=>{});
handler({componentType:'series',seriesType:'line',seriesName:'加工废',dataIndex:0});handler({componentType:'xAxis',dataIndex:0});handler({componentType:'series',seriesName:'外废',dataIndex:0});
assert.deepEqual(clicked,['加工废']);assert.deepEqual(months,[]);
drillRef.current.drilled=true;handler({componentType:'series',seriesName:'加工废',dataIndex:0});assert.equal(clicked.length,1);
modeRef.current='production';handler({componentType:'series',seriesType:'line',dataIndex:0});assert.deepEqual(months,['2026-02']);
});
test('period keys, unassigned source keys and missing values preserve their exact meaning',()=>{
const data={dimension:'period',groups:[{key:'2025-Q2',children:[{key:'缺角重量',rate:0,weight:0}]}]};
assert.equal(defectCell(data,{period:'2025-Q2'},'缺角重量','rate'),0);assert.equal(defectCell(data,{period:'2026-Q2'},'缺角重量','rate'),null);
assert.equal(defectRateLabel('加工缺角重量'),'加工缺角废率');assert.equal(defectRateLabel('加工废'),'加工废率');
assert.deepEqual(defectScope({year:2026,view:'product',trendGranularity:'month',columnFilters:[{field:'a'}]}),{year:2026,columnFilters:[{field:'a'}]});
});
test('empty and NULL product groups keep separate formal denominators',()=>{
const data={dimension:'product',groups:[{key:'',children:[{key:'x',rate:.04}]},{key:'__UNASSIGNED__',children:[{key:'x',rate:.01}]}]};
assert.equal(defectCell(data,{key:'display-fallback',sourceMetricKey:''},'x','rate'),.04);
assert.equal(defectCell(data,{key:'__UNASSIGNED__'},'x','rate'),.01);
});
test('new fine values cannot be overlaid on retained rows from a previous formal query',()=>{
for(const file of ['QueryPage.tsx','StageProductPage.tsx']) {
const code=read(file),expression=code.match(/<MetricTable[^]*?defectDrilldown=\{([^}]+)\}/)?.[1]||code.match(/defectDrilldown=\{(tableReady[^}]+)\}/)[1];
const bind=new Function('tableReady','queryBusy','defectDrill',`return ${expression}`),data={table:{groups:[]}};
assert.equal(bind(false,false,data),undefined);assert.equal(bind(true,true,data),undefined);assert.equal(bind(true,false,data),data.table);
}
});
test('fast trend renders without waiting for the slow product table, and table failure cannot hide it',async()=>{
const f=fixture();f.render();f.catalog[0].resolve(catalogData);await tick();f.render().choose('加工废');f.render();
f.requests[0].resolve({...response,dimension:'period'});await tick();let ui=f.render();
assert.equal(ui.trend?.dimension,'period','the completed trend must not wait for every product');
assert.equal(ui.trendPending,false);assert.equal(ui.tablePending,true);assert.equal(ui.table,undefined);
f.requests[1].reject(Error('table offline'));await tick();ui=f.render();
assert.equal(ui.trend?.dimension,'period');assert.equal(ui.trendError,'');assert.equal(ui.tableError,'table offline');
f.unmount();
});
test('changing only the trend object does not recalculate the unchanged product table',async()=>{
const f=fixture();f.render();f.catalog[0].resolve(catalogData);await tick();f.render().choose('加工废');f.render();
f.requests[0].resolve({...response,dimension:'period'});f.requests[1].resolve({...response,dimension:'product'});await tick();f.render();
f.props.trendQuery={...f.props.trendQuery,product:'P2'};let ui=f.render();
assert.equal(f.requests.length,3);assert.equal(f.requests[2].params.dimension,'period');assert.equal(ui.table?.dimension,'product');
assert.equal(ui.trend,undefined);assert.equal(ui.trendPending,true);f.unmount();
});
@@ -0,0 +1,31 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import ts from 'typescript';
const read=name=>fs.readFileSync(new URL(`../src/${name}`,import.meta.url),'utf8');
function fixture(code) {
let dependencies,selected='';
const useEffect=(fn,deps)=>{if(!dependencies||deps.some((v,i)=>!Object.is(v,dependencies[i]))){dependencies=deps;fn();}};
const render=new Function('input','useEffect','setDrilledProduct',ts.transpileModule(`
const {year,view,department,group,product,projectGroup,extraFilters,includeUnassigned,sourceFilters}=input;
${code}
`,{compilerOptions:{target:ts.ScriptTarget.ES2022}}).outputText);
return {render:input=>render(input,useEffect,v=>selected=v),drill:()=>selected='产品A',get selected(){return selected;}};
}
const base={year:2026,view:'product',department:[],group:[],product:[],projectGroup:[],extraFilters:{},sourceFilters:[],includeUnassigned:true};
test('product summary keeps drilldown when only the year changes and resets it for object-scope changes',()=>{
const source=read('QueryPage.tsx'),start=source.indexOf(' const drilldownContextKey =');
const code=source.slice(start,source.indexOf(' const metricParams',start));
const ui=fixture(code);ui.render(base);ui.drill();ui.render({...base,year:2025});
assert.equal(ui.selected,'产品A');
ui.render({...base,year:2025,group:['另一业务组']});assert.equal(ui.selected,'');
});
test('both single-stage pages keep drilldown across years while organization/source changes reset it',()=>{
const code=read('StageProductPage.tsx').match(/useEffect\(\(\)=>\{setDrilledProduct\(''\);\},\[[^\]]+\]\);/)[0];
const ui=fixture(code);ui.render(base);ui.drill();ui.render({...base,year:2025});
assert.equal(ui.selected,'产品A');
ui.render({...base,year:2025,sourceFilters:[{field:'customer'}]});assert.equal(ui.selected,'');
});
@@ -101,6 +101,39 @@ test('the actual summary click stores release A and filters until a new detail i
ui.openRecords(null,'black');assert.equal(ui.get().releaseId,'release-B');assert.equal(ui.get().month,2); ui.openRecords(null,'black');assert.equal(ui.get().releaseId,'release-B');assert.equal(ui.get().month,2);
}); });
test('product monthly drilldown keeps the product and clicked month for source requests and export',async()=>{
const source=read('QueryPage.tsx'),start=source.indexOf(' const openRecords = useCallback');
const callback=source.slice(start,source.indexOf(' const summaryMetricsError',start));
const query={year:2026,quarter:1,view:'product',department:'产业部',group:'业务分组',product:'18X15X4+T',extraFilters:{customer:['001']}};
const ui=new Function('freezeSourceRecordsQuery','query',transpile(`
const summaryMetricsData={release:{id:'release-A'}},queryParams={...query,product:undefined},defectQueryParams=query;
const drilledProduct='18X15X4+T',view='product',message={info(){}},useCallback=fn=>fn;
let snapshot;
const setRecordQuerySnapshot=value=>snapshot=value,setSelectedRecord=()=>{},setRecordStage=()=>{},setRecordMonth=()=>{},setRecordsOpen=()=>{};
${callback}
return {openRecords,get:()=>snapshot};
`))(freezeSourceRecordsQuery,query);
for(const month of [1,2]) {
const key=`2026-0${month}`;
ui.openRecords({key,label:key,month:key,product:null},'black',month);
const frozen=ui.get();
assert.equal(frozen.product,'18X15X4+T');assert.equal(frozen.month,month);assert.equal(frozen.quarter,undefined);
assert.equal(frozen.department,query.department);assert.equal(frozen.group,query.group);
assert.deepEqual(frozen.extraFilters,query.extraFilters);assert.equal(frozen.releaseId,'release-A');
const drawer=drawerFixture();drawer.props.query=frozen;
for(const stage of ['black','bright']) {
drawer.props.stage=stage;drawer.render();const request=drawer.requests.at(-1);
assert.equal(request.params.product,'18X15X4+T');assert.equal(request.params.month,month);assert.equal(request.params.stage,stage);
request.resolve({rows:[{id:stage,raw:{规格:'18X15X4+T'}}],total:1,pageSize:20,page:1});await tick();
}
const tree=drawer.render();
await find(tree,n=>n.type==='Button'&&textOf(n)==='导出明细 Excel').props.onClick();
assert.equal(drawer.exports[0].params.product,'18X15X4+T');assert.equal(drawer.exports[0].params.month,month);
assert.equal(drawer.exports[0].params.releaseId,'release-A');drawer.unmount();
}
assert.equal(query.quarter,1);assert.equal(query.month,undefined);
});
function drawerFixture() { function drawerFixture() {
const source=read('QueryPage.tsx');const code=source.slice(source.indexOf('function RecordsDrawer(')).replace(/export \{ fullYearTrend \};/, ''); const source=read('QueryPage.tsx');const code=source.slice(source.indexOf('function RecordsDrawer(')).replace(/export \{ fullYearTrend \};/, '');
const requests=[],exports=[];let downloaded=0,refreshKey=0; const requests=[],exports=[];let downloaded=0,refreshKey=0;
@@ -119,6 +152,26 @@ function drawerFixture() {
return {requests,exports,props,render:()=>renderer.render(props),unmount:()=>renderer.unmount(),invalidate(){refreshKey++;},get downloaded(){return downloaded;}}; return {requests,exports,props,render:()=>renderer.render(props),unmount:()=>renderer.unmount(),invalidate(){refreshKey++;},get downloaded(){return downloaded;}};
} }
test('single-stage detail callback freezes the clicked quarterly/yearly row instead of selecting unassigned product',()=>{
const source=read('StageProductPage.tsx'),start=source.indexOf(' const openDetail=');
const callback=source.slice(start,source.indexOf(' const runExport=',start));
for(const stage of ['black','bright']) {
const ui=new Function('freezeSourceRecordsQuery','stage',transpile(`
const applied={year:2026,month:10,product:'产品A',stage,columnFilters:[{field:'customer',stages:[stage],operator:'in',values:['001']}]};
const current={release:{id:'release-A'}},drilledProduct='产品A';let records;
const setRecordVisible=()=>{},setRecords=value=>records=value;
${callback}
return {openDetail,get:()=>records};
`))(freezeSourceRecordsQuery,stage);
for(const period of ['2026-Q2','2025']) {
ui.openDetail({key:period,month:period,period,product:null},stage);
const query=ui.get().query;assert.equal(query.product,'产品A');assert.equal(query.month,undefined);
assert.equal(query.year,period==='2025'?2025:2026);assert.equal(query.quarter,period==='2025'?undefined:2);
assert.equal(query.columnFilters[0].stages[0],stage);assert.equal(query.releaseId,'release-A');
}
}
});
test('actual drawer retains prior rows without enabling stale actions and retries the same frozen release',async()=>{ test('actual drawer retains prior rows without enabling stale actions and retries the same frozen release',async()=>{
const ui=drawerFixture();ui.render();assert.equal(ui.requests[0].params.page,1);assert.equal(ui.requests[0].params.releaseId,'release-A'); const ui=drawerFixture();ui.render();assert.equal(ui.requests[0].params.page,1);assert.equal(ui.requests[0].params.releaseId,'release-A');
ui.requests[0].resolve({rows:[{id:'A-1',raw:{field:'value'}}],total:45,pageSize:20,page:1});await tick();let tree=ui.render(); ui.requests[0].resolve({rows:[{id:'A-1',raw:{field:'value'}}],total:45,pageSize:20,page:1});await tick();let tree=ui.render();
+121 -27
View File
@@ -6,7 +6,21 @@ import React from 'react';
import { queryScrapWeight } from '../src/queryMetricValues.ts'; import { queryScrapWeight } from '../src/queryMetricValues.ts';
import { defectWeightRate } from '../src/trendMetrics.ts'; import { defectWeightRate } from '../src/trendMetrics.ts';
import { metricStageColumnPlan } from '../src/metricColumnPlan.ts'; import { metricStageColumnPlan } from '../src/metricColumnPlan.ts';
import { createMetricWorkbook, workbookColumns } from '../src/metricWorkbook.ts'; import { createMetricWorkbook, metricWorkbookPalette, workbookColumns } from '../src/metricWorkbook.ts';
import { defectCell, defectRateLabel as fineDefectRateLabel } from '../src/defectDrilldown.ts';
import { freezeSourceRecordsQuery } from '../src/sourceRecordsContext.ts';
test('drilled period labels and source actions preserve quarter and year instead of interpreting them as months',async()=>{
for(const [period,label,expected] of [['2026-Q2','第2季度',{year:2026,quarter:2}],['2025','2025年',{year:2025}]]) {
const data={...row,key:period,label:period,product:null,month:period,period,periodLabel:label,granularity:period.includes('Q')?'quarter':'year'};
let detail;
const ui=fixture({rows:[data],view:'overall',period:'year',onDetail:(r,s,m)=>detail=freezeSourceRecordsQuery({year:2026,month:10,product:'产品A'},'release','overall',r,m)});
const tableNode=find(ui.tree,n=>n.type==='Table');
tableNode.props.columns.find(c=>c.key==='actions').render(null,data).props.onClick();
assert.equal(detail.year,expected.year);assert.equal(detail.quarter,expected.quarter);assert.equal(detail.month,undefined);assert.equal(detail.product,'产品A');
await ui.ref.exportExcel();assert.equal(ui.exported.workbook.getWorksheet('质量汇总').getCell('A3').value,label);
}
});
const source = fs.readFileSync(new URL('../src/QueryPage.tsx', import.meta.url), 'utf8'); const source = fs.readFileSync(new URL('../src/QueryPage.tsx', import.meta.url), 'utf8');
const slice = (start, end) => source.slice(source.indexOf(start), source.indexOf(end, source.indexOf(start))); const slice = (start, end) => source.slice(source.indexOf(start), source.indexOf(end, source.indexOf(start)));
@@ -18,13 +32,13 @@ const table = slice('export function MetricTable(', 'function MetricTablePaginat
function fixture(props) { function fixture(props) {
let exported, detail = 0, basis = 0; let exported, detail = 0, basis = 0;
const ref = {}; const ref = {};
const render = new Function('React', 'useImperativeHandle', 'defectWeightRate', 'queryScrapWeight', 'metricStageColumnPlan', 'downloadMetricWorkbook', ts.transpileModule(` const render = new Function('defectCell','fineDefectRateLabel','React', 'useImperativeHandle', 'defectWeightRate', 'queryScrapWeight', 'metricStageColumnPlan', 'downloadMetricWorkbook', ts.transpileModule(`
const MONTHS=Array.from({length:12},(_,i)=>String(i+1).padStart(2,'0')+'月'); const MONTHS=Array.from({length:12},(_,i)=>String(i+1).padStart(2,'0')+'月');
const SUMMARY_PAGE_SIZE=15; const SUMMARY_PAGE_SIZE=15;
const useCompactTable=()=>false, useState=v=>[v,()=>{}], useEffect=()=>{}; const useCompactTable=()=>false, useState=v=>[v,()=>{}], useEffect=()=>{};
const AnimatedMetricText='AnimatedMetricText', Button='Button', Table='Table', MetricTablePagination='Pagination', Tooltip='Tooltip', InfoCircleOutlined='Info'; const AnimatedMetricText='AnimatedMetricText', Button='Button', Table='Table', MetricTablePagination='Pagination', Tooltip='Tooltip', InfoCircleOutlined='Info';
${helpers}\n${table}\nreturn MetricTable; ${helpers}\n${table}\nreturn MetricTable;
`, {compilerOptions:{jsx:ts.JsxEmit.React,target:ts.ScriptTarget.ES2022}}).outputText)(React, (target, create) => Object.assign(target, create()), defectWeightRate, queryScrapWeight, metricStageColumnPlan, async (filename, columns, rows, value) => { `, {compilerOptions:{jsx:ts.JsxEmit.React,target:ts.ScriptTarget.ES2022}}).outputText)(defectCell,fineDefectRateLabel,React, (target, create) => Object.assign(target, create()), defectWeightRate, queryScrapWeight, metricStageColumnPlan, async (filename, columns, rows, value) => {
exported = { filename, columns, rows, workbook: await createMetricWorkbook(columns, rows, value) }; exported = { filename, columns, rows, workbook: await createMetricWorkbook(columns, rows, value) };
}); });
const tree = render({ref,exportFilename:'验收',period:'month',month:9,quarter:3,rateBasis:'original',fullFields:false,onDetail(){detail++;},onBasis(){basis++;},onDrilldown(){},...props}); const tree = render({ref,exportFilename:'验收',period:'month',month:9,quarter:3,rateBasis:'original',fullFields:false,onDetail(){detail++;},onBasis(){basis++;},onDrilldown(){},...props});
@@ -48,11 +62,14 @@ for (const view of ['overall','department','group','product','projectGroup']) {
const {workbook,columns}=ui.exported; const {workbook,columns}=ui.exported;
const fields=workbookColumns(columns), sheet=workbook.getWorksheet('质量汇总'); const fields=workbookColumns(columns), sheet=workbook.getWorksheet('质量汇总');
if(view==='product')assert.equal(find(columns.find(c=>c.key==='object').render(null,row),n=>n.type==='small'),undefined); if(view==='product')assert.equal(find(columns.find(c=>c.key==='object').render(null,row),n=>n.type==='small'),undefined);
assert.equal(sheet.rowCount,19);assert.equal(sheet.columnCount,fullFields?13:10); assert.equal(sheet.rowCount,20);assert.equal(sheet.columnCount,fullFields?13:10);
assert.deepEqual(sheet.getRow(1).values.slice(1),fields.map(v=>v.header)); assert.equal(sheet.getCell('A1').value,fields[0].header);
assert.equal(sheet.getCell('B1').value,'黑片');
assert.equal(sheet.getCell('B2').value,'总重量\n(kg)');
assert.equal(sheet.views[0].ySplit,2);
const at=key=>fields.findIndex(field=>field.key===key)+1; const at=key=>fields.findIndex(field=>field.key===key)+1;
assert.equal(sheet.getRow(2).getCell(at('black-qualifiedWeight')).value,1234.567); assert.equal(sheet.getRow(3).getCell(at('black-qualifiedWeight')).value,1234.567);
assert.equal(sheet.getRow(2).getCell(at('black-totalWeight')).value,1250); assert.equal(sheet.getRow(3).getCell(at('black-totalWeight')).value,1250);
assert.ok(at('black-totalWeight')<at('black-qualifiedWeight')); assert.ok(at('black-totalWeight')<at('black-qualifiedWeight'));
assert.ok(at('bright-totalWeight')<at('bright-qualifiedWeight')); assert.ok(at('bright-totalWeight')<at('bright-qualifiedWeight'));
assert.equal(fields.at(-1).key,'compositeRate'); assert.equal(fields.at(-1).key,'compositeRate');
@@ -69,8 +86,8 @@ for (const view of ['overall','department','group','product','projectGroup']) {
action.props.onClick();assert.equal(ui.detail,1); action.props.onClick();assert.equal(ui.detail,1);
const reread=await createMetricWorkbook([],[],()=>null); const reread=await createMetricWorkbook([],[],()=>null);
await reread.xlsx.load(await workbook.xlsx.writeBuffer()); await reread.xlsx.load(await workbook.xlsx.writeBuffer());
assert.equal(reread.getWorksheet('质量汇总').rowCount,19); assert.equal(reread.getWorksheet('质量汇总').rowCount,20);
if(view==='product')assert.equal(reread.getWorksheet('质量汇总').getCell('A2').value,'=1+2'); if(view==='product')assert.equal(reread.getWorksheet('质量汇总').getCell('A3').value,'=1+2');
}); });
} }
@@ -78,19 +95,19 @@ test('adjusted rates, missing stage and zero weight retain their meaning', async
const ui=fixture({rows:[row,{...row,key:'missing',black:{...stage,qualifiedWeight:null,rate:null},coverage:{blackRows:0,brightRows:1}}],view:'product',rateBasis:'withoutHeadquartersScrap'}); const ui=fixture({rows:[row,{...row,key:'missing',black:{...stage,qualifiedWeight:null,rate:null},coverage:{blackRows:0,brightRows:1}}],view:'product',rateBasis:'withoutHeadquartersScrap'});
await ui.ref.exportExcel();const sheet=ui.exported.workbook.getWorksheet('质量汇总'); await ui.ref.exportExcel();const sheet=ui.exported.workbook.getWorksheet('质量汇总');
const fields=workbookColumns(ui.exported.columns), at=key=>fields.findIndex(field=>field.key===key)+1; const fields=workbookColumns(ui.exported.columns), at=key=>fields.findIndex(field=>field.key===key)+1;
assert.equal(sheet.getRow(2).getCell(at('compositeRate')).value,0.98); assert.equal(sheet.getRow(3).getCell(at('compositeRate')).value,0.98);
assert.equal(sheet.getRow(2).getCell(at('bright-rate')).value,0.99); assert.equal(sheet.getRow(3).getCell(at('bright-rate')).value,0.99);
assert.equal(sheet.getRow(3).getCell(at('black-rate')).value,null); assert.equal(sheet.getRow(4).getCell(at('black-rate')).value,null);
assert.equal(sheet.getRow(3).getCell(at('black-qualifiedWeight')).value,null); assert.equal(sheet.getRow(4).getCell(at('black-qualifiedWeight')).value,null);
assert.equal(sheet.getRow(2).getCell(at('black-scrapWeight')).value,0); assert.equal(sheet.getRow(3).getCell(at('black-scrapWeight')).value,0);
}); });
test('overall yearly export preserves month labels and leaves unavailable values blank', async () => { test('overall yearly export preserves month labels and leaves unavailable values blank', async () => {
const ui=fixture({rows:[{...row,month:2},{...row,key:'missing',month:3,noData:true,black:{...stage,qualifiedWeight:null,rate:null},bright:{...stage,qualifiedWeight:null,rate:null},coverage:{blackRows:0,brightRows:0}}],view:'overall',period:'year'}); const ui=fixture({rows:[{...row,month:2},{...row,key:'missing',month:3,noData:true,black:{...stage,qualifiedWeight:null,rate:null},bright:{...stage,qualifiedWeight:null,rate:null},coverage:{blackRows:0,brightRows:0}}],view:'overall',period:'year'});
await ui.ref.exportExcel();const sheet=ui.exported.workbook.getWorksheet('质量汇总'); await ui.ref.exportExcel();const sheet=ui.exported.workbook.getWorksheet('质量汇总');
assert.equal(sheet.getCell('A2').value,'02月');assert.equal(sheet.getCell('A3').value,'03月'); assert.equal(sheet.getCell('A3').value,'02月');assert.equal(sheet.getCell('A4').value,'03月');
const composite=workbookColumns(ui.exported.columns).findIndex(field=>field.key==='compositeRate')+1; const composite=workbookColumns(ui.exported.columns).findIndex(field=>field.key==='compositeRate')+1;
assert.equal(sheet.getRow(3).getCell(composite).value,null); assert.equal(sheet.getRow(4).getCell(composite).value,null);
}); });
for (const view of ['overall','department','group','product','projectGroup']) test(`${view}: defect-rate mode uses same-stage total and exports numeric percentages`,async()=>{ for (const view of ['overall','department','group','product','projectGroup']) test(`${view}: defect-rate mode uses same-stage total and exports numeric percentages`,async()=>{
@@ -99,20 +116,20 @@ for (const view of ['overall','department','group','product','projectGroup']) te
await ui.ref.exportExcel(); await ui.ref.exportExcel();
const fields=workbookColumns(ui.exported.columns),sheet=ui.exported.workbook.getWorksheet('质量汇总'); const fields=workbookColumns(ui.exported.columns),sheet=ui.exported.workbook.getWorksheet('质量汇总');
const at=key=>fields.findIndex(field=>field.key===key)+1; const at=key=>fields.findIndex(field=>field.key===key)+1;
assert.equal(sheet.getRow(2).getCell(at('black-defect-责废')).value,.12); assert.equal(sheet.getRow(3).getCell(at('black-defect-责废')).value,.12);
assert.equal(sheet.getRow(2).getCell(at('bright-defect-责废')).value,.04); assert.equal(sheet.getRow(3).getCell(at('bright-defect-责废')).value,.04);
assert.equal(sheet.getRow(2).getCell(at('black-scrapWeight')).value,.2); assert.equal(sheet.getRow(3).getCell(at('black-scrapWeight')).value,.2);
assert.equal(sheet.getRow(2).getCell(at('bright-headquartersScrapWeight')).value,.015); assert.equal(sheet.getRow(3).getCell(at('bright-headquartersScrapWeight')).value,.015);
assert.equal(sheet.getColumn(at('black-defect-责废')).numFmt,'0.00%'); assert.equal(sheet.getColumn(at('black-defect-责废')).numFmt,'0.00%');
assert.equal(fields[at('black-defect-责废')-1].header,'黑片 / 责废率'); assert.equal(fields[at('black-defect-责废')-1].header,'黑片 / 责废率');
assert.equal(fields[at('bright-headquartersScrapWeight')-1].header,'亮片 / 本部报废率'); assert.equal(fields[at('bright-headquartersScrapWeight')-1].header,'亮片 / 本部报废率');
assert.match(fields[at('black-totalWeight')-1].header,/总重量 \/ kg$/); assert.match(fields[at('black-totalWeight')-1].header,/总重量 \/ kg$/);
assert.equal(sheet.getRow(2).getCell(at('black-totalWeight')).value,1000); assert.equal(sheet.getRow(3).getCell(at('black-totalWeight')).value,1000);
const rendered=ui.exported.columns.flatMap(column=>column.children||[column]).find(column=>column.key==='black-defect-责废').render(null,data); const rendered=ui.exported.columns.flatMap(column=>column.children||[column]).find(column=>column.key==='black-defect-责废').render(null,data);
assert.equal(rendered.props.children.props.value,'12.00%'); assert.equal(rendered.props.children.props.value,'12.00%');
const restored=fixture({rows:[data],view,fullFields:true,defectDisplay:'weight'});await restored.ref.exportExcel(); const restored=fixture({rows:[data],view,fullFields:true,defectDisplay:'weight'});await restored.ref.exportExcel();
const originalFields=workbookColumns(restored.exported.columns),index=originalFields.findIndex(field=>field.key==='black-defect-责废')+1; const originalFields=workbookColumns(restored.exported.columns),index=originalFields.findIndex(field=>field.key==='black-defect-责废')+1;
assert.equal(restored.exported.workbook.getWorksheet('质量汇总').getRow(2).getCell(index).value,120); assert.equal(restored.exported.workbook.getWorksheet('质量汇总').getRow(3).getCell(index).value,120);
assert.equal(data.black.defects[0].weight,120); assert.equal(data.black.defects[0].weight,120);
}); });
test('single-stage rate mode exports only its stage; missing or zero denominator stays blank and zero defect stays zero',async()=>{ test('single-stage rate mode exports only its stage; missing or zero denominator stays blank and zero defect stays zero',async()=>{
@@ -120,9 +137,9 @@ test('single-stage rate mode exports only its stage; missing or zero denominator
const ui=fixture({rows,view:'product',fullFields:true,onlyStage:'black',defectDisplay:'rate'});await ui.ref.exportExcel(); const ui=fixture({rows,view:'product',fullFields:true,onlyStage:'black',defectDisplay:'rate'});await ui.ref.exportExcel();
const fields=workbookColumns(ui.exported.columns),sheet=ui.exported.workbook.getWorksheet('质量汇总'),at=key=>fields.findIndex(f=>f.key===key)+1; const fields=workbookColumns(ui.exported.columns),sheet=ui.exported.workbook.getWorksheet('质量汇总'),at=key=>fields.findIndex(f=>f.key===key)+1;
assert.equal(fields.some(f=>f.key.startsWith('bright-')),false); assert.equal(fields.some(f=>f.key.startsWith('bright-')),false);
assert.equal(sheet.getRow(2).getCell(at('black-scrapWeight')).value,null); assert.equal(sheet.getRow(3).getCell(at('black-scrapWeight')).value,null);
assert.equal(sheet.getRow(3).getCell(at('black-defect-裂纹')).value,null); assert.equal(sheet.getRow(4).getCell(at('black-defect-裂纹')).value,null);
assert.equal(sheet.getRow(4).getCell(at('black-scrapWeight')).value,0); assert.equal(sheet.getRow(5).getCell(at('black-scrapWeight')).value,0);
}); });
test('本部报废原口径紧随扩散废,调整口径隐藏并在表格及Excel同步扣减', async () => { test('本部报废原口径紧随扩散废,调整口径隐藏并在表格及Excel同步扣减', async () => {
@@ -136,8 +153,8 @@ test('本部报废原口径紧随扩散废,调整口径隐藏并在表格及Ex
assert.equal(fields.some(f=>f.key==='bright-headquartersScrapWeight'),false); assert.equal(fields.some(f=>f.key==='bright-headquartersScrapWeight'),false);
const scrapColumn=adjusted.exported.columns.flatMap(c=>c.children||[c]).find(c=>c.key==='bright-scrapWeight'); const scrapColumn=adjusted.exported.columns.flatMap(c=>c.children||[c]).find(c=>c.key==='bright-scrapWeight');
assert.equal(scrapColumn.render(null,data).props.children.props.value,'70.0'); assert.equal(scrapColumn.render(null,data).props.children.props.value,'70.0');
assert.equal(adjusted.exported.workbook.getWorksheet('质量汇总').getRow(2).getCell(at('bright-scrapWeight')).value,70); assert.equal(adjusted.exported.workbook.getWorksheet('质量汇总').getRow(3).getCell(at('bright-scrapWeight')).value,70);
assert.equal(adjusted.exported.workbook.getWorksheet('质量汇总').getRow(2).getCell(at('bright-totalWeight')).value,1250); assert.equal(adjusted.exported.workbook.getWorksheet('质量汇总').getRow(3).getCell(at('bright-totalWeight')).value,1250);
} }
assert.equal(data.bright.scrapWeight,100); assert.equal(data.bright.scrapWeight,100);
}); });
@@ -154,3 +171,80 @@ test('合格率说明图标仅在去本部报废口径下显示,黑片总重
assert.ok(find(leaves.find(c=>c.key==='black-totalWeight').title,n=>n.type==='Info')); assert.ok(find(leaves.find(c=>c.key==='black-totalWeight').title,n=>n.type==='Info'));
} }
}); });
test('导出保留分组、合并表头、语义文字颜色、综合率底色与隔行填充,保存后格式不丢失', async () => {
const ui=fixture({rows:[row,row],view:'product'});await ui.ref.exportExcel();
const workbook=ui.exported.workbook;
await workbook.xlsx.load(await workbook.xlsx.writeBuffer());
const sheet=workbook.getWorksheet('质量汇总'), palette=metricWorkbookPalette();
assert.deepEqual(sheet.model.merges.sort(),['A1:A2','B1:E1','F1:I1','J1:J2'].sort());
assert.equal(sheet.getCell('F1').value,'亮片');
assert.equal(sheet.getCell('J1').value,'综合合格率');
assert.equal(sheet.getCell('B1').fill.fgColor.argb,palette.blackHead);
assert.equal(sheet.getCell('F1').fill.fgColor.argb,palette.brightHead);
for(const address of ['J1','J3','J4']) {
assert.equal(sheet.getCell(address).fill.fgColor.argb,palette.combinedFill);
assert.equal(sheet.getCell(address).font.color.argb,palette.combined);
}
assert.equal(sheet.getCell('E3').font.color.argb,palette.black);
assert.equal(sheet.getCell('I3').font.color.argb,palette.bright);
assert.equal(sheet.getCell('B3').fill.fgColor.argb,palette.surface);
assert.equal(sheet.getCell('B4').fill.fgColor.argb,palette.alternate);
assert.equal(sheet.getCell('B3').border.bottom.color.argb,palette.grid);
assert.equal(sheet.getCell('B2').border.bottom.color.argb,palette.headGrid);
assert.equal(sheet.getCell('A3').font.name,'微软雅黑');
assert.equal(sheet.getCell('B3').font.name,'Arial');
assert.equal(sheet.getCell('J3').font.bold,true);
assert.equal(sheet.getCell('B3').alignment.horizontal,'center');
assert.equal(sheet.views[0].xSplit,1);assert.equal(sheet.views[0].ySplit,2);
assert.equal(sheet.pageSetup.printTitlesRow,'1:2');
assert.equal(sheet.pageSetup.orientation,'landscape');
assert.ok(sheet.getColumn(1).width>sheet.getColumn(2).width);
});
test('午夜黑主题默认导出与浅蓝完全一致,其余浅色主题保留各自配色', async () => {
const columns=[{key:'object',title:'产品规格'},{key:'stage-bright',title:'亮片',children:[{key:'bright-rate',title:'合格率'}]}];
const light=await createMetricWorkbook(columns,[row],key=>key==='object'?'示例产品':.98765,'light');
const previous=globalThis.window;
try {
globalThis.window={localStorage:{getItem:()=> 'dark'}};
const dark=await createMetricWorkbook(columns,[row],key=>key==='object'?'示例产品':.98765);
assert.deepEqual(dark.getWorksheet('质量汇总').model,light.getWorksheet('质量汇总').model);
} finally { if(previous===undefined)delete globalThis.window;else globalThis.window=previous; }
assert.deepEqual(metricWorkbookPalette('dark'),metricWorkbookPalette('light'));
for(const theme of ['soft','deep','bright','classic']) {
const book=await createMetricWorkbook(columns,[row],()=>.98,theme);
assert.equal(book.getWorksheet('质量汇总').getCell('B3').font.color.argb,metricWorkbookPalette(theme).bright);
assert.notEqual(metricWorkbookPalette(theme).bright,metricWorkbookPalette('light').bright);
}
});
test('调整口径注释分行、长文字自动换行,缺失和非有限值为空且零值仍为数值', async () => {
const ui=fixture({rows:[row],view:'product',rateBasis:'withoutHeadquartersScrap'});await ui.ref.exportExcel();
assert.equal(ui.exported.workbook.getWorksheet('质量汇总').getCell('J1').value,'综合合格率\n(去本部报废)');
const columns=[{key:'object',title:'产品规格',width:120},{key:'black-totalWeight',title:'总重量'}];
const workbook=await createMetricWorkbook(columns,[Infinity,NaN,0,null],(key,row)=>key==='object'?'很长的中文产品规格需要完整显示且能够自动换行\n第二行':row);
const sheet=workbook.getWorksheet('质量汇总');
assert.equal(sheet.getCell('B2').value,null);assert.equal(sheet.getCell('B3').value,null);
assert.equal(sheet.getCell('B4').value,0);assert.equal(sheet.getCell('B5').value,null);
assert.ok(sheet.getRow(2).height>26.25);assert.equal(sheet.getCell('A2').alignment.wrapText,true);
const empty=await createMetricWorkbook([{key:'actions',title:'操作'},{key:'empty',children:[]}],[],()=>null);
assert.equal(empty.getWorksheet('质量汇总').columnCount,0);
await empty.xlsx.writeBuffer();
});
test('fine defect expansion and Excel share authorized parent/child values, preserve zero/missing, and leave the other stage compact',async()=>{
const data={releaseId:'r',stage:'bright',category:'加工废',dimension:'product',granularity:'month',fields:[{key:'加工缺角重量',label:'加工缺角重量',category:'加工废'}],groups:[{key:'p',parentRate:.1,parentWeight:10,children:[{key:'加工缺角重量',weight:0,rate:0}],reconciliationStatus:'RECONCILED'}]};
const rows=[{...row,key:'p'},{...row,key:'missing'}];
const ui=fixture({rows,view:'product',fullFields:true,defectDisplay:'rate',defectDrilldown:data});
await ui.ref.exportExcel();const fields=workbookColumns(ui.exported.columns),sheet=ui.exported.workbook.getWorksheet('质量汇总');
const at=key=>fields.findIndex(f=>f.key===key)+1;
assert.ok(at('bright-fine-parent')>0);assert.ok(at('bright-fine-加工缺角重量')>0);
assert.equal(fields[at('bright-fine-加工缺角重量')-1].header,'亮片 / 加工缺角废率');
assert.equal(sheet.getRow(3).getCell(at('bright-fine-加工缺角重量')).value,0);
assert.equal(sheet.getRow(4).getCell(at('bright-fine-加工缺角重量')).value,null);
assert.equal(sheet.getRow(3).getCell(at('bright-fine-parent')).value,.1);
assert.equal(sheet.getColumn(at('bright-fine-parent')).numFmt,'0.00%');
assert.equal(fields.some(f=>f.key.startsWith('black-defect-')||f.key.startsWith('black-fine-')),false);
assert.equal(data.groups[0].children[0].rate,0);
});
@@ -22,7 +22,7 @@ test('failed drilled trends cannot ready a replacement table or enable stale exp
assert.equal(stageReady({},'B',{key:'B'},'B',false,''),true); assert.equal(stageReady({},'B',{key:'B'},'B',false,''),true);
assert.equal(stageReady({},'',null,'B',false,'offline'),true); assert.equal(stageReady({},'',null,'B',false,'offline'),true);
const exportExpression=query.match(/exporting=\{summaryExporting\} exportDisabled=\{([^\n]+)\}/)[1]; const exportExpression=query.match(/exporting=\{summaryExporting\} exportDisabled=\{([^\n]+)\}/)[1];
const disabled=new Function('queryBusy','summaryBusy','tableReady','summaryMetricsData','drilledProduct','trendLoading','currentTrendError','drilledTrendData','visibleRows',`return ${exportExpression}`); const disabled=new Function('queryBusy','summaryBusy','tableReady','summaryMetricsData','drilledProduct','trendLoading','currentTrendError','drilledTrendData','visibleRows',`const defectDrill={category:null};return ${exportExpression}`);
assert.equal(disabled(false,false,false,{},'B',false,'offline',Array(12).fill({}),[]),true); assert.equal(disabled(false,false,false,{},'B',false,'offline',Array(12).fill({}),[]),true);
}); });
@@ -0,0 +1,26 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { freezeSourceRecordsQuery } from '../src/sourceRecordsContext.ts';
test('period rows bind their own year/month/quarter and keep the selected object',()=>{
const query={year:2026,month:10,monthStart:1,monthEnd:9,product:'产品A',department:'部门A',extraFilters:{customer:['001']}};
for(const [period,expected] of [['2026-02',{year:2026,month:2}],['2026-Q2',{year:2026,quarter:2}],['2025',{year:2025}]]) {
const result=freezeSourceRecordsQuery(query,'release','overall',{key:period,label:period,month:period,period,product:null});
assert.equal(result.product,query.product);assert.equal(result.department,query.department);
assert.deepEqual(Object.fromEntries(['year','month','quarter','monthStart','monthEnd'].filter(k=>result[k]!==undefined).map(k=>[k,result[k]])),expected);
assert.equal(result.releaseId,'release');
}
assert.equal(query.month,10);assert.equal(query.monthEnd,9);
});
test('clicking a month clears the parent range; object rows retain their original period',()=>{
const query={year:2026,monthStart:1,monthEnd:9,product:'产品A'};
const monthly=freezeSourceRecordsQuery(query,'release','overall',{key:'2026-02',month:'2026-02'},2);
assert.equal(monthly.month,2);assert.equal(monthly.monthStart,undefined);assert.equal(monthly.monthEnd,undefined);
for(const view of ['department','group','product','projectGroup']) {
for(const key of ['__UNASSIGNED__','',' ']) {
const object=freezeSourceRecordsQuery(query,'release',view,{key,[view]:null});
assert.equal(object[view],'__UNASSIGNED__');assert.equal(object.monthStart,1);assert.equal(object.monthEnd,9);
}
}
});