node_configuration_service.py 13 KB

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