몰이 열린 집합이라 와이드 컬럼(gmarket_*, st11_* …) 대신 JSONB 한 컬럼으로 담는다 — 몰 추가 시 마이그레이션 0. naver/coupang/final 3선 컬럼은 그래프 하위호환 유지. - models: price_history.by_mall JSONB 추가 - crud: record/list 에 by_mall 왕복(json.dumps + CAST jsonb) - handler: _price_snapshot 에 summarize_by_mall 적재, final 은 소스무관 전체 최저로 - protocol/service: history API 응답에 by_mall 노출 - migrations/2026-07-09: 기존 dev DB 동기화용 ALTER(추적 파일) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
"""LPS 검색 요청 도메인 로직. 요청을 검색 잡으로 큐에 적재하고 상태/통계를 조회한다.
|
|
|
|
라우터는 요청 검증→service 호출→RemoveNoneResponse 만 담당(backend 컨벤션).
|
|
실제 검색/크롤링은 워커가 큐에서 잡을 꺼내 파이프라인으로 수행한다(비동기 분리).
|
|
"""
|
|
|
|
import uuid
|
|
|
|
from fastapi import Depends
|
|
|
|
from common.enums import ErrorType, JobStatus, JobType
|
|
from crud.job_crud import JobQueue
|
|
from crud.price_history import PriceHistory
|
|
from router.v1.lps.protocol import (
|
|
EnqueuedItem,
|
|
PricePoint,
|
|
Res_JobStatus,
|
|
Res_PriceHistory,
|
|
Res_QueueStats,
|
|
Res_Search,
|
|
SearchItem,
|
|
)
|
|
|
|
# 요청 유형 → 우선순위(낮을수록 우선). 알 수 없는 유형은 가장 낮은 우선순위(배치와 동급).
|
|
_PRIORITY = {"new_product": 1, "manual": 2, "partner": 3, "batch": 4}
|
|
|
|
|
|
class LpsService:
|
|
def __init__(self, queue: JobQueue = Depends(JobQueue), history: PriceHistory = Depends(PriceHistory)):
|
|
self.queue = queue
|
|
self.history = history
|
|
|
|
async def submit_search(self, items: list[SearchItem]) -> Res_Search:
|
|
res = Res_Search()
|
|
for it in items:
|
|
priority = _PRIORITY.get(it.job_type, 4)
|
|
job_id = await self.queue.enqueue(
|
|
JobType.SEARCH.value,
|
|
it.model_dump(),
|
|
priority=priority,
|
|
dedupe_key=f"search-{it.product_code}",
|
|
)
|
|
res.items.append(EnqueuedItem(product_code=it.product_code, job_id=job_id, duplicated=job_id is None))
|
|
res.accepted = sum(1 for i in res.items if i.job_id is not None)
|
|
return res
|
|
|
|
async def get_job(self, job_id_str: str) -> Res_JobStatus:
|
|
res = Res_JobStatus()
|
|
try:
|
|
uuid.UUID(job_id_str) # 잘못된 id 는 DB 조회 전에 컷
|
|
except (ValueError, TypeError):
|
|
res.result.SetResult(ErrorType.LPS_JOB_NOT_FOUND)
|
|
return res
|
|
|
|
row = await self.queue.get(job_id_str)
|
|
if row is None:
|
|
res.result.SetResult(ErrorType.LPS_JOB_NOT_FOUND)
|
|
return res
|
|
|
|
res.job_id = row["job_id"]
|
|
res.status = JobStatus(row["status"]).name
|
|
res.attempts = row["attempts"]
|
|
res.max_attempts = row["max_attempts"]
|
|
res.output = row.get("result")
|
|
res.last_error = row.get("last_error")
|
|
return res
|
|
|
|
async def stats(self) -> Res_QueueStats:
|
|
res = Res_QueueStats()
|
|
res.counts = await self.queue.counts()
|
|
return res
|
|
|
|
async def price_history(self, product_code: str, limit: int = 100) -> Res_PriceHistory:
|
|
res = Res_PriceHistory(product_code=product_code)
|
|
rows = await self.history.list_by_product(product_code, limit)
|
|
res.points = [
|
|
PricePoint(
|
|
triggered_at=r["triggered_at"].isoformat(timespec="seconds"),
|
|
outcome=r["outcome"],
|
|
matched_count=r["matched_count"],
|
|
naver=r["naver_lowest"], coupang=r["coupang_lowest"], final=r["final_lowest"],
|
|
final_source=r["final_source"],
|
|
naver_name=r["naver_name"], naver_url=r["naver_url"],
|
|
coupang_name=r["coupang_name"], coupang_url=r["coupang_url"],
|
|
by_mall=r.get("by_mall"),
|
|
)
|
|
for r in rows
|
|
]
|
|
return res
|