Update modules/database/semantic_mongo_live_db.py
Browse files
modules/database/semantic_mongo_live_db.py
CHANGED
|
@@ -104,7 +104,8 @@ def store_student_semantic_live_result(username, text, analysis_result, lang_cod
|
|
| 104 |
##########################################
|
| 105 |
def get_student_semantic_live_analysis(username, limit=10):
|
| 106 |
"""
|
| 107 |
-
Versi贸n
|
|
|
|
| 108 |
"""
|
| 109 |
try:
|
| 110 |
collection = get_collection(COLLECTION_NAME)
|
|
@@ -112,33 +113,51 @@ def get_student_semantic_live_analysis(username, limit=10):
|
|
| 112 |
logger.error("No se pudo obtener la colecci贸n")
|
| 113 |
return []
|
| 114 |
|
| 115 |
-
#
|
| 116 |
query = {
|
| 117 |
"username": username,
|
| 118 |
"concept_graph": {"$exists": True, "$ne": None}
|
| 119 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# Versi贸n alternativa sin projection
|
| 124 |
-
cursor = collection.find(query, {
|
| 125 |
-
"timestamp": 1,
|
| 126 |
-
"text": 1,
|
| 127 |
-
"key_concepts": 1,
|
| 128 |
-
"concept_graph": 1,
|
| 129 |
-
"analysis_type": 1,
|
| 130 |
-
"_id": 1
|
| 131 |
-
}).sort("timestamp", -1).limit(limit)
|
| 132 |
-
|
| 133 |
-
results = list(cursor)
|
| 134 |
-
logger.info(f"Recuperados {len(results)} an谩lisis para {username}")
|
| 135 |
return results
|
| 136 |
-
|
| 137 |
except PyMongoError as e:
|
| 138 |
-
logger.error(f"Error de MongoDB: {str(e)}")
|
| 139 |
return []
|
| 140 |
except Exception as e:
|
| 141 |
-
logger.error(f"Error inesperado: {str(e)}")
|
| 142 |
return []
|
| 143 |
|
| 144 |
#######################################################
|
|
|
|
| 104 |
##########################################
|
| 105 |
def get_student_semantic_live_analysis(username, limit=10):
|
| 106 |
"""
|
| 107 |
+
Versi贸n optimizada: Elimina redundancia y garantiza orden cronol贸gico
|
| 108 |
+
mediante normalizaci贸n de fechas al vuelo.
|
| 109 |
"""
|
| 110 |
try:
|
| 111 |
collection = get_collection(COLLECTION_NAME)
|
|
|
|
| 113 |
logger.error("No se pudo obtener la colecci贸n")
|
| 114 |
return []
|
| 115 |
|
| 116 |
+
# Criterio de b煤squeda: usuario espec铆fico y que tenga el grafo generado
|
| 117 |
query = {
|
| 118 |
"username": username,
|
| 119 |
"concept_graph": {"$exists": True, "$ne": None}
|
| 120 |
}
|
| 121 |
+
|
| 122 |
+
# Pipeline de Agregaci贸n para resolver el desorden de fechas (2025 vs 2026)
|
| 123 |
+
pipeline = [
|
| 124 |
+
{"$match": query},
|
| 125 |
+
# Convertimos el timestamp a objeto Date real para que el sort sea exacto
|
| 126 |
+
{"$addFields": {
|
| 127 |
+
"sort_date": {
|
| 128 |
+
"$convert": {
|
| 129 |
+
"input": "$timestamp",
|
| 130 |
+
"to": "date",
|
| 131 |
+
"onError": "$timestamp", # Si falla, mantiene el valor original
|
| 132 |
+
"onNull": "$timestamp"
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
}},
|
| 136 |
+
# Ordenamos por la fecha normalizada de forma descendente (m谩s reciente primero)
|
| 137 |
+
{"$sort": {"sort_date": -1}},
|
| 138 |
+
{"$limit": limit},
|
| 139 |
+
# Proyectamos solo los campos necesarios para no sobrecargar la memoria
|
| 140 |
+
{"$project": {
|
| 141 |
+
"timestamp": 1,
|
| 142 |
+
"text": 1,
|
| 143 |
+
"key_concepts": 1,
|
| 144 |
+
"concept_graph": 1,
|
| 145 |
+
"analysis_type": 1,
|
| 146 |
+
"_id": 1
|
| 147 |
+
}}
|
| 148 |
+
]
|
| 149 |
+
|
| 150 |
+
# Ejecutamos una 煤nica vez la consulta
|
| 151 |
+
results = list(collection.aggregate(pipeline))
|
| 152 |
|
| 153 |
+
logger.info(f"Recuperados {len(results)} an谩lisis 'live' para {username}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
return results
|
| 155 |
+
|
| 156 |
except PyMongoError as e:
|
| 157 |
+
logger.error(f"Error de MongoDB en live analysis: {str(e)}")
|
| 158 |
return []
|
| 159 |
except Exception as e:
|
| 160 |
+
logger.error(f"Error inesperado en live analysis: {str(e)}")
|
| 161 |
return []
|
| 162 |
|
| 163 |
#######################################################
|