Update modules/database.py
Browse files- modules/database.py +67 -67
modules/database.py
CHANGED
|
@@ -127,72 +127,6 @@ def get_user(username):
|
|
| 127 |
print(f"Error al obtener usuario {username}: {str(e)}")
|
| 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:
|
|
@@ -307,4 +241,70 @@ def store_chat_history(username, messages):
|
|
| 307 |
return True
|
| 308 |
except Exception as e:
|
| 309 |
logger.error(f"Error saving chat history for user {username}: {str(e)}")
|
| 310 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
print(f"Error al obtener usuario {username}: {str(e)}")
|
| 128 |
return None
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
#######################################################################################################
|
| 131 |
def store_morphosyntax_result(username, text, repeated_words, arc_diagrams):
|
| 132 |
if analysis_collection is None:
|
|
|
|
| 241 |
return True
|
| 242 |
except Exception as e:
|
| 243 |
logger.error(f"Error saving chat history for user {username}: {str(e)}")
|
| 244 |
+
return False
|
| 245 |
+
|
| 246 |
+
#######################################################################################################
|
| 247 |
+
def get_student_data(username):
|
| 248 |
+
if analysis_collection is None:
|
| 249 |
+
logger.error("La conexi贸n a MongoDB no est谩 inicializada")
|
| 250 |
+
return None
|
| 251 |
+
|
| 252 |
+
try:
|
| 253 |
+
logger.info(f"Buscando datos para el usuario: {username}")
|
| 254 |
+
cursor = analysis_collection.find({"username": username})
|
| 255 |
+
|
| 256 |
+
formatted_data = {
|
| 257 |
+
"username": username,
|
| 258 |
+
"entries": [],
|
| 259 |
+
"entries_count": 0,
|
| 260 |
+
"word_count": {},
|
| 261 |
+
"semantic_analyses": [],
|
| 262 |
+
"discourse_analyses": [],
|
| 263 |
+
"chat_history": []
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
for entry in cursor:
|
| 267 |
+
formatted_entry = {
|
| 268 |
+
"timestamp": entry["timestamp"],
|
| 269 |
+
"text": entry["text"],
|
| 270 |
+
"analysis_type": entry.get("analysis_type", "morphosyntax")
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
if formatted_entry["analysis_type"] == "morphosyntax":
|
| 274 |
+
formatted_entry.update({
|
| 275 |
+
"word_count": entry.get("word_count", {}),
|
| 276 |
+
"arc_diagrams": entry.get("arc_diagrams", [])
|
| 277 |
+
})
|
| 278 |
+
for category, count in formatted_entry["word_count"].items():
|
| 279 |
+
formatted_data["word_count"][category] = formatted_data["word_count"].get(category, 0) + count
|
| 280 |
+
|
| 281 |
+
elif formatted_entry["analysis_type"] == "semantic":
|
| 282 |
+
formatted_entry["network_diagram"] = entry.get("network_diagram", "")
|
| 283 |
+
formatted_data["semantic_analyses"].append(formatted_entry)
|
| 284 |
+
|
| 285 |
+
elif formatted_entry["analysis_type"] == "discourse":
|
| 286 |
+
formatted_entry.update({
|
| 287 |
+
"text1": entry.get("text1", ""),
|
| 288 |
+
"text2": entry.get("text2", ""),
|
| 289 |
+
"combined_graph": entry.get("combined_graph", "")
|
| 290 |
+
})
|
| 291 |
+
formatted_data["discourse_analyses"].append(formatted_entry)
|
| 292 |
+
|
| 293 |
+
formatted_data["entries"].append(formatted_entry)
|
| 294 |
+
|
| 295 |
+
formatted_data["entries_count"] = len(formatted_data["entries"])
|
| 296 |
+
formatted_data["entries"].sort(key=lambda x: x["timestamp"], reverse=True)
|
| 297 |
+
|
| 298 |
+
for entry in formatted_data["entries"]:
|
| 299 |
+
entry["timestamp"] = entry["timestamp"].isoformat()
|
| 300 |
+
|
| 301 |
+
# Obtener el historial del chat
|
| 302 |
+
chat_cursor = mongo_db.chat_history.find({"username": username})
|
| 303 |
+
formatted_data["chat_history"] = list(chat_cursor)
|
| 304 |
+
|
| 305 |
+
logger.info(f"Datos formateados para {username}: {formatted_data}")
|
| 306 |
+
return formatted_data
|
| 307 |
+
|
| 308 |
+
except Exception as e:
|
| 309 |
+
logger.error(f"Error al obtener datos del estudiante {username}: {str(e)}")
|
| 310 |
+
return None
|