Spaces:
Runtime error
Runtime error
Create performance_analyzer.py
Browse files- performance_analyzer.py +56 -0
performance_analyzer.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
from typing import Dict, List
|
3 |
+
|
4 |
+
logging.basicConfig(level=logging.INFO)
|
5 |
+
logger = logging.getLogger(__name__)
|
6 |
+
|
7 |
+
class PerformanceAnalyzer:
|
8 |
+
def __init__(self, db_connection):
|
9 |
+
self.conn = db_connection
|
10 |
+
|
11 |
+
def analyze_user_performance(self, user_id: str) -> Dict:
|
12 |
+
try:
|
13 |
+
cursor = self.conn.cursor()
|
14 |
+
cursor.execute('''
|
15 |
+
SELECT
|
16 |
+
topic,
|
17 |
+
AVG(performance_score) as avg_score,
|
18 |
+
COUNT(*) as total_sessions
|
19 |
+
FROM study_progress
|
20 |
+
WHERE user_id = ?
|
21 |
+
GROUP BY topic
|
22 |
+
''', (user_id,))
|
23 |
+
|
24 |
+
results = cursor.fetchall()
|
25 |
+
return {
|
26 |
+
'performance_by_topic': [dict(r) for r in results],
|
27 |
+
'overall_score': self._calculate_overall_score(results)
|
28 |
+
}
|
29 |
+
except Exception as e:
|
30 |
+
logger.error(f"Erro ao analisar performance: {e}")
|
31 |
+
return {}
|
32 |
+
|
33 |
+
def get_weak_areas(self, user_id: str) -> List[str]:
|
34 |
+
try:
|
35 |
+
cursor = self.conn.cursor()
|
36 |
+
cursor.execute('''
|
37 |
+
SELECT
|
38 |
+
topic,
|
39 |
+
AVG(performance_score) as avg_score
|
40 |
+
FROM study_progress
|
41 |
+
WHERE user_id = ?
|
42 |
+
GROUP BY topic
|
43 |
+
HAVING avg_score < 70
|
44 |
+
ORDER BY avg_score ASC
|
45 |
+
''', (user_id,))
|
46 |
+
|
47 |
+
return [row['topic'] for row in cursor.fetchall()]
|
48 |
+
except Exception as e:
|
49 |
+
logger.error(f"Erro ao identificar áreas fracas: {e}")
|
50 |
+
return []
|
51 |
+
|
52 |
+
def _calculate_overall_score(self, results) -> float:
|
53 |
+
if not results:
|
54 |
+
return 0.0
|
55 |
+
total_score = sum(r['avg_score'] for r in results)
|
56 |
+
return round(total_score / len(results), 2)
|