105 lines
5.4 KiB
Python
105 lines
5.4 KiB
Python
"""사이트 트래픽 통계 원본 테이블.
|
|
|
|
원본 1건 = 1행이고 집계 테이블은 따로 두지 않는다. 화면을 열 때 기간으로 잘라 GROUP BY 한다.
|
|
created_at 은 앱이 KST naive 로 직접 넣는다(통계를 KST 날짜로 자르므로 DB 서버 시간대에 기대지 않는다).
|
|
DDL: docs/database-schema/migration_2026-10-19_site_traffic.sql (수동 적용)
|
|
"""
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import BigInteger, Boolean, DateTime, Index, Integer, SmallInteger, String, func
|
|
from sqlalchemy.dialects.mysql import CHAR
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.database.session import Base
|
|
|
|
_TABLE_OPTS = {
|
|
"mysql_engine": "InnoDB",
|
|
"mysql_charset": "utf8mb4",
|
|
"mysql_collate": "utf8mb4_unicode_ci",
|
|
}
|
|
|
|
|
|
class SitePageView(Base):
|
|
"""사람 방문 — 비콘 1건 = 1행. IP 원문은 저장하지 않는다."""
|
|
|
|
__tablename__ = "site_page_views"
|
|
__table_args__ = (
|
|
Index("idx_site_page_views_created_at", "created_at"),
|
|
Index("idx_site_page_views_content", "content_type", "content_id", "created_at"),
|
|
Index("idx_site_page_views_visitor", "visitor_hash", "created_at"),
|
|
_TABLE_OPTS,
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime, nullable=False, server_default=func.now(), comment="기록 시각(KST)"
|
|
)
|
|
path: Mapped[str] = mapped_column(String(512), nullable=False, comment="정규화한 경로")
|
|
page_kind: Mapped[str] = mapped_column(
|
|
String(16), nullable=False, comment="landing·video·ssul·p2v_f1·p2v_playreel·static·other"
|
|
)
|
|
content_type: Mapped[Optional[str]] = mapped_column(String(16), nullable=True, comment="상세 페이지 콘텐츠 종류")
|
|
content_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, comment="상세 페이지 콘텐츠 id")
|
|
visitor_hash: Mapped[str] = mapped_column(
|
|
CHAR(64), nullable=False, comment="sha256(secret|IP|UA|KST 연-월)"
|
|
)
|
|
device: Mapped[str] = mapped_column(String(8), nullable=False, comment="desktop·mobile·tablet")
|
|
referrer_host: Mapped[Optional[str]] = mapped_column(
|
|
String(255), nullable=True, comment="유입 도메인(앞 www. 제거). NULL = 직접·알 수 없음"
|
|
)
|
|
utm_source: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, comment="?utm_source=")
|
|
ai_engine: Mapped[Optional[str]] = mapped_column(String(16), nullable=True, comment="AI 엔진 유입")
|
|
ai_signal: Mapped[Optional[str]] = mapped_column(String(8), nullable=True, comment="referrer·utm")
|
|
|
|
|
|
class SiteRequest(Base):
|
|
"""nginx 접근 로그 1줄 = 1행(정적 자원·API 요청은 거른 뒤)."""
|
|
|
|
__tablename__ = "site_requests"
|
|
__table_args__ = (
|
|
Index("idx_site_requests_created_at", "created_at"),
|
|
Index("idx_site_requests_bot", "is_bot", "created_at"),
|
|
Index("idx_site_requests_status", "status", "created_at"),
|
|
Index("idx_site_requests_content", "content_type", "content_id", "created_at"),
|
|
_TABLE_OPTS,
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime, nullable=False, server_default=func.now(), comment="요청 시각(KST, 로그 기준)"
|
|
)
|
|
method: Mapped[str] = mapped_column(String(8), nullable=False)
|
|
path: Mapped[str] = mapped_column(String(512), nullable=False, comment="쿼리 제외 경로")
|
|
status: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
|
bytes_sent: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
request_time_ms: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
kind: Mapped[str] = mapped_column(
|
|
String(16), nullable=False, comment="page·share·static_html·robots·sitemap·llms·other"
|
|
)
|
|
content_type: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
|
|
content_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
|
ip_hash: Mapped[str] = mapped_column(CHAR(64), nullable=False, comment="방문자 해시와 같은 식")
|
|
ip: Mapped[Optional[str]] = mapped_column(String(45), nullable=True, comment="목록에 있는 봇만 저장")
|
|
is_bot: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
bot_name: Mapped[Optional[str]] = mapped_column(String(40), nullable=True)
|
|
bot_company: Mapped[Optional[str]] = mapped_column(String(40), nullable=True)
|
|
bot_purpose: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
|
|
user_agent: Mapped[str] = mapped_column(String(512), nullable=False, default="")
|
|
referrer_host: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
|
|
|
|
class SiteLogCursor(Base):
|
|
"""접근 로그를 어디까지 읽었나 — id=1 한 행. 수집 시 SELECT … FOR UPDATE 로 잡는다."""
|
|
|
|
__tablename__ = "site_log_cursor"
|
|
__table_args__ = (_TABLE_OPTS,)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=False)
|
|
inode: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0, comment="읽던 파일의 inode")
|
|
file_offset: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0, comment="다음에 읽을 바이트 위치")
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, nullable=False, server_default=func.now(), onupdate=func.now()
|
|
)
|