자산관리 효율 증가 api 추가
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import os
|
||||
import json
|
||||
from datetime import date as date_type
|
||||
from typing import Optional
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Query
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import requests
|
||||
@@ -13,6 +14,7 @@ from .db import (
|
||||
add_portfolio_item, get_all_portfolio, get_portfolio_item,
|
||||
update_portfolio_item, delete_portfolio_item,
|
||||
upsert_broker_cash, get_all_broker_cash, get_broker_cash, delete_broker_cash,
|
||||
upsert_asset_snapshot, get_asset_snapshots,
|
||||
)
|
||||
from .scraper import fetch_market_news, fetch_major_indices, fetch_overseas_news
|
||||
from .price_fetcher import get_current_prices
|
||||
@@ -33,12 +35,52 @@ scheduler = BackgroundScheduler(timezone=os.getenv("TZ", "Asia/Seoul"))
|
||||
# Windows AI Server URL (NAS .env에서 설정)
|
||||
WINDOWS_AI_SERVER_URL = os.getenv("WINDOWS_AI_SERVER_URL", "http://192.168.0.5:8000")
|
||||
|
||||
# 공휴일 목록 로드
|
||||
_HOLIDAYS_PATH = os.path.join(os.path.dirname(__file__), "holidays.json")
|
||||
try:
|
||||
with open(_HOLIDAYS_PATH, "r") as f:
|
||||
_HOLIDAYS: set = set(json.load(f))
|
||||
except Exception:
|
||||
_HOLIDAYS = set()
|
||||
|
||||
def is_market_open(d: date_type) -> bool:
|
||||
return d.weekday() < 5 and d.strftime("%Y-%m-%d") not in _HOLIDAYS
|
||||
|
||||
|
||||
def save_daily_snapshot():
|
||||
today = date_type.today()
|
||||
if not is_market_open(today):
|
||||
print(f"[Snapshot] {today} 휴장일 — 스킵")
|
||||
return
|
||||
|
||||
today_str = today.strftime("%Y-%m-%d")
|
||||
items = get_all_portfolio()
|
||||
cash_rows = get_all_broker_cash()
|
||||
total_cash = sum(r["cash"] for r in cash_rows)
|
||||
|
||||
if items:
|
||||
tickers = list({item["ticker"] for item in items})
|
||||
prices = get_current_prices(tickers)
|
||||
total_eval = sum(
|
||||
prices.get(item["ticker"], item["avg_price"]) * item["quantity"]
|
||||
for item in items
|
||||
)
|
||||
else:
|
||||
total_eval = 0
|
||||
|
||||
total_assets = total_eval + total_cash
|
||||
upsert_asset_snapshot(today_str, total_eval, total_cash, total_assets)
|
||||
print(f"[Snapshot] {today_str} 저장 완료: eval={total_eval}, cash={total_cash}, total={total_assets}")
|
||||
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
init_db()
|
||||
|
||||
# 매일 아침 8시 뉴스 스크랩 (NAS 자체 수행)
|
||||
scheduler.add_job(run_scraping_job, "cron", hour="8", minute="0")
|
||||
|
||||
# 평일 15:40 총 자산 스냅샷 저장
|
||||
scheduler.add_job(save_daily_snapshot, "cron", day_of_week="mon-fri", hour=15, minute=40)
|
||||
|
||||
# 앱 시작 시에도 한 번 실행 (데이터 없으면)
|
||||
if not get_latest_articles(1):
|
||||
@@ -276,3 +318,46 @@ def delete_portfolio(item_id: int):
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
# --- Asset Snapshot API ---
|
||||
|
||||
@app.post("/api/portfolio/snapshot")
|
||||
def create_snapshot():
|
||||
"""총 자산 스냅샷 수동 저장 (오늘 날짜 기준)"""
|
||||
today = date_type.today()
|
||||
today_str = today.strftime("%Y-%m-%d")
|
||||
|
||||
items = get_all_portfolio()
|
||||
cash_rows = get_all_broker_cash()
|
||||
total_cash = sum(r["cash"] for r in cash_rows)
|
||||
|
||||
if items:
|
||||
tickers = list({item["ticker"] for item in items})
|
||||
prices = get_current_prices(tickers)
|
||||
total_eval = sum(
|
||||
prices.get(item["ticker"], item["avg_price"]) * item["quantity"]
|
||||
for item in items
|
||||
)
|
||||
else:
|
||||
total_eval = 0
|
||||
|
||||
total_assets = total_eval + total_cash
|
||||
upsert_asset_snapshot(today_str, total_eval, total_cash, total_assets)
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"snapshot": {
|
||||
"date": today_str,
|
||||
"total_eval": total_eval,
|
||||
"total_cash": total_cash,
|
||||
"total_assets": total_assets,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/portfolio/snapshot/history")
|
||||
def get_snapshot_history(days: int = Query(30, ge=0)):
|
||||
"""총 자산 스냅샷 이력 조회 (days=0: 전체, days=N: 최근 N일)"""
|
||||
snapshots = get_asset_snapshots(days)
|
||||
return {"snapshots": snapshots}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user