统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
"""Post-aggregation selections over unchanged, complete formal metric rows."""
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import math
|
||||
import operator
|
||||
import re
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
def validate_selection(value, *, pending=False):
|
||||
if not isinstance(value, dict) or set(value) - {'dimension', 'metric', 'rateStage', 'direction', 'limit', 'weightFilter', 'requestQuestion'}:
|
||||
raise ValueError('汇总排名条件格式无效')
|
||||
result = copy.deepcopy(value)
|
||||
if (result.get('dimension') not in ('product', 'department', 'group')
|
||||
or result.get('metric') != 'rate' or result.get('rateStage') not in (None, 'black', 'bright')
|
||||
or result.get('direction') not in ('asc', 'desc')
|
||||
or type(result.get('limit')) is not int or not 1 <= result['limit'] <= (10000 if pending else 100)):
|
||||
raise ValueError('汇总合格率排名支持 Top1—100,需明确对象和排序方向')
|
||||
weight = result.get('weightFilter')
|
||||
if weight is not None:
|
||||
if (not isinstance(weight, dict) or set(weight) != {'metric', 'stage', 'operator', 'value'}
|
||||
or weight.get('metric') not in ('totalWeight', 'scrapWeight')
|
||||
or weight.get('stage') not in ('black', 'bright', 'any', 'all', None)
|
||||
or weight.get('stage') is None and not pending
|
||||
or weight.get('operator') not in ('gt', 'gte', 'lt', 'lte')
|
||||
or type(weight.get('value')) not in (int, float, Decimal)
|
||||
or not math.isfinite(weight['value']) or weight['value'] < 0):
|
||||
raise ValueError('汇总重量条件需明确阶段、比较符及非负 kg 数值')
|
||||
if isinstance(weight['value'], Decimal):
|
||||
weight['value'] = float(weight['value'])
|
||||
if not isinstance(result.get('requestQuestion'), str) or not 1 <= len(result['requestQuestion']) <= 4000:
|
||||
raise ValueError('汇总排名必须保留原问题')
|
||||
return result
|
||||
|
||||
|
||||
def selection_hint(question, context=None):
|
||||
"""Bind literal Top/threshold syntax; let the model phrase any clarification."""
|
||||
context = context or {}
|
||||
previous = context.get('previousTask') or context.get('_agentTask') or (context.get('_protocol') or {}).get('previousTask') or {}
|
||||
if (previous.get('needsClarification') and isinstance(previous.get('aggregateSelection'), dict)
|
||||
and not re.search(r'新任务|换个问题|另一个问题|不查了|取消(?:分析|任务)', question)):
|
||||
result = copy.deepcopy(previous['aggregateSelection'])
|
||||
# A clarification answer may name either stage or an explicit Boolean relation.
|
||||
black, bright = '黑片' in question, '亮片' in question
|
||||
if result.get('weightFilter'):
|
||||
any_stage = bool(re.search(r'任一|任意(?:一|1)|或|(?<![a-z])or(?![a-z])', question, re.I))
|
||||
all_stages = bool(re.search(r'都|均|同时|且|(?<![a-z])and(?![a-z])|全部', question, re.I))
|
||||
if any_stage:
|
||||
result['weightFilter']['stage'] = 'any'
|
||||
elif all_stages and (black and bright or re.search(r'两阶段|两个阶段', question)):
|
||||
result['weightFilter']['stage'] = 'all'
|
||||
elif black != bright and not ('合格率' in question and not re.search(r'重量|投入|满足', question)):
|
||||
result['weightFilter']['stage'] = 'black' if black else 'bright'
|
||||
changed_count = re.search(r'(?:top|前)\s*(\d{1,4})', question, re.I)
|
||||
if changed_count:
|
||||
result['limit'] = int(changed_count[1])
|
||||
changed_rate = re.search(r'(黑片|亮片)合格率', question)
|
||||
if changed_rate:
|
||||
result['rateStage'] = {'黑片': 'black', '亮片': 'bright'}[changed_rate[1]]
|
||||
return result
|
||||
match = re.search(r'(?:top|前)\s*(\d{1,4})', question, re.I)
|
||||
if not match or not re.search(r'合格率.{0,12}(?:最低|最高)|(?:最低|最高).{0,12}合格率', question):
|
||||
return None
|
||||
dimension = next((key for label, key in (('产品', 'product'), ('产业部', 'department'), ('部门', 'department'), ('分组', 'group')) if label in question), None)
|
||||
if not dimension:
|
||||
return None
|
||||
rate = re.search(r'(黑片|亮片)(?:综合)?合格率', question)
|
||||
result = {'dimension': dimension, 'metric': 'rate', 'rateStage': {'黑片': 'black', '亮片': 'bright'}.get(rate[1]) if rate else None,
|
||||
'direction': 'asc' if '最低' in question else 'desc', 'limit': int(match[1]), 'requestQuestion': question}
|
||||
weight = re.search(r'(?P<scope>(?:(?:黑片|亮片)[^。;,,]{0,12})?)(?P<kind>投入重量|生产重量|报废重量|重量)\s*(?P<op>大于等于|不低于|至少|小于等于|不超过|大于|超过|小于|低于|>=|<=|>|<|≥|≤)\s*(?P<value>\d+(?:\.\d+)?)\s*(?:kg|公斤|千克)', question, re.I)
|
||||
if weight:
|
||||
scope = weight['scope']; black, bright = '黑片' in scope, '亮片' in scope
|
||||
stage = ('all' if re.search(r'都|均|同时|且|(?<![a-z])and(?![a-z])', scope, re.I) else 'any' if re.search(r'任一|或|(?<![a-z])or(?![a-z])', scope, re.I) else None) if black and bright else 'black' if black else 'bright' if bright else None
|
||||
op = weight['op']; op = 'gte' if op in ('大于等于', '不低于', '至少', '>=', '≥') else 'lte' if op in ('小于等于', '不超过', '<=', '≤') else 'gt' if op in ('大于', '超过', '>') else 'lt'
|
||||
result['weightFilter'] = {'metric': 'scrapWeight' if '报废' in weight['kind'] else 'totalWeight', 'stage': stage, 'operator': op, 'value': float(weight['value'])}
|
||||
return result
|
||||
|
||||
|
||||
def select_rows(result, selection):
|
||||
selection = validate_selection(selection)
|
||||
rows = result.get('rows')
|
||||
count = result.get('rowCount')
|
||||
if (result.get('kind') != 'quality_metrics' or result.get('view') != selection['dimension']
|
||||
or not isinstance(rows, list) or type(count) is not int or count != len(rows)
|
||||
or result.get('rowsTruncated') is not False):
|
||||
raise ValueError('汇总分组读取不完整,不能据此筛选排名')
|
||||
def number(value):
|
||||
return type(value) in (float, int) and math.isfinite(value)
|
||||
weight_filter = selection.get('weightFilter')
|
||||
eligible, missing, keys = [], 0, set()
|
||||
for row in rows:
|
||||
key = row.get('key')
|
||||
if not isinstance(key, str) or not key or key in keys:
|
||||
raise ValueError('分组键缺失或重复,不能稳定排序')
|
||||
keys.add(key)
|
||||
stage = selection['rateStage']
|
||||
rate = (row.get(stage) or {}).get('rate') if stage else row.get('compositeRate')
|
||||
# No-production fallback rates are not measured yields.
|
||||
coverage = row.get('coverage') or {}
|
||||
measured = coverage.get(stage + 'Rows', 0) > 0 if stage and stage + 'Rows' in coverage else True
|
||||
if not number(rate) or rate < 0 or not measured:
|
||||
missing += 1
|
||||
continue
|
||||
if weight_filter:
|
||||
stages = [weight_filter['stage']] if weight_filter['stage'] in ('black', 'bright') else ['black', 'bright']
|
||||
values = [(row.get(s) or {}).get(weight_filter['metric']) for s in stages]
|
||||
compare = {'gt': operator.gt, 'gte': operator.ge, 'lt': operator.lt, 'lte': operator.le}[weight_filter['operator']]
|
||||
matches = [number(v) and compare(v, weight_filter['value']) for v in values]
|
||||
if not (any(matches) if weight_filter['stage'] == 'any' else all(matches)):
|
||||
continue
|
||||
eligible.append((rate, key, row))
|
||||
eligible.sort(key=lambda item: (item[0] if selection['direction'] == 'asc' else -item[0], item[1]))
|
||||
selected = [dict(copy.deepcopy(row), rank=index + 1, selectedRate=rate) for index, (rate, key, row) in enumerate(eligible[:selection['limit']])]
|
||||
return {'selection': selection, 'rows': selected, 'sourceRows': count, 'eligibleRows': len(eligible),
|
||||
'missingRateRows': missing, 'returnedRows': len(selected), 'completeSource': True,
|
||||
'basis': '先按正式口径汇总全部授权分组,再按汇总重量条件筛选,按正式合格率排序,最后取Top N;不重算率,不平均产品率。'}
|
||||
|
||||
|
||||
def project_selection(result, task):
|
||||
selection = (task or {}).get('aggregateSelection')
|
||||
if not selection or result.get('kind') != 'quality_metrics' or result.get('view') != selection['dimension']:
|
||||
return result
|
||||
selected = select_rows(result, selection)
|
||||
# The signed original keeps every field. Give the model only the requested
|
||||
# ranking columns, rather than 100 copies of defects and period summaries.
|
||||
rows = []
|
||||
for row in selected['rows']:
|
||||
compact = {key: row[key] for key in ('key', 'label', 'rank', 'selectedRate') if key in row}
|
||||
for stage in ('black', 'bright'):
|
||||
compact[stage] = {key: (row.get(stage) or {}).get(key) for key in ('rate', 'totalWeight', 'scrapWeight')}
|
||||
rows.append(compact)
|
||||
view = {key: value for key, value in result.items() if key not in ('summary', 'trend', 'trendCount', 'trendTruncated')}
|
||||
return {**view, 'rows': rows, 'rowsReturned': selected['returnedRows'],
|
||||
'selectionDisplay': {key: value for key, value in selected.items() if key != 'rows'}}
|
||||
|
||||
|
||||
def ranking_chart(document, task, turn_id):
|
||||
from analysis_visuals import _chart, _chart_scope
|
||||
selection = task['aggregateSelection']
|
||||
result = document['result']; selected = select_rows(result, selection); rows = selected['rows']
|
||||
if not rows:
|
||||
return None
|
||||
stage = selection['rateStage']; label = {'black': '黑片', 'bright': '亮片', None: '综合'}[stage]
|
||||
# Split long rankings into readable native SVG panels, retaining every row.
|
||||
charts = []
|
||||
for offset in range(0, len(rows), 25):
|
||||
page = rows[offset:offset + 25]
|
||||
charts.append(_chart(turn_id=turn_id, chart_type='bar', title=f'{label}合格率排名 · {offset+1}—{offset+len(page)}',
|
||||
subtitle=f"{'由低到高' if selection['direction'] == 'asc' else '由高到低'} · 符合条件 {selected['eligibleRows']} 项 · 展示 {len(rows)} 项",
|
||||
unit='ratio', categories=[{'key': row['key'], 'label': f"{row['rank']}. {row.get('label') or row['key']}"} for row in page],
|
||||
series=[{'key': stage+'_rate' if stage else 'composite_rate', 'label': label + '合格率', 'values': [row['selectedRate'] for row in page]}],
|
||||
evidence_refs=[document['evidenceRef']], required_fields=([stage+'_good_weight', stage+'_total_weight'] if stage else ['black_good_weight', 'black_total_weight', 'bright_good_weight', 'bright_total_weight']),
|
||||
scope=_chart_scope(result.get('filters', {}), result['releaseId'], result['period']), period=result['period'],
|
||||
metric=stage+'_rate' if stage else 'composite_rate', stage=stage, dimension=selection['dimension'], section_key=selection['dimension'],
|
||||
coverage={'returnedRows': len(page), 'totalRows': selected['eligibleRows'], 'truncated': selected['returnedRows'] < selected['eligibleRows']},
|
||||
note=selected['basis']))
|
||||
return charts
|
||||
Reference in New Issue
Block a user