registry.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. r"""端点登记表:**唯一需要维护的清单** —— 端点名 → (算法模块, 函数, 允许的查询参数)。
  3. 判据:只登记"能无参或仅带可选参数调用、返回可 JSON 化数据、不改盘上产物"的算法函数。
  4. 带写盘副作用的函数一律固定成只读参数(如 `curves.build_store(write=False)`)。
  5. """
  6. from __future__ import annotations
  7. import importlib
  8. from typing import Any, Callable
  9. # 端点 → dict(module=算法模块点号名, fn=函数名, args=允许的查询参数 → (类型, 转发名, 固定值/None))
  10. # · 类型: 'str' | 'int' | 'bool'
  11. # · 固定值不为 None 时,该参数**不接受外部传入**,恒用固定值(防止服务被用来写盘)
  12. ENDPOINTS: dict[str, dict[str, Any]] = {
  13. 'systems': dict(module='app_algorithmModel.app_algorithmModel_guanlan.taxonomy', fn='__systems__',
  14. doc='判级维度字典(七系统 SYSTEMS + 温度通道→系统 TEMP2SYS)', args={}),
  15. 'matrix': dict(module='app_algorithmModel.app_algorithmModel_guanlan.taxonomy', fn='system_matrix',
  16. doc='七系统判级矩阵(与 /detail/api/fleet 的 sysmx 同源)',
  17. args={}), # span 见文件头实逮说明:不是窗口字符串,P10 不传
  18. 'reconcile': dict(module='app_algorithmModel.app_algorithmModel_guanlan.taxonomy', fn='reconcile',
  19. doc='判级与台账对账', args={}),
  20. 'reliability': dict(module='app_algorithmModel.app_algorithmModel_guanlan.perf.reliability', fn='overview',
  21. doc='可靠性总览(含可用率与故障统计)',
  22. args={'since': ('str', 'since', None)}), # span 是跨度对象,P10 不传
  23. 'problems': dict(module='app_algorithmModel.app_algorithmModel_guanlan.perf.reliability',
  24. fn='current_problems', doc='当前问题清单', args={}),
  25. # ★2026-09-29 实逮:`availability.build()` 返回 None(它只写盘、不返值)⇒ 不是接口,已撤。
  26. # 可用率对外只经 `availability_summary`(=`summary()`,返回结构化摘要)。
  27. 'availability_summary': dict(module='app_algorithmModel.app_algorithmModel_guanlan.perf.availability',
  28. fn='summary', doc='可用率摘要',
  29. args={'month_from': ('str', 'month_from', None)}),
  30. 'fusion': dict(module='app_algorithmModel.app_algorithmModel_guanlan.subsys.fusion', fn='fusion_table',
  31. doc='融合面四源表', args={'all_turbines': ('bool', 'all_turbines', None)}),
  32. 'handoff': dict(module='app_algorithmModel.app_algorithmModel_guanlan.subsys.fusion', fn='load_handoff',
  33. doc='振动融合交接件(handoff_vibration_v2)', args={}),
  34. 'pitch': dict(module='app_algorithmModel.app_algorithmModel_guanlan.subsys.pitch', fn='registry',
  35. doc='变桨面(日粒度 + 零位三口径)', args={}),
  36. 'curves': dict(module='app_algorithmModel.app_algorithmModel_guanlan.perf.curves', fn='build_store',
  37. doc='功率/转速曲线仓(**只读**:write 固定 False)',
  38. args={'write': ('bool', 'write', False)}),
  39. 'curves_check': dict(module='app_algorithmModel.app_algorithmModel_guanlan.perf.curves', fn='physics_check',
  40. doc='曲线物理性检查', args={}),
  41. }
  42. _cache: dict[str, Any] = {}
  43. def _mod(name: str):
  44. return importlib.import_module(name)
  45. def call(name: str, params: dict[str, Any]) -> Any:
  46. """按登记表取数(params 已由 HTTP 层按类型解析好)。"""
  47. spec = ENDPOINTS.get(name)
  48. if not spec:
  49. raise KeyError(f'未登记端点 {name!r};可用: {", ".join(sorted(ENDPOINTS))}')
  50. m = _mod(spec['module'])
  51. if spec['fn'] == '__systems__':
  52. return {'SYSTEMS': list(m.SYSTEMS), 'TEMP2SYS': dict(m.TEMP2SYS)}
  53. fn: Callable = getattr(m, spec['fn'])
  54. kwargs: dict[str, Any] = {}
  55. for qname, (_t, fname, fixed) in (spec.get('args') or {}).items():
  56. val = fixed if fixed is not None else params.get(fname)
  57. if val is not None:
  58. kwargs[fname] = val
  59. return fn(**kwargs)
  60. def spec_list() -> list[dict[str, Any]]:
  61. return [dict(name=k, module=v['module'], fn=v['fn'], doc=v['doc'],
  62. params=sorted((v.get('args') or {}).keys())) for k, v in sorted(ENDPOINTS.items())]
  63. __all__ = ['ENDPOINTS', 'call', 'spec_list']