同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。 补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。 统一午夜黑及其他主题的控件细节、中文和业务数字字体。 排除本地业务数据、凭据、预览、发布与验收产物。 验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
This commit is contained in:
@@ -0,0 +1,286 @@
|
||||
"""Formal stage scrap-rate evidence, coverage, report, and chart contracts."""
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
|
||||
|
||||
from analysis_visuals import _comparison_charts, _trend_chart_from_points, build_visuals
|
||||
from nao_agent_report import _build_trend_report, build_noncomparison_report
|
||||
from nao_analysis_coverage import formal_metric_coverage, semantic_metric_gaps
|
||||
from nao_evidence_finalizer import _formal_metric_report
|
||||
from nao_evidence_report import build_report
|
||||
|
||||
|
||||
RELEASE = 'release-scrap-rate'
|
||||
QUESTION = '查询2026年9月黑片报废率'
|
||||
PERIOD = {'year': 2026, 'month': 9}
|
||||
COMPARISON_PERIOD = {'year': 2026, 'month': 8}
|
||||
|
||||
|
||||
def task(question=QUESTION, *, period=PERIOD, comparison_period=None, stages=None,
|
||||
fields=None, trend=False, visual='none'):
|
||||
return {
|
||||
'version': 1,
|
||||
'question': question,
|
||||
'kind': 'metrics',
|
||||
'filters': {'product': '产品甲'},
|
||||
'period': period,
|
||||
'comparisonPeriod': comparison_period,
|
||||
'stages': stages if stages is not None else ['black'],
|
||||
'fields': fields if fields is not None else ['scrapRate'],
|
||||
'dimensions': [],
|
||||
'trendRequired': trend,
|
||||
'comparisonRequired': comparison_period is not None,
|
||||
'freshQuery': True,
|
||||
'verifySource': False,
|
||||
'fullDetails': False,
|
||||
'needsClarification': False,
|
||||
'visualPreference': visual,
|
||||
'visualType': None,
|
||||
'responseStyle': 'standard',
|
||||
'periodCoverage': 'available',
|
||||
'historyMode': 'none',
|
||||
}
|
||||
|
||||
|
||||
def metric_doc(*, value=0.125, month=9, filters=None, stage='black', rate=None,
|
||||
evidence='A', rows=2):
|
||||
period = {'year': 2026, 'month': month}
|
||||
summary_stage = {'scrapRate': value, 'totalWeight': 100,
|
||||
'scrapWeight': value * 100 if isinstance(value, (int, float)) and value >= 0 else 0}
|
||||
if rate is not None:
|
||||
summary_stage['rate'] = rate
|
||||
return {
|
||||
'tool': 'quality_data', 'mode': 'query', 'releaseId': RELEASE,
|
||||
'evidenceRef': 'Q' + evidence * 8,
|
||||
'result': {
|
||||
'kind': 'quality_metrics', 'releaseId': RELEASE, 'period': period,
|
||||
'filters': filters or {'product': '产品甲'}, 'view': 'overall',
|
||||
'summary': {stage: summary_stage},
|
||||
'coverage': {'sourceMonths': [{'year': 2026, 'month': month,
|
||||
'blackRows': rows if stage == 'black' else 0,
|
||||
'brightRows': rows if stage == 'bright' else 0}]},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def comparison_doc(*, black_values=(0.05, 0.0), release=RELEASE, filters=None,
|
||||
black_rows=(2, 2), evidence='B'):
|
||||
def snapshot(month, value, rows):
|
||||
return {
|
||||
'period': {'year': 2026, 'month': month},
|
||||
'compositeRate': 0.94,
|
||||
'black': {'scrapRate': value, 'rate': 0.95, 'totalWeight': 100, 'scrapWeight': 5},
|
||||
'bright': {'scrapRate': 0.06, 'rate': 0.96, 'totalWeight': 90, 'scrapWeight': 4},
|
||||
'coverage': {'sourceMonths': [{'year': 2026, 'month': month,
|
||||
'blackRows': rows, 'brightRows': 2}]},
|
||||
}
|
||||
result = {
|
||||
'kind': 'quality_comparison', 'releaseId': release,
|
||||
'filterBinding': filters or {'product': '产品甲'},
|
||||
'comparison': snapshot(8, black_values[0], black_rows[0]),
|
||||
'current': snapshot(9, black_values[1], black_rows[1]),
|
||||
'delta': {'available': True, 'percentagePoints': 0},
|
||||
'stageRateChangesPP': {'black': 0, 'bright': 0},
|
||||
}
|
||||
return {'tool': 'quality_data', 'mode': 'compare', 'releaseId': release,
|
||||
'evidenceRef': 'Q' + evidence * 8, 'result': result}
|
||||
|
||||
|
||||
class ScrapRateEvidenceTest(unittest.TestCase):
|
||||
def context(self, selected_task):
|
||||
return {'releaseId': RELEASE, 'period': selected_task['period'],
|
||||
'comparisonPeriod': selected_task['comparisonPeriod'],
|
||||
'filters': selected_task['filters'], '_agentTask': selected_task}
|
||||
|
||||
def test_single_period_uses_scrap_rate_directly_and_keeps_zero_and_over_one(self):
|
||||
for raw, expected in ((0, '0.00%'), (1.25, '125.00%')):
|
||||
selected = task()
|
||||
report = build_noncomparison_report(
|
||||
[metric_doc(value=raw, rate=None)], self.context(selected), QUESTION)
|
||||
self.assertEqual([fact['label'] for fact in report['facts']], ['黑片报废率'])
|
||||
self.assertIn(expected, report['facts'][0]['text'])
|
||||
self.assertTrue(report['coverageSufficient'])
|
||||
|
||||
def test_requested_numerator_and_denominator_are_displayed_as_independent_formal_values(self):
|
||||
question='黑片报废率是多少,同时列出分子分母'
|
||||
selected=task(question,fields=['scrapRate','scrapWeight','totalWeight'])
|
||||
doc=metric_doc(value=.175,rate=.99)
|
||||
doc['result']['summary']['black'].update(totalWeight=200,scrapWeight=35)
|
||||
report=build_noncomparison_report([doc],self.context(selected),question)
|
||||
rendered='\n'.join(report['sections'])
|
||||
for expected in ('17.50%','35.00','200.00'):
|
||||
self.assertIn(expected,rendered)
|
||||
self.assertEqual([f['label'] for f in report['facts']],['黑片报废率','黑片报废重量','黑片投入重量'])
|
||||
self.assertTrue(report['coverageSufficient'])
|
||||
self.assertNotIn('1.00%',rendered)
|
||||
|
||||
def test_missing_requested_denominator_is_incomplete_even_when_formal_rate_exists(self):
|
||||
question='黑片报废率及报废总量和修正总重'
|
||||
selected=task(question,fields=['scrapRate','scrapWeight','totalWeight'])
|
||||
doc=metric_doc();doc['result']['summary']['black'].pop('totalWeight')
|
||||
report=build_noncomparison_report([doc],self.context(selected),question)
|
||||
self.assertFalse(report['coverageSufficient'])
|
||||
self.assertTrue(any('重量' in gap for gap in report['missingEvidence']))
|
||||
|
||||
def test_missing_or_negative_scrap_rate_is_not_treated_as_zero_or_clamped(self):
|
||||
for raw in (None, -0.01):
|
||||
selected = task()
|
||||
report = build_noncomparison_report(
|
||||
[metric_doc(value=raw, rate=0.99)], self.context(selected), QUESTION)
|
||||
self.assertIn('未提供', report['facts'][0]['text'])
|
||||
self.assertFalse(report['coverageSufficient'])
|
||||
self.assertEqual(semantic_metric_gaps(
|
||||
[metric_doc(value=None)], QUESTION, self.context(task())),
|
||||
['未取得指定期间、范围的黑片报废率正式指标或来源覆盖。'])
|
||||
|
||||
def test_scrap_rate_coverage_does_not_require_qualified_rate_or_other_stage(self):
|
||||
selected = task()
|
||||
doc = metric_doc(value=0.02, rate=None)
|
||||
self.assertEqual(semantic_metric_gaps([doc], QUESTION, self.context(selected)), [])
|
||||
report = build_noncomparison_report([doc], self.context(selected), QUESTION)
|
||||
self.assertEqual([fact['label'] for fact in report['facts']], ['黑片报废率'])
|
||||
self.assertNotIn('亮片', '\n'.join(fact['text'] for fact in report['facts']))
|
||||
self.assertNotIn('合格率', '\n'.join(fact['text'] for fact in report['facts']))
|
||||
|
||||
def test_scope_and_period_mismatch_are_rejected(self):
|
||||
selected = task()
|
||||
wrong_scope = metric_doc(value=0.02, filters={'product': '产品乙'})
|
||||
wrong_period = metric_doc(value=0.02, month=8)
|
||||
self.assertTrue(semantic_metric_gaps([wrong_scope], QUESTION, self.context(selected)))
|
||||
self.assertTrue(semantic_metric_gaps([wrong_period], QUESTION, self.context(selected)))
|
||||
|
||||
def test_two_period_formal_contract_and_body_use_only_scrap_rate(self):
|
||||
question = '只看产品甲,2026年9月与8月黑片报废率'
|
||||
selected = task(question, period=PERIOD, comparison_period=COMPARISON_PERIOD)
|
||||
context = self.context(selected)
|
||||
doc = comparison_doc(black_values=(1.25, 0.0))
|
||||
contract = formal_metric_coverage([doc], question, context)
|
||||
self.assertEqual(contract['fields'], ['scrapRate'])
|
||||
self.assertEqual(contract['missingEvidence'], [])
|
||||
self.assertEqual([item['value'] for item in contract['metrics']], [0.0, 1.25])
|
||||
report = _formal_metric_report(contract)
|
||||
text = '\n'.join(report['sections']) + '\n' + '\n'.join(item['text'] for item in report['facts'])
|
||||
self.assertIn('125.00%', text)
|
||||
self.assertIn('0.00%', text)
|
||||
self.assertNotIn('合格率', text)
|
||||
|
||||
wrong_scope = comparison_doc(filters={'product': '产品乙'})
|
||||
rejected = formal_metric_coverage([wrong_scope], question, context)
|
||||
self.assertTrue(rejected['missingEvidence'])
|
||||
|
||||
comparison_question = '对比2026年9月与8月黑片报废率'
|
||||
selected = task(comparison_question, period=PERIOD,
|
||||
comparison_period=COMPARISON_PERIOD, stages=['black'])
|
||||
body = build_report([doc], self.context(selected), comparison_question)
|
||||
body_text = '\n'.join(body['sections']) + '\n' + '\n'.join(fact['text'] for fact in body['facts'])
|
||||
self.assertTrue(body['coverageSufficient'], body['missingEvidence'])
|
||||
self.assertIn('125.00%', body_text)
|
||||
self.assertIn('0.00%', body_text)
|
||||
self.assertIn('黑片报废率', body_text)
|
||||
self.assertNotIn('亮片报废率', body_text)
|
||||
|
||||
def test_formal_scrap_weight_contract_rejects_negative_backend_weight(self):
|
||||
question = '只看产品甲,2026年9月与8月黑片报废重量'
|
||||
selected = task(question, period=PERIOD, comparison_period=COMPARISON_PERIOD,
|
||||
fields=['scrapWeight'])
|
||||
document = comparison_doc()
|
||||
document['result']['current']['black']['scrapWeight'] = -0.01
|
||||
contract = formal_metric_coverage([document], question, self.context(selected))
|
||||
self.assertTrue(contract['missingEvidence'])
|
||||
invalid = [metric for metric in contract['metrics']
|
||||
if metric['field'] == 'scrapWeight' and metric['value'] < 0]
|
||||
self.assertEqual(len(invalid), 1)
|
||||
self.assertFalse(invalid[0]['covered'])
|
||||
|
||||
def test_omitted_stage_scrap_rate_stays_two_stage_not_a_composite(self):
|
||||
question = '查询2026年9月产品甲报废率'
|
||||
selected = task(question, stages=[])
|
||||
context = self.context(selected)
|
||||
doc = metric_doc(value=0.04)
|
||||
doc['result']['summary']['bright'] = {'scrapRate': 0.03, 'totalWeight': 90, 'scrapWeight': 2.7}
|
||||
doc['result']['coverage']['sourceMonths'][0]['brightRows'] = 2
|
||||
self.assertEqual(semantic_metric_gaps([doc], question, context), [])
|
||||
report = build_noncomparison_report([doc], context, question)
|
||||
self.assertEqual({fact['label'] for fact in report['facts']}, {'黑片报废率', '亮片报废率'})
|
||||
self.assertFalse(any('综合报废率' in fact['text'] for fact in report['facts']))
|
||||
|
||||
def test_monthly_trend_uses_server_values_and_preserves_same_value(self):
|
||||
question = '查询2026年黑片报废率趋势'
|
||||
selected = task(question, period={'year': 2026}, trend=True, visual='required')
|
||||
trend = []
|
||||
source_months = []
|
||||
for month in (1, 2, 3):
|
||||
trend.append({'period': f'2026-{month:02d}',
|
||||
'black': {'scrapRate': 0.08, 'totalWeight': 100, 'scrapWeight': 8},
|
||||
'coverage': {'blackRows': 2, 'brightRows': 0}})
|
||||
source_months.append({'year': 2026, 'month': month, 'blackRows': 2, 'brightRows': 0})
|
||||
doc = metric_doc(value=0.08)
|
||||
doc['result'].update(period={'year': 2026}, trend=trend, trendCount=3,
|
||||
trendTruncated=False,
|
||||
coverage={'sourceMonths': source_months})
|
||||
report = _build_trend_report([doc], self.context(selected), question)
|
||||
self.assertTrue(report['coverageSufficient'], report['missingEvidence'])
|
||||
self.assertEqual([fact['label'] for fact in report['facts']], ['黑片报废率月度趋势'])
|
||||
self.assertIn('1月8.00%', report['facts'][0]['text'])
|
||||
self.assertIn('3月8.00%', report['facts'][0]['text'])
|
||||
self.assertIn('持平', report['facts'][0]['text'])
|
||||
self.assertNotIn('合格率', report['facts'][0]['text'])
|
||||
|
||||
def test_trend_visual_uses_scrap_rate_contract_and_has_no_good_rate_series(self):
|
||||
points = [
|
||||
{'period': f'2026-0{month}', 'snapshot': {
|
||||
'black': {'scrapRate': value, 'rate': 0.95,
|
||||
'totalWeight': 100, 'scrapWeight': value * 100},
|
||||
'coverage': {'blackRows': 2, 'brightRows': 0}}}
|
||||
for month, value in ((1, 0.0), (2, 1.25), (3, 0.0))
|
||||
]
|
||||
charts = _trend_chart_from_points(
|
||||
points, release_id=RELEASE, scope={'product': '产品甲'}, turn_id='turn-scrap',
|
||||
evidence_refs=['QAAAAAAAA'], question='黑片报废率趋势', supplied_trend=True,
|
||||
coverage_truncated=False, semantic_fields={'scrapRate'}, semantic_stages=['black'])
|
||||
self.assertEqual([chart['metric'] for chart in charts], ['black_scrap_rate'])
|
||||
self.assertEqual(charts[0]['unit'], 'ratio')
|
||||
self.assertEqual(charts[0]['requiredFields'], ['black_scrap_weight', 'black_total_weight'])
|
||||
self.assertEqual(charts[0]['series'][0]['values'], [0.0, 1.25, 0.0])
|
||||
|
||||
def test_comparison_chart_is_stage_specific_and_uses_scrap_authorization(self):
|
||||
document = comparison_doc(black_values=(1.25, 0.0))
|
||||
charts = _comparison_charts(
|
||||
document, document['result'], 'turn-compare', '', requested_stage='black',
|
||||
semantic_fields={'scrapRate'})
|
||||
self.assertEqual([chart['metric'] for chart in charts], ['black_scrap_rate'])
|
||||
self.assertEqual(charts[0]['unit'], 'ratio')
|
||||
self.assertEqual(charts[0]['requiredFields'], ['black_scrap_weight', 'black_total_weight'])
|
||||
self.assertEqual(charts[0]['series'][0]['values'], [1.25, 0.0])
|
||||
self.assertEqual(charts[0]['type'], 'bar')
|
||||
|
||||
def test_comparison_chart_rejects_missing_coverage_and_semantic_output_has_no_extra_rates(self):
|
||||
document = comparison_doc(black_rows=(0, 2))
|
||||
charts = _comparison_charts(document, document['result'], 'turn-compare', '',
|
||||
requested_stage='black', semantic_fields={'scrapRate'})
|
||||
self.assertEqual(charts, [])
|
||||
|
||||
question = '对比2026年9月与8月黑片报废率'
|
||||
selected = task(question, period=PERIOD, comparison_period=COMPARISON_PERIOD,
|
||||
visual='required')
|
||||
context = self.context(selected)
|
||||
visuals = build_visuals([comparison_doc()], question, 'turn-semantic', context=context)['visuals']
|
||||
self.assertEqual([chart['metric'] for chart in visuals], ['black_scrap_rate'])
|
||||
|
||||
both_question = '对比2026年9月与8月黑片和亮片报废率'
|
||||
both_task = task(both_question, period=PERIOD, comparison_period=COMPARISON_PERIOD,
|
||||
stages=['black', 'bright'], visual='required')
|
||||
both_visuals = build_visuals([comparison_doc()], both_question, 'turn-both',
|
||||
context=self.context(both_task))['visuals']
|
||||
self.assertEqual({chart['metric'] for chart in both_visuals},
|
||||
{'black_scrap_rate', 'bright_scrap_rate'})
|
||||
|
||||
plain_question = '比较黑片报废率'
|
||||
plain_visuals = build_visuals([comparison_doc()], plain_question, 'turn-plain')['visuals']
|
||||
self.assertEqual([chart['metric'] for chart in plain_visuals], ['black_scrap_rate'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user