Update modules/database/morphosyntax_iterative_mongo_db.py
Browse files
modules/database/morphosyntax_iterative_mongo_db.py
CHANGED
@@ -6,18 +6,15 @@ from .mongo_db import get_collection, insert_document, find_documents, update_do
|
|
6 |
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
|
13 |
def store_student_morphosyntax_base(username, text, arc_diagrams):
|
14 |
-
"""
|
15 |
-
Almacena el análisis morfosintáctico base
|
16 |
-
Returns: ID del documento base o None si hay error
|
17 |
-
"""
|
18 |
try:
|
19 |
base_document = {
|
20 |
-
'username': username,
|
21 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
22 |
'text': text,
|
23 |
'arc_diagrams': arc_diagrams,
|
@@ -25,7 +22,7 @@ def store_student_morphosyntax_base(username, text, arc_diagrams):
|
|
25 |
'has_iterations': False
|
26 |
}
|
27 |
|
28 |
-
collection = get_collection(
|
29 |
result = collection.insert_one(base_document)
|
30 |
logger.info(f"Análisis base guardado para {username}")
|
31 |
return str(result.inserted_id)
|
@@ -35,28 +32,25 @@ def store_student_morphosyntax_base(username, text, arc_diagrams):
|
|
35 |
return None
|
36 |
|
37 |
def store_student_morphosyntax_iteration(username, base_id, original_text, iteration_text, arc_diagrams):
|
38 |
-
"""
|
39 |
-
Almacena una iteración de análisis morfosintáctico
|
40 |
-
Returns: ID de la iteración o None si hay error
|
41 |
-
"""
|
42 |
try:
|
43 |
iteration_document = {
|
44 |
-
'username': username,
|
45 |
'base_id': base_id,
|
46 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
47 |
'original_text': original_text,
|
48 |
-
'iteration_text': iteration_text,
|
49 |
'arc_diagrams': arc_diagrams,
|
50 |
'analysis_type': 'morphosyntax_iteration'
|
51 |
}
|
52 |
|
53 |
-
collection = get_collection(
|
54 |
result = collection.insert_one(iteration_document)
|
55 |
|
56 |
-
# Actualizar documento base
|
57 |
-
base_collection = get_collection(
|
58 |
base_collection.update_one(
|
59 |
-
{'_id': base_id},
|
60 |
{'$set': {'has_iterations': True}}
|
61 |
)
|
62 |
|
@@ -65,90 +59,4 @@ def store_student_morphosyntax_iteration(username, base_id, original_text, itera
|
|
65 |
|
66 |
except Exception as e:
|
67 |
logger.error(f"Error almacenando iteración: {str(e)}")
|
68 |
-
return None
|
69 |
-
|
70 |
-
def get_student_morphosyntax_analysis(username, limit=10):
|
71 |
-
"""
|
72 |
-
Obtiene los análisis base y sus iteraciones
|
73 |
-
Returns: Lista de análisis con sus iteraciones
|
74 |
-
"""
|
75 |
-
try:
|
76 |
-
# Obtener análisis base
|
77 |
-
base_collection = get_collection(BASE_COLLECTION)
|
78 |
-
base_query = {
|
79 |
-
"username": username,
|
80 |
-
"analysis_type": "morphosyntax_base"
|
81 |
-
}
|
82 |
-
base_analyses = list(base_collection.find(base_query).sort("timestamp", -1).limit(limit))
|
83 |
-
|
84 |
-
# Para cada análisis base, obtener sus iteraciones
|
85 |
-
iteration_collection = get_collection(ITERATION_COLLECTION)
|
86 |
-
for analysis in base_analyses:
|
87 |
-
base_id = analysis['_id']
|
88 |
-
iterations = list(iteration_collection.find({"base_id": base_id}).sort("timestamp", -1))
|
89 |
-
analysis['iterations'] = iterations
|
90 |
-
|
91 |
-
return base_analyses
|
92 |
-
|
93 |
-
except Exception as e:
|
94 |
-
logger.error(f"Error obteniendo análisis: {str(e)}")
|
95 |
-
return []
|
96 |
-
|
97 |
-
def update_student_morphosyntax_analysis(analysis_id, is_base, update_data):
|
98 |
-
"""
|
99 |
-
Actualiza un análisis base o iteración
|
100 |
-
"""
|
101 |
-
try:
|
102 |
-
collection_name = BASE_COLLECTION if is_base else ITERATION_COLLECTION
|
103 |
-
collection = get_collection(collection_name)
|
104 |
-
|
105 |
-
query = {"_id": analysis_id}
|
106 |
-
update = {"$set": update_data}
|
107 |
-
|
108 |
-
result = update_document(collection_name, query, update)
|
109 |
-
return result
|
110 |
-
|
111 |
-
except Exception as e:
|
112 |
-
logger.error(f"Error actualizando análisis: {str(e)}")
|
113 |
-
return False
|
114 |
-
|
115 |
-
def delete_student_morphosyntax_analysis(analysis_id, is_base):
|
116 |
-
"""
|
117 |
-
Elimina un análisis base o iteración
|
118 |
-
Si es base, también elimina todas sus iteraciones
|
119 |
-
"""
|
120 |
-
try:
|
121 |
-
if is_base:
|
122 |
-
# Primero eliminar todas las iteraciones
|
123 |
-
iteration_collection = get_collection(ITERATION_COLLECTION)
|
124 |
-
iteration_collection.delete_many({"base_id": analysis_id})
|
125 |
-
|
126 |
-
# Luego eliminar el análisis base
|
127 |
-
collection = get_collection(BASE_COLLECTION)
|
128 |
-
else:
|
129 |
-
collection = get_collection(ITERATION_COLLECTION)
|
130 |
-
|
131 |
-
query = {"_id": analysis_id}
|
132 |
-
result = delete_document(collection.name, query)
|
133 |
-
return result
|
134 |
-
|
135 |
-
except Exception as e:
|
136 |
-
logger.error(f"Error eliminando análisis: {str(e)}")
|
137 |
-
return False
|
138 |
-
|
139 |
-
def get_student_morphosyntax_data(username):
|
140 |
-
"""
|
141 |
-
Obtiene todos los datos de análisis morfosintáctico de un estudiante
|
142 |
-
Returns: Diccionario con todos los análisis y sus iteraciones
|
143 |
-
"""
|
144 |
-
try:
|
145 |
-
analyses = get_student_morphosyntax_analysis(username, limit=None)
|
146 |
-
return {
|
147 |
-
'entries': analyses,
|
148 |
-
'total_analyses': len(analyses),
|
149 |
-
'has_iterations': any(a.get('has_iterations', False) for a in analyses)
|
150 |
-
}
|
151 |
-
|
152 |
-
except Exception as e:
|
153 |
-
logger.error(f"Error obteniendo datos del estudiante: {str(e)}")
|
154 |
-
return {'entries': [], 'total_analyses': 0, 'has_iterations': False}
|
|
|
6 |
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
+
# Nombres de colecciones manteniendo coherencia
|
10 |
+
COLLECTION_NAME = 'student_morphosyntax_analysis'
|
11 |
+
COLLECTION_NAME_ITERATIONS = 'student_morphosyntax_iterations'
|
12 |
|
13 |
def store_student_morphosyntax_base(username, text, arc_diagrams):
|
14 |
+
"""Almacena el análisis morfosintáctico base"""
|
|
|
|
|
|
|
15 |
try:
|
16 |
base_document = {
|
17 |
+
'username': username, # key principal
|
18 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
19 |
'text': text,
|
20 |
'arc_diagrams': arc_diagrams,
|
|
|
22 |
'has_iterations': False
|
23 |
}
|
24 |
|
25 |
+
collection = get_collection(COLLECTION_NAME)
|
26 |
result = collection.insert_one(base_document)
|
27 |
logger.info(f"Análisis base guardado para {username}")
|
28 |
return str(result.inserted_id)
|
|
|
32 |
return None
|
33 |
|
34 |
def store_student_morphosyntax_iteration(username, base_id, original_text, iteration_text, arc_diagrams):
|
35 |
+
"""Almacena una iteración de análisis morfosintáctico"""
|
|
|
|
|
|
|
36 |
try:
|
37 |
iteration_document = {
|
38 |
+
'username': username, # key principal
|
39 |
'base_id': base_id,
|
40 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
41 |
'original_text': original_text,
|
42 |
+
'iteration_text': iteration_text,
|
43 |
'arc_diagrams': arc_diagrams,
|
44 |
'analysis_type': 'morphosyntax_iteration'
|
45 |
}
|
46 |
|
47 |
+
collection = get_collection(COLLECTION_NAME_ITERATIONS)
|
48 |
result = collection.insert_one(iteration_document)
|
49 |
|
50 |
+
# Actualizar documento base
|
51 |
+
base_collection = get_collection(COLLECTION_NAME)
|
52 |
base_collection.update_one(
|
53 |
+
{'_id': base_id, 'username': username}, # incluir username en queries
|
54 |
{'$set': {'has_iterations': True}}
|
55 |
)
|
56 |
|
|
|
59 |
|
60 |
except Exception as e:
|
61 |
logger.error(f"Error almacenando iteración: {str(e)}")
|
62 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|