[feat] solution/backend: 미니 블로그 글을 배정 날짜에 맞는 소재로 만든다
예전에는 소재 목록을 순서대로 뽑아 날짜에 차례로 붙여서, 9월 날짜에 '한겨울' 글이나 끝난 축제 글이 붙을 수 있었다. 이제 날짜를 먼저 정하고 그 날짜에 맞는 소재를 고른다. - 축제: 시작 14일 전 ~ 종료일 사이만 / 계절: 게시일 절기 하나 / 날씨: 그 달에 있을 법한 것만(눈 12~2월, 소나기 6~8월) / 주변: 늘 후보 - 프롬프트에 게시일을 넣고, 날씨를 단정하지 않게 문구를 고쳤다 - 계절·날씨·축제 주제 키에 연(월)을 넣어 해마다 다시 쓸 수 있게 했다 - 자동·구간·개별 세 경로가 _compose_for_dates 하나로 만든다 같이 고친 버그: materials 가 스냅샷에 없는 local.festivals·attractions 를 읽어 축제·주변 소재가 늘 비어 있었다. 원문 행(local.contents)을 읽는다.
This commit is contained in:
1 parent
be3e43162a
commit
36dbe26c91
4 files changed
+240
-137
No files matched your search
@@ -3,7 +3,7 @@
|
||||
★ 잡은 '대상을 고르는 것'까지만 하고 실제 일은 서비스가 한다(scheduler/jobs.py 규약).
|
||||
★ 한 번에 BATCH_SIZE 건씩 만든다. 한 달치를 한 호출로 뽑으면 앞 회차 주제를 프롬프트에
|
||||
못 넣어 중복이 막히지 않는다.
|
||||
★ 팀 사전검수 없음 — 금칙 필터(blog_service.filter_drafts)를 통과하면 바로 REVIEWED 로
|
||||
★ 팀 사전검수 없음 — 금칙 필터(blog_service.is_publishable_body)를 통과하면 바로 REVIEWED 로
|
||||
쌓이고, send_reviewed() 가 업장당 하루 한 통씩 그대로 사장님에게 보낸다.
|
||||
★ 글마다 scheduled_date(KST) 를 하나씩 배정한다 — "언제 만들어졌나"만 있고 "언제 낼
|
||||
것인가"가 없으면 달력 화면이 근거 없는 날짜를 지어내야 한다(2026-09-17).
|
||||
@@ -73,11 +73,14 @@ async def _pending_count(place_id) -> int:
|
||||
return len(result.all()) if result is not None else 0
|
||||
|
||||
|
||||
async def _generate_for_place(place) -> int:
|
||||
"""업장 하나. 재고가 이미 REFILL_BELOW 이상이면 아무것도 안 만든다(만든 수 0)."""
|
||||
if await _pending_count(place.place_id) >= REFILL_BELOW:
|
||||
return 0
|
||||
async def _compose_for_dates(place, dates: list[date]) -> list[dict]:
|
||||
"""날짜마다 그 날짜에 맞는 소재(blog_service.materials(snapshot, d))로 한 편씩 만든다 —
|
||||
세 생성 경로(자동·구간·개별)가 같이 쓴다. 저장은 부르는 쪽이 한다.
|
||||
|
||||
★ 날짜를 먼저 정하고 소재를 고른다(2026-09-23). 예전에는 소재 목록을 순서대로 뽑아 날짜에
|
||||
차례로 붙여서, 글 내용이 배정된 날짜와 무관했다.
|
||||
★ 그 날짜에 맞는 소재가 없으면 그 날짜만 비워 두고 다음 날짜로 간다 — 뒤 날짜엔 축제가 걸릴 수 있다.
|
||||
★ LLM 이 없거나 실패하면(None) 그 자리에서 멈춘다 — 날짜마다 소재를 전부 돌며 헛호출하지 않는다."""
|
||||
used = await DB_SESSION_MNG.execute_lambda(
|
||||
place_posts.DBType(), DBWRType.DB_READ.value,
|
||||
lambda s, pid=place.place_id: _crud.used_topic_keys(s, pid),
|
||||
@@ -87,39 +90,47 @@ async def _generate_for_place(place) -> int:
|
||||
region = site_payload.region_label(place.road_address, place.address)
|
||||
|
||||
rows = []
|
||||
for kind, key, material in blog_service.materials(snapshot):
|
||||
if len(rows) >= BATCH_SIZE:
|
||||
for target in dates:
|
||||
for kind, key, material in blog_service.materials(snapshot, target):
|
||||
if key in used_set:
|
||||
continue
|
||||
generated = await blog_service.generate_one(
|
||||
place_name=place.name, region=region, topic_kind=kind, material=material,
|
||||
used_topics=sorted(used_set), place_category=place.category, post_date=target,
|
||||
)
|
||||
if not generated:
|
||||
return rows
|
||||
body, model = generated
|
||||
ok, reason = blog_service.is_publishable_body(body)
|
||||
used_set.add(key) # 버린 주제도 이번 회차에서 다시 고르지 않는다
|
||||
if not ok:
|
||||
LOG.i(f"[blog] place={place.place_id} {target} 버림 — {reason}")
|
||||
continue
|
||||
rows.append({
|
||||
"place_id": place.place_id, "body": body, "topic_kind": kind, "topic_key": key,
|
||||
"scheduled_date": target, "generation_meta": {"model": model},
|
||||
"status": PostStatus.REVIEWED.value, # 금칙 필터를 이미 통과했다 — 팀 사전검수 없음
|
||||
})
|
||||
break
|
||||
if key in used_set:
|
||||
continue
|
||||
generated = await blog_service.generate_one(
|
||||
place_name=place.name, region=region, topic_kind=kind,
|
||||
material=material, used_topics=sorted(used_set), place_category=place.category,
|
||||
)
|
||||
if not generated:
|
||||
continue
|
||||
body, model = generated
|
||||
rows.append({
|
||||
"place_id": place.place_id, "body": body, "topic_kind": kind, "topic_key": key,
|
||||
"generation_meta": {"model": model},
|
||||
})
|
||||
used_set.add(key)
|
||||
return rows
|
||||
|
||||
kept, dropped = blog_service.filter_drafts(rows)
|
||||
if kept:
|
||||
latest = await DB_SESSION_MNG.execute_lambda(
|
||||
place_posts.DBType(), DBWRType.DB_READ.value,
|
||||
lambda s, pid=place.place_id: _crud.max_scheduled_date(s, pid),
|
||||
)
|
||||
next_date = max(latest + timedelta(days=1), _today_kst()) if latest else _today_kst()
|
||||
for offset, row in enumerate(kept):
|
||||
row["scheduled_date"] = next_date + timedelta(days=offset)
|
||||
|
||||
async def _generate_for_place(place) -> int:
|
||||
"""업장 하나. 재고가 이미 REFILL_BELOW 이상이면 아무것도 안 만든다(만든 수 0)."""
|
||||
if await _pending_count(place.place_id) >= REFILL_BELOW:
|
||||
return 0
|
||||
|
||||
latest = await DB_SESSION_MNG.execute_lambda(
|
||||
place_posts.DBType(), DBWRType.DB_READ.value,
|
||||
lambda s, pid=place.place_id: _crud.max_scheduled_date(s, pid),
|
||||
)
|
||||
next_date = max(latest + timedelta(days=1), _today_kst()) if latest else _today_kst()
|
||||
rows = await _compose_for_dates(place, [next_date + timedelta(days=i) for i in range(BATCH_SIZE)])
|
||||
if rows:
|
||||
await DB_SESSION_MNG.execute_lambda_run(
|
||||
[place_posts.DBType()], [lambda s, r=kept: _crud.add_many(s, r)],
|
||||
[place_posts.DBType()], [lambda s, r=rows: _crud.add_many(s, r)],
|
||||
)
|
||||
if dropped:
|
||||
LOG.i(f"[blog] place={place.place_id} 버린 {len(dropped)}건 — {dropped[0][1]}")
|
||||
return len(kept)
|
||||
return len(rows)
|
||||
|
||||
|
||||
async def generate_drafts() -> int:
|
||||
@@ -135,8 +146,8 @@ async def generate_range(place_id: str, start_date: date, end_date: date) -> dic
|
||||
(2026-09-17, 사장님 지시: "지금 생성하기에서 시작이랑 끝 날짜를 정해야하지 않을까").
|
||||
재고 상한(REFILL_BELOW)을 안 본다 — 개별 생성과 같은 이유로, 직접 고른 구간에
|
||||
상한 로직이 끼어들 자리가 아니다. 이미 글이 있는 날짜는 LLM 을 부르지 않고 건너뛴다 —
|
||||
매번 새로 만들고 유니크 충돌로 버리면 호출만 낭비된다. 소재가 떨어지면(구간 안에서
|
||||
더 만들 topic_key 가 없으면) 그 자리에서 멈춘다 — 못 채운 나머지는 그대로 빈 날짜로 남는다."""
|
||||
매번 새로 만들고 유니크 충돌로 버리면 호출만 낭비된다. 그 날짜에 맞는 소재가 없으면
|
||||
그 날짜는 빈 날짜로 남는다(_compose_for_dates)."""
|
||||
place = None
|
||||
for p, _user in await _published_places():
|
||||
if str(p.place_id) == str(place_id):
|
||||
@@ -157,38 +168,7 @@ async def generate_range(place_id: str, start_date: date, end_date: date) -> dic
|
||||
if not empty_dates:
|
||||
return {"requested": requested, "created": 0}
|
||||
|
||||
used = await DB_SESSION_MNG.execute_lambda(
|
||||
place_posts.DBType(), DBWRType.DB_READ.value,
|
||||
lambda s, pid=place.place_id: _crud.used_topic_keys(s, pid),
|
||||
)
|
||||
used_set = set(used or [])
|
||||
snapshot = await build_snapshot(place)
|
||||
region = site_payload.region_label(place.road_address, place.address)
|
||||
|
||||
rows = []
|
||||
for kind, key, material in blog_service.materials(snapshot):
|
||||
if len(rows) >= len(empty_dates):
|
||||
break
|
||||
if key in used_set:
|
||||
continue
|
||||
generated = await blog_service.generate_one(
|
||||
place_name=place.name, region=region, topic_kind=kind,
|
||||
material=material, used_topics=sorted(used_set), place_category=place.category,
|
||||
)
|
||||
if not generated:
|
||||
continue
|
||||
body, model = generated
|
||||
ok, _reason = blog_service.is_publishable_body(body)
|
||||
used_set.add(key)
|
||||
if not ok:
|
||||
continue
|
||||
rows.append({
|
||||
"place_id": place.place_id, "body": body, "topic_kind": kind, "topic_key": key,
|
||||
"generation_meta": {"model": model}, "status": PostStatus.REVIEWED.value,
|
||||
})
|
||||
|
||||
for row, d in zip(rows, empty_dates):
|
||||
row["scheduled_date"] = d
|
||||
rows = await _compose_for_dates(place, empty_dates)
|
||||
if rows:
|
||||
await DB_SESSION_MNG.execute_lambda_run(
|
||||
[place_posts.DBType()], [lambda s, r=rows: _crud.add_many(s, r)],
|
||||
@@ -208,39 +188,12 @@ async def generate_one_for_date(place_id: str, target_date: date) -> dict | None
|
||||
if place is None:
|
||||
return None
|
||||
|
||||
used = await DB_SESSION_MNG.execute_lambda(
|
||||
place_posts.DBType(), DBWRType.DB_READ.value,
|
||||
lambda s, pid=place.place_id: _crud.used_topic_keys(s, pid),
|
||||
)
|
||||
used_set = set(used or [])
|
||||
snapshot = await build_snapshot(place)
|
||||
region = site_payload.region_label(place.road_address, place.address)
|
||||
|
||||
for kind, key, material in blog_service.materials(snapshot):
|
||||
if key in used_set:
|
||||
continue
|
||||
generated = await blog_service.generate_one(
|
||||
place_name=place.name, region=region, topic_kind=kind,
|
||||
material=material, used_topics=sorted(used_set), place_category=place.category,
|
||||
)
|
||||
if not generated:
|
||||
continue
|
||||
body, model = generated
|
||||
ok, _reason = blog_service.is_publishable_body(body)
|
||||
if not ok:
|
||||
used_set.add(key) # 이 주제는 이번 시도에서 다시 고르지 않는다
|
||||
continue
|
||||
|
||||
row = {
|
||||
"place_id": place.place_id, "body": body, "topic_kind": kind, "topic_key": key,
|
||||
"scheduled_date": target_date, "generation_meta": {"model": model},
|
||||
"status": PostStatus.REVIEWED.value, # 금칙 필터를 이미 통과했다 — 벌크 경로(filter_drafts)와 동일
|
||||
}
|
||||
inserted = await DB_SESSION_MNG.execute_lambda_write(
|
||||
place_posts.DBType(), lambda s, r=row: _crud.add_one(s, r),
|
||||
)
|
||||
return inserted # None 이면 그 날짜(또는 주제)가 이미 차 있었다 — 다시 시도하지 않는다
|
||||
return None
|
||||
rows = await _compose_for_dates(place, [target_date])
|
||||
if not rows:
|
||||
return None
|
||||
return await DB_SESSION_MNG.execute_lambda_write(
|
||||
place_posts.DBType(), lambda s, r=rows[0]: _crud.add_one(s, r),
|
||||
) # None 이면 그 날짜(또는 주제)가 이미 차 있었다 — 다시 시도하지 않는다
|
||||
|
||||
|
||||
def _mail_body(*, place_name: str, post, user, origin: str, approve_token: str) -> str:
|
||||
|
||||
Reference in new issue
Block a user