demand_grade_plan.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from __future__ import annotations
  2. from datetime import datetime
  3. from sqlalchemy import BigInteger, Index, Integer, String, Text, UniqueConstraint, func
  4. from sqlalchemy.orm import Mapped, mapped_column
  5. from supply_infra.db.base import Base
  6. class DemandGradePlan(Base):
  7. """统筹规划 Agent 产出的单日全量分级计划。"""
  8. __tablename__ = "demand_grade_plan"
  9. __table_args__ = (Index("idx_demand_grade_plan_biz_dt", "biz_dt"),)
  10. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  11. plan_id: Mapped[str] = mapped_column(String(36), nullable=False, unique=True)
  12. biz_dt: Mapped[str] = mapped_column(String(8), nullable=False)
  13. status: Mapped[str] = mapped_column(String(32), nullable=False, default="planned")
  14. total_hanging_nodes: Mapped[int] = mapped_column(Integer, nullable=False)
  15. group_count: Mapped[int] = mapped_column(Integer, nullable=False)
  16. coverage_complete: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  17. plan_json: Mapped[str] = mapped_column(Text, nullable=False)
  18. create_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now())
  19. update_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now(), onupdate=func.now())
  20. class DemandGradePlanGroup(Base):
  21. """日计划中的节点组任务,由并发分级 worker 领取执行。"""
  22. __tablename__ = "demand_grade_plan_group"
  23. __table_args__ = (
  24. UniqueConstraint("plan_id", "group_no", name="uk_demand_grade_plan_group"),
  25. Index("idx_demand_grade_plan_group_claim", "plan_id", "status", "group_no"),
  26. Index("idx_demand_grade_plan_group_biz_dt", "biz_dt"),
  27. )
  28. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  29. plan_id: Mapped[str] = mapped_column(String(36), nullable=False)
  30. biz_dt: Mapped[str] = mapped_column(String(8), nullable=False)
  31. group_no: Mapped[int] = mapped_column(Integer, nullable=False)
  32. group_key: Mapped[str] = mapped_column(String(128), nullable=False)
  33. category_ids: Mapped[str] = mapped_column(Text, nullable=False)
  34. planning_reason: Mapped[str] = mapped_column(Text, nullable=False)
  35. shared_traits: Mapped[str] = mapped_column(Text, nullable=False)
  36. status: Mapped[str] = mapped_column(String(32), nullable=False, default="pending")
  37. attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  38. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  39. started_at: Mapped[datetime | None] = mapped_column(nullable=True)
  40. finished_at: Mapped[datetime | None] = mapped_column(nullable=True)
  41. create_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now())
  42. update_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now(), onupdate=func.now())
  43. class DemandGradePlanGroupItem(Base):
  44. """计划组内待执行分级的具体需求池记录。"""
  45. __tablename__ = "demand_grade_plan_group_item"
  46. __table_args__ = (
  47. UniqueConstraint("group_id", "pool_id", name="uk_demand_grade_plan_group_item"),
  48. Index("idx_demand_grade_plan_group_item_group_status", "group_id", "status", "sort_order"),
  49. Index("idx_demand_grade_plan_group_item_biz_dt", "biz_dt"),
  50. )
  51. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  52. group_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
  53. biz_dt: Mapped[str] = mapped_column(String(8), nullable=False)
  54. pool_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
  55. demand_name: Mapped[str] = mapped_column(String(512), nullable=False)
  56. sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  57. status: Mapped[str] = mapped_column(String(32), nullable=False, default="pending")
  58. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  59. finished_at: Mapped[datetime | None] = mapped_column(nullable=True)
  60. create_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now())
  61. update_time: Mapped[datetime] = mapped_column(nullable=False, server_default=func.now(), onupdate=func.now())