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 { 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; }