diff --git a/backend/app/api/chart.py b/backend/app/api/chart.py index eb66568..24f8dda 100644 --- a/backend/app/api/chart.py +++ b/backend/app/api/chart.py @@ -1,9 +1,13 @@ """차트 데이터 API: OHLCV + 보조 데이터 (감성, 거시). UI: /code 페이지 첫 로드 시 호출 → lightweight-charts 캔들 데이터로 사용. + +첫 방문 시 ohlcv_daily 가 비어 있으면 (symbols 만 시드됨, daily_batch 아직 안 돔) +즉시 pykrx 로 자동 갱신 — 사용자 입장에선 한 번의 차트 요청으로 데이터까지 충전. """ from __future__ import annotations +import logging from datetime import date, timedelta from fastapi import APIRouter, HTTPException, Query @@ -11,9 +15,25 @@ from sqlalchemy import text from app.db.connection import get_engine +logger = logging.getLogger(__name__) + router = APIRouter(prefix="/api/chart", tags=["chart"]) +def _query_ohlcv(conn, code: str, start: date, end: date): + return conn.execute( + text( + """ + SELECT date, open, high, low, close, volume + FROM ohlcv_daily + WHERE code = :c AND date BETWEEN :s AND :e + ORDER BY date + """ + ), + {"c": code, "s": start, "e": end}, + ).all() + + @router.get("/{code}") def get_chart( code: str, @@ -32,17 +52,19 @@ def get_chart( if not symbol: raise HTTPException(status_code=404, detail=f"unknown code: {code}") - ohlcv_rows = conn.execute( - text( - """ - SELECT date, open, high, low, close, volume - FROM ohlcv_daily - WHERE code = :c AND date BETWEEN :s AND :e - ORDER BY date - """ - ), - {"c": code, "s": start, "e": end}, - ).all() + ohlcv_rows = _query_ohlcv(conn, code, start, end) + if not ohlcv_rows: + # 첫 방문 — ohlcv_daily 가 비어있다. pykrx 로 즉시 채우고 재조회. + # refresh_code 는 별도 트랜잭션으로 ohlcv/trading_value/news 모두 commit 하므로 + # 이 conn 에서 다시 SELECT 하면 새 행이 보인다. lookback 은 차트 요청 범위 + + # 예측 모델 학습용 마진 (Chronos/LightGBM 이 충분한 과거 시계열을 요구) 으로 365 하한. + try: + from app.pipelines.refresh_one import refresh_code + logger.info("chart: ohlcv_daily empty for %s — auto-refresh", code) + refresh_code(symbol[0], symbol[1], lookback_days=max(days, 365)) + ohlcv_rows = _query_ohlcv(conn, code, start, end) + except Exception: # noqa: BLE001 + logger.exception("chart: auto-refresh failed for %s", code) ohlcv = [ { "date": str(r[0]),