同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。 补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。 统一午夜黑及其他主题的控件细节、中文和业务数字字体。 排除本地业务数据、凭据、预览、发布与验收产物。 验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
import copy
|
||||
import json
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from urllib.parse import parse_qs, urlsplit
|
||||
from unittest.mock import patch
|
||||
|
||||
sys.path[:0] = [str(Path(__file__).resolve().parents[1] / 'scripts'),
|
||||
str(Path(__file__).resolve().parent)]
|
||||
|
||||
from nao_agent_task import normalize_task
|
||||
from nao_protocol_execution import execute
|
||||
from test_nao_agent_task import task_value
|
||||
from test_nao_protocol_execution import BoundRankingApi, RELEASE, TURN, identity
|
||||
|
||||
|
||||
def _scope_task(root, *, purpose, fields, stages, selector_kind, product=None,
|
||||
dimension=None, visual_type=None, dimension_stages=None):
|
||||
filters = copy.deepcopy(root['filters'])
|
||||
if product is not None:
|
||||
filters['product'] = product
|
||||
task = {'version': 1, 'kind': 'analysis' if purpose == 'ranking' else 'metrics',
|
||||
'question': root['question'], 'goal': root['goal'],
|
||||
'period': copy.deepcopy(root['period']), 'comparisonPeriod': None,
|
||||
'filters': filters, 'dimensions': [dimension] if purpose == 'ranking' else [],
|
||||
'dimensionStages': ({dimension: list(stages)} if purpose == 'ranking' else {}),
|
||||
'fields': list(fields), 'stages': list(stages),
|
||||
'trendRequired': purpose == 'trend', 'comparisonRequired': False,
|
||||
'visualPreference': 'auto', 'visualType': visual_type,
|
||||
'periodCoverage': root.get('periodCoverage', 'available'),
|
||||
'freshQuery': True, 'historyMode': 'none'}
|
||||
return task
|
||||
|
||||
|
||||
class QueryPlanApi(BoundRankingApi):
|
||||
def __init__(self, *, bad_node_proof=False, missing_second_child=False):
|
||||
super().__init__()
|
||||
self.rank_done = False
|
||||
self.envelopes = []
|
||||
self.bad_node_proof = bad_node_proof
|
||||
self.missing_second_child = missing_second_child
|
||||
self.root = None
|
||||
self.plan = None
|
||||
|
||||
def call(self, path, payload=None, method=None, *, node_id=None, scope_id=None):
|
||||
self.envelopes.append({'path': path, 'nodeId': node_id, 'scopeId': scope_id})
|
||||
if path == '/api/analysis/protocol/queries':
|
||||
rank_node = next(node for node in self.plan if node['nodeId'] == 'rank')
|
||||
trend_node = next(node for node in self.plan if node['nodeId'] == 'top_trend')
|
||||
rank_scope = {'nodeId': 'rank', 'scopeId': 'rank-scope', 'label': '黑片投入重量排名',
|
||||
'purpose': 'ranking', 'limit': 5,
|
||||
'task': _scope_task(self.root, purpose='ranking', fields=rank_node['fields'],
|
||||
stages=rank_node['stages'], selector_kind='base', dimension='product',
|
||||
visual_type='pareto')}
|
||||
scopes = [rank_scope]
|
||||
pending = [{'nodeId': 'top_trend', 'reason': '等待正式排名结果'}]
|
||||
if self.rank_done:
|
||||
scopes = [rank_scope]
|
||||
for suffix, product in [('a', 'product a'), ('b', 'product b')]:
|
||||
scopes.append({'nodeId': 'top_trend', 'scopeId': 'trend-' + suffix,
|
||||
'label': product, 'purpose': 'trend',
|
||||
'task': _scope_task(self.root, purpose='trend', fields=trend_node['fields'],
|
||||
stages=trend_node['stages'], selector_kind='ranking_top', product=product,
|
||||
visual_type='line')})
|
||||
if self.missing_second_child:
|
||||
scopes.pop()
|
||||
pending = []
|
||||
else:
|
||||
pending = []
|
||||
return {'version': 1, 'scopes': scopes, 'pending': pending, 'gaps': []}
|
||||
|
||||
response = super().call(path, payload, method)
|
||||
if path.startswith('/api/metrics'):
|
||||
if isinstance(response.get('summary'), dict):
|
||||
response['summary'].setdefault('black', {})['scrapRate'] = 0.05
|
||||
for month in response.get('trend', []):
|
||||
if isinstance(month, dict):
|
||||
month.setdefault('black', {})['scrapRate'] = 0.05
|
||||
parsed = parse_qs(urlsplit(path).query)
|
||||
proof = response.get('protocolEvidence')
|
||||
if isinstance(proof, dict):
|
||||
proof['nodeId'] = 'untrusted' if self.bad_node_proof else node_id
|
||||
proof['scopeId'] = scope_id
|
||||
proof['queryId'] = proof['queryId'] + '-' + str(len(self.envelopes))
|
||||
proof['responseHash'] = f'{len(self.envelopes):064x}'
|
||||
if parsed.get('view') == ['product']:
|
||||
self.rank_done = True
|
||||
return response
|
||||
|
||||
|
||||
class QueryPlanExecutionTests(unittest.TestCase):
|
||||
def task(self):
|
||||
plan = [
|
||||
{'nodeId': 'rank', 'purpose': 'ranking', 'dimension': 'product', 'limit': 5,
|
||||
'fields': ['totalWeight'], 'stages': ['black'], 'selector': {'kind': 'base'},
|
||||
'visualType': 'pareto', 'visualPreference': 'auto'},
|
||||
{'nodeId': 'top_trend', 'purpose': 'trend', 'fields': ['scrapRate'],
|
||||
'stages': ['black'], 'selector': {'kind': 'ranking_top', 'sourceNodeId': 'rank',
|
||||
'stage': 'black', 'metric': 'totalWeight', 'count': 2},
|
||||
'visualType': 'line', 'visualPreference': 'auto'},
|
||||
]
|
||||
value = task_value(kind='analysis', queryPlan=plan, dimensions=['product'],
|
||||
dimensionStages={'product': ['black']}, stages=['black'],
|
||||
fields=['totalWeight', 'scrapRate'], trendRequired=True,
|
||||
comparisonRequired=False, period={'year': 2026}, filterUpdates={'department': '部门甲'})
|
||||
return normalize_task(value, '按黑片投入重量排名后查看前两项报废率趋势',
|
||||
{'_protocol': {'version': 1}})
|
||||
|
||||
def run_query(self, api, *, final_answer='已核对排名和趋势'):
|
||||
task = self.task()
|
||||
api.root = task
|
||||
api.plan = task['queryPlan']
|
||||
request = {'releaseId': RELEASE, 'turnId': TURN, 'question': task['question']}
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
root = Path(folder); (root / 'evidence').mkdir(); (root / 'private').mkdir()
|
||||
with patch('nao_evidence_finalizer.synthesize', return_value={
|
||||
'answer': final_answer, 'evidenceSufficient': True,
|
||||
'missingEvidence': [], 'usage': {}}) as synth:
|
||||
result = execute(request=request, task=task,
|
||||
authority={**identity(), 'task': task}, actor_root=root, delegated=api,
|
||||
connection=('offline', 'synthetic', 'model'),
|
||||
cancel_event=threading.Event(), emit=lambda _: None)
|
||||
documents = [json.loads(path.read_text()) for path in (root / 'evidence').glob('nao-quality-*.json')]
|
||||
return result, documents, synth
|
||||
|
||||
def test_rank_then_two_server_resolved_trends_use_scoped_envelopes_and_one_finalizer(self):
|
||||
api = QueryPlanApi()
|
||||
result, documents, synth = self.run_query(api)
|
||||
self.assertEqual(result['status'], 'COMPLETED', result['evidenceCompletion']['missingEvidence'])
|
||||
self.assertEqual(result['stopReason'], 'evidence_finalized')
|
||||
self.assertEqual(result['completionMethod'], 'bounded_scoped_query_plan')
|
||||
self.assertEqual(result['finishReason'], 'stop')
|
||||
self.assertEqual({doc['mode'] for doc in documents}, {'rank', 'query'})
|
||||
self.assertEqual(len(documents), 3)
|
||||
self.assertEqual(len(result['protocolEvidence']), 3)
|
||||
self.assertEqual({proof['nodeId'] for proof in result['protocolEvidence']}, {'rank', 'top_trend'})
|
||||
self.assertEqual({proof['scopeId'] for proof in result['protocolEvidence']},
|
||||
{'rank-scope', 'trend-a', 'trend-b'})
|
||||
expected_agent_context = {'releaseId': RELEASE,
|
||||
'filters': api.root['filters'],
|
||||
'period': api.root['period']}
|
||||
if api.root.get('comparisonPeriod') is not None:
|
||||
expected_agent_context['comparisonPeriod'] = api.root['comparisonPeriod']
|
||||
self.assertEqual(result['agentContext'], expected_agent_context)
|
||||
self.assertNotIn('_protocolScopes', result['agentContext'])
|
||||
self.assertNotIn('product', result['agentContext']['filters'])
|
||||
self.assertTrue(all(call['nodeId'] is not None and call['scopeId'] is not None
|
||||
for call in api.envelopes if call['path'] != '/api/analysis/protocol/queries'))
|
||||
self.assertEqual(len([call for call in api.envelopes
|
||||
if call['path'] == '/api/analysis/protocol/queries']), 3)
|
||||
synth.assert_called_once()
|
||||
context = synth.call_args.kwargs['context']
|
||||
self.assertEqual(context['filters'], api.root['filters'])
|
||||
self.assertEqual(len(context['_protocolScopes']['scopes']), 3)
|
||||
|
||||
def test_forged_scope_proof_and_missing_resolved_child_finish_partial(self):
|
||||
for api in (QueryPlanApi(bad_node_proof=True), QueryPlanApi(missing_second_child=True)):
|
||||
with self.subTest(api=api):
|
||||
result, documents, _ = self.run_query(api)
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertFalse(result['evidenceCompletion']['serverCoverageSufficient'])
|
||||
self.assertTrue(result['evidenceCompletion']['missingEvidence'])
|
||||
|
||||
def test_requested_scope_visual_gap_finishes_partial_even_when_text_evidence_is_complete(self):
|
||||
api = QueryPlanApi()
|
||||
with patch('analysis_visual_evidence.protocol_visual_result', return_value={
|
||||
'visualSchemaVersion': 1, 'visualState': 'ready', 'visuals': [],
|
||||
'missingEvidence': ['节点趋势图未覆盖。']}):
|
||||
result, _, _ = self.run_query(api)
|
||||
self.assertEqual(result['evidenceCompletion']['serverCoverageSufficient'], True)
|
||||
self.assertEqual(result['status'], 'PARTIAL')
|
||||
self.assertIn('节点趋势图未覆盖。', result['evidenceCompletion']['missingEvidence'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user