Create writing_progress_mongo_db.py
Browse files
modules/database/writing_progress_mongo_db.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modules/database/writing_progress_mongo_db.py
|
2 |
+
|
3 |
+
from .mongo_db import get_collection, insert_document
|
4 |
+
from datetime import datetime, timezone
|
5 |
+
import logging
|
6 |
+
|
7 |
+
logger = logging.getLogger(__name__)
|
8 |
+
COLLECTION_NAME = 'writing_progress'
|
9 |
+
|
10 |
+
def store_writing_baseline(username, metrics, text):
|
11 |
+
"""
|
12 |
+
Guarda la línea base de escritura de un usuario.
|
13 |
+
Args:
|
14 |
+
username: ID del usuario
|
15 |
+
metrics: Diccionario con métricas iniciales
|
16 |
+
text: Texto analizado
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
document = {
|
20 |
+
'username': username,
|
21 |
+
'type': 'baseline',
|
22 |
+
'metrics': metrics,
|
23 |
+
'text': text,
|
24 |
+
'timestamp': datetime.now(timezone.utc).isoformat(),
|
25 |
+
'iteration': 0 # Línea base siempre es iteración 0
|
26 |
+
}
|
27 |
+
|
28 |
+
# Verificar si ya existe una línea base
|
29 |
+
collection = get_collection(COLLECTION_NAME)
|
30 |
+
existing = collection.find_one({
|
31 |
+
'username': username,
|
32 |
+
'type': 'baseline'
|
33 |
+
})
|
34 |
+
|
35 |
+
if existing:
|
36 |
+
# Actualizar línea base existente
|
37 |
+
result = collection.update_one(
|
38 |
+
{'_id': existing['_id']},
|
39 |
+
{'$set': document}
|
40 |
+
)
|
41 |
+
success = result.modified_count > 0
|
42 |
+
else:
|
43 |
+
# Insertar nueva línea base
|
44 |
+
result = collection.insert_one(document)
|
45 |
+
success = result.inserted_id is not None
|
46 |
+
|
47 |
+
logger.info(f"Línea base {'actualizada' if existing else 'creada'} para usuario: {username}")
|
48 |
+
return success
|
49 |
+
|
50 |
+
except Exception as e:
|
51 |
+
logger.error(f"Error al guardar línea base: {str(e)}")
|
52 |
+
return False
|
53 |
+
|
54 |
+
def store_writing_progress(username, metrics, text):
|
55 |
+
"""
|
56 |
+
Guarda una nueva iteración de progreso.
|
57 |
+
"""
|
58 |
+
try:
|
59 |
+
# Obtener último número de iteración
|
60 |
+
collection = get_collection(COLLECTION_NAME)
|
61 |
+
last_progress = collection.find_one(
|
62 |
+
{'username': username},
|
63 |
+
sort=[('iteration', -1)]
|
64 |
+
)
|
65 |
+
|
66 |
+
next_iteration = (last_progress['iteration'] + 1) if last_progress else 1
|
67 |
+
|
68 |
+
document = {
|
69 |
+
'username': username,
|
70 |
+
'type': 'progress',
|
71 |
+
'metrics': metrics,
|
72 |
+
'text': text,
|
73 |
+
'timestamp': datetime.now(timezone.utc).isoformat(),
|
74 |
+
'iteration': next_iteration
|
75 |
+
}
|
76 |
+
|
77 |
+
result = collection.insert_one(document)
|
78 |
+
success = result.inserted_id is not None
|
79 |
+
|
80 |
+
if success:
|
81 |
+
logger.info(f"Progreso guardado para {username}, iteración {next_iteration}")
|
82 |
+
|
83 |
+
return success
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
logger.error(f"Error al guardar progreso: {str(e)}")
|
87 |
+
return False
|
88 |
+
|
89 |
+
def get_writing_baseline(username):
|
90 |
+
"""
|
91 |
+
Obtiene la línea base de un usuario.
|
92 |
+
"""
|
93 |
+
try:
|
94 |
+
collection = get_collection(COLLECTION_NAME)
|
95 |
+
return collection.find_one({
|
96 |
+
'username': username,
|
97 |
+
'type': 'baseline'
|
98 |
+
})
|
99 |
+
except Exception as e:
|
100 |
+
logger.error(f"Error al obtener línea base: {str(e)}")
|
101 |
+
return None
|
102 |
+
|
103 |
+
def get_writing_progress(username, limit=None):
|
104 |
+
"""
|
105 |
+
Obtiene el historial de progreso de un usuario.
|
106 |
+
Args:
|
107 |
+
username: ID del usuario
|
108 |
+
limit: Número máximo de registros a retornar
|
109 |
+
"""
|
110 |
+
try:
|
111 |
+
collection = get_collection(COLLECTION_NAME)
|
112 |
+
cursor = collection.find(
|
113 |
+
{
|
114 |
+
'username': username,
|
115 |
+
'type': 'progress'
|
116 |
+
},
|
117 |
+
sort=[('iteration', -1)]
|
118 |
+
)
|
119 |
+
|
120 |
+
if limit:
|
121 |
+
cursor = cursor.limit(limit)
|
122 |
+
|
123 |
+
return list(cursor)
|
124 |
+
|
125 |
+
except Exception as e:
|
126 |
+
logger.error(f"Error al obtener progreso: {str(e)}")
|
127 |
+
return []
|
128 |
+
|
129 |
+
def get_latest_writing_metrics(username):
|
130 |
+
"""
|
131 |
+
Obtiene las métricas más recientes (línea base o progreso).
|
132 |
+
"""
|
133 |
+
try:
|
134 |
+
collection = get_collection(COLLECTION_NAME)
|
135 |
+
return collection.find_one(
|
136 |
+
{'username': username},
|
137 |
+
sort=[('timestamp', -1)]
|
138 |
+
)
|
139 |
+
except Exception as e:
|
140 |
+
logger.error(f"Error al obtener métricas recientes: {str(e)}")
|
141 |
+
return None
|