Update modules/database.py
Browse files- modules/database.py +65 -0
modules/database.py
CHANGED
@@ -128,7 +128,72 @@ def get_user(username):
|
|
128 |
return None
|
129 |
|
130 |
#######################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
|
|
|
|
|
|
|
|
|
|
132 |
def store_morphosyntax_result(username, text, repeated_words, arc_diagrams):
|
133 |
if analysis_collection is None:
|
134 |
logger.error("La conexión a MongoDB no está inicializada")
|
|
|
128 |
return None
|
129 |
|
130 |
#######################################################################################################
|
131 |
+
def get_student_data(username):
|
132 |
+
if analysis_collection is None:
|
133 |
+
logger.error("La conexión a MongoDB no está inicializada")
|
134 |
+
return None
|
135 |
+
|
136 |
+
try:
|
137 |
+
logger.info(f"Buscando datos para el usuario: {username}")
|
138 |
+
cursor = analysis_collection.find({"username": username})
|
139 |
+
|
140 |
+
formatted_data = {
|
141 |
+
"username": username,
|
142 |
+
"entries": [],
|
143 |
+
"entries_count": 0,
|
144 |
+
"word_count": {},
|
145 |
+
"semantic_analyses": [],
|
146 |
+
"discourse_analyses": [],
|
147 |
+
"chat_history": []
|
148 |
+
}
|
149 |
+
|
150 |
+
for entry in cursor:
|
151 |
+
formatted_entry = {
|
152 |
+
"timestamp": entry["timestamp"],
|
153 |
+
"text": entry["text"],
|
154 |
+
"analysis_type": entry.get("analysis_type", "morphosyntax")
|
155 |
+
}
|
156 |
+
|
157 |
+
if formatted_entry["analysis_type"] == "morphosyntax":
|
158 |
+
formatted_entry.update({
|
159 |
+
"word_count": entry.get("word_count", {}),
|
160 |
+
"arc_diagrams": entry.get("arc_diagrams", [])
|
161 |
+
})
|
162 |
+
for category, count in formatted_entry["word_count"].items():
|
163 |
+
formatted_data["word_count"][category] = formatted_data["word_count"].get(category, 0) + count
|
164 |
+
|
165 |
+
elif formatted_entry["analysis_type"] == "semantic":
|
166 |
+
formatted_entry["network_diagram"] = entry.get("network_diagram", "")
|
167 |
+
formatted_data["semantic_analyses"].append(formatted_entry)
|
168 |
+
|
169 |
+
elif formatted_entry["analysis_type"] == "discourse":
|
170 |
+
formatted_entry.update({
|
171 |
+
"text1": entry.get("text1", ""),
|
172 |
+
"text2": entry.get("text2", ""),
|
173 |
+
"combined_graph": entry.get("combined_graph", "")
|
174 |
+
})
|
175 |
+
formatted_data["discourse_analyses"].append(formatted_entry)
|
176 |
+
|
177 |
+
formatted_data["entries"].append(formatted_entry)
|
178 |
+
|
179 |
+
formatted_data["entries_count"] = len(formatted_data["entries"])
|
180 |
+
formatted_data["entries"].sort(key=lambda x: x["timestamp"], reverse=True)
|
181 |
+
|
182 |
+
for entry in formatted_data["entries"]:
|
183 |
+
entry["timestamp"] = entry["timestamp"].isoformat()
|
184 |
+
|
185 |
+
# Obtener el historial del chat
|
186 |
+
chat_cursor = mongo_db.chat_history.find({"username": username})
|
187 |
+
formatted_data["chat_history"] = list(chat_cursor)
|
188 |
+
|
189 |
+
logger.info(f"Datos formateados para {username}: {formatted_data}")
|
190 |
+
return formatted_data
|
191 |
|
192 |
+
except Exception as e:
|
193 |
+
logger.error(f"Error al obtener datos del estudiante {username}: {str(e)}")
|
194 |
+
return None
|
195 |
+
|
196 |
+
#######################################################################################################
|
197 |
def store_morphosyntax_result(username, text, repeated_words, arc_diagrams):
|
198 |
if analysis_collection is None:
|
199 |
logger.error("La conexión a MongoDB no está inicializada")
|