"""Rank formal stage weights; all shares retain the full authorized denominator.""" from decimal import Decimal import math FIELDS = {'totalWeight':'投入重量', 'scrapWeight':'报废重量'} DIMENSIONS = {'product':'产品规格', 'department':'产业部', 'group':'业务分组'} def ranking_task(task): return bool(_ranking_shape(task) and not task.get('comparisonPeriod') and not task.get('comparisonRequired') and not task.get('trendRequired')) def _ranking_shape(task): """Shared structural gate for weight-ranking tasks and compound requests.""" if not (task and task.get('period') and task.get('kind') in ('analysis','metrics','report') and isinstance(task.get('dimensions'), list) and task['dimensions'] and set(task['dimensions']) <= set(DIMENSIONS) and isinstance(task.get('fields'), list) and task['fields'] and set(task['fields']) <= (set(FIELDS) | {'rate'}) and task.get('visualType') in ('pareto','bar','column',None)): return False # Empty dimensionStages denotes one combined scrap ranking. Never turn it # into two independent stage Top lists or combine stage input denominators. from nao_agent_task import dimension_stages return all((stages := dimension_stages(task, dimension, combined=False)) and (stages == [None] and set(task['fields']) <= {'rate', 'scrapWeight'} and 'scrapWeight' in task['fields'] or all(stage in ('black', 'bright') for stage in stages) and set(task['fields']) <= set(FIELDS)) for dimension in task['dimensions']) def _single_filter_value(value): """Match the server SelectionValues wire contract, including encoded sets.""" if not isinstance(value, str) or not value.strip(): return False if not value.startswith('~qv1~'): return len(value) <= 256 import base64 import json import re encoded = value[5:] if len(value) > 16384 or not re.fullmatch(r'[A-Za-z0-9_-]+', encoded): return False try: values = json.loads(base64.b64decode(encoded + '=' * ((-len(encoded)) % 4), altchars=b'-_', validate=True)) except (ValueError, TypeError, UnicodeError): return False return (isinstance(values, list) and 1 <= len(values) <= 50 and all(isinstance(item, str) and item.strip() and len(item) <= 256 for item in values) and len(set(values)) == 1) def protocol_scalar_metrics_scope(root_task, context, document): """Confirm that a combined ranking result is serving one exact scalar metrics goal. The protocol scope itself may be an `analysis` task because its operator is a combined ranking. Presentation can use the root `metrics` intent only when the single declared node, child frame, evidence proof, and result all match the same exact scope. """ if not isinstance(root_task, dict) or root_task.get('kind') != 'metrics': return False dimension = root_task.get('dimensions') if not isinstance(dimension, list) or len(dimension) != 1 or dimension[0] not in DIMENSIONS: return False dimension = dimension[0] filters = root_task.get('filters') if not isinstance(filters, dict) or not _single_filter_value(filters.get(dimension)): return False if (set(root_task.get('fields') or []) != {'rate', 'scrapWeight'} or root_task.get('stages') != [] or root_task.get('dimensionStages') != {dimension: []} or root_task.get('comparisonRequired') is True or root_task.get('comparisonPeriod') is not None or root_task.get('trendRequired') is True): return False nodes = root_task.get('queryPlan') if not isinstance(nodes, list) or len(nodes) != 1: return False node = nodes[0] if (not isinstance(node, dict) or node.get('purpose') != 'ranking' or node.get('dimension') != dimension or node.get('limit') != 1 or set(node.get('fields') or []) != {'rate', 'scrapWeight'} or node.get('stages') != [] or node.get('comparisonRequired') is True or node.get('selector') != {'kind': 'base'}): return False if not isinstance(context, dict) or not isinstance(document, dict): return False result = document.get('result') if (document.get('mode') != 'rank' or not isinstance(result, dict) or result.get('kind') != 'quality_weight_ranking' or result.get('dimension') != dimension or result.get('stage') is not None or result.get('metric') != 'scrapWeight'): return False state = context.get('_protocolScopes') scopes = state.get('scopes') if isinstance(state, dict) else None if (not isinstance(scopes, list) or len(scopes) != 1 or state.get('pending') or state.get('gaps')): return False scope = scopes[0] if (not isinstance(scope, dict) or scope.get('purpose') != 'ranking' or scope.get('nodeId') != node.get('nodeId')): return False child = scope.get('task') if (not isinstance(child, dict) or child.get('kind') not in ('analysis', 'metrics') or child.get('fields') is None or set(child.get('fields') or []) != {'rate', 'scrapWeight'} or child.get('dimensions') != [dimension] or child.get('stages') != [] or child.get('dimensionStages') != {dimension: []} or child.get('comparisonRequired') is True or child.get('comparisonPeriod') is not None or child.get('trendRequired') is True): return False from nao_evidence_report import _filters from nao_period import normalize_period try: if (_filters(child.get('filters')) != _filters(filters) or normalize_period(child.get('period')) != normalize_period(root_task.get('period'))): return False except (TypeError, ValueError): return False from nao_query_plan import scope_documents try: selected = scope_documents([document], scope, release_id=context.get('releaseId')) except (KeyError, TypeError, ValueError): return False return len(selected) == 1 and selected[0] is document def ranking_intent(task): """A structurally valid ranking request, including incompatible trend/compare asks.""" return _ranking_shape(task) def source_period(coverage, period, stage): from nao_period import expected_months expected = expected_months(period) months = coverage.get('sourceMonths') if isinstance(coverage,dict) else None available = sorted({row['month'] for row in months or [] if isinstance(row,dict) and row.get('year')==period['year'] and row.get('month') in expected and isinstance(row.get(stage+'Rows'),int) and row[stage+'Rows']>0}) missing = [m for m in expected if m not in available] internal = [m for m in missing if available and m <= available[-1]] suffix = (str(available[0])+'月' if len(available)==1 else str(available[0])+'—'+str(available[-1])+'月' if not internal else '、'.join(map(str,available))+'月') if available else '无已发布月份' return {'availableMonths':available,'missingMonths':missing,'internalMissingMonths':internal, 'complete':not missing,'label':str(period['year'])+'年'+suffix+'已发布数据'} def empty_scrap_ranking(result): return (result.get('metric') == 'scrapWeight' and result.get('totalWeight') == 0 and result.get('rows') == [] and result.get('totalRows') == 0 and result.get('emptyReason') == 'no_positive_scrap' and (result.get('rankingCoverage') or {}).get('completeRanking') is True and bool((result.get('periodCoverage') or {}).get('availableMonths'))) def rank_response(response, release, period, filters, dimension, stage, metric, limit, *, _allow_zero=False): if stage is None and metric == 'scrapWeight': from nao_combined_ranking import combined_response return combined_response(response, release, period, filters, dimension, limit) from nao_quality_tools import _number, QualityToolError rows = response.get('rows') if not isinstance(rows,list) or len(rows)>100000: raise QualityToolError('分组重量未取得完整有界结果,不能作为全量排名。') if (response.get('context') or {}).get('rowCount',len(rows)) != len(rows): raise QualityToolError('分组数据已被截断,不能以其生成全量排名。') denominator = _number((response.get('summary',{}).get(stage) or {}).get(metric),minimum=Decimal(0)) if denominator is None or denominator<0 or denominator==0 and not (_allow_zero or metric == 'scrapWeight'): raise QualityToolError('所选阶段的正式重量总量不可用,不能计算占比。') allow_signed_black_total = stage == 'black' and metric == 'totalWeight' all_rows=[] seen=set() unavailable_groups=0 for row in rows: if not isinstance(row, dict): raise QualityToolError('分组行结构无效,不能生成全量排名。') key=str(row.get('key') or '') if not key or key in seen:raise QualityToolError('分组标识缺失或重复,不能生成全量排名。') seen.add(key) raw_value=_number((row.get(stage) or {}).get(metric)) if raw_value is not None and raw_value<0 and not allow_signed_black_total: raise QualityToolError('除黑片投入重量负向修正外,分组重量不能为负。') value=raw_value if allow_signed_black_total else _number((row.get(stage) or {}).get(metric),minimum=Decimal(0)) if value is None: if (row.get('coverage') or {}).get(stage+'Rows')==0: unavailable_groups+=1 continue raise QualityToolError('分组重量有缺失或无效值,未将缺失补零。') all_rows.append({'key':key,'label':str(row.get('label') or key),'weight':value}) grouped=sum((row['weight'] for row in all_rows),Decimal(0)) if abs(grouped-denominator)>max(Decimal('.00001'),abs(denominator)*Decimal('1e-10')): raise QualityToolError('分组重量合计与同范围正式总量不一致,不能计算全量占比。') all_rows.sort(key=lambda row:(-row['weight'],row['key'])) positive=[row for row in all_rows if row['weight']>0] negative=[row for row in all_rows if row['weight']<0] negative_weight=sum((row['weight'] for row in negative),Decimal(0)) selected=[]; cumulative=Decimal(0) for index,row in enumerate(positive[:limit]): cumulative+=row['weight'] selected.append({**row,'weight':float(row['weight']),'rank':index+1, 'share':float(row['weight']/denominator),'cumulativeShare':float(cumulative/denominator)}) coverage=response.get('coverage') or {} result = {'kind':'quality_weight_ranking','release':release,'releaseId':release['id'], 'period':period,'filters':filters,'dimension':dimension,'stage':stage,'metric':metric, 'totalWeight':float(denominator),'rows':selected,'totalRows':len(positive), 'returnedRows':len(selected),'truncated':len(selected)0 or empty_scrap_ranking(result)) and period['availableMonths'] and not period['internalMissingMonths'] and (task.get('periodCoverage','available')!='complete' or period['complete'])) if not any(valid(r) for r in candidates): gaps.append('未取得'+DIMENSIONS[dimension]+'级'+('黑片' if stage=='black' else '亮片' if stage=='bright' else '黑片+亮片')+FIELDS[metric]+'完整排名及同范围全量分母。') return gaps def _negative_correction_chart_limit(result): if (result.get('stage') != 'black' or result.get('metric') != 'totalWeight' or (result.get('rankingCoverage') or {}).get('negativeGroups', 0) <= 0): return False denominator=result.get('totalWeight');rows=result.get('rows') if (not isinstance(denominator,(int,float)) or isinstance(denominator,bool) or not math.isfinite(denominator) or not isinstance(rows,list)): return False weights=[row.get('weight') for row in rows[:10] if isinstance(row,dict)] if any(not isinstance(value,(int,float)) or isinstance(value,bool) or not math.isfinite(value) for value in weights): return False return sum(weights)>denominator+max(.00001,abs(denominator)*1e-10) def ranking_chart(document,turn_id,task): if empty_scrap_ranking(document.get('result', {})): return None from nao_agent_task import dimension_stages from analysis_visuals import _chart,_chart_scope,_scope,_period r=document.get('result',{}) if r.get('kind')!='quality_weight_ranking' or r.get('dimension') not in DIMENSIONS:return None if r.get('stage') is None: from nao_combined_ranking import combined_chart return combined_chart(document, turn_id, task) if r.get('stage') not in dimension_stages(task, r['dimension'], combined=False) or r.get('metric') not in task['fields']:return None check={**task,'dimensions':[r['dimension']],'stages':[r['stage']],'fields':[r['metric']], 'dimensionStages':{r['dimension']:[r['stage']]}} if ranking_gaps([document],check,{'releaseId':r.get('releaseId')}):return None scope,_=_scope(r.get('filters'));period,_,_=_period(r.get('period')) if scope is None or period is None:return None rows=r['rows'][:10];weights=[row.get('weight') for row in rows];total=r['totalWeight'] if any(not isinstance(v,(int,float)) or isinstance(v,bool) or not math.isfinite(v) or v<0 for v in weights):return None if sum(weights)>total+max(.00001,total*1e-10) or any(weights[i]>weights[i-1] for i in range(1,len(weights))):return None metric=r['stage']+('_total_weight' if r['metric']=='totalWeight' else '_scrap_weight') label=('黑片' if r['stage']=='black' else '亮片')+FIELDS[r['metric']] source=source_period(r.get('coverage'),r['period'],r['stage']) count=r['totalRows'];shown_count=len(rows);chart_type=task.get('visualType') or 'bar' negative_groups=(r.get('rankingCoverage') or {}).get('negativeGroups',0) negative_weight=(r.get('rankingCoverage') or {}).get('negativeWeight',0) note='占比与累计占比=所列重量及累计重量÷同范围全部重量;Top末项可以低于100%。未发布月份不补零,不冒充完整全年。' if r.get('stage')=='black' and r.get('metric')=='totalWeight' and negative_groups: from nao_evidence_report import shown as format_shown note+=f'黑片含{negative_groups}项负向投入重量修正,净额{format_shown(negative_weight)} kg仍计入正式净总重分母;图中仅展示正重量Top。' return _chart(turn_id=turn_id,chart_type=chart_type, title=DIMENSIONS[r['dimension']]+'级'+label+('柏拉图' if chart_type=='pareto' else '排名'), subtitle=source['label']+';展示前'+str(shown_count)+'项,占比以同范围全部重量为分母。', unit='kg',categories=[{'key':row['key'],'label':row['label']} for row in rows], series=[{'key':metric,'label':label,'values':weights}],evidence_refs=[document['evidenceRef']], required_fields=[metric],scope=_chart_scope(scope,r['releaseId'],r['period'],view=r['dimension'],stage=r['stage']), period=period,stage=r['stage'],metric=metric, coverage={'totalRows':count,'returnedRows':shown_count,'truncated':shown_count