DOCS/troubleshooting/20251002_emotion_db_storage_fix.md
happybell80 95431bf2f8 docs: 감정 DB 저장 문제 원인 분석 및 파일 크기 제한 300줄로 강화
- 20251002_emotion_db_storage_fix.md: llm_service.py 주석 처리 문제 확인
- 311_FastAPI_구조_원칙.md: 파일 크기 제한 500줄 → 300줄 변경

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-02 15:31:19 +09:00

2.4 KiB

감정 DB 저장 문제 해결 (완료)

작성일: 2025-10-02 14:13~14:30 문서 커밋:

  • 1e0596b (13:54:17): EmotionState 에러 해결 전략 추가
  • 251cc6d (14:30시경): 7감정 모델로 문서 업데이트 코드 커밋: ba1edb1 (14:13:38): emotion_llm.py 7감정 구조 변경 해결자: Claude & happybell80 문제: Slack 대화 시 감정 분석은 되지만 emotion_readings 테이블에 저장 안 됨 원인: rb8001/app/llm/emotion_llm.py:159-181에서 save_emotion_reading() 호출 누락

해결 내역

1. DB 연결 설정

  • 테이블: emotion_readings
  • DB: robeing_metrics (51123 서버)
  • .env 추가: METRICS_DATABASE_URL 환경변수

2. EmotionState 구조 변경 (완료)

문제 위치:

  • emotion_llm.py (line 35, 67, 80-83, 89) - EmotionState 생성 오류 4곳
  • emotion/base.py (line 16-23) - 6개 인자 필요했음

해결:

  • create_default_emotion_state() 함수 추가
  • 6필드 구조 → 4필드로 단순화 (emotion_dist, entropy, dominant, confidence)
  • EMOTIONS 7개: fear, surprise, anger, sadness, neutral, happiness, disgust

3. DB 저장 코드 추가 (완료)

  • 파일: emotion_llm.py:159-181
  • 함수: generate_response_with_emotion() 내부
  • 추가: save_emotion_reading(emotion_data) 호출

현황

  • emotion_readings 테이블: TimescaleDB 하이퍼테이블, 5개 인덱스
  • 기존 데이터: 3건 (2025-10-02 12:21~12:27 테스트)
  • 에러 로그: 339회 "Failed to save emotion reading"

검증 필요

  • 배포 후 Slack 대화 시 실제 저장 확인
  • user_id UUID 변환 정상 작동 확인

추가 발견 (2025-10-02 15:00)

문제: emotion_readings 테이블 여전히 3건, DB 저장 로그 없음

코드 흐름 확인

  1. Slack 메시지 처리: router.py:116 route_message()
  2. LLM 호출: router.py:384 _call_internal_llm() → llm_service.py:81 process_request()
  3. 감정 분석: llm_service.py:119 analyze_user_emotion() 호출 (정상)
  4. DB 저장 누락: llm_service.py:134-138 generate_response_with_emotion() 주석 처리됨

원인

  • DB 저장 코드: emotion_llm.py:159-181 (generate_response_with_emotion 내부)
  • llm_service.py:134-138 주석 처리로 해당 함수 도달 불가능
  • analyze_user_emotion()은 감정 분석만 수행, DB 저장 없음
  • 계층 확인: emotion_llm.py:161에서 state.database.save_emotion_reading() 호출 (정상 패턴)

해결 방법

llm_service.py:134-138 주석 해제