#!/usr/bin/env python3
"""
WBS-9.2: snapshot_admin 성능 최적화

목표: P99 < 2초 달성
"""

import time
import json
from pathlib import Path
from datetime import datetime
from typing import Dict, List

class PerformanceOptimizer:
    """snapshot_admin 성능 최적화"""

    def __init__(self):
        self.results = {
            "timestamp": datetime.now().isoformat(),
            "optimizations": [],
            "summary": {}
        }

    def identify_optimization_opportunities(self) -> List[Dict]:
        """최적화 기회 식별"""
        opportunities = [
            {
                "name": "Query Indexing",
                "current_impact": "테이블 스캔으로 인한 지연",
                "optimization": "기존 인덱스 확인 및 추가",
                "tables": ["data_feed", "performance", "positions"],
                "expected_improvement": "3-5배 빠르기",
                "complexity": "LOW",
                "status": "IDENTIFIED"
            },
            {
                "name": "Connection Pooling",
                "current_impact": "매 요청마다 새로운 DB 연결",
                "optimization": "SQLite 커넥션 풀 또는 WAL 모드",
                "expected_improvement": "2-3배 빠르기",
                "complexity": "MEDIUM",
                "status": "IDENTIFIED"
            },
            {
                "name": "Query Caching",
                "current_impact": "반복적인 동일 쿼리 실행",
                "optimization": "Redis 또는 메모리 캐시 추가",
                "expected_improvement": "10-100배 빠르기 (캐시 히트)",
                "complexity": "MEDIUM",
                "status": "IDENTIFIED"
            },
            {
                "name": "Table Partitioning",
                "current_impact": "큰 테이블에서 느린 조회",
                "optimization": "성능 메트릭별 파티셔닝",
                "expected_improvement": "5-10배 빠르기",
                "complexity": "HIGH",
                "status": "IDENTIFIED"
            },
            {
                "name": "WAL Mode",
                "current_impact": "동시 접근 시 락 경합",
                "optimization": "SQLite PRAGMA journal_mode=WAL",
                "expected_improvement": "2-3배 빠르기 + 동시성 향상",
                "complexity": "LOW",
                "status": "READY_TO_IMPLEMENT"
            },
            {
                "name": "PRAGMA Optimization",
                "current_impact": "기본 설정으로 인한 오버헤드",
                "optimization": "cache_size, synchronous, temp_store 조정",
                "expected_improvement": "1.5-2배 빠르기",
                "complexity": "LOW",
                "status": "READY_TO_IMPLEMENT"
            }
        ]

        return opportunities

    def implement_wal_mode(self) -> Dict:
        """WAL 모드 적용"""
        import sqlite3

        optimizations = []

        for db_name, db_path in [
            ("kis_data_collection", "src/quant_engine/kis_data_collection.db"),
            ("snapshot_admin", "src/quant_engine/snapshot_admin.db")
        ]:
            try:
                conn = sqlite3.connect(db_path)
                cursor = conn.cursor()

                # WAL 모드 활성화
                cursor.execute("PRAGMA journal_mode=WAL")
                mode = cursor.fetchone()[0]

                # 추가 최적화
                cursor.execute("PRAGMA synchronous=NORMAL")
                cursor.execute("PRAGMA cache_size=10000")
                cursor.execute("PRAGMA temp_store=MEMORY")

                conn.commit()
                conn.close()

                optimizations.append({
                    "database": db_name,
                    "optimization": "WAL mode enabled",
                    "journal_mode": mode,
                    "status": "SUCCESS"
                })

                print(f"[OK] {db_name}: WAL mode = {mode}")
            except Exception as e:
                optimizations.append({
                    "database": db_name,
                    "error": str(e),
                    "status": "FAILED"
                })
                print(f"[FAIL] {db_name}: {e}")

        return {
            "optimization": "WAL Mode",
            "results": optimizations
        }

    def add_performance_indexes(self) -> Dict:
        """성능 인덱스 추가"""
        import sqlite3

        indexes = [
            ("kis_data_collection", "data_feed", "entry_date"),
            ("kis_data_collection", "data_feed", "ticker"),
            ("snapshot_admin", "performance", "entry_date"),
            ("snapshot_admin", "performance", "ticker"),
            ("snapshot_admin", "positions", "ticker"),
        ]

        results = []
        for db_name, table, column in indexes:
            db_path = "src/quant_engine/" + db_name + ".db"
            try:
                conn = sqlite3.connect(db_path)
                cursor = conn.cursor()

                # 인덱스 생성
                index_name = f"idx_{table}_{column}"
                cursor.execute(f"CREATE INDEX IF NOT EXISTS {index_name} ON {table}({column})")

                conn.commit()
                conn.close()

                results.append({
                    "database": db_name,
                    "table": table,
                    "column": column,
                    "index_name": index_name,
                    "status": "SUCCESS"
                })

                print(f"[OK] {db_name}.{table}.{column} indexed")
            except Exception as e:
                results.append({
                    "database": db_name,
                    "table": table,
                    "column": column,
                    "error": str(e),
                    "status": "FAILED"
                })
                print(f"[FAIL] {db_name}.{table}.{column}: {e}")

        return {
            "optimization": "Performance Indexes",
            "results": results
        }

    def generate_report(self) -> Dict:
        """최적화 리포트"""
        print("\n" + "="*80)
        print("WBS-9.2: snapshot_admin 성능 최적화")
        print("="*80)

        opportunities = self.identify_optimization_opportunities()

        print("\n[식별된 최적화 기회]")
        for opp in opportunities:
            status = "[READY]" if opp["status"] == "READY_TO_IMPLEMENT" else "[FUTURE]"
            print(f"  {status} {opp['name']}")
            print(f"       └─ 개선: {opp['expected_improvement']}")

        # 즉시 적용 가능한 최적화
        print("\n[즉시 적용 가능한 최적화]")

        wal_result = self.implement_wal_mode()
        self.results["optimizations"].append(wal_result)

        index_result = self.add_performance_indexes()
        self.results["optimizations"].append(index_result)

        # 성능 목표
        print("\n[성능 목표]")
        print("  P99 < 2000ms (2초)")
        print("  동시 접근 10개 테이블")
        print("  데이터 무결성: 100%")

        print("\n[예상 효과]")
        print("  1. WAL 모드: 2-3배 빠르기 + 동시성 향상")
        print("  2. 인덱싱: 3-5배 빠르기 (entry_date, ticker 조회)")
        print("  3. PRAGMA: 1.5-2배 빠르기 (캐시 최적화)")
        print("  4. 누적 효과: 5-10배 성능 개선 예상")

        self.results["summary"] = {
            "target_p99_ms": 2000,
            "optimizations_applied": 2,
            "opportunities_identified": len(opportunities),
            "expected_improvement_factor": "5-10x",
            "status": "IN_PROGRESS"
        }

        return self.results

if __name__ == "__main__":
    optimizer = PerformanceOptimizer()
    result = optimizer.generate_report()

    # 결과 저장
    output_file = Path("Temp/wbs92_optimization_report.json")
    output_file.parent.mkdir(parents=True, exist_ok=True)
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(result, f, indent=2, ensure_ascii=False)

    print(f"\n[저장] 최적화 리포트: {output_file}")
    print("[완료] WBS-9.2 성능 최적화 적용 완료")
