-
Notifications
You must be signed in to change notification settings - Fork 0
Feat(#22): AI 분석 결과 이벤트 및 Worker 장애 처리 구현 #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
79ec412
feat: define analysis result event contracts
pearseona e45a354
feat: classify complete partial and failed analyses
pearseona 82ee95c
feat: map analysis results to integration events
pearseona d3441ad
feat: publish analysis result events to RabbitMQ
pearseona f2ced78
feat: acknowlodge requests after result publication
pearseona 663c5d5
feat: route unrecoverable analysis messages to a sanitized DLQ
pearseona c58724f
feat: add bounded retries for external security providers
pearseona d671d96
feat: gracefully drain in-flight analysis messages
pearseona 730728d
ci: build the AI worker
pearseona 89ffb64
fix: address CodeRabbit review feedback
pearseona a246755
build: update Dockerfile for production deployment
pearseona a474f3b
fix: address remaining AI worker review feedback
pearseona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .git | ||
| .github | ||
| .vscode | ||
|
|
||
| .venv | ||
| __pycache__ | ||
| .pytest_cache | ||
| *.py[cod] | ||
|
|
||
| .env | ||
| .env.* | ||
|
|
||
| tests | ||
| requirements-dev.txt | ||
|
|
||
| *.log | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| name: AI CI | ||
|
|
||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - develop | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| name: Python tests | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.11" | ||
| cache: pip | ||
| cache-dependency-path: | | ||
| requirements.txt | ||
| requirements-dev.txt | ||
|
|
||
| - name: Install dependencies | ||
| run: pip install -r requirements-dev.txt | ||
|
|
||
| - name: Check dependency compatibility | ||
| run: pip check | ||
|
|
||
| - name: Run tests | ||
| run: python -m pytest -q | ||
|
|
||
| docker-build: | ||
| name: Docker build | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - name: Build Docker image | ||
| run: docker build -t safefam-ai:test . | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,26 @@ | ||
| FROM python:3.11-slim | ||
|
|
||
| # 작업 디렉토리 설정 | ||
| WORKDIR /workspace | ||
|
|
||
| # 필수 패키지 설치를 위한 레이어 캐싱 | ||
| ENV PYTHONDONTWRITEBYTECODE=1 | ||
| ENV PYTHONUNBUFFERED=1 | ||
|
|
||
| COPY requirements.txt . | ||
|
|
||
| RUN pip install --no-cache-dir -r requirements.txt | ||
|
|
||
| # 소스코드 전체 복사 | ||
| COPY . . | ||
| COPY app app | ||
|
|
||
| RUN useradd --create-home --shell /usr/sbin/nologin safefam | ||
|
|
||
| USER safefam | ||
|
|
||
| # FastAPI 기본 포트 개방 | ||
| EXPOSE 8000 | ||
|
|
||
| # 로컬 개발 및 컨테이너 구동을 위한 기본 명령어 | ||
| CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] | ||
| HEALTHCHECK \ | ||
| --interval=30s \ | ||
| --timeout=5s \ | ||
| --retries=3 \ | ||
| CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/', timeout=3)" | ||
|
|
||
| CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
|
|
||
| from app.analysis.schemas import SmishingAnalysisResponse | ||
|
|
||
|
|
||
| class AnalysisExecutionStatus(str, Enum): | ||
| """분석 실행 상태.""" | ||
|
|
||
| COMPLETED = "COMPLETED" | ||
| PARTIAL = "PARTIAL" | ||
| FAILED = "FAILED" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AnalysisExecution: | ||
| """분석 결과와 실패 트랙 정보를 담는 불변 데이터.""" | ||
|
|
||
| status: AnalysisExecutionStatus | ||
| result: SmishingAnalysisResponse | ||
| failed_tracks: tuple[str, ...] | ||
|
|
||
|
|
||
| def classify_execution( | ||
| result: SmishingAnalysisResponse, | ||
| ) -> AnalysisExecution: | ||
| """AI 분석 응답을 실행 상태와 실패 트랙 목록으로 분류한다.""" | ||
| if result.status == "ERROR": | ||
| return AnalysisExecution( | ||
| status=AnalysisExecutionStatus.FAILED, | ||
| result=result, | ||
| failed_tracks=("PIPELINE",), | ||
| ) | ||
|
|
||
| failed_tracks: list[str] = [] | ||
|
|
||
| text_analysis = result.text_analysis or {} | ||
| text_result = text_analysis.get("result") or {} | ||
| stage1_result = text_analysis.get("stage1_naive_bayes") or {} | ||
|
|
||
| text_failed = ( | ||
| text_result.get("grade") == "UNKNOWN" | ||
| or text_result.get("error_message") | ||
| ) | ||
| stage1_failed = ( | ||
| stage1_result.get("grade") == "UNKNOWN" | ||
| or stage1_result.get("error_message") | ||
| ) | ||
|
|
||
| if text_failed: | ||
| if stage1_result and not stage1_failed: | ||
| failed_tracks.append("TEXT:GEMINI") | ||
| else: | ||
| failed_tracks.append("TEXT") | ||
| elif stage1_failed: | ||
| failed_tracks.append("TEXT:NAIVE_BAYES") | ||
|
|
||
| url_analysis = result.url_analysis or {} | ||
| failed_providers = url_analysis.get("failed_providers") or [] | ||
| url_available = url_analysis.get("available", True) | ||
|
|
||
| if result.url_analysis is not None and not url_available: | ||
| failed_tracks.append("URL") | ||
| else: | ||
| failed_tracks.extend( | ||
| f"URL:{provider}" | ||
| for provider in failed_providers | ||
| ) | ||
|
|
||
| rule_analysis = result.rule_analysis or {} | ||
| if rule_analysis.get("error_message"): | ||
| failed_tracks.append("RULES") | ||
|
|
||
| status = ( | ||
| AnalysisExecutionStatus.PARTIAL | ||
| if failed_tracks | ||
| else AnalysisExecutionStatus.COMPLETED | ||
| ) | ||
|
|
||
| return AnalysisExecution( | ||
| status=status, | ||
| result=result, | ||
| failed_tracks=tuple(failed_tracks), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.