node_configuration_service.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. )
  13. class NodeConfigurationService:
  14. ENTRY_MODEL_MAP: Dict[str, Type[Any]] = {
  15. "booker": BookerEntry,
  16. "sentinel": SentinelEntry,
  17. "standalone": StandaloneEntry,
  18. }
  19. @classmethod
  20. def get_model(cls, entry_type: str) -> Type[Any]:
  21. model = cls.ENTRY_MODEL_MAP.get(entry_type.lower())
  22. if not model:
  23. raise BizException(
  24. code=40000,
  25. message=f"Invalid entry_type: '{entry_type}'. Must be one of {list(cls.ENTRY_MODEL_MAP.keys())}",
  26. http_status=400,
  27. )
  28. return model
  29. @classmethod
  30. def _to_snapshot(cls, obj: Any) -> Dict[str, Any]:
  31. """Serialize SQLAlchemy model columns to dict snapshot"""
  32. snapshot = {}
  33. for column in obj.__table__.columns:
  34. val = getattr(obj, column.name)
  35. if hasattr(val, "isoformat"):
  36. val = val.isoformat()
  37. snapshot[column.name] = copy.deepcopy(val)
  38. return snapshot
  39. @classmethod
  40. async def _record_revision(
  41. cls,
  42. db: AsyncSession,
  43. entry_type: str,
  44. entry_id: int,
  45. version: int,
  46. action: str,
  47. snapshot: Dict[str, Any],
  48. updated_by: str,
  49. ) -> NodeConfigurationRevision:
  50. rev = NodeConfigurationRevision(
  51. entry_type=entry_type,
  52. entry_id=entry_id,
  53. version=version,
  54. action=action,
  55. snapshot=snapshot,
  56. updated_by=updated_by or "system",
  57. )
  58. db.add(rev)
  59. await db.flush()
  60. return rev
  61. @classmethod
  62. async def list_entries(
  63. cls,
  64. db: AsyncSession,
  65. entry_type: str,
  66. page: int = 1,
  67. page_size: int = 20,
  68. identifier: Optional[str] = None,
  69. node_id: Optional[str] = None,
  70. order_id: Optional[str] = None,
  71. ) -> Tuple[int, List[Any]]:
  72. model = cls.get_model(entry_type)
  73. stmt = select(model)
  74. count_stmt = select(func.count(model.id))
  75. if identifier:
  76. stmt = stmt.where(model.identifier.like(f"%{identifier}%"))
  77. count_stmt = count_stmt.where(model.identifier.like(f"%{identifier}%"))
  78. if node_id:
  79. stmt = stmt.where(model.node_id == node_id)
  80. count_stmt = count_stmt.where(model.node_id == node_id)
  81. if order_id and hasattr(model, "order_id"):
  82. stmt = stmt.where(model.order_id == order_id)
  83. count_stmt = count_stmt.where(model.order_id == order_id)
  84. total = await db.scalar(count_stmt) or 0
  85. offset = (page - 1) * page_size
  86. stmt = stmt.order_by(desc(model.updated_at), desc(model.id)).offset(offset).limit(page_size)
  87. result = await db.execute(stmt)
  88. items = list(result.scalars().all())
  89. return total, items
  90. @classmethod
  91. async def get_entry(
  92. cls, db: AsyncSession, entry_type: str, entry_id: int
  93. ) -> Any:
  94. model = cls.get_model(entry_type)
  95. stmt = select(model).where(model.id == entry_id)
  96. entry = (await db.execute(stmt)).scalar_one_or_none()
  97. if not entry:
  98. raise BizException(
  99. code=40400,
  100. message=f"Configuration entry ({entry_type} ID: {entry_id}) not found",
  101. http_status=404,
  102. )
  103. return entry
  104. @classmethod
  105. async def create_entry(
  106. cls,
  107. db: AsyncSession,
  108. entry_type: str,
  109. data: Dict[str, Any],
  110. operator: str = "system",
  111. ) -> Any:
  112. model = cls.get_model(entry_type)
  113. node_id = data.get("node_id") or "default"
  114. identifier = data.get("identifier")
  115. if not identifier:
  116. raise BizException(
  117. code=40000,
  118. message="Field 'identifier' is required",
  119. http_status=400,
  120. )
  121. # Check duplicate (node_id, identifier)
  122. check_stmt = select(model).where(
  123. model.node_id == node_id, model.identifier == identifier
  124. )
  125. existing = (await db.execute(check_stmt)).scalar_one_or_none()
  126. if existing:
  127. raise BizException(
  128. code=40902,
  129. message=f"Identifier '{identifier}' on node '{node_id}' already exists",
  130. http_status=409,
  131. )
  132. # Prepare model payload
  133. entry_data = data.copy()
  134. entry_data["node_id"] = node_id
  135. entry_data["enable"] = 0 # Force disabled upon creation
  136. entry_data["updated_by"] = operator
  137. entry_data["version"] = 1
  138. # Instantiate
  139. valid_cols = {c.name for c in model.__table__.columns}
  140. filtered_data = {k: v for k, v in entry_data.items() if k in valid_cols}
  141. entry = model(**filtered_data)
  142. db.add(entry)
  143. await db.flush()
  144. # Record revision
  145. snapshot = cls._to_snapshot(entry)
  146. await cls._record_revision(
  147. db=db,
  148. entry_type=entry_type,
  149. entry_id=entry.id,
  150. version=entry.version,
  151. action="create",
  152. snapshot=snapshot,
  153. updated_by=operator,
  154. )
  155. await db.commit()
  156. await db.refresh(entry)
  157. return entry
  158. @classmethod
  159. async def sparse_update_entry(
  160. cls,
  161. db: AsyncSession,
  162. entry_type: str,
  163. entry_id: int,
  164. set_data: Optional[Dict[str, Any]] = None,
  165. clear_fields: Optional[List[str]] = None,
  166. base_version: Optional[int] = None,
  167. operator: str = "system",
  168. ) -> Any:
  169. entry = await cls.get_entry(db, entry_type, entry_id)
  170. set_data = set_data or {}
  171. clear_fields = clear_fields or []
  172. # 1. Apply set_data with dot-path support
  173. for path_key, val in set_data.items():
  174. # Strip leading 'payload.' if present
  175. clean_key = path_key[8:] if path_key.startswith("payload.") else path_key
  176. if "." in clean_key:
  177. root_field, *nested_keys = clean_key.split(".")
  178. if hasattr(entry, root_field):
  179. root_val = getattr(entry, root_field)
  180. if not isinstance(root_val, dict):
  181. root_val = {}
  182. else:
  183. root_val = copy.deepcopy(root_val)
  184. curr = root_val
  185. for nk in nested_keys[:-1]:
  186. if nk not in curr or not isinstance(curr[nk], dict):
  187. curr[nk] = {}
  188. curr = curr[nk]
  189. curr[nested_keys[-1]] = val
  190. setattr(entry, root_field, root_val)
  191. flag_modified(entry, root_field)
  192. else:
  193. if hasattr(entry, clean_key) and clean_key not in ("id", "created_at"):
  194. if isinstance(val, (dict, list)):
  195. val = copy.deepcopy(val)
  196. setattr(entry, clean_key, val)
  197. flag_modified(entry, clean_key)
  198. else:
  199. setattr(entry, clean_key, val)
  200. # 2. Apply clear_fields
  201. default_empty_map = {
  202. "proxy_pool": [],
  203. "appointment_types": [],
  204. "booker": {},
  205. "sentinel": {},
  206. "account": {},
  207. "query_wait": {},
  208. "user_preferences": {},
  209. "plugin_config": {},
  210. "free_config": {},
  211. "website": "",
  212. "order_id": None,
  213. }
  214. for field in clear_fields:
  215. clean_field = field[8:] if field.startswith("payload.") else field
  216. if hasattr(entry, clean_field) and clean_field not in ("id", "created_at", "version"):
  217. default_val = default_empty_map.get(clean_field, None)
  218. setattr(entry, clean_field, default_val)
  219. if isinstance(default_val, (dict, list)):
  220. flag_modified(entry, clean_field)
  221. # Increment version and update operator
  222. entry.version = (entry.version or 0) + 1
  223. entry.updated_by = operator
  224. await db.flush()
  225. # Record revision snapshot
  226. snapshot = cls._to_snapshot(entry)
  227. await cls._record_revision(
  228. db=db,
  229. entry_type=entry_type,
  230. entry_id=entry.id,
  231. version=entry.version,
  232. action="update",
  233. snapshot=snapshot,
  234. updated_by=operator,
  235. )
  236. await db.commit()
  237. await db.refresh(entry)
  238. return entry
  239. @classmethod
  240. async def toggle_enable(
  241. cls,
  242. db: AsyncSession,
  243. entry_type: str,
  244. entry_id: int,
  245. enable: bool,
  246. base_version: Optional[int] = None,
  247. operator: str = "system",
  248. ) -> Any:
  249. entry = await cls.get_entry(db, entry_type, entry_id)
  250. entry.enable = 1 if enable else 0
  251. entry.version = (entry.version or 0) + 1
  252. entry.updated_by = operator
  253. await db.flush()
  254. snapshot = cls._to_snapshot(entry)
  255. await cls._record_revision(
  256. db=db,
  257. entry_type=entry_type,
  258. entry_id=entry.id,
  259. version=entry.version,
  260. action="enable" if enable else "disable",
  261. snapshot=snapshot,
  262. updated_by=operator,
  263. )
  264. await db.commit()
  265. await db.refresh(entry)
  266. return entry
  267. @classmethod
  268. async def delete_entry(
  269. cls,
  270. db: AsyncSession,
  271. entry_type: str,
  272. entry_id: int,
  273. confirm_purge: bool = False,
  274. ) -> bool:
  275. model = cls.get_model(entry_type)
  276. entry = await cls.get_entry(db, entry_type, entry_id)
  277. # Deletion guard: count remaining entries for this entry_type
  278. total_count = await db.scalar(select(func.count(model.id))) or 0
  279. if total_count <= 1 and not confirm_purge:
  280. raise BizException(
  281. code=40000,
  282. message="Deleting the last entry requires confirm_purge=true",
  283. http_status=400,
  284. )
  285. await db.delete(entry)
  286. await db.commit()
  287. return True
  288. @classmethod
  289. async def get_revisions(
  290. cls,
  291. db: AsyncSession,
  292. entry_type: str,
  293. entry_id: int,
  294. ) -> List[NodeConfigurationRevision]:
  295. # Validate existence
  296. await cls.get_entry(db, entry_type, entry_id)
  297. stmt = (
  298. select(NodeConfigurationRevision)
  299. .where(
  300. NodeConfigurationRevision.entry_type == entry_type,
  301. NodeConfigurationRevision.entry_id == entry_id,
  302. )
  303. .order_by(desc(NodeConfigurationRevision.id))
  304. )
  305. result = await db.execute(stmt)
  306. return list(result.scalars().all())
  307. @classmethod
  308. async def rollback_revision(
  309. cls,
  310. db: AsyncSession,
  311. entry_type: str,
  312. entry_id: int,
  313. revision_id: int,
  314. base_version: Optional[int] = None,
  315. operator: str = "system",
  316. ) -> Any:
  317. entry = await cls.get_entry(db, entry_type, entry_id)
  318. rev_stmt = select(NodeConfigurationRevision).where(
  319. NodeConfigurationRevision.id == revision_id,
  320. NodeConfigurationRevision.entry_type == entry_type,
  321. NodeConfigurationRevision.entry_id == entry_id,
  322. )
  323. rev = (await db.execute(rev_stmt)).scalar_one_or_none()
  324. if not rev:
  325. raise BizException(
  326. code=40400,
  327. message=f"Revision {revision_id} for {entry_type} entry {entry_id} not found",
  328. http_status=404,
  329. )
  330. snapshot = rev.snapshot or {}
  331. model = cls.get_model(entry_type)
  332. ignore_cols = {"id", "created_at", "updated_at", "version"}
  333. for col in model.__table__.columns:
  334. if col.name not in ignore_cols and col.name in snapshot:
  335. val = snapshot[col.name]
  336. if isinstance(val, (dict, list)):
  337. val = copy.deepcopy(val)
  338. setattr(entry, col.name, val)
  339. flag_modified(entry, col.name)
  340. else:
  341. setattr(entry, col.name, val)
  342. entry.version = (entry.version or 0) + 1
  343. entry.updated_by = operator
  344. await db.flush()
  345. new_snapshot = cls._to_snapshot(entry)
  346. await cls._record_revision(
  347. db=db,
  348. entry_type=entry_type,
  349. entry_id=entry.id,
  350. version=entry.version,
  351. action="rollback",
  352. snapshot=new_snapshot,
  353. updated_by=operator,
  354. )
  355. await db.commit()
  356. await db.refresh(entry)
  357. return entry