[feat] solution/backend,postgres-init: AI 크롤러 요청 기록과 통계 API — 사이트관리 통계의 수집·조회 쪽

크롤러는 JS 를 실행하지 않아 브라우저 트래커로는 안 잡힌다. nginx mirror 가 /s/·robots.txt·
sitemap.xml 요청의 사본을 /internal/track/hit 로 넘기고, 백엔드가 UA 로 봇만 골라
site_crawler_hits 에 1건 = 1행으로 쌓는다(Umami 와 같은 원본 이벤트 방식, 기한 없이 보관).

- crawler_detect: AEORanking 과 같은 19개 봇을 training/search/user-fetch/search-engine 으로 분류,
  목록 밖이지만 스스로 봇이라 밝힌 UA 는 other. HeadlessChrome 은 우리 목업 검증 스크립트라 뺀다
- track_service: 슬러그→site_id 는 발행 때와 같은 publish_slug() 로 만든다 — domain 이 빈
  사이트는 <이름>-<place_id 앞 8자> 로 발행되므로 domain 칼럼만 보면 못 찾는다. 퍼센트 인코딩·NFC 정규화
- /internal/track/hit: /v1 밖이라 nginx 가 밖으로 노출하지 않는다. 저장은 응답을 보낸 뒤 백그라운드 —
  nginx 는 mirror 가 끝나기 전까지 같은 연결의 다음 요청을 읽지 않아서, DB 를 기다렸다 204 를 주면
  DB 가 느릴 때 그 연결의 다른 요청까지 늦어진다
- GET /v1/ops/stats/crawlers · /v1/ops/sites/{place_id}/stats/crawlers (RequireDeveloper):
  일별(KST)×용도 추이, 봇별, 사이트 순위, robots·sitemap, 기타 UA, 많이 읽힌 페이지, 최근 50건
- 0024 마이그레이션과 init.sql 을 함께 수정(둘 중 하나만 고치면 새 DB 와 서버 DB 가 갈라진다)

크롤러 테스트 31건 추가(전체 43건 통과). 전체 스위트 1081 passed · 50 failed 는 LLM 키 미설정·
cp949·스케줄러·SNS·날씨 문구 등 이번 변경과 무관한 기존 실패로 수정 전후 목록이 같다.
This commit is contained in:
skkim committed 2026-10-08 11:31:49 +09:00
1 parent c0f361b831
commit 603189d3a1
16 files changed
+945 -3

No files matched your search

@@ -447,6 +447,29 @@ class alert_outbox(MainTableMixin, MAIN_BASE):
resolved_at = Column(DateTime(timezone=True), nullable=True)
class site_crawler_hits(MainTableMixin, MAIN_BASE):
"""봇 요청 1건 = 1행 — nginx mirror 가 /internal/track/hit 로 넘긴 것. 기한 없이 보관한다(Umami 와 같다)."""
__tablename__ = "site_crawler_hits"
__table_args__ = (
Index("ix_site_crawler_hits_site", "site_id", "created_at"),
Index("ix_site_crawler_hits_created", "created_at"),
Index("ix_site_crawler_hits_bot", "bot", "created_at"),
)
hit_id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
# robots·sitemap 은 오리진 공용이라, 없는 슬러그는 사이트를 못 찾아서 NULL 이다.
site_id = Column(UUID(as_uuid=True), nullable=True)
path = Column(Text, nullable=False)
kind = Column(String(10), nullable=False) # page · robots · sitemap
bot = Column(String(40), nullable=True) # 목록 밖(other)이면 NULL
vendor = Column(String(40), nullable=True)
purpose = Column(String(20), nullable=False) # training · search · user-fetch · search-engine · other
user_agent = Column(Text, nullable=False)
# 목록 봇만 남긴다 — other 에는 봇 UA 를 흉내 낸 사람이 섞일 수 있다.
ip = Column(String(45), nullable=True)
class site_sections(MainTableMixin, MAIN_BASE):
"""섹션 하나의 콘텐츠."""