Update modules/database.py
Browse files- modules/database.py +27 -9
modules/database.py
CHANGED
|
@@ -88,15 +88,26 @@ def create_user(user_data):
|
|
| 88 |
logger.error(f"Error al crear usuario: {str(e)}")
|
| 89 |
return False
|
| 90 |
|
|
|
|
| 91 |
# Funciones para Cosmos DB MongoDB API (an谩lisis de texto)
|
|
|
|
| 92 |
def get_student_data(username):
|
| 93 |
if analysis_collection is None:
|
| 94 |
logger.error("La conexi贸n a MongoDB no est谩 inicializada")
|
| 95 |
return None
|
| 96 |
|
| 97 |
try:
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# Formatear los datos
|
| 102 |
formatted_data = {
|
|
@@ -109,12 +120,11 @@ def get_student_data(username):
|
|
| 109 |
}
|
| 110 |
|
| 111 |
for entry in cursor:
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
"
|
| 115 |
-
"
|
| 116 |
-
}
|
| 117 |
-
formatted_data["entries"].append(entry_data)
|
| 118 |
formatted_data["entries_count"] += 1
|
| 119 |
|
| 120 |
# Agregar conteo de palabras
|
|
@@ -128,11 +138,19 @@ def get_student_data(username):
|
|
| 128 |
formatted_data["arc_diagrams"].extend(entry.get("arc_diagrams", []))
|
| 129 |
formatted_data["network_diagrams"].append(entry.get("network_diagram", ""))
|
| 130 |
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
return formatted_data if formatted_data["entries_count"] > 0 else None
|
| 133 |
except Exception as e:
|
| 134 |
logger.error(f"Error al obtener datos del estudiante {username}: {str(e)}")
|
| 135 |
return None
|
|
|
|
| 136 |
|
| 137 |
def store_analysis_result(username, text, repeated_words, arc_diagrams, network_diagram):
|
| 138 |
if analysis_collection is None:
|
|
|
|
| 88 |
logger.error(f"Error al crear usuario: {str(e)}")
|
| 89 |
return False
|
| 90 |
|
| 91 |
+
################################################################################
|
| 92 |
# Funciones para Cosmos DB MongoDB API (an谩lisis de texto)
|
| 93 |
+
|
| 94 |
def get_student_data(username):
|
| 95 |
if analysis_collection is None:
|
| 96 |
logger.error("La conexi贸n a MongoDB no est谩 inicializada")
|
| 97 |
return None
|
| 98 |
|
| 99 |
try:
|
| 100 |
+
logger.info(f"Buscando datos para el usuario: {username}")
|
| 101 |
+
# Obtener todos los documentos para el usuario sin ordenar
|
| 102 |
+
cursor = analysis_collection.find({"username": username})
|
| 103 |
+
|
| 104 |
+
# Contar documentos
|
| 105 |
+
count = analysis_collection.count_documents({"username": username})
|
| 106 |
+
logger.info(f"N煤mero de documentos encontrados para {username}: {count}")
|
| 107 |
+
|
| 108 |
+
if count == 0:
|
| 109 |
+
logger.info(f"No se encontraron datos para el usuario {username}")
|
| 110 |
+
return None
|
| 111 |
|
| 112 |
# Formatear los datos
|
| 113 |
formatted_data = {
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
for entry in cursor:
|
| 123 |
+
logger.debug(f"Procesando entrada: {entry}")
|
| 124 |
+
formatted_data["entries"].append({
|
| 125 |
+
"timestamp": entry["timestamp"],
|
| 126 |
+
"text": entry["text"]
|
| 127 |
+
})
|
|
|
|
| 128 |
formatted_data["entries_count"] += 1
|
| 129 |
|
| 130 |
# Agregar conteo de palabras
|
|
|
|
| 138 |
formatted_data["arc_diagrams"].extend(entry.get("arc_diagrams", []))
|
| 139 |
formatted_data["network_diagrams"].append(entry.get("network_diagram", ""))
|
| 140 |
|
| 141 |
+
# Ordenar las entradas por timestamp despu茅s de obtenerlas
|
| 142 |
+
formatted_data["entries"].sort(key=lambda x: x["timestamp"], reverse=True)
|
| 143 |
+
|
| 144 |
+
# Convertir los timestamps a formato ISO despu茅s de ordenar
|
| 145 |
+
for entry in formatted_data["entries"]:
|
| 146 |
+
entry["timestamp"] = entry["timestamp"].isoformat()
|
| 147 |
+
|
| 148 |
+
logger.info(f"Datos formateados para {username}: {formatted_data}")
|
| 149 |
return formatted_data if formatted_data["entries_count"] > 0 else None
|
| 150 |
except Exception as e:
|
| 151 |
logger.error(f"Error al obtener datos del estudiante {username}: {str(e)}")
|
| 152 |
return None
|
| 153 |
+
#######################################################################################################
|
| 154 |
|
| 155 |
def store_analysis_result(username, text, repeated_words, arc_diagrams, network_diagram):
|
| 156 |
if analysis_collection is None:
|