first commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
"""Preparation domain"""
|
||||
from .sync_node import PreparationSyncNode
|
||||
from .sync_strategy import PreparationSyncStrategy
|
||||
from .jsonl_handler import PreparationJsonlHandler
|
||||
|
||||
__all__ = ["PreparationSyncNode", "PreparationSyncStrategy", "PreparationJsonlHandler"]
|
||||
@@ -0,0 +1,153 @@
|
||||
"""
|
||||
Preparation (里程碑) API Handler
|
||||
|
||||
职责:
|
||||
- 更新项目里程碑数据
|
||||
- 不支持创建和删除(里程碑随项目自动生成)
|
||||
|
||||
实现的接口:
|
||||
PUT:
|
||||
- /preparation - 更新里程碑数据
|
||||
"""
|
||||
|
||||
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 PreparationSyncNode
|
||||
from schemas.project_extensions.preparation import PreparationDetail, PostPreparation
|
||||
|
||||
|
||||
class PreparationApiHandler(BaseApiHandler):
|
||||
"""里程碑 API Handler"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._node_type = "preparation"
|
||||
self._node_class = PreparationSyncNode
|
||||
self._schema = PreparationDetail
|
||||
self._filter_project_id = None
|
||||
self.update_schemas = [PostPreparation]
|
||||
|
||||
def set_filter_project_id(self, project_id: str | List[str]):
|
||||
"""设置要加载的项目ID过滤列表"""
|
||||
self._filter_project_id = project_id
|
||||
|
||||
def set_target_project_ids(self, target_project_ids: List[str]) -> None:
|
||||
"""设置目标项目ID列表"""
|
||||
self.set_filter_project_id(target_project_ids)
|
||||
|
||||
async def load(self) -> List[SyncNode]:
|
||||
"""从 API 加载里程碑数据(需要项目上下文)"""
|
||||
from ..project.api_handler import ProjectApiHandler
|
||||
|
||||
# 获取要加载的项目列表
|
||||
projects = self._collection.filter(node_type="project")
|
||||
if not projects:
|
||||
print(" No projects found, skipping preparation 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
|
||||
)
|
||||
|
||||
# 提取 preparation_list
|
||||
preparation_list = all_info.get("preparation_list", [])
|
||||
print(f" Project {project_id}: {len(preparation_list)} preparations")
|
||||
|
||||
for prep_data in preparation_list:
|
||||
# 验证数据
|
||||
try:
|
||||
validated = self._schema.model_validate(prep_data)
|
||||
node = self._create_node(validated.model_dump())
|
||||
nodes.append(node)
|
||||
except Exception as e:
|
||||
print(f" Skipping invalid preparation: {e}")
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
print(f" Failed to load preparations 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="Preparation 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, PostPreparation)
|
||||
if not update_data:
|
||||
reason = f"No physical diff found for preparation {node.data_id} despite strategy update request (Normalization Discrepancy)."
|
||||
print(f" [WARNING] [preparation] {reason}")
|
||||
results.append(TaskResult.skipped(node_id=node.node_id, reason=reason))
|
||||
continue
|
||||
|
||||
# 验证并转换为 Pydantic model
|
||||
try:
|
||||
update_model = PostPreparation.model_validate(update_data)
|
||||
except Exception 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_preparation(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="Preparation deletion not supported")
|
||||
for node in nodes
|
||||
]
|
||||
|
||||
def _create_node(self, data: Dict[str, Any]) -> SyncNode:
|
||||
return self._create_basic_node(data, depend_ids=[])
|
||||
|
||||
|
||||
# ========== 静态 API 函数 ==========
|
||||
|
||||
async def api_update_preparation(client, data: PostPreparation, push_id: str, biz_id: str) -> None:
|
||||
"""
|
||||
PUT /preparation_nodes - 更新里程碑数据
|
||||
"""
|
||||
request_data = {"push_id": push_id, "biz_id": biz_id, "data": data.model_dump(exclude_unset=True)}
|
||||
response = await client.request(method="PUT", endpoint="/preparation_nodes", data=request_data)
|
||||
if response.get("code") != 200:
|
||||
raise Exception(f"API error: {response.get('message', 'Unknown error')}")
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Preparation Domain - JSONL Handler"""
|
||||
from ...datasource.jsonl.handler import BaseJsonlHandler
|
||||
from ...datasource.jsonl.datasource import JsonlDataSource
|
||||
from .sync_node import PreparationSyncNode
|
||||
from schemas.project_extensions.preparation import PreparationDetail
|
||||
|
||||
|
||||
class PreparationJsonlHandler(BaseJsonlHandler):
|
||||
"""里程碑 JSONL Handler"""
|
||||
def __init__(self, datasource: JsonlDataSource):
|
||||
super().__init__(datasource, "preparation", PreparationSyncNode, PreparationDetail)
|
||||
@@ -0,0 +1,23 @@
|
||||
"""Preparation domain registration"""
|
||||
from ...common.registry import DomainRegistry
|
||||
from .sync_node import PreparationSyncNode, PreparationDetail
|
||||
from .sync_strategy import PreparationSyncStrategy
|
||||
from .jsonl_handler import PreparationJsonlHandler
|
||||
from .api_handler import PreparationApiHandler
|
||||
from ...sync_system.config import StrategyConfig, OrphanAction
|
||||
|
||||
# 注册 preparation domain
|
||||
DomainRegistry.register(
|
||||
node_type="preparation",
|
||||
schema=PreparationDetail,
|
||||
node_class=PreparationSyncNode,
|
||||
strategy_class=PreparationSyncStrategy,
|
||||
jsonl_handler_class=PreparationJsonlHandler,
|
||||
api_handler_class=PreparationApiHandler,
|
||||
config=StrategyConfig(
|
||||
auto_bind_fields=["project_id", "code"],
|
||||
local_orphan_action=OrphanAction.NONE,
|
||||
remote_orphan_action=OrphanAction.NONE,
|
||||
depend_fields={"project_id": "project"}
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Preparation SyncNode"""
|
||||
|
||||
from ...common.sync_node import SyncNode
|
||||
from schemas.project_extensions.preparation import PreparationDetail
|
||||
from pydantic import Field
|
||||
from typing import Optional
|
||||
|
||||
class PreparationSyncNode(SyncNode[PreparationDetail]):
|
||||
"""里程碑同步节点"""
|
||||
node_type = "preparation"
|
||||
schema = PreparationDetail
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
@@ -0,0 +1,38 @@
|
||||
from ...sync_system.strategy import DefaultSyncStrategy
|
||||
from ...sync_system.config import StrategyConfig, OrphanAction, UpdateDirection
|
||||
from ...sync_system.strategy_ops.compare_ops import normalized_data_for_compare, collect_payload_diffs
|
||||
from schemas.project_extensions.preparation import PreparationDetail, PostPreparation
|
||||
|
||||
|
||||
class PreparationSyncStrategy(DefaultSyncStrategy[PreparationDetail]):
|
||||
"""
|
||||
Preparation 同步策略(项目扩展)。
|
||||
"""
|
||||
|
||||
schema = PreparationDetail
|
||||
|
||||
default_config = StrategyConfig(
|
||||
auto_bind=True,
|
||||
auto_bind_fields=["project_id", "code"],
|
||||
depend_fields={"project_id": "project"},
|
||||
local_orphan_action=OrphanAction.NONE,
|
||||
remote_orphan_action=OrphanAction.NONE,
|
||||
update_direction=UpdateDirection.PUSH,
|
||||
)
|
||||
|
||||
def _needs_update(self, source_node, target_node, resolved_data=None, data_id_map=None) -> bool:
|
||||
if resolved_data is None:
|
||||
raise ValueError(f"[{self.node_type}] resolved_data is required for differential sync.")
|
||||
|
||||
target_data = target_node.get_data()
|
||||
if target_data is None:
|
||||
return False
|
||||
|
||||
update_fields = set(PostPreparation.model_fields.keys())
|
||||
source_payload = {key: value for key, value in resolved_data.items() if key in update_fields}
|
||||
target_payload = {key: value for key, value in target_data.items() if key in update_fields}
|
||||
|
||||
normalized_source = normalized_data_for_compare(source_payload, data_id_map or {})
|
||||
normalized_target = normalized_data_for_compare(target_payload, data_id_map or {})
|
||||
diff_map = collect_payload_diffs(normalized_source, normalized_target, max_items=8)
|
||||
return bool(diff_map)
|
||||
Reference in New Issue
Block a user