统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
This commit is contained in:
+205
-4
@@ -8,6 +8,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from decimal import Decimal, InvalidOperation
|
||||
|
||||
from nao_period import PERIOD_KEYS, expected_months, normalize_period
|
||||
|
||||
@@ -628,6 +629,104 @@ def _investigation_docs(evidence, context):
|
||||
return selected, task, history_all, periods, stages, expected_filters
|
||||
|
||||
|
||||
def _source_coverage_complete(coverage, total_rows):
|
||||
if not isinstance(coverage, dict) or type(total_rows) is not int or total_rows <= 0:
|
||||
return False
|
||||
if coverage.get('rows') != total_rows or coverage.get('numericRows') != total_rows:
|
||||
return False
|
||||
return all(type(coverage.get(key)) is int and coverage.get(key) == 0
|
||||
for key in ('keyAbsentRows', 'nullOrBlankRows', 'invalidNumericRows', 'negativeRows'))
|
||||
|
||||
|
||||
def _source_category_fields(category):
|
||||
rows = []
|
||||
for item in category.get('fields') or []:
|
||||
if isinstance(item, dict) and isinstance(item.get('coverage'), dict):
|
||||
rows.append({'field': item.get('field'), **item['coverage']})
|
||||
for item in category.get('fieldCoverageLimits') or []:
|
||||
if isinstance(item, dict):
|
||||
rows.append(dict(item))
|
||||
columns, values = category.get('fieldCoverageColumns'), category.get('fieldCoverage')
|
||||
if isinstance(columns, list) and isinstance(values, list):
|
||||
for row in values:
|
||||
if isinstance(row, list) and len(row) == len(columns):
|
||||
rows.append(dict(zip(columns, row)))
|
||||
unique = {}
|
||||
for row in rows:
|
||||
field = row.get('field')
|
||||
if isinstance(field, str) and field:
|
||||
unique[field] = row
|
||||
return list(unique.values())
|
||||
|
||||
|
||||
def _source_category_reconciliation(category, *, total_rows, selected_fields, source_proof,
|
||||
stage, period, filters, release_id):
|
||||
"""Return a safe, scope-bound summary of formal/source category reconciliation."""
|
||||
category_name = category.get('category')
|
||||
formal_coverage = category.get('formalCoverage')
|
||||
raw_coverage = category.get('rawTotalCoverage')
|
||||
fields = _source_category_fields(category)
|
||||
reasons = []
|
||||
|
||||
proof_complete = (isinstance(source_proof, dict)
|
||||
and source_proof.get('complete') is True
|
||||
and source_proof.get('allSelectedValuesMatchStored') is True
|
||||
and source_proof.get('expectedRows') == total_rows
|
||||
and source_proof.get('verifiedRows') == total_rows
|
||||
and source_proof.get('missingRows') == 0
|
||||
and source_proof.get('mismatchRows') == 0)
|
||||
if not proof_complete:
|
||||
reasons.append('原表所选字段与已存储原始值尚未完整匹配')
|
||||
|
||||
if not _source_coverage_complete(raw_coverage, total_rows):
|
||||
reasons.append('原表对应类别总量字段缺失或覆盖不完整')
|
||||
if not _source_coverage_complete(formal_coverage, total_rows):
|
||||
reasons.append('正式类别分量覆盖不完整')
|
||||
|
||||
field_names = {row.get('field') for row in fields if isinstance(row.get('field'), str)}
|
||||
if category.get('noFineFields') is True or not fields:
|
||||
reasons.append('没有可用的来源细项,无法核对分项合计')
|
||||
else:
|
||||
if not field_names.issubset(selected_fields):
|
||||
reasons.append('本次原表核验未覆盖全部来源细项')
|
||||
if any(not _source_coverage_complete(row, total_rows) for row in fields):
|
||||
reasons.append('来源细项存在缺失或无效值,覆盖不完整')
|
||||
|
||||
if type(category.get('rawFormalMismatchRows')) is not int:
|
||||
reasons.append('缺少原表类别总量与正式分量的逐行对账结果')
|
||||
elif category['rawFormalMismatchRows'] != 0:
|
||||
reasons.append('原表类别总量与正式分量存在逐行差异')
|
||||
|
||||
comparable = category.get('comparableRows')
|
||||
missing_formal = category.get('missingOrInvalidFormalRows')
|
||||
if comparable != total_rows or missing_formal != 0:
|
||||
reasons.append('正式分量与来源细项未覆盖全部对应行')
|
||||
difference = category.get('differenceFormalMinusSubitems')
|
||||
try:
|
||||
delta = Decimal(str(difference)) if difference is not None else None
|
||||
except (InvalidOperation, TypeError, ValueError):
|
||||
delta = None
|
||||
if delta is None:
|
||||
reasons.append('正式分量与来源细项合计无法完整对账')
|
||||
elif abs(delta) > Decimal('0.000001'):
|
||||
reasons.append('正式分量与来源细项合计存在差异')
|
||||
if category.get('requiresFurtherInvestigation') is not False:
|
||||
reasons.append('服务端标记仍需进一步核查')
|
||||
|
||||
reasons = list(dict.fromkeys(reasons))
|
||||
reconciled = not reasons
|
||||
return {
|
||||
'category': category_name,
|
||||
'status': 'RECONCILED' if reconciled else 'UNRESOLVED',
|
||||
'reconciled': reconciled,
|
||||
'releaseId': release_id,
|
||||
'stage': stage,
|
||||
'period': dict(period) if isinstance(period, dict) else {},
|
||||
'filters': _filters(filters),
|
||||
'reasons': reasons,
|
||||
}
|
||||
|
||||
|
||||
def _append_investigation_report(report, evidence, context):
|
||||
documents, task, history_all, expected_periods, expected_stages, expected_filters = _investigation_docs(evidence, context)
|
||||
if not documents:
|
||||
@@ -675,15 +774,117 @@ def _append_investigation_report(report, evidence, context):
|
||||
and type(verified) is int and verified == total_rows
|
||||
and type(proof.get('missingRows')) is int and proof.get('missingRows') == 0
|
||||
and type(proof.get('mismatchRows')) is int and proof.get('mismatchRows') == 0)
|
||||
status = '原表值全部匹配且核验完整' if complete else '原表核验未完整通过'
|
||||
status = '所选原表值与已存储原始值全部匹配且核验完整' if complete else '原表核验未完整通过'
|
||||
count_text = f'已核验{verified}行/应核验{expected}行' if type(verified) is int and type(expected) is int else '核验行数未提供'
|
||||
mismatch_text = ';'.join(
|
||||
f'{label}{proof[name]}条' if type(proof.get(name)) is int else f'{label}数量未提供'
|
||||
for name, label in (('missingRows', '未核验'), ('mismatchRows', '不一致')))
|
||||
fact_text = f'{side_period};{stage_label};{status},{count_text};{mismatch_text}。'
|
||||
proof_boundary = '该核验只证明所选原表值与已存储原始值一致,不证明正式类别与来源细项已对账。'
|
||||
fact_text = f'{side_period};{stage_label};{status},{count_text};{mismatch_text};{proof_boundary}'
|
||||
source_count_values = []
|
||||
for count, metric in ((verified, 'sourceVerifiedRows'), (expected, 'sourceExpectedRows'),
|
||||
(proof.get('missingRows'), 'sourceMissingRows'),
|
||||
(proof.get('mismatchRows'), 'sourceMismatchRows')):
|
||||
if type(count) is int and count >= 0:
|
||||
source_count_values.append({'rawValue': str(count), 'displayValue': str(count),
|
||||
'unit': 'metadata_count', 'metricField': metric, 'stageHint': stage_label,
|
||||
'period': selected_period, 'sourceVerificationCount': True})
|
||||
rows.append([side_period, stage_label, '原表核验', status + ';' + count_text + ';' + mismatch_text, reference])
|
||||
facts.append({'kind': 'source', 'label': stage_label + '原表核验',
|
||||
'text': fact_text, 'evidenceRef': reference})
|
||||
source_fact = {'kind': 'source', 'label': stage_label + '原表核验',
|
||||
'text': fact_text, 'evidenceRef': reference,
|
||||
'_nativeSourceValues': source_count_values}
|
||||
facts.append(source_fact)
|
||||
# Surface only authorized aggregate coverage and official
|
||||
# category components. Raw rows, workbook values, filenames,
|
||||
# and mismatch examples never enter the narration facts.
|
||||
selected_fields = side.get('selectedFields')
|
||||
selected_fields = {value for value in selected_fields
|
||||
if isinstance(value, str) and value} if isinstance(selected_fields, list) else set()
|
||||
categories = side.get('categories') if isinstance(side.get('categories'), list) else []
|
||||
category_names = [str(item.get('category')) for item in categories
|
||||
if isinstance(item, dict) and isinstance(item.get('category'), str)]
|
||||
category_relations = []
|
||||
for category in categories:
|
||||
if not isinstance(category, dict) or not isinstance(category.get('category'), str):
|
||||
continue
|
||||
category_name = category['category']
|
||||
relation = _source_category_reconciliation(category, total_rows=total_rows,
|
||||
selected_fields=selected_fields, source_proof=proof, stage=stage,
|
||||
period=selected_period, filters=result.get('filters'),
|
||||
release_id=result.get('releaseId'))
|
||||
category_relations.append(relation)
|
||||
if relation['reconciled']:
|
||||
reconciliation_text = (f'{side_period};{stage_label};{category_name}正式类别分量、原表类别总量与来源细项已完整对账一致。')
|
||||
else:
|
||||
reasons = ';'.join(relation['reasons'])
|
||||
reconciliation_text = (f'{side_period};{stage_label};{category_name}正式类别分量与原表类别总量/来源细项未完整对账;'
|
||||
f'当前卡点:{reasons}。')
|
||||
rows.append([side_period, stage_label, category_name + '类别对账',
|
||||
'已完整对账一致' if relation['reconciled'] else '未完整对账:' + reasons,
|
||||
reference])
|
||||
facts.append({'kind': 'source_reconciliation', 'label': category_name + '类别对账',
|
||||
'text': reconciliation_text, 'evidenceRef': reference,
|
||||
'_nativeSourceCategoryReconciliation': relation})
|
||||
formal_weight = category.get('formalWeight')
|
||||
formal_coverage = category.get('formalCoverage') if isinstance(category.get('formalCoverage'), dict) else {}
|
||||
numeric_rows = formal_coverage.get('numericRows')
|
||||
coverage_rows = formal_coverage.get('rows')
|
||||
complete_component = (type(total_rows) is int and total_rows > 0
|
||||
and coverage_rows == total_rows and type(numeric_rows) is int and numeric_rows == total_rows
|
||||
and all(type(formal_coverage.get(key)) is int and formal_coverage.get(key) == 0
|
||||
for key in ('keyAbsentRows', 'nullOrBlankRows', 'invalidNumericRows', 'negativeRows')))
|
||||
from nao_evidence_report import number
|
||||
formal_value = number(formal_weight)
|
||||
if complete_component and formal_value is not None and formal_value >= 0:
|
||||
facts.append({'kind': 'category', 'label': category_name,
|
||||
'text': f'{side_period};{stage_label}正式大类分量:{category_name}为{shown(formal_value)} kg。',
|
||||
'evidenceRef': reference,
|
||||
'_nativeSourceValues': [{'rawValue': str(formal_value),
|
||||
'displayValue': shown(formal_value), 'unit': 'kg',
|
||||
'metricField': 'categoryWeight', 'stageHint': stage_label,
|
||||
'categoryHint': category_name, 'categoryNames': category_names,
|
||||
'period': selected_period, 'releaseId': result.get('releaseId')}],
|
||||
'_nativeSourceCategoryReconciliation': relation})
|
||||
|
||||
# These field names are emitted only when the server says
|
||||
# they were selected for this source check. Missing or bad
|
||||
# values remain coverage gaps; they are never represented
|
||||
# as zero weight.
|
||||
field_rows = []
|
||||
for item in category.get('fields') or []:
|
||||
if isinstance(item, dict) and isinstance(item.get('coverage'), dict):
|
||||
field_rows.append((item.get('field'), item['coverage']))
|
||||
for item in category.get('fieldCoverageLimits') or []:
|
||||
if isinstance(item, dict):
|
||||
field_rows.append((item.get('field'), item))
|
||||
columns, values = category.get('fieldCoverageColumns'), category.get('fieldCoverage')
|
||||
if isinstance(columns, list) and isinstance(values, list):
|
||||
for row in values:
|
||||
if isinstance(row, list) and len(row) == len(columns):
|
||||
mapped = dict(zip(columns, row))
|
||||
field_rows.append((mapped.get('field'), mapped))
|
||||
deduped = {}
|
||||
for field_name, coverage in field_rows:
|
||||
if isinstance(field_name, str) and field_name in selected_fields and isinstance(coverage, dict):
|
||||
deduped[field_name] = coverage
|
||||
for field_name, coverage in deduped.items():
|
||||
issues = []
|
||||
for key, label in (('keyAbsentRows', '缺列'), ('nullOrBlankRows', '空值'),
|
||||
('invalidNumericRows', '非数值'), ('negativeRows', '负值')):
|
||||
count = coverage.get(key)
|
||||
if type(count) is int and count > 0:
|
||||
issues.append(label)
|
||||
rows_with_coverage = coverage.get('rows')
|
||||
numeric_count = coverage.get('numericRows')
|
||||
if (type(rows_with_coverage) is int and type(numeric_count) is int
|
||||
and numeric_count < rows_with_coverage and not issues):
|
||||
issues.append('有效数值覆盖不足')
|
||||
if issues:
|
||||
fact_text = (f'{side_period};{stage_label}来源字段“{field_name}”存在'
|
||||
+ '、'.join(issues) + ';缺失或非法覆盖不能按零处理。')
|
||||
facts.append({'kind': 'source_coverage', 'label': field_name,
|
||||
'text': fact_text, 'evidenceRef': reference})
|
||||
source_fact['_nativeSourceCategoryReconciliations'] = category_relations
|
||||
else:
|
||||
matching = side.get('matchingRows')
|
||||
returned = side.get('returnedRows')
|
||||
|
||||
Reference in New Issue
Block a user