优化:未分类连续维护与柏拉图预汇总,同步已发布修复
This commit is contained in:
+147
-17
@@ -17,18 +17,115 @@ def _ranking_shape(task):
|
||||
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)
|
||||
and set(task['fields']) <= (set(FIELDS) | {'rate'})
|
||||
and task.get('visualType') in ('pareto','bar','column',None)):
|
||||
return False
|
||||
# A rank requires a concrete black/bright stage for every requested
|
||||
# dimension. Empty dimensionStages denotes a combined view and cannot be
|
||||
# silently converted into two stage-specific rankings.
|
||||
# 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 all(stage in ('black', 'bright') for stage in stages)
|
||||
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)
|
||||
@@ -50,7 +147,18 @@ def source_period(coverage, period, stage):
|
||||
'complete':not missing,'label':str(period['year'])+'年'+suffix+'已发布数据'}
|
||||
|
||||
|
||||
def rank_response(response, release, period, filters, dimension, stage, metric, limit):
|
||||
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:
|
||||
@@ -58,7 +166,7 @@ def rank_response(response, release, period, filters, dimension, stage, metric,
|
||||
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:
|
||||
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=[]
|
||||
@@ -106,6 +214,8 @@ def rank_response(response, release, period, filters, dimension, stage, metric,
|
||||
'basis':'先读取同范围全部正式分组值,再按所选阶段重量降序选Top;占比和累计占比的分母均为同范围全量正式重量,Top末项不强制为100%。'}
|
||||
if isinstance(response.get('protocolEvidence'), dict):
|
||||
result['protocolEvidence'] = response['protocolEvidence']
|
||||
if metric == 'scrapWeight' and denominator == 0 and result['periodCoverage']['availableMonths']:
|
||||
result['emptyReason'] = 'no_positive_scrap'
|
||||
return result
|
||||
|
||||
|
||||
@@ -123,18 +233,20 @@ def ranking_gaps(evidence,task,context):
|
||||
for dimension in task['dimensions']:
|
||||
for stage in dimension_stages(task, dimension, combined=False):
|
||||
for metric in task['fields']:
|
||||
if stage is None and metric == 'rate':
|
||||
continue # Companion formal rate, not an independently sorted ranking.
|
||||
candidates=[d['result'] for d in docs if d['result'].get('dimension')==dimension
|
||||
and d['result'].get('stage')==stage and d['result'].get('metric')==metric]
|
||||
def valid(result):
|
||||
proof=result.get('rankingCoverage',{});period=source_period(result.get('coverage'),result['period'],stage)
|
||||
proof=result.get('rankingCoverage',{});period=(result.get('periodCoverage') or {}) if stage is None else source_period(result.get('coverage'),result['period'],stage)
|
||||
rows=result.get('rows');denominator=result.get('totalWeight')
|
||||
return (proof.get('completeRanking') is True and isinstance(rows,list) and bool(rows)
|
||||
return (proof.get('completeRanking') is True and isinstance(rows,list)
|
||||
and isinstance(denominator,(int,float)) and not isinstance(denominator,bool)
|
||||
and math.isfinite(denominator) and denominator>0
|
||||
and math.isfinite(denominator) and (bool(rows) and denominator>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 '亮片')+FIELDS[metric]+'完整排名及同范围全量分母。')
|
||||
gaps.append('未取得'+DIMENSIONS[dimension]+'级'+('黑片' if stage=='black' else '亮片' if stage=='bright' else '黑片+亮片')+FIELDS[metric]+'完整排名及同范围全量分母。')
|
||||
return gaps
|
||||
|
||||
|
||||
@@ -153,10 +265,15 @@ def _negative_correction_chart_limit(result):
|
||||
|
||||
|
||||
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']]}}
|
||||
@@ -179,7 +296,7 @@ def ranking_chart(document,turn_id,task):
|
||||
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':'item-'+str(i+1),'label':row['label']} for i,row in enumerate(rows)],
|
||||
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,
|
||||
@@ -195,10 +312,15 @@ def missing_ranking_visuals(task,visuals,evidence=None):
|
||||
for dimension in task['dimensions']:
|
||||
for stage in dimension_stages(task, dimension, combined=False):
|
||||
for field in task['fields']:
|
||||
metric=stage+('_total_weight' if field=='totalWeight' else '_scrap_weight')
|
||||
if stage is None and field == 'rate':continue
|
||||
if any(empty_scrap_ranking(doc['result']) and doc['result'].get('dimension') == dimension
|
||||
and doc['result'].get('stage') == stage and doc['result'].get('metric') == field
|
||||
for doc in matching_rankings(evidence or [], task, {})):
|
||||
continue
|
||||
metric='current_scrap_weight' if stage is None else stage+('_total_weight' if field=='totalWeight' else '_scrap_weight')
|
||||
if not any(v.get('dimension')==dimension and v.get('stage')==stage
|
||||
and v.get('metric')==metric and v.get('type')==(task.get('visualType') or 'bar') for v in visuals):
|
||||
label=DIMENSIONS[dimension]+'级'+('黑片' if stage=='black' else '亮片')+FIELDS[field]+'图'
|
||||
label=DIMENSIONS[dimension]+'级'+('黑片' if stage=='black' else '亮片' if stage=='bright' else '黑片+亮片')+FIELDS[field]+'图'
|
||||
if stage=='black' and field=='totalWeight':
|
||||
limited=any(_negative_correction_chart_limit(d.get('result',{}))
|
||||
and d.get('result',{}).get('dimension')==dimension
|
||||
@@ -210,14 +332,22 @@ def missing_ranking_visuals(task,visuals,evidence=None):
|
||||
|
||||
|
||||
def ranking_report(evidence,task,context):
|
||||
from nao_evidence_report import shown,cell,_scope_label
|
||||
from nao_agent_task import dimension_stages
|
||||
if task.get('dimensions') and all(dimension_stages(task, dimension, combined=False) == [None] for dimension in task['dimensions']):
|
||||
from nao_combined_ranking import combined_report
|
||||
return combined_report(evidence, task, context)
|
||||
from nao_evidence_report import shown,cell,_scope_label
|
||||
facts=[];sections=[];seen=set()
|
||||
for document in matching_rankings(evidence,task,context):
|
||||
r=document['result'];key=(r['dimension'],r['stage'],r['metric'])
|
||||
if key in seen or r['dimension'] not in task['dimensions'] or r['stage'] not in dimension_stages(task, r['dimension'], combined=False) or r['metric'] not in task['fields']:continue
|
||||
seen.add(key);ref=document['evidenceRef'];name=('黑片' if r['stage']=='black' else '亮片')+FIELDS[r['metric']]
|
||||
p=source_period(r.get('coverage'),r['period'],r['stage']);scope=_scope_label(r['filters']);top=r['rows']
|
||||
if empty_scrap_ranking(r):
|
||||
text=p['label']+';'+scope+';'+name+'为0.00 kg,没有正报废重量的'+DIMENSIONS[r['dimension']]+',无需生成排名或占比图。'
|
||||
facts.append({'id':'F'+str(len(facts)+1),'kind':'overall','label':name,'text':text,'evidenceRef':ref})
|
||||
sections.append('### '+name+'\n\n'+text+'\n\n['+ref+']')
|
||||
continue
|
||||
title=DIMENSIONS[r['dimension']]+'级'+name+' Top '+str(len(top))
|
||||
table=['| 排名 | '+DIMENSIONS[r['dimension']]+' | '+name+'(kg) | 占全部重量 | 累计占全部重量 |','| --- | --- | --- | --- | --- |']
|
||||
for row in top:
|
||||
@@ -230,10 +360,10 @@ def ranking_report(evidence,task,context):
|
||||
'排名表只展示正重量Top。')
|
||||
facts.append({'id':'F'+str(len(facts)+1),'kind':'overall','label':title,
|
||||
'text':p['label']+';'+scope+';'+DIMENSIONS[r['dimension']]+'级'+name+'全量分母为'+shown(r['totalWeight'])+' kg,展示前'+str(len(top))+'项,累计占全部重量'+shown(top[-1]['cumulativeShare'],percent=True)+'。'+correction_note,'evidenceRef':ref})
|
||||
for row in top[:3]:
|
||||
for row in top:
|
||||
facts.append({'id':'F'+str(len(facts)+1),'kind':r['dimension'],'label':row['label'],
|
||||
'text':DIMENSIONS[r['dimension']]+':'+row['label']+';'+name+shown(row['weight'])+' kg,占全部重量'+shown(row['share'],percent=True)+'。','evidenceRef':ref,
|
||||
'dimension':r['dimension'],'stage':r['stage']})
|
||||
'dimension':r['dimension'],'stage':r['stage'],'rank':row['rank']})
|
||||
sections.append('### '+title+'\n\n'+p['label']+';'+scope+'。分母:全部'+name+shown(r['totalWeight'])+' kg。\n\n'+'\n'.join(table)
|
||||
+('\n\n'+correction_note if correction_note else '')+'\n\n['+ref+']')
|
||||
if not facts:raise ValueError('未取得与本轮阶段、期间和分组一致的重量排名')
|
||||
|
||||
Reference in New Issue
Block a user