|
@@ -141,6 +141,66 @@ def _grade_priority_key(row: Any) -> tuple[float, int, str, int]:
|
|
|
return (-score, grade_rank, str(row.demand_name), int(row.id))
|
|
return (-score, grade_rank, str(row.demand_name), int(row.id))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def _build_context_from_grade_row(
|
|
|
|
|
+ session,
|
|
|
|
|
+ grade_row: Any,
|
|
|
|
|
+ *,
|
|
|
|
|
+ biz_dt: str | None = None,
|
|
|
|
|
+) -> FindDemandContext | None:
|
|
|
|
|
+ """由单条 demand_grade 记录组装 FindDemandContext;无有效点位时返回 None。"""
|
|
|
|
|
+ resolved_biz_dt = str(biz_dt or grade_row.biz_dt).strip()
|
|
|
|
|
+ expansion_repo = DemandVideoExpansionRepository(session)
|
|
|
|
|
+ detail_repo = MultiDemandVideoDetailRepository(session)
|
|
|
|
|
+
|
|
|
|
|
+ expansions = expansion_repo.list_by_demand_grade(
|
|
|
|
|
+ resolved_biz_dt, int(grade_row.id)
|
|
|
|
|
+ )
|
|
|
|
|
+ if not expansions:
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ by_video: dict[str, list[Any]] = {}
|
|
|
|
|
+ video_order: list[str] = []
|
|
|
|
|
+ for row in expansions:
|
|
|
|
|
+ video_id = str(row.video_id or "").strip()
|
|
|
|
|
+ if not video_id:
|
|
|
|
|
+ continue
|
|
|
|
|
+ if video_id not in by_video:
|
|
|
|
|
+ by_video[video_id] = []
|
|
|
|
|
+ video_order.append(video_id)
|
|
|
|
|
+ by_video[video_id].append(row)
|
|
|
|
|
+
|
|
|
|
|
+ if not video_order:
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ details = detail_repo.list_by_vids(video_order)
|
|
|
|
|
+ videos: list[FindDemandVideo] = []
|
|
|
|
|
+ for video_id in video_order:
|
|
|
|
|
+ points = _build_video_points(by_video[video_id])
|
|
|
|
|
+ if not points:
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ detail = details.get(video_id)
|
|
|
|
|
+ title = str(detail.title).strip() if detail and detail.title else ""
|
|
|
|
|
+ videos.append(
|
|
|
|
|
+ FindDemandVideo(
|
|
|
|
|
+ video_id=video_id,
|
|
|
|
|
+ title=title or f"(无标题|{video_id})",
|
|
|
|
|
+ points=points,
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ if not videos:
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ return FindDemandContext(
|
|
|
|
|
+ biz_dt=resolved_biz_dt,
|
|
|
|
|
+ demand_grade_id=int(grade_row.id),
|
|
|
|
|
+ demand_name=str(grade_row.demand_name),
|
|
|
|
|
+ grade=str(grade_row.grade),
|
|
|
|
|
+ videos=videos,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def load_find_demand_contexts(
|
|
def load_find_demand_contexts(
|
|
|
session,
|
|
session,
|
|
|
biz_dt: str,
|
|
biz_dt: str,
|
|
@@ -157,60 +217,30 @@ def load_find_demand_contexts(
|
|
|
if top_limit is not None and top_limit > 0:
|
|
if top_limit is not None and top_limit > 0:
|
|
|
grades_rows = grades_rows[: int(top_limit)]
|
|
grades_rows = grades_rows[: int(top_limit)]
|
|
|
|
|
|
|
|
- expansion_repo = DemandVideoExpansionRepository(session)
|
|
|
|
|
- detail_repo = MultiDemandVideoDetailRepository(session)
|
|
|
|
|
-
|
|
|
|
|
contexts: list[FindDemandContext] = []
|
|
contexts: list[FindDemandContext] = []
|
|
|
for grade_row in grades_rows:
|
|
for grade_row in grades_rows:
|
|
|
- expansions = expansion_repo.list_by_demand_grade(biz_dt, int(grade_row.id))
|
|
|
|
|
- if not expansions:
|
|
|
|
|
- continue
|
|
|
|
|
|
|
+ ctx = _build_context_from_grade_row(session, grade_row, biz_dt=biz_dt)
|
|
|
|
|
+ if ctx is not None:
|
|
|
|
|
+ contexts.append(ctx)
|
|
|
|
|
|
|
|
- by_video: dict[str, list[Any]] = {}
|
|
|
|
|
- video_order: list[str] = []
|
|
|
|
|
- for row in expansions:
|
|
|
|
|
- video_id = str(row.video_id or "").strip()
|
|
|
|
|
- if not video_id:
|
|
|
|
|
- continue
|
|
|
|
|
- if video_id not in by_video:
|
|
|
|
|
- by_video[video_id] = []
|
|
|
|
|
- video_order.append(video_id)
|
|
|
|
|
- by_video[video_id].append(row)
|
|
|
|
|
-
|
|
|
|
|
- if not video_order:
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- details = detail_repo.list_by_vids(video_order)
|
|
|
|
|
- videos: list[FindDemandVideo] = []
|
|
|
|
|
- for video_id in video_order:
|
|
|
|
|
- points = _build_video_points(by_video[video_id])
|
|
|
|
|
- if not points:
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- detail = details.get(video_id)
|
|
|
|
|
- title = str(detail.title).strip() if detail and detail.title else ""
|
|
|
|
|
- videos.append(
|
|
|
|
|
- FindDemandVideo(
|
|
|
|
|
- video_id=video_id,
|
|
|
|
|
- title=title or f"(无标题|{video_id})",
|
|
|
|
|
- points=points,
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ return contexts
|
|
|
|
|
|
|
|
- if not videos:
|
|
|
|
|
- continue
|
|
|
|
|
|
|
|
|
|
- contexts.append(
|
|
|
|
|
- FindDemandContext(
|
|
|
|
|
- biz_dt=biz_dt,
|
|
|
|
|
- demand_grade_id=int(grade_row.id),
|
|
|
|
|
- demand_name=str(grade_row.demand_name),
|
|
|
|
|
- grade=str(grade_row.grade),
|
|
|
|
|
- videos=videos,
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
|
|
+def load_find_demand_context_by_id(
|
|
|
|
|
+ demand_grade_id: int,
|
|
|
|
|
+ *,
|
|
|
|
|
+ biz_dt: str | None = None,
|
|
|
|
|
+) -> FindDemandContext | None:
|
|
|
|
|
+ """按 demand_grade.id(需求 id)直接加载一条 find_agent 上下文。
|
|
|
|
|
|
|
|
- return contexts
|
|
|
|
|
|
|
+ 不限制 S/A 等级;若记录不存在、或该需求下无有效拓展点位,返回 None。
|
|
|
|
|
+ biz_dt 默认取该分级记录自身的业务日。
|
|
|
|
|
+ """
|
|
|
|
|
+ with get_session() as session:
|
|
|
|
|
+ grade_row = DemandGradeRepository(session).get_by_id(int(demand_grade_id))
|
|
|
|
|
+ if grade_row is None:
|
|
|
|
|
+ return None
|
|
|
|
|
+ return _build_context_from_grade_row(session, grade_row, biz_dt=biz_dt)
|
|
|
|
|
|
|
|
|
|
|
|
|
def pick_find_demand_context(
|
|
def pick_find_demand_context(
|
|
@@ -221,6 +251,9 @@ def pick_find_demand_context(
|
|
|
grades: Iterable[str] = ("S", "A"),
|
|
grades: Iterable[str] = ("S", "A"),
|
|
|
) -> FindDemandContext | None:
|
|
) -> FindDemandContext | None:
|
|
|
"""从数据库选取一条待执行的 find_agent 上下文。"""
|
|
"""从数据库选取一条待执行的 find_agent 上下文。"""
|
|
|
|
|
+ if demand_grade_id is not None:
|
|
|
|
|
+ return load_find_demand_context_by_id(int(demand_grade_id), biz_dt=biz_dt)
|
|
|
|
|
+
|
|
|
resolved_biz_dt = _resolve_biz_dt(biz_dt)
|
|
resolved_biz_dt = _resolve_biz_dt(biz_dt)
|
|
|
with get_session() as session:
|
|
with get_session() as session:
|
|
|
contexts = load_find_demand_contexts(
|
|
contexts = load_find_demand_contexts(
|
|
@@ -229,12 +262,6 @@ def pick_find_demand_context(
|
|
|
grades=grades,
|
|
grades=grades,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- if demand_grade_id is not None:
|
|
|
|
|
- for ctx in contexts:
|
|
|
|
|
- if ctx.demand_grade_id == int(demand_grade_id):
|
|
|
|
|
- return ctx
|
|
|
|
|
- return None
|
|
|
|
|
-
|
|
|
|
|
if 0 <= index < len(contexts):
|
|
if 0 <= index < len(contexts):
|
|
|
return contexts[index]
|
|
return contexts[index]
|
|
|
return None
|
|
return None
|