from typing import Dict, Any
from datetime import datetime

class DataQualityMonitor:
    """数据质量监控"""
    
    def __init__(self):
        self.provider_stats = {}
    
    def record_access(self, provider_name: str, success: bool, response_time: float, data_size: int):
        """记录数据访问统计"""
        if provider_name not in self.provider_stats:
            self.provider_stats[provider_name] = {
                'total_requests': 0,
                'successful_requests': 0,
                'failed_requests': 0,
                'total_response_time': 0,
                'total_data_size': 0,
                'last_access': None
            }
        
        stats = self.provider_stats[provider_name]
        stats['total_requests'] += 1
        if success:
            stats['successful_requests'] += 1
        else:
            stats['failed_requests'] += 1
        stats['total_response_time'] += response_time
        stats['total_data_size'] += data_size
        stats['last_access'] = datetime.now()
    
    def get_provider_health(self, provider_name: str) -> Dict[str, Any]:
        """获取提供商健康状况"""
        if provider_name not in self.provider_stats:
            return {'status': 'unknown'}
        
        stats = self.provider_stats[provider_name]
        success_rate = stats['successful_requests'] / stats['total_requests'] if stats['total_requests'] > 0 else 0
        avg_response_time = stats['total_response_time'] / stats['total_requests'] if stats['total_requests'] > 0 else 0
        
        status = 'healthy' if success_rate > 0.95 and avg_response_time < 2.0 else 'degraded' if success_rate > 0.8 else 'unhealthy'
        
        return {
            'status': status,
            'success_rate': success_rate,
            'avg_response_time': avg_response_time,
            'total_requests': stats['total_requests'],
            'last_access': stats['last_access']
        }