first commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
"""Units domain"""
|
||||
from .sync_node import UnitsSyncNode
|
||||
from .sync_strategy import UnitsSyncStrategy
|
||||
from .jsonl_handler import UnitsJsonlHandler
|
||||
|
||||
__all__ = ["UnitsSyncNode", "UnitsSyncStrategy", "UnitsJsonlHandler"]
|
||||
@@ -0,0 +1,159 @@
|
||||
"""
|
||||
Units (机组) API Handler
|
||||
|
||||
职责:
|
||||
- 更新项目机组数据
|
||||
- 不支持创建和删除(机组随项目自动生成)
|
||||
|
||||
实现的接口:
|
||||
PUT:
|
||||
- /units - 更新机组数据
|
||||
"""
|
||||
|
||||
from typing import List, Dict, Any
|
||||
from ...datasource.api.handler import BaseApiHandler
|
||||
from ...datasource.task_result import TaskResult
|
||||
from ...common.sync_node import SyncNode
|
||||
from ...datasource.api.errors import ERROR_VALIDATION_FAILED
|
||||
from .sync_node import UnitsSyncNode
|
||||
from schemas.project_extensions.generator_unit import GeneratorUnitProject, PostGeneratorUnits
|
||||
|
||||
|
||||
class UnitsApiHandler(BaseApiHandler):
|
||||
"""机组 API Handler"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._node_type = "units"
|
||||
self._node_class = UnitsSyncNode
|
||||
self._schema = GeneratorUnitProject
|
||||
|
||||
async def load(self) -> List[SyncNode]:
|
||||
"""加载机组数据(从 project all_info 中提取)"""
|
||||
from ..project.api_handler import ProjectApiHandler
|
||||
|
||||
# 获取已加载的项目
|
||||
projects = self._collection.filter(node_type="project")
|
||||
if not projects:
|
||||
print("⚠️ No projects found, skipping units load")
|
||||
return []
|
||||
|
||||
nodes = []
|
||||
for project in projects:
|
||||
project_id = project.data_id
|
||||
|
||||
try:
|
||||
# 从 project context 或缓存获取 all_info
|
||||
all_info = project.context.get("all_info")
|
||||
if not all_info:
|
||||
all_info = await ProjectApiHandler.get_all_info(
|
||||
self.api_client,
|
||||
project_id
|
||||
)
|
||||
|
||||
# 提取 generator_units
|
||||
units_list = all_info.get("generator_units", [])
|
||||
print(f" Project {project_id}: {len(units_list)} units")
|
||||
|
||||
for unit_data in units_list:
|
||||
# 验证数据
|
||||
try:
|
||||
validated = self._schema.model_validate(unit_data)
|
||||
node = self._create_node(validated.model_dump())
|
||||
node.context["project_id"] = project_id
|
||||
nodes.append(node)
|
||||
except Exception as e:
|
||||
print(f"⚠️ Skipping invalid unit: {e}")
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load units for project {project_id}: {e}")
|
||||
continue
|
||||
|
||||
return nodes
|
||||
|
||||
async def create_all(self, nodes: List[SyncNode]) -> List[TaskResult]:
|
||||
"""不支持创建"""
|
||||
return [
|
||||
TaskResult.failed(node_id=node.node_id, error="Units creation not supported (auto-generated with project)")
|
||||
for node in nodes
|
||||
]
|
||||
|
||||
async def update_all(self, nodes: List[SyncNode]) -> List[TaskResult]:
|
||||
"""批量更新机组数据"""
|
||||
from pydantic import ValidationError
|
||||
|
||||
results = []
|
||||
|
||||
for node in nodes:
|
||||
node_data = node.get_data()
|
||||
if node_data is None:
|
||||
error_msg = node.error or "Node data is missing or invalid"
|
||||
results.append(TaskResult.failed(node_id=node.node_id, error=f"Cannot update: {error_msg}"))
|
||||
continue
|
||||
|
||||
origin_data = node.get_origin_data() or {}
|
||||
update_data = self._filter_update_data(node_data, origin_data, PostGeneratorUnits)
|
||||
if not update_data:
|
||||
reason = f"No physical diff found for units {node.data_id} despite strategy update request (Normalization Discrepancy)."
|
||||
print(f" [WARNING] [units] {reason}")
|
||||
results.append(TaskResult.skipped(node_id=node.node_id, reason=reason))
|
||||
continue
|
||||
|
||||
# 验证并转换为 Pydantic model
|
||||
try:
|
||||
update_model = PostGeneratorUnits.model_validate(update_data)
|
||||
except ValidationError as e:
|
||||
results.append(TaskResult.failed(node_id=node.node_id, error=f"{ERROR_VALIDATION_FAILED}: {e}"))
|
||||
continue
|
||||
|
||||
push_id = self._generate_push_id()
|
||||
biz_id = node.data_id or update_model.id
|
||||
if not biz_id:
|
||||
results.append(TaskResult.failed(node_id=node.node_id, error="Missing biz_id for update"))
|
||||
continue
|
||||
|
||||
try:
|
||||
await api_update_units(self.api_client, update_model, push_id, biz_id)
|
||||
results.append(TaskResult.success(node_id=node.node_id))
|
||||
except Exception as e:
|
||||
results.append(TaskResult.failed(node_id=node.node_id, error=str(e)))
|
||||
|
||||
return results
|
||||
|
||||
async def delete_all(self, nodes: List[SyncNode]) -> List[TaskResult]:
|
||||
"""不支持删除"""
|
||||
return [
|
||||
TaskResult.failed(node_id=node.node_id, error="Units deletion not supported")
|
||||
for node in nodes
|
||||
]
|
||||
|
||||
async def poll_tasks(self, task_ids: List[str]) -> Dict[str, TaskResult]:
|
||||
"""轮询异步任务状态"""
|
||||
return await super().poll_tasks(task_ids)
|
||||
|
||||
def _create_node(self, data: Dict[str, Any]) -> SyncNode:
|
||||
node = self._create_basic_node(data, depend_ids=[])
|
||||
|
||||
if data.get("project_id"):
|
||||
node.context["project_id"] = data["project_id"]
|
||||
|
||||
return node
|
||||
|
||||
|
||||
# ========== 静态 API 函数 ==========
|
||||
|
||||
async def api_update_units(client, data: PostGeneratorUnits, push_id: str, biz_id: str) -> None:
|
||||
"""
|
||||
PUT /units - 更新机组数据
|
||||
|
||||
Args:
|
||||
client: API 客户端
|
||||
data: 更新数据(Pydantic model)
|
||||
push_id: 推送ID
|
||||
biz_id: 业务ID
|
||||
"""
|
||||
request_data = {"push_id": push_id, "biz_id": biz_id, "data": data.model_dump(exclude_unset=True)}
|
||||
response = await client.request(method="PUT", endpoint="/units", data=request_data)
|
||||
if response.get("code") != 200:
|
||||
raise Exception(f"API error: {response.get('message', 'Unknown error')}")
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Units Domain - JSONL Handler"""
|
||||
import json
|
||||
import re
|
||||
from typing import List
|
||||
|
||||
from ...datasource import BaseJsonlHandler, JsonlDataSource
|
||||
from .sync_node import UnitsSyncNode, SyncNode
|
||||
from schemas.project_extensions.generator_unit import GeneratorUnitProject
|
||||
|
||||
|
||||
class UnitsJsonlHandler(BaseJsonlHandler):
|
||||
"""机组 JSONL Handler"""
|
||||
def __init__(self, datasource: JsonlDataSource):
|
||||
super().__init__(datasource, "units", UnitsSyncNode, GeneratorUnitProject)
|
||||
|
||||
async def load(self) -> List['SyncNode']:
|
||||
"""加载机组数据(支持 generator_unit_N.jsonl 与 units_N.jsonl)"""
|
||||
nodes: List[SyncNode] = []
|
||||
seen_ids: set[str] = set()
|
||||
|
||||
if not self.datasource.dir_path.exists():
|
||||
return nodes
|
||||
|
||||
# 查找所有匹配的文件:generator_unit_N.jsonl 或 units_N.jsonl
|
||||
patterns = (
|
||||
re.compile(r"^generator_unit_\d+\.jsonl$"),
|
||||
re.compile(r"^units_\d+\.jsonl$"),
|
||||
)
|
||||
|
||||
for file_path in self.datasource.dir_path.iterdir():
|
||||
if not any(pattern.match(file_path.name) for pattern in patterns):
|
||||
continue
|
||||
|
||||
# 读取文件
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
for line_num, line in enumerate(f, 1):
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
try:
|
||||
item = json.loads(line)
|
||||
item_id = self.extract_id(item)
|
||||
if item_id and item_id in seen_ids:
|
||||
continue
|
||||
|
||||
# 创建节点
|
||||
node = self.create_node(item)
|
||||
nodes.append(node)
|
||||
|
||||
# 同时加载到缓存
|
||||
if item_id:
|
||||
seen_ids.add(item_id)
|
||||
bucket = self.datasource._data.setdefault(self.node_type, {})
|
||||
bucket[item_id] = item
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Warning: Invalid JSON in {file_path.name}:{line_num}: {e}")
|
||||
|
||||
return nodes
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Units domain registration"""
|
||||
from ...common.registry import DomainRegistry
|
||||
from schemas.project_extensions.generator_unit import GeneratorUnitProject
|
||||
from .sync_node import UnitsSyncNode
|
||||
from .sync_strategy import UnitsSyncStrategy
|
||||
from .jsonl_handler import UnitsJsonlHandler
|
||||
from .api_handler import UnitsApiHandler
|
||||
|
||||
# 注册 units domain
|
||||
DomainRegistry.register(
|
||||
node_type="units",
|
||||
schema=GeneratorUnitProject,
|
||||
node_class=UnitsSyncNode,
|
||||
strategy_class=UnitsSyncStrategy,
|
||||
jsonl_handler_class=UnitsJsonlHandler,
|
||||
api_handler_class=UnitsApiHandler,
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
"""Units SyncNode"""
|
||||
|
||||
from ...common.sync_node import SyncNode
|
||||
from schemas.project_extensions.generator_unit import GeneratorUnitProject
|
||||
|
||||
|
||||
class UnitsSyncNode(SyncNode[GeneratorUnitProject]):
|
||||
"""机组同步节点"""
|
||||
node_type = "units"
|
||||
schema = GeneratorUnitProject
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
@@ -0,0 +1,20 @@
|
||||
from ...sync_system.strategy import DefaultSyncStrategy
|
||||
from ...sync_system.config import StrategyConfig, OrphanAction, UpdateDirection
|
||||
from schemas.project_extensions.generator_unit import GeneratorUnitProject
|
||||
|
||||
|
||||
class UnitsSyncStrategy(DefaultSyncStrategy[GeneratorUnitProject]):
|
||||
"""
|
||||
Units 同步策略(项目扩展)。
|
||||
"""
|
||||
|
||||
schema = GeneratorUnitProject
|
||||
|
||||
default_config = StrategyConfig(
|
||||
auto_bind=True,
|
||||
auto_bind_fields=["project_id", "serial_number"],
|
||||
depend_fields={"project_id": "project"},
|
||||
local_orphan_action=OrphanAction.NONE,
|
||||
remote_orphan_action=OrphanAction.NONE,
|
||||
update_direction=UpdateDirection.PUSH,
|
||||
)
|
||||
Reference in New Issue
Block a user