feat(lps): 2단계 — price_history 에 몰별 확인 상태 저장
1단계에서 만든 SourceState 가 job.result 에만 있어 화면까지 못 갔다. by_mall 은 **가격이 있는
몰만** 담으므로, 빠진 몰이 '거기엔 없더라'인지 '거기를 못 봤다'인지 구분할 자리가 없었다.
- price_history.sources (JSONB): 몰별 상태를 그대로 담는다.
{"naver": {"state": "matched", "count": 40}, "coupang": {"state": "blocked", "error": "..."}}
열린 스키마라 몰이 늘거나 근거를 덧붙여도 마이그레이션이 필요 없다(by_mall 과 같은 방침).
- price_history.partial (bool): 결과가 완전한가. sources 에서 유도 가능하지만 컬럼으로 둔다 —
소비자가 '어떤 상태가 확인된 것인가'라는 판단 규칙까지 알아야 하면 **상태 정의가 두 곳으로
흩어진다**. 판단은 LPS 가 끝내고 소비자(negodata·lps-admin)는 사실 하나만 읽는다.
- 부분 인덱스 ix_price_history_partial — partial=true 행만 담아 작게 유지(운영 점검·알림용).
- _record_history 가 per_source 를 받아 partial 을 계산해 기록한다. 네거티브 캐시 히트 경로는
sources 없이 남긴다(부분 결과는 애초에 캐시하지 않으므로 항상 확정).
- 마이그레이션: postgres-init/dbeaver/7_lps_source_state_dbeaver.sql (재실행 안전, **운영 적용 필요**)
검증(로컬 실 DB): 쿠팡 차단 vs 쿠팡 0건은 by_mall 이 둘 다 ['naver'] 로 같지만
partial(true/false)·sources.coupang.state(blocked/empty)가 두 경우를 갈라낸다.
JSONB 는 ensure_ascii=False 로 한글 사유가 깨지지 않는 것도 테스트로 고정.
테스트 3건 추가, 전체 289 passed. 진행 상황은 docs/result-states.md 4절.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b278f58d9c
commit
74db218410
7 files changed
+120
-13
No files matched your search
+15
-5
@@ -101,10 +101,20 @@ def build_search_handler(
|
||||
use = list(sources) if sources else list(adapters.keys())
|
||||
fallbacks = fallback_adapters or {}
|
||||
|
||||
async def _record_history(product_code: str, job_id, outcome: str, matched: list):
|
||||
async def _record_history(product_code: str, job_id, outcome: str, matched: list,
|
||||
sources: dict | None = None):
|
||||
"""price_history 1행 기록.
|
||||
|
||||
sources 를 함께 남기는 게 중요하다 — by_mall 은 **가격이 있는 몰만** 담으므로, 어떤 몰이
|
||||
빠졌을 때 '거기엔 없더라'인지 '거기를 못 봤다'인지 이 값 없이는 알 수 없다.
|
||||
partial 은 여기서 판단해 사실로 넘긴다(소비자가 상태 분류 규칙을 알 필요 없게).
|
||||
"""
|
||||
if history is None:
|
||||
return
|
||||
event = {"product_code": product_code, "job_id": job_id, "outcome": outcome, **_price_snapshot(matched)}
|
||||
confirmed = {s.name.lower() for s in SourceState if s.confirmed}
|
||||
partial = any((i or {}).get("state") not in confirmed for i in (sources or {}).values())
|
||||
event = {"product_code": product_code, "job_id": job_id, "outcome": outcome,
|
||||
"sources": sources or None, "partial": partial, **_price_snapshot(matched)}
|
||||
try:
|
||||
await history.record(event)
|
||||
except Exception as ex:
|
||||
@@ -268,7 +278,7 @@ def build_search_handler(
|
||||
partial=bool(failed_sources), metrics=metrics.snapshot())
|
||||
if failed_sources:
|
||||
LOG.w(f"[partial] {failed_sources} 없이 결과를 냈습니다 — 그 몰의 더 싼 값은 못 봤을 수 있습니다")
|
||||
await _record_history(cache_key, job.get("job_id"), "found", candidates)
|
||||
await _record_history(cache_key, job.get("job_id"), "found", candidates, per_source)
|
||||
return result
|
||||
|
||||
# 0매칭 + 일부 소스 실패. **살아있는 소스가 하나라도 있으면 그 결과로 진행한다** —
|
||||
@@ -287,7 +297,7 @@ def build_search_handler(
|
||||
result.update(outcome="error", query=query, rounds_tried=rounds_done,
|
||||
sources=per_source, sources_ok=[], sources_failed=failed_sources,
|
||||
partial=True, metrics=metrics.snapshot())
|
||||
await _record_history(cache_key, job.get("job_id"), "error", [])
|
||||
await _record_history(cache_key, job.get("job_id"), "error", [], per_source)
|
||||
return result
|
||||
raise RuntimeError(f"모든 소스 실패로 0매칭(round={label}) — 잡 재시도: {per_source}")
|
||||
|
||||
@@ -303,7 +313,7 @@ def build_search_handler(
|
||||
partial=partial, metrics=metrics.snapshot())
|
||||
if partial:
|
||||
LOG.w(f"[partial] {last_failed} 없이 not_found — 네거티브 캐시는 건너뜁니다(그 몰엔 있었을 수 있음)")
|
||||
await _record_history(cache_key, job.get("job_id"), "not_found", [])
|
||||
await _record_history(cache_key, job.get("job_id"), "not_found", [], last_sources)
|
||||
return result
|
||||
|
||||
return handler
|
||||
Reference in new issue
Block a user