Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
3 changes: 2 additions & 1 deletion apps/pre-processing-service/app/api/endpoints/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ async def search(request: RequestNaverSearch):
schedule_id=schedule_id,
category=category,
keyword=keywords,
total_keyword = {1: "바밥밥", 2: "밥밥밥", 3: "바밤바"}
total_keyword = {1: "바밥밥", 2: "밥밥밥", 3: "바밤바"},
status="SUCCESS"
)

@router.post("/search/test",response_model=ResponsetSadaguValidate)
Expand Down
Empty file.
Empty file.
63 changes: 63 additions & 0 deletions apps/pre-processing-service/app/errors/CrawlingException.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from CustomException import CustomException
from typing import List

class PageLoadTimeoutException(CustomException):
"""
페이지 로드 타임아웃 예외
@:param url: 로드하려는 페이지의 URL
"""
def __init__(self, url : str):
super().__init__(
status_code=408,
detail=f"페이지 로드가 시간 초과되었습니다. URL: {url}",
code="PAGE_LOAD_TIMEOUT"
)

class WebDriverConnectionException(CustomException):
"""
웹 드라이버 연결 실패 예외
"""
def __init__(self):
super().__init__(
status_code=500,
detail="웹 드라이버 연결에 실패했습니다.",
code="WEBDRIVER_ERROR"
)


class ElementNotFoundException(CustomException):
"""
특정 HTML 요소를 찾을 수 없는 예외
@:param selector: 찾으려는 요소의 CSS 선택자
"""
def __init__(self, selector: str):
super().__init__(
status_code=404,
detail=f"요소를 찾을 수 없습니다. 선택자: {selector}",
code="ELEMENT_NOT_FOUND"
)

class HtmlParsingException(CustomException):
"""
HTML 파싱 실패 예외
@:param reason: 파싱 실패 이유
"""
def __init__(self, reason: str):
super().__init__(
status_code=422,
detail=f"HTML 파싱에 실패했습니다. 이유: {reason}",
code="HTML_PARSING_ERROR"
)

class DataExtractionException(CustomException):
"""
데이터 추출 실패 예외
@:param field: 추출하려는 데이터 필드 목록
"""
def __init__(self, field: List[str]):
super().__init__(
status_code=422,
detail=f"데이터 추출에 실패했습니다. 필드: {', '.join(field)}",
code="DATA_EXTRACTION_ERROR"
)

18 changes: 18 additions & 0 deletions apps/pre-processing-service/app/errors/CustomException.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def __init__(self, status_code: int, detail: str, code: str):

# 구체적인 커스텀 예외 정의
class ItemNotFoundException(CustomException):
"""
아이템을 찾을수 없는 예외
@:param item_id: 찾을수 없는 아이템의 ID
"""
def __init__(self, item_id: int):
super().__init__(
status_code=404,
Expand All @@ -18,9 +22,23 @@ def __init__(self, item_id: int):
)

class InvalidItemDataException(CustomException):
"""
데이터 유효성 검사 실패 예외
"""
def __init__(self):
super().__init__(
status_code=422,
detail="데이터가 유효하지않습니다..",
code="INVALID_ITEM_DATA"
)

class DatabaseConnectionException(CustomException):
"""
데이터베이스 연결 실패 예외
"""
def __init__(self):
super().__init__(
status_code=500,
detail="데이터베이스 연결에 실패했습니다.",
code="DATABASE_CONNECTION_ERROR"
)
2 changes: 2 additions & 0 deletions apps/pre-processing-service/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi import FastAPI
from starlette.exceptions import HTTPException as StarletteHTTPException
from fastapi.exceptions import RequestValidationError
from app.middleware.ServiceLoggerMiddleware import ServiceLoggerMiddleware

# --- 애플리케이션 구성 요소 임포트 ---
from app.api.router import api_router
Expand All @@ -25,6 +26,7 @@
app.add_exception_handler(Exception, unhandled_exception_handler)

# --- 미들웨어 등록 ---
app.add_middleware(ServiceLoggerMiddleware)
app.add_middleware(LoggingMiddleware)

# --- 라우터 등록 ---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# import time
# from typing import Dict, Any, List, Optional
# from fastapi import Request
# from loguru import logger
# from contextvars import ContextVar
#
# trace_id_context: ContextVar[str] = ContextVar('trace_id', default="NO_TRACE_ID")
#
#
# class ServiceLoggingDependency:
# """
# 서비스 로깅을 위한 의존성 클래스
# :param service_type: 서비스 유형 (예: "CHUNKING", "PARSING", "EMBEDDING")
# :param track_params: 추적할 매개변수 이름 목록
# :param response_trackers: 응답에서 추적할 필드 이름 목록 (딕셔너리)
# """
#
# def __init__(self, service_type: str,
# track_params: List[str] = None,
# response_trackers: List[str] = None):
# self.service_type = service_type
# self.track_params = track_params or []
# self.response_trackers = response_trackers or []
#
# async def __call__(self, request: Request):
# """
# 의존성 주입 시 호출되는 메서드
# :param request: FastAPI Request 객체
# :return: 서비스 유형과 추출된 매개변수 딕셔너리
# """
# trace_id = trace_id_context.get("NO_TRACE_ID")
# start_time = time.time()
#
# # 파라미터 추출
# params = await self._extract_params(request)
# param_str = ""
# if params:
# param_strs = [f"{k}={v}" for k, v in params.items()]
# param_str = " " + " ".join(param_strs)
#
# logger.info(f"[{self.service_type}_START] trace_id={trace_id}{param_str}")
#
# # 응답 시 사용할 정보를 request.state에 저장
# request.state.service_type = self.service_type
# request.state.start_time = start_time
# request.state.param_str = param_str
# request.state.response_trackers = self.response_trackers
#
# return {"service_type": self.service_type, "params": params}
#
# async def _extract_params(self, request: Request) -> Dict[str, Any]:
# """
# 요청에서 추적 파라미터 추출
# :param request: FastAPI Request 객체
# :return: 추출된 매개변수 딕셔너리
# """
# params = {}
#
# try:
# # Query Parameters 추출
# for key, value in request.query_params.items():
# if key in self.track_params:
# params[key] = value
#
# # JSON Body 추출
# try:
# json_body = await request.json()
# if json_body:
# for key, value in json_body.items():
# if key in self.track_params:
# if isinstance(value, str) and len(value) > 50:
# params[f"{key}_length"] = len(value)
# elif isinstance(value, list):
# params[f"{key}_count"] = len(value)
# else:
# params[key] = value
# except:
# pass
# except:
# pass
#
# return params
#
# # 서비스 응답 시 성공 로그 함수
# async def log_service_response_with_data(request: Request, response_data: Optional[Dict] = None):
# """
# 서비스 응답 시 성공 로그 기록
# :param request: FastAPI Request 객체
# :param response_data: 응답 데이터
# """
# if hasattr(request.state, 'service_type'):
# trace_id = trace_id_context.get("NO_TRACE_ID")
# duration = time.time() - request.state.start_time
#
# # 기본 로그 문자열
# log_parts = [f"[{request.state.service_type}_SUCCESS]",
# f"trace_id={trace_id}",
# f"execution_time={duration:.4f}s{request.state.param_str}"]
#
# # 응답 데이터에서 추적할 필드 추출
# if response_data and hasattr(request.state, 'response_trackers'):
# response_params = []
# for tracker in request.state.response_trackers:
# if tracker in response_data:
# value = response_data[tracker]
# if isinstance(value, dict):
# response_params.append(f"{tracker}_keys={list(value.keys())}")
# response_params.append(f"{tracker}_count={len(value)}")
# elif isinstance(value, list):
# response_params.append(f"{tracker}_count={len(value)}")
# else:
# response_params.append(f"{tracker}={value}")
#
# if response_params:
# log_parts.append(" ".join(response_params))
#
# logger.info(" ".join(log_parts))
# return None
#
# naver_search_dependency = ServiceLoggingDependency(
# "NAVER_CRAWLING",
# track_params=["job_id", "schedule_id", "tag", "category", "startDate", "endDate"],
# response_trackers=["keyword", "total_keyword"]
# )

This file was deleted.

Loading
Loading