统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import copy
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
|
||||
from analysis_visuals import build_visuals
|
||||
from nao_agent_task import bind_task
|
||||
from analysis_visual_evidence import protocol_visual_result
|
||||
|
||||
|
||||
def fixture(dimension='group', fields=None, stages=None, count=3):
|
||||
fields = fields or ['scrapWeight']
|
||||
stages = ['black', 'bright'] if stages is None else stages
|
||||
task = dict(version=1, kind='metrics', question='各对象指标排名', fields=fields,
|
||||
stages=stages, dimensions=[dimension], dimensionStages={dimension: stages},
|
||||
period={'year': 2026, 'month': 10}, filters={},
|
||||
visualPreference='auto', visualType=None, trendRequired=False,
|
||||
comparisonRequired=False)
|
||||
rows = [dict(key=f'item-{i:03}', label=f'对象{i}',
|
||||
black={'totalWeight': 100, 'scrapWeight': i + 1, 'rate': .99 - i / 100, 'scrapRate': .01 + i / 100},
|
||||
bright={'totalWeight': 200, 'scrapWeight': 2 * (i + 1), 'rate': .99 - i / 100, 'scrapRate': .01 + i / 100},
|
||||
compositeRate=.98 - i / 100, coverage={'blackRows': 1, 'brightRows': 1}) for i in range(count)]
|
||||
total = count * (count + 1) / 2
|
||||
result = dict(kind='quality_metrics', releaseId='release', release={'id': 'release'},
|
||||
view=dimension, period=task['period'], filters={}, rows=rows,
|
||||
rowCount=count, rowsReturned=count, rowsTruncated=False,
|
||||
summary={'black': {'totalWeight': count * 100, 'scrapWeight': total},
|
||||
'bright': {'totalWeight': count * 200, 'scrapWeight': 2 * total}},
|
||||
coverage={'sourceMonths': [{'year': 2026, 'month': 10, 'blackRows': count, 'brightRows': count}]})
|
||||
return task, {'tool': 'quality_data', 'mode': 'query', 'releaseId': 'release',
|
||||
'evidenceRef': 'Q1234ABCD', 'result': result}
|
||||
|
||||
|
||||
class GroupedQueryVisualTests(unittest.TestCase):
|
||||
def render(self, task, document):
|
||||
return build_visuals([document], task['question'], 'turn-grouped', context=bind_task({'releaseId': 'release'}, task))
|
||||
|
||||
def test_all_dimensions_and_stage_weights_use_the_ranking_converter(self):
|
||||
for dimension in ('department', 'group', 'product'):
|
||||
for field in ('totalWeight', 'scrapWeight'):
|
||||
with self.subTest(dimension=dimension, field=field):
|
||||
task, doc = fixture(dimension, [field])
|
||||
before = copy.deepcopy(doc)
|
||||
visuals = self.render(task, doc)['visuals']
|
||||
self.assertEqual(len(visuals), 2)
|
||||
self.assertEqual({v['stage'] for v in visuals}, {'black', 'bright'})
|
||||
self.assertEqual(visuals[0]['categories'][0]['key'], 'item-000' if field == 'totalWeight' else 'item-002')
|
||||
self.assertEqual(doc, before)
|
||||
|
||||
def test_single_stage_and_combined_scrap_keep_their_distinct_meanings(self):
|
||||
for stages, expected in ((['bright'], {'bright'}), ([], {None})):
|
||||
task, doc = fixture(stages=stages)
|
||||
visuals = self.render(task, doc)['visuals']
|
||||
self.assertEqual({v.get('stage') for v in visuals}, expected)
|
||||
|
||||
def test_full_top100_and_truncated_input(self):
|
||||
task, doc = fixture(count=123)
|
||||
charts = self.render(task, doc)['visuals']
|
||||
self.assertEqual(len(charts[0]['categories']), 100)
|
||||
self.assertEqual(charts[0]['coverage']['totalRows'], 123)
|
||||
doc['result']['rowsTruncated'] = True
|
||||
self.assertEqual(self.render(task, doc)['visuals'], [])
|
||||
|
||||
def test_query_top_count_controls_chart_rows_without_changing_query_data(self):
|
||||
task, doc = fixture(count=30)
|
||||
task['question'] = '各分组不合格重量Top20'
|
||||
charts = self.render(task, doc)['visuals']
|
||||
self.assertEqual([len(chart['categories']) for chart in charts], [20, 20])
|
||||
self.assertEqual(len(doc['result']['rows']), 30)
|
||||
|
||||
def test_published_rates_are_not_recalculated_and_bad_metrics_are_isolated(self):
|
||||
task, doc = fixture(fields=['rate', 'scrapWeight'], stages=['bright'])
|
||||
doc['result']['summary']['bright']['scrapWeight'] = -1
|
||||
visuals = self.render(task, doc)['visuals']
|
||||
rate = next(v for v in visuals if v['metric'] == 'bright_rate')
|
||||
self.assertEqual(rate['series'][0]['values'], [.99, .98, .97])
|
||||
self.assertFalse(any(v['metric'] == 'bright_scrap_weight' for v in visuals))
|
||||
|
||||
def test_no_charts_for_empty_data_or_explicit_none(self):
|
||||
task, doc = fixture(count=0)
|
||||
self.assertEqual(self.render(task, doc)['visuals'], [])
|
||||
task, doc = fixture()
|
||||
task['visualPreference'] = 'none'
|
||||
self.assertEqual(self.render(task, doc)['visuals'], [])
|
||||
|
||||
def test_each_dimension_uses_published_scrap_rates(self):
|
||||
for dimension in ('department', 'group', 'product'):
|
||||
task, doc = fixture(dimension, ['scrapRate'])
|
||||
charts = self.render(task, doc)['visuals']
|
||||
self.assertEqual({v['metric'] for v in charts}, {'black_scrap_rate', 'bright_scrap_rate'})
|
||||
self.assertEqual(charts[0]['series'][0]['values'], [.01, .02, .03])
|
||||
|
||||
def test_duplicate_rows_and_wrong_release_cannot_enter_charts(self):
|
||||
task, doc = fixture()
|
||||
doc['result']['rows'][1]['key'] = doc['result']['rows'][0]['key']
|
||||
self.assertEqual(self.render(task, doc)['visuals'], [])
|
||||
task, doc = fixture()
|
||||
doc['releaseId'] = doc['result']['releaseId'] = 'wrong-release'
|
||||
self.assertEqual(self.render(task, doc)['visuals'], [])
|
||||
|
||||
def test_broken_scope_does_not_remove_a_healthy_query_chart(self):
|
||||
task, doc = fixture()
|
||||
doc['result']['protocolEvidence'] = {'queryId': 'q1', 'nodeId': 'good', 'scopeId': 'good-scope'}
|
||||
scope = {'nodeId': 'good', 'scopeId': 'good-scope', 'purpose': 'metrics', 'task': task}
|
||||
broken = copy.deepcopy(scope)
|
||||
broken['task']['stages'] = 5
|
||||
broken['nodeId'] = 'broken'
|
||||
result = protocol_visual_result([doc], [broken, scope], task['question'], 'turn-grouped', release_id='release')
|
||||
self.assertEqual(result['visualState'], 'ready')
|
||||
self.assertEqual(len(result['visuals']), 2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user