feat: v3.1 대시보드 추가 (React + FastAPI)
Media Engine Control Panel — 6탭 웹 대시보드 [백엔드] FastAPI (dashboard/backend/) - server.py: 포트 8080, CORS, React SPA 서빙 - api_overview.py: KPI 카드 + 파이프라인 상태 + 활동 로그 - api_content.py: 칸반 보드 + 승인/거부 + 수동 트리거 - api_analytics.py: 방문자 추이 + 플랫폼/코너별 성과 - api_novels.py: 소설 목록/생성/에피소드 관리 - api_settings.py: engine.json CRUD - api_connections.py: AI 서비스 연결 관리 + 키 저장 - api_tools.py: 기능별 AI 도구 선택 - api_cost.py: 구독 현황 + API 사용량 추적 - api_logs.py: 시스템 로그 필터/검색 [프론트엔드] React + Vite + Tailwind + Recharts (dashboard/frontend/) - Overview: KPI 카드 + 파이프라인 + 코너별 바차트 + 활동 로그 - Content: 4열 칸반 보드 + 상세 모달 + 승인/거부 - Analytics: LineChart 방문자 추이 + 플랫폼별 성과 - Novel: 소설 목록 + 에피소드 테이블 + 새 소설 생성 폼 - Settings: 5개 서브탭 (AI연결/도구선택/배포채널/품질/비용관리) - Logs: 필터/검색 시스템 로그 뷰어 [디자인] CNN 다크+골드 테마 - 배경 #0a0a0d + 액센트 #c8a84e - 모바일 반응형 (Tailscale 외부 접속 대응) [실행] - dashboard/start.bat 더블클릭 → http://localhost:8080 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
133
dashboard/backend/api_cost.py
Normal file
133
dashboard/backend/api_cost.py
Normal file
@@ -0,0 +1,133 @@
|
||||
"""
|
||||
dashboard/backend/api_cost.py
|
||||
Settings > 비용관리 탭 API — 구독 정보, API 사용량
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
from datetime import date, datetime
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
BASE_DIR = Path(__file__).parent.parent.parent
|
||||
CONFIG_PATH = BASE_DIR / "config" / "engine.json"
|
||||
LOGS_DIR = BASE_DIR / "logs"
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
SUBSCRIPTION_PLANS = [
|
||||
{
|
||||
"id": "claude_pro",
|
||||
"name": "Claude Pro",
|
||||
"provider": "Anthropic",
|
||||
"monthly_cost_usd": 20.0,
|
||||
"env_key": "ANTHROPIC_API_KEY",
|
||||
"renewal_day": 1, # 매월 1일 갱신
|
||||
},
|
||||
{
|
||||
"id": "openai_plus",
|
||||
"name": "OpenAI API",
|
||||
"provider": "OpenAI",
|
||||
"monthly_cost_usd": 0.0, # 종량제
|
||||
"env_key": "OPENAI_API_KEY",
|
||||
"renewal_day": None,
|
||||
},
|
||||
{
|
||||
"id": "gemini_api",
|
||||
"name": "Google Gemini API",
|
||||
"provider": "Google",
|
||||
"monthly_cost_usd": 0.0, # 무료 티어 + 종량제
|
||||
"env_key": "GEMINI_API_KEY",
|
||||
"renewal_day": None,
|
||||
},
|
||||
{
|
||||
"id": "elevenlabs",
|
||||
"name": "ElevenLabs Starter",
|
||||
"provider": "ElevenLabs",
|
||||
"monthly_cost_usd": 5.0,
|
||||
"env_key": "ELEVENLABS_API_KEY",
|
||||
"renewal_day": 1,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def _days_until_renewal(renewal_day):
|
||||
if renewal_day is None:
|
||||
return None
|
||||
today = date.today()
|
||||
next_renewal = date(today.year, today.month, renewal_day)
|
||||
if next_renewal <= today:
|
||||
# 다음 달
|
||||
if today.month == 12:
|
||||
next_renewal = date(today.year + 1, 1, renewal_day)
|
||||
else:
|
||||
next_renewal = date(today.year, today.month + 1, renewal_day)
|
||||
return (next_renewal - today).days
|
||||
|
||||
|
||||
def _parse_api_usage() -> list:
|
||||
"""logs/*.log에서 API 사용량 파싱"""
|
||||
usage_map: dict = {}
|
||||
patterns = {
|
||||
"claude": re.compile(r"claude.*?(\d+)\s*토큰|tokens[:\s]+(\d+)", re.IGNORECASE),
|
||||
"openai": re.compile(r"openai.*?(\d+)\s*토큰|gpt.*?tokens[:\s]+(\d+)", re.IGNORECASE),
|
||||
"gemini": re.compile(r"gemini.*?(\d+)\s*토큰", re.IGNORECASE),
|
||||
}
|
||||
|
||||
if not LOGS_DIR.exists():
|
||||
return []
|
||||
|
||||
for log_file in LOGS_DIR.glob("*.log"):
|
||||
try:
|
||||
content = log_file.read_text(encoding="utf-8", errors="ignore")
|
||||
for provider, pattern in patterns.items():
|
||||
matches = pattern.findall(content)
|
||||
tokens = sum(int(m[0] or m[1] or 0) for m in matches if any(m))
|
||||
if tokens:
|
||||
usage_map[provider] = usage_map.get(provider, 0) + tokens
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
result = []
|
||||
for provider, tokens in usage_map.items():
|
||||
result.append({
|
||||
"provider": provider,
|
||||
"tokens": tokens,
|
||||
"estimated_cost_usd": round(tokens / 1_000_000 * 3.0, 4), # 근사치
|
||||
})
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/cost/subscriptions")
|
||||
async def get_subscriptions():
|
||||
"""구독 정보 + 만료일 계산"""
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
subscriptions = []
|
||||
for plan in SUBSCRIPTION_PLANS:
|
||||
key_set = bool(os.getenv(plan["env_key"], ""))
|
||||
days_left = _days_until_renewal(plan.get("renewal_day"))
|
||||
subscriptions.append({
|
||||
"id": plan["id"],
|
||||
"name": plan["name"],
|
||||
"provider": plan["provider"],
|
||||
"monthly_cost_usd": plan["monthly_cost_usd"],
|
||||
"active": key_set,
|
||||
"renewal_day": plan.get("renewal_day"),
|
||||
"days_until_renewal": days_left,
|
||||
"alert": days_left is not None and days_left <= 5,
|
||||
})
|
||||
|
||||
total_monthly = sum(p["monthly_cost_usd"] for p in subscriptions if p["active"])
|
||||
return {
|
||||
"subscriptions": subscriptions,
|
||||
"total_monthly_usd": total_monthly,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/cost/usage")
|
||||
async def get_usage():
|
||||
"""logs에서 API 사용량 파싱"""
|
||||
return {"usage": _parse_api_usage()}
|
||||
Reference in New Issue
Block a user