Spaces:
Sleeping
Sleeping
Update modules/database/current_situation_mongo_db.py
Browse files
modules/database/current_situation_mongo_db.py
CHANGED
|
@@ -1,18 +1,12 @@
|
|
| 1 |
# modules/database/current_situation_mongo_db.py
|
| 2 |
-
|
| 3 |
-
from datetime import datetime, timezone
|
| 4 |
import logging
|
| 5 |
-
from .mongo_db import get_collection
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
COLLECTION_NAME = 'student_current_situation'
|
| 9 |
|
| 10 |
-
# En modules/database/current_situation_mongo_db.py
|
| 11 |
def store_current_situation_result(username, text, metrics, feedback):
|
| 12 |
-
success = store_current_situation_result(username, text, metrics, feedback)
|
| 13 |
-
if success:
|
| 14 |
-
verify_storage(username) #Verificar que se guarda correctamente
|
| 15 |
-
return success
|
| 16 |
"""
|
| 17 |
Guarda los resultados del an谩lisis de situaci贸n actual.
|
| 18 |
"""
|
|
@@ -21,8 +15,8 @@ def store_current_situation_result(username, text, metrics, feedback):
|
|
| 21 |
if not all([username, text, metrics]):
|
| 22 |
logger.error("Faltan par谩metros requeridos")
|
| 23 |
return False
|
| 24 |
-
|
| 25 |
-
collection = get_collection(
|
| 26 |
if collection is None:
|
| 27 |
logger.error("No se pudo obtener la colecci贸n")
|
| 28 |
return False
|
|
@@ -46,20 +40,19 @@ def store_current_situation_result(username, text, metrics, feedback):
|
|
| 46 |
'details': metrics['clarity']['details']
|
| 47 |
}
|
| 48 |
}
|
| 49 |
-
|
| 50 |
# Crear documento
|
| 51 |
document = {
|
| 52 |
'username': username,
|
| 53 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
| 54 |
'text': text,
|
| 55 |
-
'metrics':
|
| 56 |
'feedback': feedback,
|
| 57 |
'analysis_type': 'current_situation'
|
| 58 |
}
|
| 59 |
-
|
| 60 |
# Insertar documento
|
| 61 |
result = collection.insert_one(document)
|
| 62 |
-
|
| 63 |
if result.inserted_id:
|
| 64 |
logger.info(f"""
|
| 65 |
An谩lisis guardado exitosamente:
|
|
@@ -68,11 +61,18 @@ def store_current_situation_result(username, text, metrics, feedback):
|
|
| 68 |
- Longitud del texto: {len(text)}
|
| 69 |
- M茅tricas: {formatted_metrics}
|
| 70 |
""")
|
| 71 |
-
return True
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
logger.error("No se pudo insertar el documento")
|
| 74 |
return False
|
| 75 |
-
|
| 76 |
except Exception as e:
|
| 77 |
logger.error(f"Error guardando an谩lisis de situaci贸n actual: {str(e)}")
|
| 78 |
return False
|
|
@@ -84,14 +84,15 @@ def verify_storage(username):
|
|
| 84 |
try:
|
| 85 |
collection = get_collection(COLLECTION_NAME)
|
| 86 |
if collection is None:
|
|
|
|
| 87 |
return False
|
| 88 |
|
| 89 |
# Buscar documentos recientes del usuario
|
|
|
|
|
|
|
| 90 |
recent_docs = collection.find({
|
| 91 |
'username': username,
|
| 92 |
-
'timestamp': {
|
| 93 |
-
'$gte': (datetime.now(timezone.utc) - timedelta(minutes=5)).isoformat()
|
| 94 |
-
}
|
| 95 |
}).sort('timestamp', -1).limit(1)
|
| 96 |
|
| 97 |
docs = list(recent_docs)
|
|
@@ -109,4 +110,23 @@ def verify_storage(username):
|
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
logger.error(f"Error verificando almacenamiento: {str(e)}")
|
| 112 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# modules/database/current_situation_mongo_db.py
|
| 2 |
+
from datetime import datetime, timezone, timedelta
|
|
|
|
| 3 |
import logging
|
| 4 |
+
from .mongo_db import get_collection
|
| 5 |
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
COLLECTION_NAME = 'student_current_situation'
|
| 8 |
|
|
|
|
| 9 |
def store_current_situation_result(username, text, metrics, feedback):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
Guarda los resultados del an谩lisis de situaci贸n actual.
|
| 12 |
"""
|
|
|
|
| 15 |
if not all([username, text, metrics]):
|
| 16 |
logger.error("Faltan par谩metros requeridos")
|
| 17 |
return False
|
| 18 |
+
|
| 19 |
+
collection = get_collection(COLLECTION_NAME)
|
| 20 |
if collection is None:
|
| 21 |
logger.error("No se pudo obtener la colecci贸n")
|
| 22 |
return False
|
|
|
|
| 40 |
'details': metrics['clarity']['details']
|
| 41 |
}
|
| 42 |
}
|
| 43 |
+
|
| 44 |
# Crear documento
|
| 45 |
document = {
|
| 46 |
'username': username,
|
| 47 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
| 48 |
'text': text,
|
| 49 |
+
'metrics': formatted_metrics, # Usar las m茅tricas formateadas
|
| 50 |
'feedback': feedback,
|
| 51 |
'analysis_type': 'current_situation'
|
| 52 |
}
|
| 53 |
+
|
| 54 |
# Insertar documento
|
| 55 |
result = collection.insert_one(document)
|
|
|
|
| 56 |
if result.inserted_id:
|
| 57 |
logger.info(f"""
|
| 58 |
An谩lisis guardado exitosamente:
|
|
|
|
| 61 |
- Longitud del texto: {len(text)}
|
| 62 |
- M茅tricas: {formatted_metrics}
|
| 63 |
""")
|
|
|
|
| 64 |
|
| 65 |
+
# Verificar almacenamiento
|
| 66 |
+
if verify_storage(username):
|
| 67 |
+
logger.info("Verificaci贸n de almacenamiento exitosa")
|
| 68 |
+
return True
|
| 69 |
+
else:
|
| 70 |
+
logger.warning("Verificaci贸n de almacenamiento fall贸")
|
| 71 |
+
return False
|
| 72 |
+
|
| 73 |
logger.error("No se pudo insertar el documento")
|
| 74 |
return False
|
| 75 |
+
|
| 76 |
except Exception as e:
|
| 77 |
logger.error(f"Error guardando an谩lisis de situaci贸n actual: {str(e)}")
|
| 78 |
return False
|
|
|
|
| 84 |
try:
|
| 85 |
collection = get_collection(COLLECTION_NAME)
|
| 86 |
if collection is None:
|
| 87 |
+
logger.error("No se pudo obtener la colecci贸n para verificaci贸n")
|
| 88 |
return False
|
| 89 |
|
| 90 |
# Buscar documentos recientes del usuario
|
| 91 |
+
timestamp_threshold = (datetime.now(timezone.utc) - timedelta(minutes=5)).isoformat()
|
| 92 |
+
|
| 93 |
recent_docs = collection.find({
|
| 94 |
'username': username,
|
| 95 |
+
'timestamp': {'$gte': timestamp_threshold}
|
|
|
|
|
|
|
| 96 |
}).sort('timestamp', -1).limit(1)
|
| 97 |
|
| 98 |
docs = list(recent_docs)
|
|
|
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
logger.error(f"Error verificando almacenamiento: {str(e)}")
|
| 113 |
+
return False
|
| 114 |
+
|
| 115 |
+
def get_recent_situation_analysis(username, limit=5):
|
| 116 |
+
"""
|
| 117 |
+
Obtiene los an谩lisis m谩s recientes de un usuario.
|
| 118 |
+
"""
|
| 119 |
+
try:
|
| 120 |
+
collection = get_collection(COLLECTION_NAME)
|
| 121 |
+
if collection is None:
|
| 122 |
+
return []
|
| 123 |
+
|
| 124 |
+
results = collection.find(
|
| 125 |
+
{'username': username}
|
| 126 |
+
).sort('timestamp', -1).limit(limit)
|
| 127 |
+
|
| 128 |
+
return list(results)
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
logger.error(f"Error obteniendo an谩lisis recientes: {str(e)}")
|
| 132 |
+
return []
|