import logging from typing import Dict, List logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) class PerformanceAnalyzer: def __init__(self, db_connection): # Construtor que aceita a conexão do banco self.conn = db_connection def analyze_user_performance(self, user_id: str) -> Dict: try: cursor = self.conn.cursor() cursor.execute(''' SELECT topic, AVG(performance_score) as avg_score, COUNT(*) as total_sessions FROM study_progress WHERE user_id = ? GROUP BY topic ''', (user_id,)) results = cursor.fetchall() return { 'performance_by_topic': [dict(r) for r in results], 'overall_score': self._calculate_overall_score(results) } except Exception as e: logger.error(f"Erro ao analisar performance: {e}") return {} def get_weak_areas(self, user_id: str) -> List[str]: try: cursor = self.conn.cursor() cursor.execute(''' SELECT topic, AVG(performance_score) as avg_score FROM study_progress WHERE user_id = ? GROUP BY topic HAVING avg_score < 70 ORDER BY avg_score ASC ''', (user_id,)) return [row['topic'] for row in cursor.fetchall()] except Exception as e: logger.error(f"Erro ao identificar áreas fracas: {e}") return [] def _calculate_overall_score(self, results) -> float: if not results: return 0.0 total_score = sum(r['avg_score'] for r in results) return round(total_score / len(results), 2)