[feat] solution/backend,postgres-init: 사람 방문 비콘 수집과 방문 통계 API — 쿠키 없이 월별 해시로 방문자 구분

발행 사이트를 연 사람의 페이지뷰를 쿠키·식별자 없이 모은다. 방문자 구분은
sha256(JWT_ACCESS_SECRET|IP|UA|KST 월) 해시로만 하고 IP 원문은 저장하지 않는다(Umami 와 같은 방식).

- POST /v1/track/view(공개): 응답을 먼저 주고 BackgroundTasks 로 저장 — sendBeacon 은 응답을 읽지 않는다.
  본문은 2KB 를 넘으면 다 읽기 전에 끊는다(nginx 상한 20MB 를 매번 메모리에 올리지 않게)
- 봇(crawler_detect)과 HeadlessChrome(우리 목업 검증 스크립트)은 세지 않는다
- IP 는 X-Forwarded-For 뒤에서 두 번째(Apache → nginx) — 첫 값은 손님이 위조해 한도·중복 제거를 피할 수 있다
- 같은 해시+경로 10초 1회, IP 당 분당 120건. common.utils.rate_limit 은 키를 안 지워 공개 엔드포인트에선
  메모리가 끝없이 늘어 상한 있는 표를 따로 둔다
- 경로는 발행 경로 + 탭(#/stay) — 하위 페이지가 해시 탭이라 빼면 "많이 본 페이지" 가 한 줄로 뭉친다. 끝 슬래시는 걷는다
- GET /v1/ops/stats/visits · /v1/ops/sites/{place_id}/stats/visits(RequireDeveloper): 실시간(최근 5분 고유
  방문자), 일별(KST)·사이트 순위·인기 페이지·유입·utm·기기. 기간 해석은 크롤러 통계와 같은 함수를 쓴다
- TrackService._site_id → site_id_for(공개) — 방문도 같은 슬러그 표를 쓴다
- 0025 마이그레이션과 init.sql 함께 수정

방문 테스트 25건 추가(방문·크롤러 관련 56 passed). 전체 1106 passed · 50 failed — 실패 목록이
1단계 기준과 같다(LLM 키 미설정·cp949·스케줄러·SNS·날씨 문구의 기존 실패).
This commit is contained in:
skkim committed 2026-10-12 15:31:14 +09:00
1 parent cae3de5586
commit 465ac4bb8b
15 files changed
+866 -3

No files matched your search

+3 -2
View File
@@ -67,7 +67,7 @@ class TrackService:
match = classify(user_agent)
if match is None:
return
site_id = await self._site_id(slug_of(uri)) if kind == "page" else None
site_id = await self.site_id_for(slug_of(uri)) if kind == "page" else None
hit = site_crawler_hits(
site_id=site_id,
path=unquote(uri),
@@ -84,7 +84,8 @@ class TrackService:
if err != ErrorType.SUCCESS:
LOG.w(f"[track] 크롤러 기록 저장 실패: {err.name} {match.bot or 'other'} {kind}")
async def _site_id(self, slug: str | None):
async def site_id_for(self, slug: str | None):
"""발행 슬러그 → site_id. 방문 비콘도 같은 표를 쓴다."""
if not slug:
return None
cls = TrackService