Initial commit
This commit is contained in:
69
backend/app/api/endpoints/monitoring.py
Normal file
69
backend/app/api/endpoints/monitoring.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
API 모니터링 엔드포인트
|
||||
|
||||
KIS API 호출 통계 및 상태 확인
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter
|
||||
from typing import Dict, Any
|
||||
import logging
|
||||
|
||||
from app.services.korea_investment_api import korea_investment_api
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.get("/api-stats")
|
||||
async def get_api_stats() -> Dict[str, Any]:
|
||||
"""
|
||||
KIS API 호출 통계 조회
|
||||
|
||||
Returns:
|
||||
- total_calls: 총 API 호출 수
|
||||
- total_errors: 총 오류 수
|
||||
- total_retries: 총 재시도 수
|
||||
- error_rate: 오류율 (0.0 ~ 1.0)
|
||||
- avg_duration: 평균 응답 시간 (초)
|
||||
- recent_calls: 최근 모니터링 윈도우 내 호출 수
|
||||
"""
|
||||
stats = korea_investment_api.get_monitor_stats()
|
||||
|
||||
# 상태 평가
|
||||
status = "healthy"
|
||||
if stats["error_rate"] > 0.5:
|
||||
status = "critical"
|
||||
elif stats["error_rate"] > 0.3:
|
||||
status = "warning"
|
||||
|
||||
return {
|
||||
"status": status,
|
||||
"stats": stats,
|
||||
"message": f"오류율: {stats['error_rate']:.1%}, 평균 응답: {stats['avg_duration']:.2f}초"
|
||||
}
|
||||
|
||||
|
||||
@router.post("/log-stats")
|
||||
async def log_api_stats() -> Dict[str, str]:
|
||||
"""
|
||||
현재 API 통계를 로그에 기록
|
||||
"""
|
||||
korea_investment_api.log_monitor_stats()
|
||||
return {"message": "통계가 로그에 기록되었습니다"}
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
async def health_check() -> Dict[str, Any]:
|
||||
"""
|
||||
API 헬스체크
|
||||
"""
|
||||
stats = korea_investment_api.get_monitor_stats()
|
||||
|
||||
is_healthy = stats["error_rate"] < 0.5
|
||||
|
||||
return {
|
||||
"status": "healthy" if is_healthy else "unhealthy",
|
||||
"error_rate": stats["error_rate"],
|
||||
"total_calls": stats["total_calls"],
|
||||
"timestamp": __import__("datetime").datetime.now().isoformat()
|
||||
}
|
||||
Reference in New Issue
Block a user