fix: display defect trends without waiting for all product details

This commit is contained in:
2026-11-13 14:16:05 +08:00
parent 696c3d2f6a
commit f8f7335831
5 changed files with 63 additions and 27 deletions
+8
View File
@@ -3,6 +3,14 @@
核实日期:2026年11月12日(上海时区)。这是可更新的接续记录,不替代实际代码与运行状态。 核实日期:2026年11月12日(上海时区)。这是可更新的接续记录,不替代实际代码与运行状态。
## 2026-11-13:不良率钻取显示提速
定位:趋势请求约0.7秒,年度全产品细项约12秒(6715个产品)。原页面通过 Promise.all 等待两者,导致趋势被整张产品细项表拖慢;趋势对象变化也会重复请求未变化的表格。SQL 分析确认细项数据使用与柏拉图相同的预汇总来源,按产品分组产生约9.4万条统计结果;本轮未改计算口径或数据库结构。
实现:拆分趋势和表格的请求范围、取消、加载与错误状态。趋势返回即展示;底表独立更新并保留导出门禁。只切趋势对象时复用仍属于当前范围的表格结果。新增回归覆盖慢表不阻塞趋势、表失败不隐藏趋势和请求不重复。
验证与发布:相关39项前端测试及生产构建通过,等待页面实测和发布结果更新。
## 2026-11-13:不良率钻取与查询修复 ## 2026-11-13:不良率钻取与查询修复
实现:分类曲线钻取细项趋势,保留分类总废率虚线;表格和 Excel 同步展开。分类切换、返回、换年/换产品及阶段切换遵循 README 中的交互约定。不良率图取消月份点击筛选。细项与正式指标在同一可重复读快照中计算,复用正式分母和来源权限,返回前再次校验权限;支持批量预汇总和原始来源回退。 实现:分类曲线钻取细项趋势,保留分类总废率虚线;表格和 Excel 同步展开。分类切换、返回、换年/换产品及阶段切换遵循 README 中的交互约定。不良率图取消月份点击筛选。细项与正式指标在同一可重复读快照中计算,复用正式分母和来源权限,返回前再次校验权限;支持批量预汇总和原始来源回退。
+3 -2
View File
@@ -792,7 +792,7 @@ export default function QueryPage({ page, onUnauthorized }: {
<QueryTablePanel <QueryTablePanel
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} 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 ? tableProductLabel : 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>}
@@ -804,7 +804,8 @@ export default function QueryPage({ page, onUnauthorized }: {
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} defectDrilldown={tableReady&&!queryBusy?defectDrill.table:undefined}
+3 -2
View File
@@ -151,11 +151,12 @@ export default function StageProductPage({stage,onUnauthorized}:{stage:Stage;onU
</div> </div>
</QueryTrendPanel> </QueryTrendPanel>
<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 合格率 / %'}/>} <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={!!defectDrill.category&&(!defectDrill.table||defectDrill.pending||!!defectDrill.error)||!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}
<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?'完整字段':'关键指标'}`} <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}
+31 -23
View File
@@ -11,14 +11,14 @@ export function useDefectDrilldown({active,mode,stage,query,trendQuery,tableQuer
const [selection,setSelection]=useState<{stage:Stage;category:string}|null>(null); const [selection,setSelection]=useState<{stage:Stage;category:string}|null>(null);
const [catalogFailure,setCatalogFailure]=useState(''); const [catalogFailure,setCatalogFailure]=useState('');
const [catalog,setCatalog]=useState<FineDefectCatalogResponse|null>(null); const [catalog,setCatalog]=useState<FineDefectCatalogResponse|null>(null);
const [loaded,setLoaded]=useState<{key:string;trend:DefectDrilldownResponse;table:DefectDrilldownResponse}|null>(null);
const [failure,setFailure]=useState<{key:string;message:string}|null>(null);
const [revision,setRevision]=useState(0); const [revision,setRevision]=useState(0);
const previousDisplay=useRef(display); const previousDisplay=useRef(display);
const enabled=active&&mode==='defects'; const enabled=active&&mode==='defects';
const category=mode==='defects'&&selection?.stage===stage?selection.category:null; const category=mode==='defects'&&selection?.stage===stage?selection.category:null;
const catalogKey=JSON.stringify([enabled,stage,defectScope(query),revision,refreshKey]); const catalogKey=JSON.stringify([enabled,stage,defectScope(query),revision,refreshKey]);
const key=JSON.stringify([category,stage,trendQuery,tableQuery,tableDimension,tableGranularity,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(()=>{ useEffect(()=>{
if(!enabled||!query.releaseId)return; if(!enabled||!query.releaseId)return;
const controller=new AbortController();setCatalog(null);setCatalogFailure(''); const controller=new AbortController();setCatalog(null);setCatalogFailure('');
@@ -30,27 +30,35 @@ export function useDefectDrilldown({active,mode,stage,query,trendQuery,tableQuer
useEffect(()=>{ useEffect(()=>{
if(selection&&(selection.stage!==stage||mode!=='defects')){setSelection(null);onDisplay(previousDisplay.current);} if(selection&&(selection.stage!==stage||mode!=='defects')){setSelection(null);onDisplay(previousDisplay.current);}
},[stage,mode]); },[stage,mode]);
useEffect(()=>{ const error=trend.error||table.error;
if(!enabled||!category||!query.releaseId)return; const pending=trend.pending||table.pending;
const controller=new AbortController();setFailure(null);
const common={stage,category,releaseId:query.releaseId};
Promise.all([
api.defectDrilldown({...defectScope(trendQuery),...common,dimension:'period',trendGranularity:trendQuery.trendGranularity||'month'},controller.signal),
api.defectDrilldown({...defectScope(tableQuery),...common,dimension:tableDimension,trendGranularity:tableGranularity},controller.signal),
]).then(([trend,table])=>{
if(controller.signal.aborted)return;
if([trend,table].some(value=>value.releaseId!==query.releaseId||value.stage!==stage||value.category!==category))throw new Error('细分查询范围已变化,请重试。');
setLoaded({key,trend,table});
}).catch(error=>{if(!controller.signal.aborted&&!onUnauthorized(error))setFailure({key,message:error instanceof Error?error.message:'细分查询失败'});});
return()=>controller.abort();
},[enabled,key,onUnauthorized]);
const current=loaded?.key===key?loaded:null;
const error=failure?.key===key?failure.message:'';
const pending=!!category&&!current&&!error;
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 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 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 clear=()=>{setSelection(null);onDisplay(previousDisplay.current);};
const incomplete=!!current&&[current.trend,current.table].some(data=>data.groups.some(group=>group.reconciliationStatus!=='RECONCILED')); const incomplete=[trend.data,table.data].some(data=>data?.groups.some(group=>group.reconciliationStatus!=='RECONCILED'));
const controls=category?<div className="defect-drilldown-controls"><Button type="link" onClick={clear}>全部分类</Button><span>› {category}</span>{pending&&<span><Spin size="small"/> 正在读取细分</span>}{error&&<Alert type="error" showIcon message={error} action={<Button size="small" onClick={()=>setRevision(v=>v+1)}>重试</Button>}/>} {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 retry=<Button size="small" onClick={()=>setRevision(v=>v+1)}>重试</Button>;
return {category,categories,choose,clear,controls,pending,error,trend:current?.trend,table:current?.table}; 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};
} }
@@ -72,3 +72,21 @@ test('new fine values cannot be overlaid on retained rows from a previous formal
assert.equal(bind(false,false,data),undefined);assert.equal(bind(true,true,data),undefined);assert.equal(bind(true,false,data),data.table); 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();
});