Files
quality_analyse/scripts/nao_query_contract.py
lyun ff7bbe3e46 同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。
补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。
统一午夜黑及其他主题的控件细节、中文和业务数字字体。
排除本地业务数据、凭据、预览、发布与验收产物。

验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
2026-10-30 22:40:58 +08:00

158 lines
9.3 KiB
Python

"""Bind reads to a model-resolved business frame; never calculate an indicator."""
import copy
DEPARTMENT_ALIASES = {'汽车交通':'新能源汽车磁钢'}
SOURCE_STAGES = {'black', 'bright'}
COLUMN_OPERATORS = {'in', 'not_in', 'is_empty', 'not_empty'}
def source_field_labels(source_fields):
"""Return the current authorized labels without inventing field names."""
if not isinstance(source_fields, list):
return {}
return {item['key']: item.get('label', item['key']) for item in source_fields
if isinstance(item, dict) and isinstance(item.get('key'), str)
and item['key'] and isinstance(item.get('label', item['key']), str)}
def parse_column_filters(value):
"""Validate only the wire shape; query entry points also require the actor catalog."""
import re
if value is None:return []
if not isinstance(value,list) or len(value)>20:
raise ValueError('columnFilters必须是最多20项的完整规则数组,null或[]表示清除。')
result=[]
for index,rule in enumerate(value):
name=f'columnFilters[{index}]'
if not isinstance(rule,dict) or set(rule)-{'field','stages','operator','values'}:
raise ValueError(f'{name}结构无效。')
key=rule.get('field');stages=rule.get('stages');operator=rule.get('operator')
if not isinstance(key,str) or not re.fullmatch(r'[A-Za-z0-9_]{1,100}',key):
raise ValueError(f'{name}.field无效。')
if (not isinstance(stages,list) or not 1<=len(stages)<=2
or any(not isinstance(stage,str) or stage not in SOURCE_STAGES for stage in stages)
or len(set(stages))!=len(stages)):
raise ValueError(f'{name}.stages必须是非空black/bright数组。')
if operator not in COLUMN_OPERATORS:raise ValueError(f'{name}.operator不在当前字段支持目录中。')
clean={'field':key,'stages':list(stages),'operator':operator}
if operator in ('in','not_in'):
values=rule.get('values')
if (not isinstance(values,list) or not 1<=len(values)<=50
or any(not isinstance(item,str) or item=='' or len(item)>256 for item in values)):
raise ValueError(f'{name}.values必须是1到50个不超过256字符的完整文本值。')
clean['values']=list(dict.fromkeys(values))
elif 'values' in rule:raise ValueError(f'{name}的空值操作不接受values。')
result.append(clean)
return result
def validate_column_filters(value, source_fields):
"""Recheck the complete rule set against the current authorized stage mappings."""
result=parse_column_filters(value)
if not result:return result
if not isinstance(source_fields,list):
raise ValueError('授权原表字段目录不可用,不能确认列筛选权限。')
directory={item['key']:item for item in source_fields if isinstance(item,dict) and isinstance(item.get('key'),str)}
for index,rule in enumerate(result):
name=f'columnFilters[{index}]';entry=directory.get(rule['field'])
if entry is None:raise ValueError(f'{name}.field未授权或不在原表字段目录中。')
if entry.get('type')!='source_value':raise ValueError(f'{name}.field不是受支持的原表文本字段。')
allowed=entry.get('stages');columns=entry.get('columns')
if not isinstance(allowed,list) or not isinstance(columns,dict):raise ValueError(f'{name}.field目录映射不完整。')
for stage in rule['stages']:
if stage not in allowed or not isinstance(columns.get(stage),str) or not columns[stage].strip():
raise ValueError(f'{name}.field在{stage}阶段没有授权源列,不能忽略该条件。')
if not isinstance(entry.get('operators'),list) or rule['operator'] not in entry['operators']:
raise ValueError(f'{name}.operator不在当前字段支持目录中。')
return result
def canonical_filters(value):
filters = copy.deepcopy(value or {})
if isinstance(filters.get('department'), str) and filters['department'] in DEPARTMENT_ALIASES:
filters['department'] = DEPARTMENT_ALIASES[filters['department']]
if isinstance(filters.get('group'), str) and filters['group'] in DEPARTMENT_ALIASES:
department = DEPARTMENT_ALIASES[filters.pop('group')]
if filters.get('department') not in (None, department):
raise ValueError('业务别名与产业部范围冲突')
filters['department'] = department
return filters
def bind_query(arguments, contract):
if not contract or arguments.get('mode') == 'catalog': return copy.deepcopy(arguments)
from nao_quality_tools import normalize_filters
task = contract['task']; bound = copy.deepcopy(arguments); mode = bound.get('mode')
task_filters = copy.deepcopy(task.get('filters') or {})
task_has_column_filters = 'columnFilters' in task_filters
expected_columns = task_filters.pop('columnFilters', None)
column_modes = ('query', 'compare', 'rank', 'investigate', 'defects')
if contract.get('exactScope'):
column_modes += ('changes',)
if 'columnFilters' in bound and mode not in column_modes:
raise ValueError('columnFilters只能用于已确认的正式query/compare/rank/investigate/defects模式。')
if expected_columns and mode not in column_modes:
raise ValueError('本轮原表列筛选只能用于已确认的正式query/compare/rank/investigate/defects,不能在其他模式中忽略。')
release = contract['releaseId']
if bound.get('releaseId') not in (None, release): raise ValueError('查询版本偏离本轮固定范围')
bound['releaseId'] = release
expected, _ = normalize_filters(None, canonical_filters(task_filters), scope_was_explicit=False)
raw = canonical_filters(bound.get('filters'))
supplied, _ = normalize_filters(bound.get('scope'), raw, scope_was_explicit='scope' in bound)
if 'includeUnassigned' not in raw: supplied.pop('includeUnassigned', None)
for key, value in expected.items():
if key in supplied and supplied[key] != value:
raise ValueError('查询范围偏离本轮约定:请沿用 '+str(expected))
extras = set(supplied) - set(expected)
allowed = set()
dimensions = set(task.get('dimensions', []))
if mode in ('compare','query','changes','rank','top','defects','investigate','details'):
# Permit a requested hierarchy drilldown, retaining every parent filter.
# Filtering the ranked dimension itself would replace the parent view.
allowed |= (dimensions & {'department','group','product'}) - {bound.get('dimension')}
if mode in ('defects','investigate','details') and ('product' in dimensions or task.get('defectScope') in ('products','both')):
allowed.add('product')
if extras - allowed:
raise ValueError('查询范围不能用子分组集合替代父范围,或添加未要求的筛选:请沿用 '+str(expected))
bound_filters = {**expected, **supplied}
if contract.get('exactScope'):
comparable = lambda value: {key: item for key, item in value.items()
if key != 'includeUnassigned' or item is not False}
if comparable(bound_filters) != comparable(expected):
raise ValueError('查询筛选偏离服务器签发的queryPlan子范围。')
bound['filters'] = bound_filters
bound.pop('scope', None)
if mode in column_modes and task_has_column_filters:
expected_columns = copy.deepcopy(expected_columns or [])
if 'columnFilters' in bound:
supplied_columns = bound['columnFilters']
if supplied_columns is None: supplied_columns = []
if supplied_columns != expected_columns:
raise ValueError('正式原表列筛选偏离本轮任务约定。')
bound['columnFilters'] = expected_columns
elif mode in column_modes and 'columnFilters' in bound:
if bound['columnFilters'] not in (None, []):
raise ValueError('正式原表列筛选未在本轮任务中授权。')
bound.pop('columnFilters', None)
for key in ('period','comparisonPeriod'):
target = task.get(key)
if key == 'comparisonPeriod' and mode not in ('compare','changes','defects','investigate'): continue
if target is not None:
if bound.get(key) not in (None, target): raise ValueError('查询期间偏离本轮固定范围')
bound[key] = copy.deepcopy(target)
dimension = bound.get('dimension')
stages = task.get('dimensionStages', {}).get(dimension)
if stages is None and dimension in dimensions and len(task.get('stages', [])) == 1:
stages = task['stages']
if mode in ('changes','rank','top','defects') and stages is not None:
# [] requires a combined view for coverage, but does not prohibit
# supplemental stage reads. Coverage still requires the combined read.
if stages and len(stages) == 1:
if bound.get('stage') not in (None, stages[0]): raise ValueError('查询阶段偏离本轮范围')
bound['stage'] = stages[0]
elif stages and bound.get('stage') not in stages:
raise ValueError('本轮要求分阶段查询,请分别传入 black 或 bright')
if mode == 'investigate' and bound.get('stage') not in task.get('stages', []):
raise ValueError('原始读取阶段偏离服务器签发的queryPlan节点。')
return bound