node_configuration_service.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. import copy
  2. from typing import Dict, Any, List, Optional, Tuple, Type
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from sqlalchemy import select, func, desc
  5. from sqlalchemy.orm.attributes import flag_modified
  6. from app.core.biz_exception import BizException
  7. from app.models.node_configuration import (
  8. BookerEntry,
  9. SentinelEntry,
  10. StandaloneEntry,
  11. NodeConfigurationRevision,
  12. ClusterNodeEntry,
  13. )
  14. from app.schemas.node_configuration import (
  15. ClusterNodeCreate,
  16. ClusterNodeUpdate,
  17. )
  18. class NodeConfigurationService:
  19. ENTRY_MODEL_MAP: Dict[str, Type[Any]] = {
  20. "booker": BookerEntry,
  21. "sentinel": SentinelEntry,
  22. "standalone": StandaloneEntry,
  23. }
  24. @classmethod
  25. def get_model(cls, entry_type: str) -> Type[Any]:
  26. model = cls.ENTRY_MODEL_MAP.get(entry_type.lower())
  27. if not model:
  28. raise BizException(
  29. code=40000,
  30. message=f"Invalid entry_type: '{entry_type}'. Must be one of {list(cls.ENTRY_MODEL_MAP.keys())}",
  31. http_status=400,
  32. )
  33. return model
  34. @classmethod
  35. def _to_snapshot(cls, obj: Any) -> Dict[str, Any]:
  36. """Serialize SQLAlchemy model columns to dict snapshot"""
  37. snapshot = {}
  38. for column in obj.__table__.columns:
  39. val = getattr(obj, column.name)
  40. if hasattr(val, "isoformat"):
  41. val = val.isoformat()
  42. snapshot[column.name] = copy.deepcopy(val)
  43. return snapshot
  44. @classmethod
  45. async def _record_revision(
  46. cls,
  47. db: AsyncSession,
  48. entry_type: str,
  49. entry_id: int,
  50. version: int,
  51. action: str,
  52. snapshot: Dict[str, Any],
  53. updated_by: str,
  54. ) -> NodeConfigurationRevision:
  55. rev = NodeConfigurationRevision(
  56. entry_type=entry_type,
  57. entry_id=entry_id,
  58. version=version,
  59. action=action,
  60. snapshot=snapshot,
  61. updated_by=updated_by or "system",
  62. )
  63. db.add(rev)
  64. await db.flush()
  65. return rev
  66. @classmethod
  67. async def list_entries(
  68. cls,
  69. db: AsyncSession,
  70. entry_type: str,
  71. page: int = 1,
  72. page_size: int = 20,
  73. identifier: Optional[str] = None,
  74. node_id: Optional[str] = None,
  75. order_id: Optional[str] = None,
  76. enable: Optional[int] = None,
  77. ) -> Tuple[int, List[Any]]:
  78. model = cls.get_model(entry_type)
  79. stmt = select(model)
  80. count_stmt = select(func.count(model.id))
  81. if identifier:
  82. stmt = stmt.where(model.identifier.like(f"%{identifier}%"))
  83. count_stmt = count_stmt.where(model.identifier.like(f"%{identifier}%"))
  84. if node_id:
  85. stmt = stmt.where(model.node_id == node_id)
  86. count_stmt = count_stmt.where(model.node_id == node_id)
  87. if order_id and hasattr(model, "order_id"):
  88. stmt = stmt.where(model.order_id == order_id)
  89. count_stmt = count_stmt.where(model.order_id == order_id)
  90. if enable is not None and hasattr(model, "enable"):
  91. stmt = stmt.where(model.enable == enable)
  92. count_stmt = count_stmt.where(model.enable == enable)
  93. total = await db.scalar(count_stmt) or 0
  94. offset = (page - 1) * page_size
  95. stmt = stmt.order_by(desc(model.enable), desc(model.updated_at), desc(model.id)).offset(offset).limit(page_size)
  96. result = await db.execute(stmt)
  97. items = list(result.scalars().all())
  98. return total, items
  99. @classmethod
  100. async def get_entry(
  101. cls, db: AsyncSession, entry_type: str, entry_id: int
  102. ) -> Any:
  103. model = cls.get_model(entry_type)
  104. stmt = select(model).where(model.id == entry_id)
  105. entry = (await db.execute(stmt)).scalar_one_or_none()
  106. if not entry:
  107. raise BizException(
  108. code=40400,
  109. message=f"Configuration entry ({entry_type} ID: {entry_id}) not found",
  110. http_status=404,
  111. )
  112. return entry
  113. @classmethod
  114. async def create_entry(
  115. cls,
  116. db: AsyncSession,
  117. entry_type: str,
  118. data: Dict[str, Any],
  119. operator: str = "system",
  120. ) -> Any:
  121. model = cls.get_model(entry_type)
  122. node_id = data.get("node_id") or "default"
  123. identifier = data.get("identifier")
  124. if not identifier:
  125. raise BizException(
  126. code=40000,
  127. message="Field 'identifier' is required",
  128. http_status=400,
  129. )
  130. # Check duplicate (node_id, identifier)
  131. check_stmt = select(model).where(
  132. model.node_id == node_id, model.identifier == identifier
  133. )
  134. existing = (await db.execute(check_stmt)).scalar_one_or_none()
  135. if existing:
  136. raise BizException(
  137. code=40902,
  138. message=f"Identifier '{identifier}' on node '{node_id}' already exists",
  139. http_status=409,
  140. )
  141. # Prepare model payload
  142. entry_data = data.copy()
  143. entry_data["node_id"] = node_id
  144. entry_data["enable"] = 0 # Force disabled upon creation
  145. entry_data["updated_by"] = operator
  146. entry_data["version"] = 1
  147. # Instantiate
  148. valid_cols = {c.name for c in model.__table__.columns}
  149. filtered_data = {k: v for k, v in entry_data.items() if k in valid_cols}
  150. entry = model(**filtered_data)
  151. db.add(entry)
  152. await db.flush()
  153. # Record revision
  154. snapshot = cls._to_snapshot(entry)
  155. await cls._record_revision(
  156. db=db,
  157. entry_type=entry_type,
  158. entry_id=entry.id,
  159. version=entry.version,
  160. action="create",
  161. snapshot=snapshot,
  162. updated_by=operator,
  163. )
  164. await db.commit()
  165. await db.refresh(entry)
  166. return entry
  167. @classmethod
  168. async def sparse_update_entry(
  169. cls,
  170. db: AsyncSession,
  171. entry_type: str,
  172. entry_id: int,
  173. set_data: Optional[Dict[str, Any]] = None,
  174. clear_fields: Optional[List[str]] = None,
  175. base_version: Optional[int] = None,
  176. operator: str = "system",
  177. ) -> Any:
  178. entry = await cls.get_entry(db, entry_type, entry_id)
  179. set_data = set_data or {}
  180. clear_fields = clear_fields or []
  181. # 1. Apply set_data with dot-path support
  182. for path_key, val in set_data.items():
  183. # Strip leading 'payload.' if present
  184. clean_key = path_key[8:] if path_key.startswith("payload.") else path_key
  185. if "." in clean_key:
  186. root_field, *nested_keys = clean_key.split(".")
  187. if hasattr(entry, root_field):
  188. root_val = getattr(entry, root_field)
  189. if not isinstance(root_val, dict):
  190. root_val = {}
  191. else:
  192. root_val = copy.deepcopy(root_val)
  193. curr = root_val
  194. for nk in nested_keys[:-1]:
  195. if nk not in curr or not isinstance(curr[nk], dict):
  196. curr[nk] = {}
  197. curr = curr[nk]
  198. curr[nested_keys[-1]] = val
  199. setattr(entry, root_field, root_val)
  200. flag_modified(entry, root_field)
  201. else:
  202. if hasattr(entry, clean_key) and clean_key not in ("id", "created_at"):
  203. if isinstance(val, (dict, list)):
  204. val = copy.deepcopy(val)
  205. setattr(entry, clean_key, val)
  206. flag_modified(entry, clean_key)
  207. else:
  208. setattr(entry, clean_key, val)
  209. # 2. Apply clear_fields
  210. default_empty_map = {
  211. "proxy_pool": [],
  212. "appointment_types": [],
  213. "booker": {},
  214. "sentinel": {},
  215. "account": {},
  216. "query_wait": {},
  217. "user_preferences": {},
  218. "plugin_config": {},
  219. "free_config": {},
  220. "website": "",
  221. "order_id": None,
  222. }
  223. for field in clear_fields:
  224. clean_field = field[8:] if field.startswith("payload.") else field
  225. if hasattr(entry, clean_field) and clean_field not in ("id", "created_at", "version"):
  226. default_val = default_empty_map.get(clean_field, None)
  227. setattr(entry, clean_field, default_val)
  228. if isinstance(default_val, (dict, list)):
  229. flag_modified(entry, clean_field)
  230. # Increment version and update operator
  231. entry.version = (entry.version or 0) + 1
  232. entry.updated_by = operator
  233. await db.flush()
  234. # Record revision snapshot
  235. snapshot = cls._to_snapshot(entry)
  236. await cls._record_revision(
  237. db=db,
  238. entry_type=entry_type,
  239. entry_id=entry.id,
  240. version=entry.version,
  241. action="update",
  242. snapshot=snapshot,
  243. updated_by=operator,
  244. )
  245. await db.commit()
  246. await db.refresh(entry)
  247. return entry
  248. @classmethod
  249. async def toggle_enable(
  250. cls,
  251. db: AsyncSession,
  252. entry_type: str,
  253. entry_id: int,
  254. enable: bool,
  255. base_version: Optional[int] = None,
  256. operator: str = "system",
  257. ) -> Any:
  258. entry = await cls.get_entry(db, entry_type, entry_id)
  259. entry.enable = 1 if enable else 0
  260. entry.version = (entry.version or 0) + 1
  261. entry.updated_by = operator
  262. await db.flush()
  263. snapshot = cls._to_snapshot(entry)
  264. await cls._record_revision(
  265. db=db,
  266. entry_type=entry_type,
  267. entry_id=entry.id,
  268. version=entry.version,
  269. action="enable" if enable else "disable",
  270. snapshot=snapshot,
  271. updated_by=operator,
  272. )
  273. await db.commit()
  274. await db.refresh(entry)
  275. return entry
  276. @classmethod
  277. async def delete_entry(
  278. cls,
  279. db: AsyncSession,
  280. entry_type: str,
  281. entry_id: int,
  282. confirm_purge: bool = False,
  283. ) -> bool:
  284. model = cls.get_model(entry_type)
  285. entry = await cls.get_entry(db, entry_type, entry_id)
  286. # Deletion guard: count remaining entries for this entry_type
  287. total_count = await db.scalar(select(func.count(model.id))) or 0
  288. if total_count <= 1 and not confirm_purge:
  289. raise BizException(
  290. code=40000,
  291. message="Deleting the last entry requires confirm_purge=true",
  292. http_status=400,
  293. )
  294. await db.delete(entry)
  295. await db.commit()
  296. return True
  297. @classmethod
  298. async def get_revisions(
  299. cls,
  300. db: AsyncSession,
  301. entry_type: str,
  302. entry_id: int,
  303. ) -> List[NodeConfigurationRevision]:
  304. # Validate existence
  305. await cls.get_entry(db, entry_type, entry_id)
  306. stmt = (
  307. select(NodeConfigurationRevision)
  308. .where(
  309. NodeConfigurationRevision.entry_type == entry_type,
  310. NodeConfigurationRevision.entry_id == entry_id,
  311. )
  312. .order_by(desc(NodeConfigurationRevision.id))
  313. )
  314. result = await db.execute(stmt)
  315. return list(result.scalars().all())
  316. @classmethod
  317. async def rollback_revision(
  318. cls,
  319. db: AsyncSession,
  320. entry_type: str,
  321. entry_id: int,
  322. revision_id: int,
  323. base_version: Optional[int] = None,
  324. operator: str = "system",
  325. ) -> Any:
  326. entry = await cls.get_entry(db, entry_type, entry_id)
  327. rev_stmt = select(NodeConfigurationRevision).where(
  328. NodeConfigurationRevision.id == revision_id,
  329. NodeConfigurationRevision.entry_type == entry_type,
  330. NodeConfigurationRevision.entry_id == entry_id,
  331. )
  332. rev = (await db.execute(rev_stmt)).scalar_one_or_none()
  333. if not rev:
  334. raise BizException(
  335. code=40400,
  336. message=f"Revision {revision_id} for {entry_type} entry {entry_id} not found",
  337. http_status=404,
  338. )
  339. snapshot = rev.snapshot or {}
  340. model = cls.get_model(entry_type)
  341. ignore_cols = {"id", "created_at", "updated_at", "version"}
  342. for col in model.__table__.columns:
  343. if col.name not in ignore_cols and col.name in snapshot:
  344. val = snapshot[col.name]
  345. if isinstance(val, (dict, list)):
  346. val = copy.deepcopy(val)
  347. setattr(entry, col.name, val)
  348. flag_modified(entry, col.name)
  349. else:
  350. setattr(entry, col.name, val)
  351. entry.version = (entry.version or 0) + 1
  352. entry.updated_by = operator
  353. await db.flush()
  354. new_snapshot = cls._to_snapshot(entry)
  355. await cls._record_revision(
  356. db=db,
  357. entry_type=entry_type,
  358. entry_id=entry.id,
  359. version=entry.version,
  360. action="rollback",
  361. snapshot=new_snapshot,
  362. updated_by=operator,
  363. )
  364. await db.commit()
  365. await db.refresh(entry)
  366. return entry
  367. # ==================== 集群节点档案注册管理 ====================
  368. @classmethod
  369. async def list_cluster_nodes(
  370. cls,
  371. db: AsyncSession,
  372. status: Optional[int] = None,
  373. keyword: Optional[str] = None,
  374. ) -> List[ClusterNodeEntry]:
  375. stmt = select(ClusterNodeEntry)
  376. if status is not None:
  377. stmt = stmt.where(ClusterNodeEntry.status == status)
  378. if keyword and keyword.strip():
  379. k = f"%{keyword.strip()}%"
  380. stmt = stmt.where(
  381. (ClusterNodeEntry.node_id.ilike(k))
  382. | (ClusterNodeEntry.name.ilike(k))
  383. | (ClusterNodeEntry.ip_address.ilike(k))
  384. | (ClusterNodeEntry.region.ilike(k))
  385. )
  386. stmt = stmt.order_by(ClusterNodeEntry.created_at.desc())
  387. res = await db.execute(stmt)
  388. return res.scalars().all()
  389. @classmethod
  390. async def get_cluster_node(
  391. cls,
  392. db: AsyncSession,
  393. node_id: str,
  394. ) -> Optional[ClusterNodeEntry]:
  395. stmt = select(ClusterNodeEntry).where(ClusterNodeEntry.node_id == node_id)
  396. return (await db.execute(stmt)).scalar_one_or_none()
  397. @classmethod
  398. async def create_cluster_node(
  399. cls,
  400. db: AsyncSession,
  401. data: ClusterNodeCreate,
  402. ) -> ClusterNodeEntry:
  403. existing = await cls.get_cluster_node(db, data.node_id.strip())
  404. if existing:
  405. raise BizException(
  406. code=40001,
  407. message=f"节点标识 '{data.node_id.strip()}' 已存在,请勿重复注册",
  408. http_status=400,
  409. )
  410. node = ClusterNodeEntry(
  411. node_id=data.node_id.strip(),
  412. name=data.name.strip(),
  413. ip_address=data.ip_address.strip() if data.ip_address else None,
  414. region=data.region.strip() if data.region else None,
  415. status=data.status if data.status is not None else 1,
  416. notes=data.notes.strip() if data.notes else None,
  417. )
  418. db.add(node)
  419. await db.commit()
  420. await db.refresh(node)
  421. return node
  422. @classmethod
  423. async def update_cluster_node(
  424. cls,
  425. db: AsyncSession,
  426. node_id: str,
  427. data: ClusterNodeUpdate,
  428. ) -> ClusterNodeEntry:
  429. node = await cls.get_cluster_node(db, node_id)
  430. if not node:
  431. raise BizException(
  432. code=40401,
  433. message=f"未找到节点: '{node_id}'",
  434. http_status=404,
  435. )
  436. if data.name is not None:
  437. node.name = data.name.strip()
  438. if data.ip_address is not None:
  439. node.ip_address = data.ip_address.strip() if data.ip_address else None
  440. if data.region is not None:
  441. node.region = data.region.strip() if data.region else None
  442. if data.status is not None:
  443. node.status = data.status
  444. if data.notes is not None:
  445. node.notes = data.notes.strip() if data.notes else None
  446. await db.commit()
  447. await db.refresh(node)
  448. return node
  449. @classmethod
  450. async def delete_cluster_node(
  451. cls,
  452. db: AsyncSession,
  453. node_id: str,
  454. ) -> bool:
  455. node = await cls.get_cluster_node(db, node_id)
  456. if not node:
  457. raise BizException(
  458. code=40401,
  459. message=f"未找到节点: '{node_id}'",
  460. http_status=404,
  461. )
  462. await db.delete(node)
  463. await db.commit()
  464. return True