"""Rank the union of formal stage scrap, retaining the published composite rate. Both stages reconcile independently against the same full-scope response before their weights are added. An absent stage is not presented as an observed zero. No rate or cross-stage input denominator is calculated here. """ from decimal import Decimal import math def _stage_has_explicit_zero_source(response, period, stage): """Prove a scoped stage has no source rows before treating its missing weight as absent. A missing stage summary is not a zero by itself. Require available monthly source counts, the aggregate stage row count, and every returned grouping row to explicitly report zero source rows for that stage. The other stage must contain real source rows; this helper cannot validate an empty scope. """ from nao_period import expected_months from nao_quality_tools import _number coverage = response.get('coverage') summary = response.get('summary') rows = response.get('rows') if not isinstance(coverage, dict) or not isinstance(summary, dict) or not isinstance(rows, list) or not rows: return False summary_coverage = summary.get('coverage') stage_rows_key = stage + 'Rows' other_rows_key = ('bright' if stage == 'black' else 'black') + 'Rows' if (not isinstance(summary_coverage, dict) or type(summary_coverage.get(stage_rows_key)) is not int or summary_coverage[stage_rows_key] != 0): return False source_months = coverage.get('sourceMonths') if not isinstance(source_months, list): return False wanted = set(expected_months(period)) by_month = {} other_source_rows = 0 for item in source_months: if not isinstance(item, dict) or item.get('year') != period.get('year'): continue month = item.get('month') if month not in wanted: continue if month in by_month: return False stage_count = item.get(stage_rows_key) other_count = item.get(other_rows_key) if (type(stage_count) is not int or stage_count != 0 or type(other_count) is not int or other_count < 0): return False by_month[month] = item other_source_rows += other_count # sourceMonths contains only months with source records; months omitted # from it are outside the observed available-source set. The caller's # periodCoverage policy decides whether those omitted months are allowed. if not by_month or other_source_rows <= 0: return False for row in rows: if not isinstance(row, dict): return False row_coverage = row.get('coverage') if (not isinstance(row_coverage, dict) or type(row_coverage.get(stage_rows_key)) is not int or row_coverage[stage_rows_key] != 0): return False block = row.get(stage) if block is None: continue if not isinstance(block, dict): return False if 'scrapWeight' in block: raw_weight = block.get('scrapWeight') if raw_weight is not None: weight = _number(raw_weight) if weight is None or weight != 0: return False stage_summary = summary.get(stage) if stage_summary is not None: if not isinstance(stage_summary, dict): return False if 'scrapWeight' in stage_summary: raw_weight = stage_summary.get('scrapWeight') if raw_weight is not None: weight = _number(raw_weight) if weight is None or weight != 0: return False return True def combined_response(response, release, period, filters, dimension, limit): from nao_quality_tools import _number, _row_composite_rate, QualityToolError from nao_weight_ranking import rank_response, source_period # Reuse each stage's full-population validation, including absent-stage and # negative-value rules. Never merge independently truncated stage Top lists. stage_without_source = {stage: _stage_has_explicit_zero_source(response, period, stage) for stage in ('black', 'bright')} for stage in ('black', 'bright'): if not stage_without_source[stage]: rank_response(response, release, period, filters, dimension, stage, 'scrapWeight', 1, _allow_zero=True) summary = response['summary'] stage_totals = {} for stage in ('black', 'bright'): stage_summary = summary.get(stage) raw_total = stage_summary.get('scrapWeight') if isinstance(stage_summary, dict) else None total = _number(raw_total, minimum=Decimal(0)) if total is None: if not stage_without_source[stage]: raise QualityToolError('合计排名缺少有来源阶段的正式报废总重。') total = Decimal(0) elif stage_without_source[stage] and total != 0: raise QualityToolError('无来源阶段不能出现非零正式报废总重。') stage_totals[stage] = total denominator = sum(stage_totals.values(), Decimal(0)) summary_rate = _row_composite_rate(summary) rows = [] for row in response['rows']: components = {} for stage in ('black', 'bright'): count = (row.get('coverage') or {}).get(stage + 'Rows') block = row.get(stage) or {} weight = _number(block.get('scrapWeight'), minimum=Decimal(0)) if count == 0: if weight is not None and weight != 0: raise QualityToolError('无来源阶段不能出现非零分组报废重量。') components[stage] = None elif weight is None: raise QualityToolError('合计排名缺少有来源阶段的正式报废重量。') else: components[stage] = weight weight = sum((value for value in components.values() if value is not None), Decimal(0)) if weight <= 0: continue rate = _row_composite_rate(row) rows.append({'key': row['key'], 'label': str(row.get('label') or row['key']), 'weight': weight, 'compositeRate': float(rate) if rate is not None else None, 'blackScrapWeight': float(components['black']) if components['black'] is not None else None, 'brightScrapWeight': float(components['bright']) if components['bright'] is not None else None}) rows.sort(key=lambda row: (-row['weight'], row['key'])) cumulative = Decimal(0) selected = [] for index, row in enumerate(rows[:limit], 1): cumulative += row['weight'] selected.append({**row, 'rank': index, 'weight': float(row['weight']), 'share': float(row['weight']/denominator), 'cumulativeShare': float(cumulative/denominator)}) coverage = response.get('coverage') or {} periods = [source_period(coverage, period, stage) for stage in ('black', 'bright')] from nao_period import expected_months known = {item.get('month') for item in coverage.get('sourceMonths', []) if isinstance(item, dict) and item.get('year') == period['year'] and all(type(item.get(stage+'Rows')) is int and item[stage+'Rows'] >= 0 for stage in ('black','bright'))} available = sorted((set(periods[0]['availableMonths']) | set(periods[1]['availableMonths'])) & known) missing = sorted(set(expected_months(period)) - set(available)) result = {'kind': 'quality_weight_ranking', 'release': release, 'releaseId': release['id'], 'period': period, 'filters': filters, 'dimension': dimension, 'stage': None, 'metric': 'scrapWeight', 'fields': ['rate', 'scrapWeight'], 'compositeRate': float(summary_rate) if summary_rate is not None else None, 'totalWeight': float(denominator), 'rows': selected, 'totalRows': len(rows), 'returnedRows': len(selected), 'truncated': len(rows) > len(selected), 'rankingCoverage': {'completeRanking': True, 'groups': len(response['rows']), 'combinedStages': ['black', 'bright']}, 'coverage': coverage, 'periodCoverage': {'availableMonths': available, 'missingMonths': missing, 'internalMissingMonths': [m for m in missing if available and m <= available[-1]], 'complete': not missing}, 'shareBasis': 'all_scope_weight', 'basis': '同期间、同筛选的黑片报废重量加亮片报废重量,全量合计后降序取Top;综合合格率直接采用正式值,不合并两阶段投入作分母。'} if isinstance(response.get('protocolEvidence'), dict): result['protocolEvidence'] = response['protocolEvidence'] if denominator == 0 and available: result['emptyReason'] = 'no_positive_scrap' return result def combined_report(documents, task, context): from nao_evidence_report import shown, cell, period, _scope_label from nao_weight_ranking import matching_rankings, ranking_gaps, empty_scrap_ranking, DIMENSIONS facts, sections, gaps = [], [], [] for doc in matching_rankings(documents, task, context): result = doc['result'] if result.get('stage') is not None or result.get('metric') != 'scrapWeight': continue ref = doc['evidenceRef'] dimension = result.get('dimension') filters = result.get('filters') if isinstance(result.get('filters'), dict) else {} scalar_target = ( task.get('kind') == 'metrics' and dimension in ('product', 'department', 'group') and task.get('dimensions') == [dimension] and task.get('dimensionStages', {}).get(dimension) == [] and task.get('stages') == [] and set(task.get('fields') or []) == {'rate', 'scrapWeight'} and isinstance(filters.get(dimension), str) and bool(filters[dimension].strip()) and not task.get('comparisonRequired') and not task.get('comparisonPeriod') and not task.get('trendRequired')) if scalar_target: validation_gaps = ranking_gaps([doc], task, context) if validation_gaps: gaps.extend(validation_gaps) continue scope_label = _scope_label(filters) period_label = period(result['period']) rows = [] total_weight = result.get('totalWeight') if (isinstance(total_weight, (int, float)) and not isinstance(total_weight, bool) and math.isfinite(total_weight) and total_weight >= 0): weight_text = (period_label+';'+scope_label+';黑片+亮片正式报废总重:' +shown(total_weight)+' kg。') facts.append({'id': 'F'+str(len(facts)+1), 'kind': 'overall', 'label': '黑片+亮片报废总重', 'metric': 'scrapWeight', 'value': total_weight, 'text': weight_text, 'evidenceRef': ref, 'dimension': dimension, 'stage': None}) rows.append(['黑片+亮片报废总重', shown(total_weight)+' kg']) else: gaps.append(period_label+';'+scope_label+';正式合计报废重量无效,未作为单值展示。') rate = result.get('compositeRate') if (isinstance(rate, (int, float)) and not isinstance(rate, bool) and math.isfinite(rate) and rate >= 0): rate_text = period_label+';'+scope_label+';正式综合合格率:'+shown(rate, percent=True)+'。' facts.append({'id': 'F'+str(len(facts)+1), 'kind': 'overall', 'label': '正式综合合格率', 'metric': 'rate', 'value': rate, 'text': rate_text, 'evidenceRef': ref, 'dimension': dimension, 'stage': None}) rows.append(['正式综合合格率', shown(rate, percent=True)]) else: gaps.append(period_label+';'+scope_label+';缺少有效正式综合合格率。') if rows: sections.append('### '+DIMENSIONS[dimension]+'正式指标\n\n'+period_label+';'+scope_label+'。\n\n' +'| 指标 | 正式值 |\n| --- | --- |\n' +'\n'.join('| '+cell(label)+' | '+cell(value)+' |' for label, value in rows) +'\n\n['+ref+']') continue if empty_scrap_ranking(result): text = period(result['period'])+'黑片+亮片正式报废总重为0.00 kg,没有正报废重量的'+DIMENSIONS[result['dimension']]+',无需生成排名或占比图。' facts.append({'id': 'F'+str(len(facts)+1), 'kind': 'overall', 'label': '本期无正报废项', 'text': text, 'evidenceRef': ref}) section = '### 本期报废情况\n\n'+text rate = result.get('compositeRate') if isinstance(rate, (int, float)) and not isinstance(rate, bool) and math.isfinite(rate) and rate >= 0: rate_text = period(result['period'])+'同范围正式综合合格率为'+shown(rate, percent=True)+'。' facts.append({'id': 'F'+str(len(facts)+1), 'kind': 'rate', 'label': '正式综合合格率', 'metric': 'rate', 'value': rate, 'text': rate_text, 'evidenceRef': ref}) section += '\n\n'+rate_text sections.append(section+'\n\n['+ref+']') continue title = DIMENSIONS[result['dimension']] + '报废总重 TOP' + str(len(result['rows'])) table = ['| 排名 | '+DIMENSIONS[result['dimension']]+' | 报废总重(kg) | 综合合格率 | 占全部报废 |', '| --- | --- | --- | --- | --- |'] for row in result['rows']: rate = shown(row.get('compositeRate'), percent=True) text = (row['label']+';黑片+亮片报废总重'+shown(row['weight'])+' kg;综合合格率'+rate +';占全部报废'+shown(row['share'], percent=True)+'。') if row.get('compositeRate') is None: text += '阶段来源或有效投入不足,综合合格率暂不可用。' facts.append({'id': 'F'+str(len(facts)+1), 'kind': result['dimension'], 'label': row['label'], 'rank': row['rank'], 'text': text, 'evidenceRef': ref, 'dimension': result['dimension'], 'stage': None}) table.append('| '+' | '.join(cell(str(value)) for value in (row['rank'], row['label'], shown(row['weight']), rate, shown(row['share'], percent=True)))+' |') sections.append('### '+title+'\n\n'+period(result['period'])+',按黑片+亮片报废总重排序。\n\n' +'\n'.join(table)+'\n\n['+ref+']') if not facts: gaps.append('尚未取得同范围黑片+亮片报废总重完整排名。') gaps.extend(ranking_gaps(documents, task, context)) return {'facts': facts, 'sections': sections, 'missingEvidence': gaps, 'coverageSufficient': not gaps, 'requiredFactIds': [fact['id'] for fact in facts], 'rawMetrics': [doc['result'] for doc in documents], 'policy': '报废总重合计;综合合格率沿用正式值。缺失来源不冒充零报废或100%合格率。'} def combined_chart(document, turn_id, task): from analysis_visuals import _chart, _chart_scope, _scope, _period from nao_weight_ranking import DIMENSIONS, ranking_gaps result = document['result'] if ranking_gaps([document], task, {'releaseId': result['releaseId']}): return None scope, _ = _scope(result.get('filters')) period, _, _ = _period(result.get('period')) if scope is None or period is None: return None rows = result['rows'][:100] return _chart(turn_id=turn_id, chart_type=task.get('visualType') or 'bar', title=DIMENSIONS[result['dimension']]+'报废总重排名', subtitle='黑片+亮片;综合合格率见本节表格', unit='kg', categories=[{'key': row['key'], 'label': row['label']} for row in rows], series=[{'key': 'current_scrap_weight', 'label': '报废总重', 'values': [row['weight'] for row in rows]}], evidence_refs=[document['evidenceRef']], required_fields=['black_scrap_weight', 'bright_scrap_weight'], scope=_chart_scope(scope, result['releaseId'], result['period'], view=result['dimension']), period=period, metric='current_scrap_weight', dimension=result['dimension'], section_key=result['dimension'], coverage={'totalRows': result['totalRows'], 'returnedRows': len(rows), 'truncated': result['totalRows'] > len(rows), 'shareBasis': 'all_scope_weight', 'denominator': result['totalWeight']}, note='先合计同范围全部规格的黑片、亮片报废重量,再选Top;占比以全部报废总重为分母。')